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.
21 #include "UnicodeUtils.h"
25 * Helper classes for libgit2 references.
27 template <typename HandleType
, class FreeFunction
>
28 class CSmartBuffer
: public FreeFunction
33 HandleType tmp
= { 0 };
37 operator HandleType
*()
42 HandleType
* operator->()
53 CSmartBuffer(const CSmartBuffer
& that
);
54 CSmartBuffer
& operator=(const CSmartBuffer
& that
);
63 void Free(git_buf
* ref
)
76 void Free(git_strarray
* ref
)
78 git_strarray_free(ref
);
86 typedef CSmartBuffer
<git_buf
, CFreeBuf
> CAutoBuf
;
87 typedef CSmartBuffer
<git_strarray
, CFreeStrArray
> CAutoStrArray
;
89 template <typename ReferenceType
>
90 class CSmartLibgit2Ref
98 void Swap(CSmartLibgit2Ref
& tmp
)
112 ReferenceType
* Detach()
122 operator ReferenceType
*()
128 * Free the wrapped object and return a pointer to the wrapped object ReferenceType*
130 ReferenceType
** GetPointer()
143 return m_Ref
!= nullptr;
147 CSmartLibgit2Ref(const CSmartLibgit2Ref
& that
);
148 CSmartLibgit2Ref
& operator=(const CSmartLibgit2Ref
& that
);
151 virtual void FreeRef() = 0;
162 ReferenceType
* m_Ref
;
165 class CAutoRepository
: public CSmartLibgit2Ref
<git_repository
>
168 CAutoRepository() {};
170 CAutoRepository(git_repository
* h
)
175 CAutoRepository(const CString
& gitDir
)
180 CAutoRepository(const CStringA
& gitDirA
)
185 int Open(const CString
& gitDir
)
187 return Open(CUnicodeUtils::GetUTF8(gitDir
));
190 int Open(const CStringA
& gitDirA
)
193 return git_repository_open(GetPointer(), gitDirA
);
202 CAutoRepository(const CAutoRepository
& that
);
203 CAutoRepository
& operator=(const CAutoRepository
& that
);
206 virtual void FreeRef()
208 git_repository_free(m_Ref
);
212 class CAutoSubmodule
: public CSmartLibgit2Ref
<git_submodule
>
223 CAutoSubmodule(const CAutoSubmodule
& that
);
224 CAutoSubmodule
& operator=(const CAutoSubmodule
& that
);
227 virtual void FreeRef()
229 git_submodule_free(m_Ref
);
233 class CAutoCommit
: public CSmartLibgit2Ref
<git_commit
>
238 CAutoCommit(git_commit
* h
)
249 CAutoCommit(const CAutoCommit
& that
);
250 CAutoCommit
& operator=(const CAutoCommit
& that
);
253 virtual void FreeRef()
255 git_commit_free(m_Ref
);
259 class CAutoTree
: public CSmartLibgit2Ref
<git_tree
>
264 git_tree
* operator=(git_tree
* h
)
280 CAutoTree(const CAutoTree
& that
);
281 CAutoTree
& operator=(const CAutoTree
& that
);
284 virtual void FreeRef()
286 git_tree_free(m_Ref
);
290 class CAutoObject
: public CSmartLibgit2Ref
<git_object
>
301 CAutoObject(const CAutoObject
& that
);
302 CAutoObject
& operator=(const CAutoObject
& that
);
305 virtual void FreeRef()
307 git_object_free(m_Ref
);
311 class CAutoBlob
: public CSmartLibgit2Ref
<git_blob
>
322 CAutoBlob(const CAutoBlob
& that
);
323 CAutoBlob
& operator=(const CAutoBlob
& that
);
326 virtual void FreeRef()
328 git_blob_free(m_Ref
);
332 class CAutoConfig
: public CSmartLibgit2Ref
<git_config
>
337 CAutoConfig(CAutoRepository
&repo
)
339 git_repository_config(&m_Ref
, repo
);
342 CAutoConfig(bool init
)
351 git_config_new(&m_Ref
);
359 int GetString(const CString
&key
, CString
&value
) const
366 if ((ret
= git_config_get_string_buf(buf
, m_Ref
, CUnicodeUtils::GetUTF8(key
))))
372 value
= CUnicodeUtils::GetUnicode((CStringA
)buf
->ptr
);
377 int GetBOOL(const CString
&key
, BOOL
&b
) const
382 return git_config_get_bool(&b
, m_Ref
, CUnicodeUtils::GetUTF8(key
));
385 int GetBool(const CString
&key
, bool &b
) const
392 if ((ret
= GetBOOL(key
, value
)) < 0)
401 CAutoConfig(const CAutoConfig
& that
);
402 CAutoConfig
& operator=(const CAutoConfig
& that
);
405 virtual void FreeRef()
407 git_config_free(m_Ref
);
411 class CAutoReference
: public CSmartLibgit2Ref
<git_reference
>
416 CAutoReference(git_reference
* h
)
427 CAutoReference(const CAutoReference
& that
);
428 CAutoReference
& operator=(const CAutoReference
& that
);
431 virtual void FreeRef()
433 git_reference_free(m_Ref
);
437 class CAutoTreeEntry
: public CSmartLibgit2Ref
<git_tree_entry
>
448 CAutoTreeEntry(const CAutoTreeEntry
& that
);
449 CAutoTreeEntry
& operator=(const CAutoTreeEntry
& that
);
452 virtual void FreeRef()
454 git_tree_entry_free(m_Ref
);
458 class CAutoDiff
: public CSmartLibgit2Ref
<git_diff
>
469 CAutoDiff(const CAutoDiff
& that
);
470 CAutoDiff
& operator=(const CAutoDiff
& that
);
473 virtual void FreeRef()
475 git_diff_free(m_Ref
);
479 class CAutoPatch
: public CSmartLibgit2Ref
<git_patch
>
490 CAutoPatch(const CAutoPatch
& that
);
491 CAutoPatch
& operator=(const CAutoPatch
& that
);
494 virtual void FreeRef()
496 git_patch_free(m_Ref
);
500 class CAutoDiffStats
: public CSmartLibgit2Ref
<git_diff_stats
>
511 CAutoDiffStats(const CAutoDiffStats
& that
);
512 CAutoDiffStats
& operator=(const CAutoDiffStats
& that
);
515 virtual void FreeRef()
517 git_diff_stats_free(m_Ref
);
521 class CAutoIndex
: public CSmartLibgit2Ref
<git_index
>
532 CAutoIndex(const CAutoIndex
& that
);
533 CAutoIndex
& operator=(const CAutoIndex
& that
);
536 virtual void FreeRef()
538 git_index_free(m_Ref
);
542 class CAutoRemote
: public CSmartLibgit2Ref
<git_remote
>
553 CAutoRemote(const CAutoRemote
& that
);
554 CAutoRemote
& operator=(const CAutoRemote
& that
);
557 virtual void FreeRef()
559 git_remote_free(m_Ref
);
563 class CAutoReflog
: public CSmartLibgit2Ref
<git_reflog
>
574 CAutoReflog(const CAutoReflog
& that
);
575 CAutoReflog
& operator=(const CAutoReflog
& that
);
578 virtual void FreeRef()
580 git_reflog_free(m_Ref
);
584 class CAutoRevwalk
: public CSmartLibgit2Ref
<git_revwalk
>
595 CAutoRevwalk(const CAutoRevwalk
& that
);
596 CAutoRevwalk
& operator=(const CAutoRevwalk
& that
);
599 virtual void FreeRef()
601 git_revwalk_free(m_Ref
);
605 class CAutoBranchIterator
: public CSmartLibgit2Ref
<git_branch_iterator
>
608 CAutoBranchIterator() {};
610 ~CAutoBranchIterator()
616 CAutoBranchIterator(const CAutoBranchIterator
& that
) ;
617 CAutoBranchIterator
& operator=(const CAutoBranchIterator
& that
);
620 virtual void FreeRef()
622 git_branch_iterator_free(m_Ref
);
626 class CAutoDescribeResult
: public CSmartLibgit2Ref
<git_describe_result
>
629 CAutoDescribeResult() {};
631 ~CAutoDescribeResult()
637 CAutoDescribeResult(const CAutoDescribeResult
& that
);
638 CAutoDescribeResult
& operator=(const CAutoDescribeResult
& that
);
641 virtual void FreeRef()
643 git_describe_result_free(m_Ref
);
647 class CAutoStatusList
: public CSmartLibgit2Ref
<git_status_list
>
650 CAutoStatusList() {};
658 CAutoStatusList(const CAutoStatusList
& that
);
659 CAutoStatusList
& operator=(const CAutoStatusList
& that
);
662 virtual void FreeRef()
664 git_status_list_free(m_Ref
);