Show diff stat using libgit2
[TortoiseGit.git] / src / Utils / SmartLibgit2Ref.h
blob90c2408cc1e78c000dbf79ea174f7f8121253443
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2014 - TortoiseGit
4 // based on SmartHandle of TortoiseSVN
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software Foundation,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #pragma once
21 #include "UnicodeUtils.h"
23 /**
24 * \ingroup Utils
25 * Helper classes for libgit2 references.
27 template <typename ReferenceType>
28 class CSmartLibgit2Ref
30 public:
31 CSmartLibgit2Ref()
33 m_Ref = nullptr;
36 void Swap(CSmartLibgit2Ref& tmp)
38 ReferenceType* p;
40 p = m_Ref;
41 m_Ref = tmp.m_Ref;
42 tmp.m_Ref = p;
45 void Free()
47 CleanUp();
50 ReferenceType* Detach()
52 ReferenceType* p;
54 p = m_Ref;
55 m_Ref = nullptr;
57 return p;
60 operator ReferenceType*()
62 return m_Ref;
65 ReferenceType** GetPointer()
67 return &m_Ref;
70 operator bool()
72 return IsValid();
75 bool IsValid() const
77 return m_Ref != nullptr;
80 protected:
81 virtual void FreeRef() = 0;
83 void CleanUp()
85 if (IsValid())
87 FreeRef();
88 m_Ref = nullptr;
92 ReferenceType* m_Ref;
95 class CAutoRepository : public CSmartLibgit2Ref<git_repository>
97 public:
98 CAutoRepository() {};
100 CAutoRepository(git_repository* h)
102 m_Ref = h;
105 CAutoRepository(const CString& gitDir)
107 Open(gitDir);
110 CAutoRepository(const CStringA& gitDirA)
112 Open(gitDirA);
115 int Open(const CString& gitDir)
117 return Open(CUnicodeUtils::GetUTF8(gitDir));
120 int Open(const CStringA& gitDirA)
122 CleanUp();
123 return git_repository_open(GetPointer(), gitDirA);
126 ~CAutoRepository()
128 CleanUp();
131 protected:
132 virtual void FreeRef()
134 git_repository_free(m_Ref);
138 class CAutoCommit : public CSmartLibgit2Ref<git_commit>
140 public:
141 CAutoCommit() {}
143 CAutoCommit(git_commit* h)
145 m_Ref = h;
148 ~CAutoCommit()
150 CleanUp();
152 protected:
153 virtual void FreeRef()
155 git_commit_free(m_Ref);
159 class CAutoTree : public CSmartLibgit2Ref<git_tree>
161 public:
162 git_tree* operator=(git_tree* h)
164 if (m_Ref != h)
166 CleanUp();
167 m_Ref = h;
169 return (*this);
172 ~CAutoTree()
174 CleanUp();
177 protected:
178 virtual void FreeRef()
180 git_tree_free(m_Ref);
184 class CAutoObject : public CSmartLibgit2Ref<git_object>
186 public:
187 ~CAutoObject()
189 CleanUp();
192 protected:
193 virtual void FreeRef()
195 git_object_free(m_Ref);
199 class CAutoBlob : public CSmartLibgit2Ref<git_blob>
201 public:
202 ~CAutoBlob()
204 CleanUp();
207 protected:
208 virtual void FreeRef()
210 git_blob_free(m_Ref);
214 class CAutoConfig : public CSmartLibgit2Ref<git_config>
216 public:
217 CAutoConfig() {}
219 CAutoConfig(CAutoRepository &repo)
221 git_repository_config(&m_Ref, repo);
224 CAutoConfig(bool init)
226 if (init)
227 New();
230 void New()
232 CleanUp();
233 git_config_new(&m_Ref);
236 ~CAutoConfig()
238 CleanUp();
241 int GetString(const CString &key, CString &value) const
243 if (!IsValid())
244 return -1;
246 const char* out = nullptr;
247 int ret = 0;
248 if ((ret = git_config_get_string(&out, m_Ref, CUnicodeUtils::GetUTF8(key))))
250 value.Empty();
251 return ret;
254 value = CUnicodeUtils::GetUnicode((CStringA)out);
256 return ret;
259 int GetBOOL(const CString &key, BOOL &b) const
261 if (!IsValid())
262 return -1;
264 return git_config_get_bool(&b, m_Ref, CUnicodeUtils::GetUTF8(key));
267 int GetBool(const CString &key, bool &b) const
269 if (!IsValid())
270 return -1;
272 int value = FALSE;
273 int ret = 0;
274 if ((ret = GetBOOL(key, value)))
275 return ret;
277 b = (value == TRUE);
279 return ret;
282 protected:
283 virtual void FreeRef()
285 git_config_free(m_Ref);
289 class CAutoReference : public CSmartLibgit2Ref<git_reference>
291 public:
292 CAutoReference() {}
294 CAutoReference(git_reference* h)
296 m_Ref = h;
299 ~CAutoReference()
301 CleanUp();
304 protected:
305 virtual void FreeRef()
307 git_reference_free(m_Ref);
311 class CAutoTreeEntry : public CSmartLibgit2Ref<git_tree_entry>
313 public:
314 ~CAutoTreeEntry()
316 CleanUp();
319 protected:
320 virtual void FreeRef()
322 git_tree_entry_free(m_Ref);
326 class CAutoDiff : public CSmartLibgit2Ref<git_diff>
328 public:
329 ~CAutoDiff()
331 CleanUp();
334 protected:
335 virtual void FreeRef()
337 git_diff_free(m_Ref);
341 class CAutoPatch : public CSmartLibgit2Ref<git_patch>
343 public:
344 ~CAutoPatch()
346 CleanUp();
349 protected:
350 virtual void FreeRef()
352 git_patch_free(m_Ref);
356 class CAutoDiffStats : public CSmartLibgit2Ref<git_diff_stats>
358 public:
359 ~CAutoDiffStats()
361 CleanUp();
364 protected:
365 virtual void FreeRef()
367 git_diff_stats_free(m_Ref);
371 class CAutoIndex : public CSmartLibgit2Ref<git_index>
373 public:
374 ~CAutoIndex()
376 CleanUp();
379 protected:
380 virtual void FreeRef()
382 git_index_free(m_Ref);
386 class CAutoRemote : public CSmartLibgit2Ref<git_remote>
388 public:
389 ~CAutoRemote()
391 CleanUp();
394 protected:
395 virtual void FreeRef()
397 if (git_remote_connected(m_Ref) == 1)
398 git_remote_disconnect(m_Ref);
399 git_remote_free(m_Ref);
403 class CAutoPush : public CSmartLibgit2Ref<git_push>
405 public:
406 ~CAutoPush()
408 CleanUp();
411 protected:
412 virtual void FreeRef()
414 git_push_free(m_Ref);
418 class CAutoReflog : public CSmartLibgit2Ref<git_reflog>
420 public:
421 ~CAutoReflog()
423 CleanUp();
426 protected:
427 virtual void FreeRef()
429 git_reflog_free(m_Ref);
433 class CAutoRevwalk : public CSmartLibgit2Ref<git_revwalk>
435 public:
436 ~CAutoRevwalk()
438 CleanUp();
441 protected:
442 virtual void FreeRef()
444 git_revwalk_free(m_Ref);
448 class CAutoBranchIterator : public CSmartLibgit2Ref<git_branch_iterator>
450 public:
451 ~CAutoBranchIterator()
453 CleanUp();
456 protected:
457 virtual void FreeRef()
459 git_branch_iterator_free(m_Ref);
463 class CAutoDescribeResult : public CSmartLibgit2Ref<git_describe_result>
465 public:
466 ~CAutoDescribeResult()
468 CleanUp();
471 protected:
472 virtual void FreeRef()
474 git_describe_result_free(m_Ref);
478 template <typename HandleType, class FreeFunction>
479 class CSmartBuffer : public FreeFunction
481 public:
482 CSmartBuffer()
484 HandleType tmp = { 0 };
485 m_Ref = tmp;
488 operator HandleType*()
490 return &m_Ref;
493 HandleType* operator->()
495 return &m_Ref;
498 ~CSmartBuffer()
500 Free(&m_Ref);
503 protected:
504 HandleType m_Ref;
507 struct CFreeBuf
509 protected:
510 void Free(git_buf* ref)
512 git_buf_free(ref);
515 ~CFreeBuf()
520 struct CFreeStrArray
522 protected:
523 void Free(git_strarray* ref)
525 git_strarray_free(ref);
528 ~CFreeStrArray()
533 typedef CSmartBuffer<git_buf, CFreeBuf> CAutoBuf;
534 typedef CSmartBuffer<git_strarray, CFreeStrArray> CAutoStrArray;