| 1 |
#pragma once |
| 2 |
|
| 3 |
#include <string> |
| 4 |
#include <memory> |
| 5 |
#include <git2.h> |
| 6 |
#include "GitException.h" |
| 7 |
|
| 8 |
namespace vcs |
| 9 |
{ |
| 10 |
|
| 11 |
namespace git |
| 12 |
{ |
| 13 |
|
| 14 |
struct RefSyncStatus |
| 15 |
{ |
| 16 |
RefSyncStatus() : |
| 17 |
localCommitsAhead(0), |
| 18 |
remoteCommitsAhead(0), |
| 19 |
localCanBePushed(true) |
| 20 |
{} |
| 21 |
|
| 22 |
// The number of commits the local branch is ahead of the remote |
| 23 |
std::size_t localCommitsAhead; |
| 24 |
|
| 25 |
// The number of commits the remote branch is ahead of the local |
| 26 |
std::size_t remoteCommitsAhead; |
| 27 |
|
| 28 |
// whether the local branch can be pushed on top of the remote |
| 29 |
bool localCanBePushed; |
| 30 |
|
| 31 |
// whether the local branch is up to date with the remote |
| 32 |
bool localIsUpToDate; |
| 33 |
}; |
| 34 |
|
| 35 |
class Reference final |
| 36 |
{ |
| 37 |
private: |
| 38 |
git_reference* _reference; |
| 39 |
|
| 40 |
public: |
| 41 |
using Ptr = std::shared_ptr<Reference>; |
| 42 |
|
| 43 |
Reference(git_reference* reference) : |
| 44 |
_reference(reference) |
| 45 |
{} |
| 46 |
|
| 47 |
std::string getName() const |
| 48 |
{ |
| 49 |
return git_reference_name(_reference); |
| 50 |
} |
| 51 |
|
| 52 |
std::string getShorthandName() const |
| 53 |
{ |
| 54 |
return git_reference_shorthand(_reference); |
| 55 |
} |
| 56 |
|
| 57 |
// Returns the upstream of this reference (if configured) |
| 58 |
Ptr getUpstream() const |
| 59 |
{ |
| 60 |
git_reference* upstream = nullptr; |
| 61 |
auto error = git_branch_upstream(&upstream, _reference); |
| 62 |
|
| 63 |
return upstream != nullptr ? std::make_shared<Reference>(upstream) : Ptr(); |
| 64 |
} |
| 65 |
|
| 66 |
// Create a new reference with the same name as the given reference but a |
| 67 |
// different OID target. The new reference will be written to disk, overwriting the given reference. |
| 68 |
void setTarget(git_oid* oid) |
| 69 |
{ |
| 70 |
git_reference* newTargetRef; |
| 71 |
auto error = git_reference_set_target(&newTargetRef, _reference, oid, "Reference set to new target by DarkRadiant"); |
| 72 |
GitException::ThrowOnError(error); |
| 73 |
|
| 74 |
// Swap the wrapped reference pointer, release the old one |
| 75 |
git_reference_free(_reference); |
| 76 |
|
| 77 |
_reference = newTargetRef; |
| 78 |
} |
| 79 |
|
| 80 |
~Reference() |
| 81 |
{ |
| 82 |
git_reference_free(_reference); |
| 83 |
} |
| 84 |
|
| 85 |
static std::string OidToString(const git_oid* oid) |
| 86 |
{ |
| 87 |
std::string hexOid(GIT_OID_HEXSZ, '\0'); |
| 88 |
git_oid_fmt(hexOid.data(), oid); |
| 89 |
|
| 90 |
return hexOid; |
| 91 |
} |
| 92 |
}; |
| 93 |
|
| 94 |
} |
| 95 |
|
| 96 |
} |