Disable unsupported commands
[TortoiseGit.git] / src / Utils / SmartLibgit2Ref.h
blob8cb0b25d0f29a385fa54079ea917b8b3cf864c9b
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.
20 #pragma once
21 #include "UnicodeUtils.h"
23 /**
24 * \ingroup Utils
25 * Helper classes for libgit2 references.
27 template <typename HandleType, void FreeFunction(HandleType*)>
28 class CSmartBuffer
30 public:
31 CSmartBuffer()
32 : m_Ref({0})
36 operator HandleType*()
38 return &m_Ref;
41 HandleType* operator->()
43 return &m_Ref;
46 ~CSmartBuffer()
48 FreeFunction(&m_Ref);
51 private:
52 CSmartBuffer(const CSmartBuffer&) = delete;
53 CSmartBuffer& operator=(const CSmartBuffer&) = delete;
55 HandleType m_Ref;
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
64 public:
65 CSmartLibgit2Ref()
66 : m_Ref(nullptr)
70 ~CSmartLibgit2Ref()
72 Free();
75 void Swap(CSmartLibgit2Ref& tmp)
77 ReferenceType* p;
79 p = m_Ref;
80 m_Ref = tmp.m_Ref;
81 tmp.m_Ref = p;
84 void Free()
86 FreeFunction(m_Ref);
87 m_Ref = nullptr;
90 ReferenceType* Detach()
92 ReferenceType* p = m_Ref;
93 m_Ref = nullptr;
95 return p;
98 operator ReferenceType*() const
100 return m_Ref;
104 * Free the wrapped object and return a pointer to the wrapped object ReferenceType*
106 ReferenceType** GetPointer()
108 Free();
109 return &m_Ref;
112 operator bool() const
114 return IsValid();
117 bool IsValid() const
119 return m_Ref != nullptr;
122 private:
123 CSmartLibgit2Ref(const CSmartLibgit2Ref&) = delete;
124 CSmartLibgit2Ref& operator=(const CSmartLibgit2Ref&) = delete;
126 protected:
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>
151 public:
152 CAutoRepository() {};
154 explicit CAutoRepository(git_repository*&& h)
156 m_Ref = h;
159 CAutoRepository(CAutoRepository&& that)
161 m_Ref = that.Detach();
164 #if defined(_MFC_VER) || defined(CSTRING_AVAILABLE)
165 CAutoRepository(const CString& gitDir)
167 Open(gitDir);
170 CAutoRepository(const CStringA& gitDirA)
172 Open(gitDirA);
175 int Open(const CString& gitDir)
177 return Open(CUnicodeUtils::GetUTF8(gitDir));
179 #endif
181 private:
182 CAutoRepository(const CAutoRepository&) = delete;
183 CAutoRepository& operator=(const CAutoRepository&) = delete;
185 protected:
186 int Open(const char* gitDirA)
188 return git_repository_open(GetPointer(), gitDirA);
192 class CAutoCommit : public CSmartLibgit2Ref<git_commit, git_commit_free>
194 public:
195 CAutoCommit() {}
197 explicit CAutoCommit(git_commit*&& h)
199 m_Ref = h;
202 private:
203 CAutoCommit(const CAutoCommit&) = delete;
204 CAutoCommit& operator=(const CAutoCommit&) = delete;
207 class CAutoTree : public CSmartLibgit2Ref<git_tree, git_tree_free>
209 public:
210 CAutoTree() {};
212 void ConvertFrom(CAutoObject&& h)
214 if (m_Ref != (git_tree*)(git_object*)h)
216 Free();
217 m_Ref = (git_tree*)h.Detach();
221 private:
222 CAutoTree(const CAutoTree&) = delete;
223 CAutoTree& operator=(const CAutoTree&) = delete;
226 class CAutoConfig : public CSmartLibgit2Ref<git_config, git_config_free>
228 public:
229 CAutoConfig() {}
231 CAutoConfig(CAutoRepository &repo)
233 git_repository_config(&m_Ref, repo);
236 CAutoConfig(bool init)
238 if (init)
239 New();
242 void New()
244 Free();
245 git_config_new(&m_Ref);
248 #if defined(_MFC_VER) || defined(CSTRING_AVAILABLE)
249 int GetString(const CString &key, CString &value) const
251 if (!IsValid())
252 return -1;
254 CAutoBuf buf;
255 int ret = 0;
256 if ((ret = git_config_get_string_buf(buf, m_Ref, CUnicodeUtils::GetUTF8(key))))
258 value.Empty();
259 return ret;
262 value = CUnicodeUtils::GetUnicode((CStringA)buf->ptr);
264 return ret;
267 int GetBOOL(const CString &key, BOOL &b) const
269 if (!IsValid())
270 return -1;
272 return git_config_get_bool(&b, m_Ref, CUnicodeUtils::GetUTF8(key));
275 int GetBool(const CString &key, bool &b) const
277 if (!IsValid())
278 return -1;
280 int value = FALSE;
281 int ret = 0;
282 if ((ret = GetBOOL(key, value)) < 0)
283 return ret;
285 b = (value == TRUE);
287 return ret;
289 #endif
291 private:
292 CAutoConfig(const CAutoConfig&) = delete;
293 CAutoConfig& operator=(const CAutoConfig&) = delete;