Fix a regression: Libgit2 changed to way to read string config values
[TortoiseGit.git] / src / Utils / SmartLibgit2Ref.h
blob8b122174ad2af08d18c9a9ef6d35e49d5d3a3d33
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 protected:
53 HandleType m_Ref;
56 struct CFreeBuf
58 protected:
59 void Free(git_buf* ref)
61 git_buf_free(ref);
64 ~CFreeBuf()
69 struct CFreeStrArray
71 protected:
72 void Free(git_strarray* ref)
74 git_strarray_free(ref);
77 ~CFreeStrArray()
82 typedef CSmartBuffer<git_buf, CFreeBuf> CAutoBuf;
83 typedef CSmartBuffer<git_strarray, CFreeStrArray> CAutoStrArray;
85 template <typename ReferenceType>
86 class CSmartLibgit2Ref
88 public:
89 CSmartLibgit2Ref()
91 m_Ref = nullptr;
94 void Swap(CSmartLibgit2Ref& tmp)
96 ReferenceType* p;
98 p = m_Ref;
99 m_Ref = tmp.m_Ref;
100 tmp.m_Ref = p;
103 void Free()
105 CleanUp();
108 ReferenceType* Detach()
110 ReferenceType* p;
112 p = m_Ref;
113 m_Ref = nullptr;
115 return p;
118 operator ReferenceType*()
120 return m_Ref;
124 * Free the wrapped object and return a pointer to the wrapped object ReferenceType*
126 ReferenceType** GetPointer()
128 CleanUp();
129 return &m_Ref;
132 operator bool()
134 return IsValid();
137 bool IsValid() const
139 return m_Ref != nullptr;
142 protected:
143 virtual void FreeRef() = 0;
145 void CleanUp()
147 if (IsValid())
149 FreeRef();
150 m_Ref = nullptr;
154 ReferenceType* m_Ref;
157 class CAutoRepository : public CSmartLibgit2Ref<git_repository>
159 public:
160 CAutoRepository() {};
162 CAutoRepository(git_repository* h)
164 m_Ref = h;
167 CAutoRepository(const CString& gitDir)
169 Open(gitDir);
172 CAutoRepository(const CStringA& gitDirA)
174 Open(gitDirA);
177 int Open(const CString& gitDir)
179 return Open(CUnicodeUtils::GetUTF8(gitDir));
182 int Open(const CStringA& gitDirA)
184 CleanUp();
185 return git_repository_open(GetPointer(), gitDirA);
188 ~CAutoRepository()
190 CleanUp();
193 protected:
194 virtual void FreeRef()
196 git_repository_free(m_Ref);
200 class CAutoCommit : public CSmartLibgit2Ref<git_commit>
202 public:
203 CAutoCommit() {}
205 CAutoCommit(git_commit* h)
207 m_Ref = h;
210 ~CAutoCommit()
212 CleanUp();
214 protected:
215 virtual void FreeRef()
217 git_commit_free(m_Ref);
221 class CAutoTree : public CSmartLibgit2Ref<git_tree>
223 public:
224 git_tree* operator=(git_tree* h)
226 if (m_Ref != h)
228 CleanUp();
229 m_Ref = h;
231 return (*this);
234 ~CAutoTree()
236 CleanUp();
239 protected:
240 virtual void FreeRef()
242 git_tree_free(m_Ref);
246 class CAutoObject : public CSmartLibgit2Ref<git_object>
248 public:
249 ~CAutoObject()
251 CleanUp();
254 protected:
255 virtual void FreeRef()
257 git_object_free(m_Ref);
261 class CAutoBlob : public CSmartLibgit2Ref<git_blob>
263 public:
264 ~CAutoBlob()
266 CleanUp();
269 protected:
270 virtual void FreeRef()
272 git_blob_free(m_Ref);
276 class CAutoConfig : public CSmartLibgit2Ref<git_config>
278 public:
279 CAutoConfig() {}
281 CAutoConfig(CAutoRepository &repo)
283 git_repository_config(&m_Ref, repo);
286 CAutoConfig(bool init)
288 if (init)
289 New();
292 void New()
294 CleanUp();
295 git_config_new(&m_Ref);
298 ~CAutoConfig()
300 CleanUp();
303 int GetString(const CString &key, CString &value) const
305 if (!IsValid())
306 return -1;
308 CAutoBuf buf;
309 int ret = 0;
310 if ((ret = git_config_get_string_buf(buf, m_Ref, CUnicodeUtils::GetUTF8(key))))
312 value.Empty();
313 return ret;
316 value = CUnicodeUtils::GetUnicode((CStringA)buf->ptr);
318 return ret;
321 int GetBOOL(const CString &key, BOOL &b) const
323 if (!IsValid())
324 return -1;
326 return git_config_get_bool(&b, m_Ref, CUnicodeUtils::GetUTF8(key));
329 int GetBool(const CString &key, bool &b) const
331 if (!IsValid())
332 return -1;
334 int value = FALSE;
335 int ret = 0;
336 if ((ret = GetBOOL(key, value)) < 0)
337 return ret;
339 b = (value == TRUE);
341 return ret;
344 protected:
345 virtual void FreeRef()
347 git_config_free(m_Ref);
351 class CAutoReference : public CSmartLibgit2Ref<git_reference>
353 public:
354 CAutoReference() {}
356 CAutoReference(git_reference* h)
358 m_Ref = h;
361 ~CAutoReference()
363 CleanUp();
366 protected:
367 virtual void FreeRef()
369 git_reference_free(m_Ref);
373 class CAutoTreeEntry : public CSmartLibgit2Ref<git_tree_entry>
375 public:
376 ~CAutoTreeEntry()
378 CleanUp();
381 protected:
382 virtual void FreeRef()
384 git_tree_entry_free(m_Ref);
388 class CAutoDiff : public CSmartLibgit2Ref<git_diff>
390 public:
391 ~CAutoDiff()
393 CleanUp();
396 protected:
397 virtual void FreeRef()
399 git_diff_free(m_Ref);
403 class CAutoPatch : public CSmartLibgit2Ref<git_patch>
405 public:
406 ~CAutoPatch()
408 CleanUp();
411 protected:
412 virtual void FreeRef()
414 git_patch_free(m_Ref);
418 class CAutoDiffStats : public CSmartLibgit2Ref<git_diff_stats>
420 public:
421 ~CAutoDiffStats()
423 CleanUp();
426 protected:
427 virtual void FreeRef()
429 git_diff_stats_free(m_Ref);
433 class CAutoIndex : public CSmartLibgit2Ref<git_index>
435 public:
436 ~CAutoIndex()
438 CleanUp();
441 protected:
442 virtual void FreeRef()
444 git_index_free(m_Ref);
448 class CAutoRemote : public CSmartLibgit2Ref<git_remote>
450 public:
451 ~CAutoRemote()
453 CleanUp();
456 protected:
457 virtual void FreeRef()
459 git_remote_free(m_Ref);
463 class CAutoReflog : public CSmartLibgit2Ref<git_reflog>
465 public:
466 ~CAutoReflog()
468 CleanUp();
471 protected:
472 virtual void FreeRef()
474 git_reflog_free(m_Ref);
478 class CAutoRevwalk : public CSmartLibgit2Ref<git_revwalk>
480 public:
481 ~CAutoRevwalk()
483 CleanUp();
486 protected:
487 virtual void FreeRef()
489 git_revwalk_free(m_Ref);
493 class CAutoBranchIterator : public CSmartLibgit2Ref<git_branch_iterator>
495 public:
496 ~CAutoBranchIterator()
498 CleanUp();
501 protected:
502 virtual void FreeRef()
504 git_branch_iterator_free(m_Ref);
508 class CAutoDescribeResult : public CSmartLibgit2Ref<git_describe_result>
510 public:
511 ~CAutoDescribeResult()
513 CleanUp();
516 protected:
517 virtual void FreeRef()
519 git_describe_result_free(m_Ref);
523 class CAutoStatusList : public CSmartLibgit2Ref<git_status_list>
525 public:
526 ~CAutoStatusList()
528 CleanUp();
530 protected:
531 virtual void FreeRef()
533 git_status_list_free(m_Ref);