Prefer to use VS2013 for compiling and testing on AppVeyor
[TortoiseGit.git] / src / Utils / SmartLibgit2Ref.h
blob45638aa9f6445fbdc3be6aa79901aaeb5f2e3c65
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 private:
202 CAutoRepository(const CAutoRepository& that);
203 CAutoRepository& operator=(const CAutoRepository& that);
205 protected:
206 virtual void FreeRef()
208 git_repository_free(m_Ref);
212 class CAutoSubmodule : public CSmartLibgit2Ref<git_submodule>
214 public:
215 CAutoSubmodule() {};
217 ~CAutoSubmodule()
219 CleanUp();
222 private:
223 CAutoSubmodule(const CAutoSubmodule& that);
224 CAutoSubmodule& operator=(const CAutoSubmodule& that);
226 protected:
227 virtual void FreeRef()
229 git_submodule_free(m_Ref);
233 class CAutoCommit : public CSmartLibgit2Ref<git_commit>
235 public:
236 CAutoCommit() {}
238 CAutoCommit(git_commit* h)
240 m_Ref = h;
243 ~CAutoCommit()
245 CleanUp();
248 private:
249 CAutoCommit(const CAutoCommit& that);
250 CAutoCommit& operator=(const CAutoCommit& that);
252 protected:
253 virtual void FreeRef()
255 git_commit_free(m_Ref);
259 class CAutoTree : public CSmartLibgit2Ref<git_tree>
261 public:
262 CAutoTree() {};
264 git_tree* operator=(git_tree* h)
266 if (m_Ref != h)
268 CleanUp();
269 m_Ref = h;
271 return (*this);
274 ~CAutoTree()
276 CleanUp();
279 private:
280 CAutoTree(const CAutoTree& that);
281 CAutoTree& operator=(const CAutoTree& that);
283 protected:
284 virtual void FreeRef()
286 git_tree_free(m_Ref);
290 class CAutoObject : public CSmartLibgit2Ref<git_object>
292 public:
293 CAutoObject() {};
295 ~CAutoObject()
297 CleanUp();
300 private:
301 CAutoObject(const CAutoObject& that);
302 CAutoObject& operator=(const CAutoObject& that);
304 protected:
305 virtual void FreeRef()
307 git_object_free(m_Ref);
311 class CAutoBlob : public CSmartLibgit2Ref<git_blob>
313 public:
314 CAutoBlob() {};
316 ~CAutoBlob()
318 CleanUp();
321 private:
322 CAutoBlob(const CAutoBlob& that);
323 CAutoBlob& operator=(const CAutoBlob& that);
325 protected:
326 virtual void FreeRef()
328 git_blob_free(m_Ref);
332 class CAutoConfig : public CSmartLibgit2Ref<git_config>
334 public:
335 CAutoConfig() {}
337 CAutoConfig(CAutoRepository &repo)
339 git_repository_config(&m_Ref, repo);
342 CAutoConfig(bool init)
344 if (init)
345 New();
348 void New()
350 CleanUp();
351 git_config_new(&m_Ref);
354 ~CAutoConfig()
356 CleanUp();
359 int GetString(const CString &key, CString &value) const
361 if (!IsValid())
362 return -1;
364 CAutoBuf buf;
365 int ret = 0;
366 if ((ret = git_config_get_string_buf(buf, m_Ref, CUnicodeUtils::GetUTF8(key))))
368 value.Empty();
369 return ret;
372 value = CUnicodeUtils::GetUnicode((CStringA)buf->ptr);
374 return ret;
377 int GetBOOL(const CString &key, BOOL &b) const
379 if (!IsValid())
380 return -1;
382 return git_config_get_bool(&b, m_Ref, CUnicodeUtils::GetUTF8(key));
385 int GetBool(const CString &key, bool &b) const
387 if (!IsValid())
388 return -1;
390 int value = FALSE;
391 int ret = 0;
392 if ((ret = GetBOOL(key, value)) < 0)
393 return ret;
395 b = (value == TRUE);
397 return ret;
400 private:
401 CAutoConfig(const CAutoConfig& that);
402 CAutoConfig& operator=(const CAutoConfig& that);
404 protected:
405 virtual void FreeRef()
407 git_config_free(m_Ref);
411 class CAutoReference : public CSmartLibgit2Ref<git_reference>
413 public:
414 CAutoReference() {}
416 CAutoReference(git_reference* h)
418 m_Ref = h;
421 ~CAutoReference()
423 CleanUp();
426 private:
427 CAutoReference(const CAutoReference& that);
428 CAutoReference& operator=(const CAutoReference& that);
430 protected:
431 virtual void FreeRef()
433 git_reference_free(m_Ref);
437 class CAutoTreeEntry : public CSmartLibgit2Ref<git_tree_entry>
439 public:
440 CAutoTreeEntry() {};
442 ~CAutoTreeEntry()
444 CleanUp();
447 private:
448 CAutoTreeEntry(const CAutoTreeEntry& that);
449 CAutoTreeEntry& operator=(const CAutoTreeEntry& that);
451 protected:
452 virtual void FreeRef()
454 git_tree_entry_free(m_Ref);
458 class CAutoDiff : public CSmartLibgit2Ref<git_diff>
460 public:
461 CAutoDiff() {};
463 ~CAutoDiff()
465 CleanUp();
468 private:
469 CAutoDiff(const CAutoDiff& that);
470 CAutoDiff& operator=(const CAutoDiff& that);
472 protected:
473 virtual void FreeRef()
475 git_diff_free(m_Ref);
479 class CAutoPatch : public CSmartLibgit2Ref<git_patch>
481 public:
482 CAutoPatch() {};
484 ~CAutoPatch()
486 CleanUp();
489 private:
490 CAutoPatch(const CAutoPatch& that);
491 CAutoPatch& operator=(const CAutoPatch& that);
493 protected:
494 virtual void FreeRef()
496 git_patch_free(m_Ref);
500 class CAutoDiffStats : public CSmartLibgit2Ref<git_diff_stats>
502 public:
503 CAutoDiffStats() {};
505 ~CAutoDiffStats()
507 CleanUp();
510 private:
511 CAutoDiffStats(const CAutoDiffStats& that);
512 CAutoDiffStats& operator=(const CAutoDiffStats& that);
514 protected:
515 virtual void FreeRef()
517 git_diff_stats_free(m_Ref);
521 class CAutoIndex : public CSmartLibgit2Ref<git_index>
523 public:
524 CAutoIndex() {};
526 ~CAutoIndex()
528 CleanUp();
531 private:
532 CAutoIndex(const CAutoIndex& that);
533 CAutoIndex& operator=(const CAutoIndex& that);
535 protected:
536 virtual void FreeRef()
538 git_index_free(m_Ref);
542 class CAutoRemote : public CSmartLibgit2Ref<git_remote>
544 public:
545 CAutoRemote() {};
547 ~CAutoRemote()
549 CleanUp();
552 private:
553 CAutoRemote(const CAutoRemote& that);
554 CAutoRemote& operator=(const CAutoRemote& that);
556 protected:
557 virtual void FreeRef()
559 git_remote_free(m_Ref);
563 class CAutoReflog : public CSmartLibgit2Ref<git_reflog>
565 public:
566 CAutoReflog() {};
568 ~CAutoReflog()
570 CleanUp();
573 private:
574 CAutoReflog(const CAutoReflog& that);
575 CAutoReflog& operator=(const CAutoReflog& that);
577 protected:
578 virtual void FreeRef()
580 git_reflog_free(m_Ref);
584 class CAutoRevwalk : public CSmartLibgit2Ref<git_revwalk>
586 public:
587 CAutoRevwalk() {};
589 ~CAutoRevwalk()
591 CleanUp();
594 private:
595 CAutoRevwalk(const CAutoRevwalk& that);
596 CAutoRevwalk& operator=(const CAutoRevwalk& that);
598 protected:
599 virtual void FreeRef()
601 git_revwalk_free(m_Ref);
605 class CAutoBranchIterator : public CSmartLibgit2Ref<git_branch_iterator>
607 public:
608 CAutoBranchIterator() {};
610 ~CAutoBranchIterator()
612 CleanUp();
615 private:
616 CAutoBranchIterator(const CAutoBranchIterator& that) ;
617 CAutoBranchIterator& operator=(const CAutoBranchIterator& that);
619 protected:
620 virtual void FreeRef()
622 git_branch_iterator_free(m_Ref);
626 class CAutoDescribeResult : public CSmartLibgit2Ref<git_describe_result>
628 public:
629 CAutoDescribeResult() {};
631 ~CAutoDescribeResult()
633 CleanUp();
636 private:
637 CAutoDescribeResult(const CAutoDescribeResult& that);
638 CAutoDescribeResult& operator=(const CAutoDescribeResult& that);
640 protected:
641 virtual void FreeRef()
643 git_describe_result_free(m_Ref);
647 class CAutoStatusList : public CSmartLibgit2Ref<git_status_list>
649 public:
650 CAutoStatusList() {};
652 ~CAutoStatusList()
654 CleanUp();
657 private:
658 CAutoStatusList(const CAutoStatusList& that);
659 CAutoStatusList& operator=(const CAutoStatusList& that);
661 protected:
662 virtual void FreeRef()
664 git_status_list_free(m_Ref);