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 ReferenceType
>
28 class CSmartLibgit2Ref
36 void Swap(CSmartLibgit2Ref
& tmp
)
50 ReferenceType
* Detach()
60 operator ReferenceType
*()
65 ReferenceType
** GetPointer()
77 return m_Ref
!= nullptr;
81 virtual void FreeRef() = 0;
95 class CAutoRepository
: public CSmartLibgit2Ref
<git_repository
>
100 CAutoRepository(git_repository
* h
)
105 CAutoRepository(const CString
& gitDir
)
110 CAutoRepository(const CStringA
& gitDirA
)
115 int Open(const CString
& gitDir
)
117 return Open(CUnicodeUtils::GetUTF8(gitDir
));
120 int Open(const CStringA
& gitDirA
)
123 return git_repository_open(GetPointer(), gitDirA
);
132 virtual void FreeRef()
134 git_repository_free(m_Ref
);
138 class CAutoCommit
: public CSmartLibgit2Ref
<git_commit
>
143 CAutoCommit(git_commit
* h
)
153 virtual void FreeRef()
155 git_commit_free(m_Ref
);
159 class CAutoTree
: public CSmartLibgit2Ref
<git_tree
>
162 git_tree
* operator=(git_tree
* h
)
178 virtual void FreeRef()
180 git_tree_free(m_Ref
);
184 class CAutoObject
: public CSmartLibgit2Ref
<git_object
>
193 virtual void FreeRef()
195 git_object_free(m_Ref
);
199 class CAutoBlob
: public CSmartLibgit2Ref
<git_blob
>
208 virtual void FreeRef()
210 git_blob_free(m_Ref
);
214 class CAutoConfig
: public CSmartLibgit2Ref
<git_config
>
219 CAutoConfig(CAutoRepository
&repo
)
221 git_repository_config(&m_Ref
, repo
);
224 CAutoConfig(bool init
)
233 git_config_new(&m_Ref
);
241 int GetString(const CString
&key
, CString
&value
) const
246 const char* out
= nullptr;
248 if ((ret
= git_config_get_string(&out
, m_Ref
, CUnicodeUtils::GetUTF8(key
))))
254 value
= CUnicodeUtils::GetUnicode((CStringA
)out
);
259 int GetBOOL(const CString
&key
, BOOL
&b
) const
264 return git_config_get_bool(&b
, m_Ref
, CUnicodeUtils::GetUTF8(key
));
267 int GetBool(const CString
&key
, bool &b
) const
274 if ((ret
= GetBOOL(key
, value
)))
283 virtual void FreeRef()
285 git_config_free(m_Ref
);
289 class CAutoReference
: public CSmartLibgit2Ref
<git_reference
>
294 CAutoReference(git_reference
* h
)
305 virtual void FreeRef()
307 git_reference_free(m_Ref
);
311 class CAutoTreeEntry
: public CSmartLibgit2Ref
<git_tree_entry
>
320 virtual void FreeRef()
322 git_tree_entry_free(m_Ref
);
326 class CAutoDiff
: public CSmartLibgit2Ref
<git_diff
>
335 virtual void FreeRef()
337 git_diff_free(m_Ref
);
341 class CAutoPatch
: public CSmartLibgit2Ref
<git_patch
>
350 virtual void FreeRef()
352 git_patch_free(m_Ref
);
356 class CAutoDiffStats
: public CSmartLibgit2Ref
<git_diff_stats
>
365 virtual void FreeRef()
367 git_diff_stats_free(m_Ref
);
371 class CAutoIndex
: public CSmartLibgit2Ref
<git_index
>
380 virtual void FreeRef()
382 git_index_free(m_Ref
);
386 class CAutoRemote
: public CSmartLibgit2Ref
<git_remote
>
395 virtual void FreeRef()
397 if (git_remote_connected(m_Ref
) == 1)
398 git_remote_disconnect(m_Ref
);
399 git_remote_free(m_Ref
);
403 class CAutoReflog
: public CSmartLibgit2Ref
<git_reflog
>
412 virtual void FreeRef()
414 git_reflog_free(m_Ref
);
418 class CAutoRevwalk
: public CSmartLibgit2Ref
<git_revwalk
>
427 virtual void FreeRef()
429 git_revwalk_free(m_Ref
);
433 class CAutoBranchIterator
: public CSmartLibgit2Ref
<git_branch_iterator
>
436 ~CAutoBranchIterator()
442 virtual void FreeRef()
444 git_branch_iterator_free(m_Ref
);
448 class CAutoDescribeResult
: public CSmartLibgit2Ref
<git_describe_result
>
451 ~CAutoDescribeResult()
457 virtual void FreeRef()
459 git_describe_result_free(m_Ref
);
463 template <typename HandleType
, class FreeFunction
>
464 class CSmartBuffer
: public FreeFunction
469 HandleType tmp
= { 0 };
473 operator HandleType
*()
478 HandleType
* operator->()
495 void Free(git_buf
* ref
)
508 void Free(git_strarray
* ref
)
510 git_strarray_free(ref
);
518 typedef CSmartBuffer
<git_buf
, CFreeBuf
> CAutoBuf
;
519 typedef CSmartBuffer
<git_strarray
, CFreeStrArray
> CAutoStrArray
;