Debian Codegraph @

plugins/vcs/Repository.h

raw
1 #pragma once
2
3 #include <string>
4 #include <memory>
5 #include "Reference.h"
6 #include "CommitMetadata.h"
7 #include "Index.h"
8
9 struct git_repository;
10
11 namespace vcs
12 {
13
14 namespace git
15 {
16
17 class Remote;
18 class Commit;
19 class Diff;
20 class Tree;
21
22 /**
23 * Represents a Git repository at a certain path
24 */
25 class Repository final
26 {
27 private:
28 git_repository* _repository;
29 bool _isOk;
30
31 std::string _path;
32
33 public:
34 Repository(const std::string& path);
35 ~Repository();
36
37 // Status query of this repository object,
38 // returns true if this repository exists and has been successfully opened
39 bool isOk() const;
40
41 const std::string& getPath() const;
42
43 std::string getRepositoryRelativePath(const std::string& path);
44
45 // Returns the remote with the given name
46 std::shared_ptr<Remote> getRemote(const std::string& name);
47
48 std::string getCurrentBranchName();
49
50 std::string getUpstreamRemoteName(const Reference& reference);
51
52 Reference::Ptr getHead();
53
54 // Performs a fetch from the remote the current branch is tracking
55 void fetchFromTrackedRemote();
56
57 // Fast-forwards the current head to the tracked remote branch
58 void fastForwardToTrackedRemote();
59
60 // Pushes the current head to its tracked remote branch
61 void pushToTrackedRemote();
62
63 bool isUpToDateWithRemote();
64 bool fileIsIndexed(const std::string& relativePath);
65 bool fileHasUncommittedChanges(const std::string& relativePath);
66
67 // Compares the state of the given ref to the state of its tracked remote,
68 // returns the number of commits each of them is ahead of the other one.
69 RefSyncStatus getSyncStatusOfBranch(const Reference& reference);
70
71 bool isReadyForMerge();
72 bool mergeIsInProgress();
73
74 void abortMerge();
75
76 Index::Ptr getIndex();
77
78 // Finds a common ancestor of the two refs, to base a merge operation on
79 std::shared_ptr<Commit> findMergeBase(const Reference& first, const Reference& second);
80
81 // Get the diff of the reference against the given commit
82 std::shared_ptr<Diff> getDiff(const Reference& ref, Commit& commit);
83
84 std::shared_ptr<Tree> getTreeByRevision(const std::string& revision);
85
86 // Create a commit with the current repository HEAD as only parent
87 void createCommit(const CommitMetadata& metadata);
88
89 // Create a commit with the currrent repository HEAD and the given ref as parent
90 // It's valid to pass an empty additionalParent if no second parent is needed
91 void createCommit(const CommitMetadata& metadata, const Reference::Ptr& additionalParent);
92
93 std::string getConfigValue(const std::string& key);
94
95 void cleanupState();
96
97 // Creates a new instance of this repository, not sharing any libgit2 handles with the original
98 std::shared_ptr<Repository> clone();
99
100 // Return the raw libgit2 object
101 git_repository* _get();
102
103 private:
104 std::shared_ptr<Remote> getTrackedRemote();
105
106 unsigned int getFileStatus(const std::string& relativePath);
107 };
108
109 }
110
111 }