-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGit.h
484 lines (375 loc) · 13.5 KB
/
Git.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
#pragma once
#include <vector>
#include <list>
#include <string>
#include <iostream>
#include <stdexcept>
#include <functional>
#include <memory>
#include "git2.h"
namespace Git
{
using std::string;
class CRepo;
typedef std::vector<string> StringVector;
class CGitException : public std::runtime_error
{
public:
CGitException(int errorCode, const char* P_szDoingPtr);
CGitException(const char* P_szDoingPtr);
virtual ~CGitException() throw() {}
int m_errorCode;
string m_errorText;
string m_doing;
};
void ThrowIfError(int P_iGitReturnCode, const char* P_szDoingPtr);
template<class T_GitObj, void (*CloseFunc)(T_GitObj*) >
class CLibGitObjWrapper
{
public:
CLibGitObjWrapper():m_obj(NULL), m_isOwner(false) {}
CLibGitObjWrapper(T_GitObj* obj, bool makeOwner = true):m_obj(obj), m_isOwner(makeOwner) {}
CLibGitObjWrapper(CLibGitObjWrapper&& that):m_obj(NULL), m_isOwner(false) { using std::swap; swap(m_obj, that.m_obj); swap(m_isOwner, that.m_isOwner); }
CLibGitObjWrapper& operator=(CLibGitObjWrapper&& that) { using std::swap; swap(m_obj, that.m_obj); swap(m_isOwner, that.m_isOwner); return *this; }
virtual ~CLibGitObjWrapper() { Close(); }
void Attach(T_GitObj* obj, bool makeOwner = true) { Close(); m_obj = obj; m_isOwner = makeOwner; }
void Close() { if(!IsValid()) return; if(m_isOwner && CloseFunc) CloseFunc(m_obj); m_obj = NULL; m_isOwner = false; }
bool IsValid() const { return m_obj != NULL; }
void CheckValid() const { if(!IsValid())
throw std::runtime_error("libgit object not valid"); }
T_GitObj* GetInternalObj() const { CheckValid(); return m_obj; }
typedef bool (CLibGitObjWrapper::*T_IsValidFuncPtr)() const;
operator T_IsValidFuncPtr () const {return IsValid() ? &CLibGitObjWrapper::IsValid : NULL;}
private:
CLibGitObjWrapper(const CLibGitObjWrapper&); //Libgit objects are not copyable
CLibGitObjWrapper& operator=(const CLibGitObjWrapper&);
protected:
T_GitObj* m_obj;
bool m_isOwner;
};
template<class T_GitObj>
class CLibGitCopyableObjWrapper : public CLibGitObjWrapper<T_GitObj, nullptr>
{
public:
CLibGitCopyableObjWrapper(){}
CLibGitCopyableObjWrapper(T_GitObj* obj):CLibGitObjWrapper<T_GitObj, nullptr>(obj, false){}
CLibGitCopyableObjWrapper(const CLibGitCopyableObjWrapper& obj):CLibGitObjWrapper<T_GitObj, nullptr>(obj.IsValid() ? obj.GetInternalObj() : NULL, false){}
CLibGitCopyableObjWrapper& operator=(const CLibGitCopyableObjWrapper& obj){ if(obj.IsValid()) Attach(obj.GetInternalObj(), false); return *this; }
void Attach(T_GitObj* obj){ CLibGitObjWrapper<T_GitObj, nullptr>::Attach(obj, false); }
};
class CConfig : public CLibGitObjWrapper<git_config, git_config_free>
{
public:
CConfig(){}
#ifdef WIN32
void Open(const wchar_t* file);
#endif
void Open(const char* file);
void OpenDefault();
void Open(CRepo& repo);
bool BoolVal(const char* name) const;
string StringVal(const char* name) const;
int IntVal(const char* name) const;
long long Int64Val(const char* name) const;
void BoolVal(const char* name, bool val);
void StringVal(const char* name, const char* val);
void IntVal(const char* name, int val);
void Int64Val(const char* name, long long val);
template<class T>
struct CVal
{
CVal():m_config(NULL){}
void Init(CConfig* config, const char* name){ m_config=config; m_name = name; }
protected:
CConfig* Config() const { if(!m_config) throw std::logic_error("CConfig::CVal::Config() CVal was not initialized."); return m_config; }
CConfig* m_config;
string m_name;
};
template<class T>
CVal<T> Val(const char* name){ CVal<T> val; val.Init(this, name); return val; }
};
template<>
struct CConfig::CVal<string> : CConfig::CVal<void>
{
const string& operator*()const { m_val = Config()->StringVal(m_name.c_str()); return m_val; }
const string* operator->()const{ m_val = Config()->StringVal(m_name.c_str()); return &m_val; }
void operator =(const string& val){ Config()->StringVal(m_name.c_str(), val.c_str()); }
void operator =(const char* val){ Config()->StringVal(m_name.c_str(), val); }
mutable string m_val;
};
template<>
struct CConfig::CVal<bool> : CConfig::CVal<void>
{
bool operator*()const{ return Config()->BoolVal(m_name.c_str()); }
void operator =(bool val){ Config()->BoolVal(m_name.c_str(), val); }
};
template<>
struct CConfig::CVal<int> : CConfig::CVal<void>
{
int operator*()const { return Config()->IntVal(m_name.c_str()); }
void operator =(int val){ Config()->IntVal(m_name.c_str(), val); }
};
class COid
{
friend std::ostream& operator<<(std::ostream& str, const COid& oid);
friend class COdb;
friend CRepo;
public:
COid();
COid(const char* hexStr);
COid(const git_oid& P_oid):m_oid(P_oid){}
static COid FromHexString(const char* hexStr);
COid& operator=(const char* hexStr);
git_oid& GetInternalObj(){ return m_oid; }
const git_oid& GetInternalObj() const { return m_oid; }
int compare(const COid& that) const;
bool operator<(const COid& that) const;
bool operator==(const COid& that) const;
bool operator!=(const COid& that) const;
bool isNull() const;
private:
git_oid m_oid;
};
std::ostream& operator<<(std::ostream& str, const COid& oid);
class COids
{
public:
typedef std::vector<COid> VectorCOid;
COids();
COids(const COid& oid);
COids& Add(const COid& oid);
COids& operator<<(const COid& oid);
VectorCOid m_oids;
};
std::ostream& operator<<(std::ostream& str, const COid& oid);
class CRef : public CLibGitCopyableObjWrapper<git_reference>//Is copyable because pointer is owned by the repo.
{
public:
CRef(){}
CRef(git_reference* ref):CLibGitCopyableObjWrapper(ref){}
CRef(const CRepo& repo, const char* name);
string Name() const ;
COid Oid(bool forceResolve = false) const ;
bool IsSymbolic() const;
void Resolve();
};
typedef std::vector<CRef> RefVector;
class CSignature : public CLibGitObjWrapper<git_signature, &git_signature_free>
{
public:
CSignature(){}
CSignature(CRepo& repo);
CSignature(const char* name, const char* email);
CSignature(const char* name, const char* email, git_time_t time, int offset);
void Create(const char* name, const char* email);
};
class CObjType
{
public:
CObjType(git_otype otype);
const char* AsString() const;
git_otype Get() const {return m_otype;}
private:
git_otype m_otype;
};
std::ostream& operator<<(std::ostream& str, const CObjType& ot);
class CObject { public: virtual ~CObject() {} };
template<class T_GitObj>
void CloseWithObjectClose(T_GitObj* obj) { git_object_free((git_object*)obj); }
template<class T_GitObj, void (*CloseFunc)(T_GitObj*) >
class CObjectTempl : public CLibGitObjWrapper<T_GitObj, CloseFunc>, public CObject
{
public:
CObjectTempl(){}
CObjectTempl(T_GitObj* obj):CLibGitObjWrapper<T_GitObj, CloseFunc>(obj){}
};
class CRawObject : public CObjectTempl<git_odb_object, &git_odb_object_free>
{
public:
CRawObject();
CRawObject(git_odb_object* obj);
CRawObject(CRepo& repo, const COid& oid);
const char* Data() const;
size_t Size() const;
CObjType Type() const;
};
std::ostream& operator<<(std::ostream& P_os, const CRawObject& P_Obj);
class CObjectBase : public CObjectTempl<git_object, &git_object_free>
{
public:
CObjectBase();
CObjectBase(git_object* obj);
CObjectBase(CRepo& repo, const COid& oid, git_otype type = GIT_OBJ_ANY);
COid Oid() const;
};
class CCommit : public CObjectTempl<git_commit, &CloseWithObjectClose<git_commit> >
{
public:
CCommit();
CCommit(git_commit* obj);
CCommit(CRepo& repo, const COid& oid);
virtual ~CCommit();
string Message() const;
// string MessageShort() const;
const git_signature* Author() const;
const git_signature* Committer() const;
time_t Time() const;
COid Tree() const;
};
typedef std::vector<std::shared_ptr<CCommit> > VectorCommit;
class CTreeEntry : public CLibGitCopyableObjWrapper<const git_tree_entry>
{
public:
CTreeEntry();
CTreeEntry(const git_tree_entry* entry);
COid Oid() const;
string Name() const;
git_filemode_t FileMode() const;
bool IsFile() const;
};
class CTree : public CObjectTempl<git_tree, &CloseWithObjectClose<git_tree> >
{
public:
CTree();
CTree(CRepo& repo, const COid& oid);
size_t EntryCount() const;
CTreeEntry Entry(size_t i) const;
CTreeEntry Entry(const char* name) const;
COid ID() const;
};
class CBlob : public CObjectTempl<git_blob, &CloseWithObjectClose<git_blob> >
{
public:
CBlob();
CBlob(CRepo& repo, const COid& oid);
const void* Content()const;
git_off_t Size()const;
};
class CTreeBuilder : public CObjectTempl<git_treebuilder, &git_treebuilder_free>
{
public:
CTreeBuilder(CRepo& repo);
CTreeBuilder(CRepo& repo, const CTree* source);
CTreeBuilder(CRepo& repo, const CTree& source);
void Reset(const CTree* source = NULL);
void Clear();
CTreeEntry Insert(const char* filename, const COid& id, git_filemode_t attributes = GIT_FILEMODE_BLOB);
#ifdef WIN32
CTreeEntry Insert(const wchar_t* filename, const COid& id, git_filemode_t attributes = GIT_FILEMODE_BLOB);
#endif
COid Write();
private:
CRepo& m_repo;
};
class CTreeNode
{
public:
typedef std::list<CTreeNode> list_t;
CTreeNode();
CTreeNode(const string& name);
virtual ~CTreeNode();
CTreeNode* GetByPath(const char* name, bool createIfNotExist = true);
void Insert(const char* name, COid oid, git_filemode_t attributes = GIT_FILEMODE_BLOB);
bool Delete(const char* name);
COid Write(CRepo& repo);
git_filemode_t GetAttributes()const;
bool IsFile()const;
string m_name;
COid m_oid;
list_t m_subTree; //when this one is not empty, its not a leaf
git_filemode_t m_attributes;
};
class COdb
{
public:
COdb(git_odb* odb);
void Read(CRawObject& obj, const COid& oid);
COid Write(const CObjType& ot, const void* data, size_t size);
private:
git_odb* m_odb;
};
class CRemote : public CLibGitObjWrapper<git_remote, &git_remote_free>
{
public:
CRemote();
CRemote(CRepo& repo, const char* name);
void Connect(git_direction direction = GIT_DIRECTION_FETCH);
void Disconnect();
bool IsConnected() const;
void Download();
void UpdateTips(const char* refLogMsg = NULL);
};
typedef std::function<void (CRef&)> T_forEachRefCallback;
typedef std::function<int (CRef&)> T_forEachRefNothrowCallback;
class CRepo : public CLibGitObjWrapper<git_repository, &git_repository_free>
{
public:
CRepo();
CRepo(const char* path);
virtual ~CRepo();
void Open(const char* path);
void Create(const char* path, bool isBare);
static std::string DiscoverPath(const char* startPath, bool acrossFs = false, const char* ceilingDirs = NULL);
#ifdef WIN32
CRepo(const wchar_t* path);
void Open(const wchar_t* path);
void Create(const wchar_t* path, bool isBare);
static std::wstring DiscoverPath(const wchar_t* startPath, bool acrossFs = false, const wchar_t* ceilingDirs = NULL);
#endif
void Read(CObjectBase& obj, const COid& oid, git_otype type = GIT_OBJ_ANY);
void Read(CCommit& obj, const COid& oid);
void Read(CTree& obj, const COid& oid);
void Read(CBlob& obj, const COid& oid);
COid WriteBlob(const void* data, size_t size);
COid WriteBlob(const string& data);
COid Write(CTreeBuilder& tree);
#ifdef WIN32
std::wstring GetWPath() const;
#endif
string GetPath() const;
COdb Odb();
bool IsBare()const;
void GetReferences(RefVector& refs) const;
void ForEachRef(const T_forEachRefCallback& callback) const;
int ForEachRef_NoThrow(const T_forEachRefNothrowCallback& callback) const;
VectorCommit ToCommits(const COids& oids);
CRef GetRef(const char* name) const;
CRef MakeRef(const char* name, const COid& oid, bool force = false, const char* refLogMsg = NULL);
COid Commit(const char* updateRef, const CSignature& author, const CSignature& committer, const char* msg, const COid& tree, const COids& parents);
COid Commit(const char* updateRef, const CSignature& author, const CSignature& committer, const char* msg, const CTree& tree, const VectorCommit& parents);
COid CreateTag(const char* name, const CObjectBase& target, bool force);
COid CreateTag(const char* name, const COid& target, bool force);
CTreeEntry TreeFind(const CTree& start, const char* path);
CTreeEntry TreeFind(const COid& treeStart, const char* path);
void BuildTreeNode(CTreeNode& node, const CTree& tree);
void BuildTreeNode(CTreeNode& node, const COid& tree);
void DefaultSig(CSignature& sig);
private:
CRepo(const CRepo&); //Non copyable
CRepo& operator=(const CRepo&);
};
class CCommitWalker : public CLibGitObjWrapper<git_revwalk, &git_revwalk_free>
{
public:
CCommitWalker();
CCommitWalker(CRepo& repo);
void Init(CRepo& repo);
void Sort(unsigned int sort);
void Reset();
void AddRev(const COid& oid);
void Hide(const COid& oid);
bool End() const;
void Next();
COid Curr() const;
operator T_IsValidFuncPtr () const { return IsValid() && !End() ? &CLibGitObjWrapper::IsValid : NULL; }
CCommitWalker& operator ++() { Next(); return *this; }
COid operator*() const { return Curr(); }
virtual ~CCommitWalker();
private:
CCommitWalker(const CCommitWalker&); //Non copyable
CCommitWalker& operator=(const CCommitWalker&);
bool m_nextCalled;
bool m_end;
COid m_curr;
};
}