1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2014-2017 - 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
, void FreeFunction(HandleType
*)>
36 operator HandleType
*()
41 HandleType
* operator->()
52 CSmartBuffer(const CSmartBuffer
&) = delete;
53 CSmartBuffer
& operator=(const CSmartBuffer
&) = delete;
58 typedef CSmartBuffer
<git_buf
, git_buf_free
> CAutoBuf
;
59 typedef CSmartBuffer
<git_strarray
, git_strarray_free
> CAutoStrArray
;
61 template <typename ReferenceType
, void FreeFunction(ReferenceType
*)>
62 class CSmartLibgit2Ref
75 void Swap(CSmartLibgit2Ref
& tmp
)
90 ReferenceType
* Detach()
92 ReferenceType
* p
= m_Ref
;
98 operator ReferenceType
*() const
104 * Free the wrapped object and return a pointer to the wrapped object ReferenceType*
106 ReferenceType
** GetPointer()
112 operator bool() const
119 return m_Ref
!= nullptr;
123 CSmartLibgit2Ref(const CSmartLibgit2Ref
&) = delete;
124 CSmartLibgit2Ref
& operator=(const CSmartLibgit2Ref
&) = delete;
127 ReferenceType
* m_Ref
;
130 typedef CSmartLibgit2Ref
<git_object
, git_object_free
> CAutoObject
;
131 typedef CSmartLibgit2Ref
<git_submodule
, git_submodule_free
> CAutoSubmodule
;
132 typedef CSmartLibgit2Ref
<git_blob
, git_blob_free
> CAutoBlob
;
133 typedef CSmartLibgit2Ref
<git_reference
, git_reference_free
> CAutoReference
;
134 typedef CSmartLibgit2Ref
<git_tag
, git_tag_free
> CAutoTag
;
135 typedef CSmartLibgit2Ref
<git_tree_entry
, git_tree_entry_free
> CAutoTreeEntry
;
136 typedef CSmartLibgit2Ref
<git_diff
, git_diff_free
> CAutoDiff
;
137 typedef CSmartLibgit2Ref
<git_patch
, git_patch_free
> CAutoPatch
;
138 typedef CSmartLibgit2Ref
<git_diff_stats
, git_diff_stats_free
> CAutoDiffStats
;
139 typedef CSmartLibgit2Ref
<git_index
, git_index_free
> CAutoIndex
;
140 typedef CSmartLibgit2Ref
<git_remote
, git_remote_free
> CAutoRemote
;
141 typedef CSmartLibgit2Ref
<git_reflog
, git_reflog_free
> CAutoReflog
;
142 typedef CSmartLibgit2Ref
<git_revwalk
, git_revwalk_free
> CAutoRevwalk
;
143 typedef CSmartLibgit2Ref
<git_branch_iterator
, git_branch_iterator_free
> CAutoBranchIterator
;
144 typedef CSmartLibgit2Ref
<git_reference_iterator
, git_reference_iterator_free
> CAutoReferenceIterator
;
145 typedef CSmartLibgit2Ref
<git_describe_result
, git_describe_result_free
> CAutoDescribeResult
;
146 typedef CSmartLibgit2Ref
<git_status_list
, git_status_list_free
> CAutoStatusList
;
147 typedef CSmartLibgit2Ref
<git_note
, git_note_free
> CAutoNote
;
149 class CAutoRepository
: public CSmartLibgit2Ref
<git_repository
, git_repository_free
>
152 CAutoRepository() {};
154 explicit CAutoRepository(git_repository
*&& h
)
159 CAutoRepository(CAutoRepository
&& that
)
161 m_Ref
= that
.Detach();
164 #if defined(_MFC_VER) || defined(CSTRING_AVAILABLE)
165 CAutoRepository(const CString
& gitDir
)
170 CAutoRepository(const CStringA
& gitDirA
)
175 int Open(const CString
& gitDir
)
177 return Open(CUnicodeUtils::GetUTF8(gitDir
));
182 CAutoRepository(const CAutoRepository
&) = delete;
183 CAutoRepository
& operator=(const CAutoRepository
&) = delete;
186 int Open(const char* gitDirA
)
188 return git_repository_open(GetPointer(), gitDirA
);
192 class CAutoCommit
: public CSmartLibgit2Ref
<git_commit
, git_commit_free
>
197 explicit CAutoCommit(git_commit
*&& h
)
203 CAutoCommit(const CAutoCommit
&) = delete;
204 CAutoCommit
& operator=(const CAutoCommit
&) = delete;
207 class CAutoTree
: public CSmartLibgit2Ref
<git_tree
, git_tree_free
>
212 void ConvertFrom(CAutoObject
&& h
)
214 if (m_Ref
!= (git_tree
*)(git_object
*)h
)
217 m_Ref
= (git_tree
*)h
.Detach();
222 CAutoTree(const CAutoTree
&) = delete;
223 CAutoTree
& operator=(const CAutoTree
&) = delete;
226 class CAutoConfig
: public CSmartLibgit2Ref
<git_config
, git_config_free
>
231 CAutoConfig(CAutoRepository
&repo
)
233 git_repository_config(&m_Ref
, repo
);
236 CAutoConfig(bool init
)
245 git_config_new(&m_Ref
);
248 #if defined(_MFC_VER) || defined(CSTRING_AVAILABLE)
249 int GetString(const CString
&key
, CString
&value
) const
256 if ((ret
= git_config_get_string_buf(buf
, m_Ref
, CUnicodeUtils::GetUTF8(key
))))
262 value
= CUnicodeUtils::GetUnicode((CStringA
)buf
->ptr
);
267 int GetBOOL(const CString
&key
, BOOL
&b
) const
272 return git_config_get_bool(&b
, m_Ref
, CUnicodeUtils::GetUTF8(key
));
275 int GetBool(const CString
&key
, bool &b
) const
282 if ((ret
= GetBOOL(key
, value
)) < 0)
292 CAutoConfig(const CAutoConfig
&) = delete;
293 CAutoConfig
& operator=(const CAutoConfig
&) = delete;