CommitDlg: Update empty file list message
[TortoiseGit.git] / src / Utils / SmartLibgit2Ref.h
bloba38df7ad754b1a879de15984bf99b8c1cb9b9540
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2014-2015 - 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 HandleType, class FreeFunction>
28 class CSmartBuffer : public FreeFunction
30 public:
31 CSmartBuffer()
33 HandleType tmp = { 0 };
34 m_Ref = tmp;
37 operator HandleType*()
39 return &m_Ref;
42 HandleType* operator->()
44 return &m_Ref;
47 ~CSmartBuffer()
49 Free(&m_Ref);
52 private:
53 CSmartBuffer(const CSmartBuffer& that);
54 CSmartBuffer& operator=(const CSmartBuffer& that);
56 protected:
57 HandleType m_Ref;
60 struct CFreeBuf
62 protected:
63 void Free(git_buf* ref)
65 git_buf_free(ref);
68 ~CFreeBuf()
73 struct CFreeStrArray
75 protected:
76 void Free(git_strarray* ref)
78 git_strarray_free(ref);
81 ~CFreeStrArray()
86 typedef CSmartBuffer<git_buf, CFreeBuf> CAutoBuf;
87 typedef CSmartBuffer<git_strarray, CFreeStrArray> CAutoStrArray;
89 template <typename ReferenceType>
90 class CSmartLibgit2Ref
92 public:
93 CSmartLibgit2Ref()
95 m_Ref = nullptr;
98 void Swap(CSmartLibgit2Ref& tmp)
100 ReferenceType* p;
102 p = m_Ref;
103 m_Ref = tmp.m_Ref;
104 tmp.m_Ref = p;
107 void Free()
109 CleanUp();
112 ReferenceType* Detach()
114 ReferenceType* p;
116 p = m_Ref;
117 m_Ref = nullptr;
119 return p;
122 operator ReferenceType*()
124 return m_Ref;
128 * Free the wrapped object and return a pointer to the wrapped object ReferenceType*
130 ReferenceType** GetPointer()
132 CleanUp();
133 return &m_Ref;
136 operator bool()
138 return IsValid();
141 bool IsValid() const
143 return m_Ref != nullptr;
146 private:
147 CSmartLibgit2Ref(const CSmartLibgit2Ref& that);
148 CSmartLibgit2Ref& operator=(const CSmartLibgit2Ref& that);
150 protected:
151 virtual void FreeRef() = 0;
153 void CleanUp()
155 if (IsValid())
157 FreeRef();
158 m_Ref = nullptr;
162 ReferenceType* m_Ref;
165 class CAutoRepository : public CSmartLibgit2Ref<git_repository>
167 public:
168 CAutoRepository() {};
170 CAutoRepository(git_repository* h)
172 m_Ref = h;
175 CAutoRepository(const CString& gitDir)
177 Open(gitDir);
180 CAutoRepository(const CStringA& gitDirA)
182 Open(gitDirA);
185 int Open(const CString& gitDir)
187 return Open(CUnicodeUtils::GetUTF8(gitDir));
190 int Open(const CStringA& gitDirA)
192 CleanUp();
193 return git_repository_open(GetPointer(), gitDirA);
196 ~CAutoRepository()
198 CleanUp();
201 protected:
202 virtual void FreeRef()
204 git_repository_free(m_Ref);
208 class CAutoCommit : public CSmartLibgit2Ref<git_commit>
210 public:
211 CAutoCommit() {}
213 CAutoCommit(git_commit* h)
215 m_Ref = h;
218 ~CAutoCommit()
220 CleanUp();
222 protected:
223 virtual void FreeRef()
225 git_commit_free(m_Ref);
229 class CAutoTree : public CSmartLibgit2Ref<git_tree>
231 public:
232 git_tree* operator=(git_tree* h)
234 if (m_Ref != h)
236 CleanUp();
237 m_Ref = h;
239 return (*this);
242 ~CAutoTree()
244 CleanUp();
247 protected:
248 virtual void FreeRef()
250 git_tree_free(m_Ref);
254 class CAutoObject : public CSmartLibgit2Ref<git_object>
256 public:
257 ~CAutoObject()
259 CleanUp();
262 protected:
263 virtual void FreeRef()
265 git_object_free(m_Ref);
269 class CAutoBlob : public CSmartLibgit2Ref<git_blob>
271 public:
272 ~CAutoBlob()
274 CleanUp();
277 protected:
278 virtual void FreeRef()
280 git_blob_free(m_Ref);
284 class CAutoConfig : public CSmartLibgit2Ref<git_config>
286 public:
287 CAutoConfig() {}
289 CAutoConfig(CAutoRepository &repo)
291 git_repository_config(&m_Ref, repo);
294 CAutoConfig(bool init)
296 if (init)
297 New();
300 void New()
302 CleanUp();
303 git_config_new(&m_Ref);
306 ~CAutoConfig()
308 CleanUp();
311 int GetString(const CString &key, CString &value) const
313 if (!IsValid())
314 return -1;
316 CAutoBuf buf;
317 int ret = 0;
318 if ((ret = git_config_get_string_buf(buf, m_Ref, CUnicodeUtils::GetUTF8(key))))
320 value.Empty();
321 return ret;
324 value = CUnicodeUtils::GetUnicode((CStringA)buf->ptr);
326 return ret;
329 int GetBOOL(const CString &key, BOOL &b) const
331 if (!IsValid())
332 return -1;
334 return git_config_get_bool(&b, m_Ref, CUnicodeUtils::GetUTF8(key));
337 int GetBool(const CString &key, bool &b) const
339 if (!IsValid())
340 return -1;
342 int value = FALSE;
343 int ret = 0;
344 if ((ret = GetBOOL(key, value)) < 0)
345 return ret;
347 b = (value == TRUE);
349 return ret;
352 protected:
353 virtual void FreeRef()
355 git_config_free(m_Ref);
359 class CAutoReference : public CSmartLibgit2Ref<git_reference>
361 public:
362 CAutoReference() {}
364 CAutoReference(git_reference* h)
366 m_Ref = h;
369 ~CAutoReference()
371 CleanUp();
374 protected:
375 virtual void FreeRef()
377 git_reference_free(m_Ref);
381 class CAutoTreeEntry : public CSmartLibgit2Ref<git_tree_entry>
383 public:
384 ~CAutoTreeEntry()
386 CleanUp();
389 protected:
390 virtual void FreeRef()
392 git_tree_entry_free(m_Ref);
396 class CAutoDiff : public CSmartLibgit2Ref<git_diff>
398 public:
399 ~CAutoDiff()
401 CleanUp();
404 protected:
405 virtual void FreeRef()
407 git_diff_free(m_Ref);
411 class CAutoPatch : public CSmartLibgit2Ref<git_patch>
413 public:
414 ~CAutoPatch()
416 CleanUp();
419 protected:
420 virtual void FreeRef()
422 git_patch_free(m_Ref);
426 class CAutoDiffStats : public CSmartLibgit2Ref<git_diff_stats>
428 public:
429 ~CAutoDiffStats()
431 CleanUp();
434 protected:
435 virtual void FreeRef()
437 git_diff_stats_free(m_Ref);
441 class CAutoIndex : public CSmartLibgit2Ref<git_index>
443 public:
444 ~CAutoIndex()
446 CleanUp();
449 protected:
450 virtual void FreeRef()
452 git_index_free(m_Ref);
456 class CAutoRemote : public CSmartLibgit2Ref<git_remote>
458 public:
459 ~CAutoRemote()
461 CleanUp();
464 protected:
465 virtual void FreeRef()
467 git_remote_free(m_Ref);
471 class CAutoReflog : public CSmartLibgit2Ref<git_reflog>
473 public:
474 ~CAutoReflog()
476 CleanUp();
479 protected:
480 virtual void FreeRef()
482 git_reflog_free(m_Ref);
486 class CAutoRevwalk : public CSmartLibgit2Ref<git_revwalk>
488 public:
489 ~CAutoRevwalk()
491 CleanUp();
494 protected:
495 virtual void FreeRef()
497 git_revwalk_free(m_Ref);
501 class CAutoBranchIterator : public CSmartLibgit2Ref<git_branch_iterator>
503 public:
504 ~CAutoBranchIterator()
506 CleanUp();
509 protected:
510 virtual void FreeRef()
512 git_branch_iterator_free(m_Ref);
516 class CAutoDescribeResult : public CSmartLibgit2Ref<git_describe_result>
518 public:
519 ~CAutoDescribeResult()
521 CleanUp();
524 protected:
525 virtual void FreeRef()
527 git_describe_result_free(m_Ref);
531 class CAutoStatusList : public CSmartLibgit2Ref<git_status_list>
533 public:
534 ~CAutoStatusList()
536 CleanUp();
538 protected:
539 virtual void FreeRef()
541 git_status_list_free(m_Ref);