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 virtual void FreeRef()
204 git_repository_free(m_Ref
);
208 class CAutoCommit
: public CSmartLibgit2Ref
<git_commit
>
213 CAutoCommit(git_commit
* h
)
223 virtual void FreeRef()
225 git_commit_free(m_Ref
);
229 class CAutoTree
: public CSmartLibgit2Ref
<git_tree
>
232 git_tree
* operator=(git_tree
* h
)
248 virtual void FreeRef()
250 git_tree_free(m_Ref
);
254 class CAutoObject
: public CSmartLibgit2Ref
<git_object
>
263 virtual void FreeRef()
265 git_object_free(m_Ref
);
269 class CAutoBlob
: public CSmartLibgit2Ref
<git_blob
>
278 virtual void FreeRef()
280 git_blob_free(m_Ref
);
284 class CAutoConfig
: public CSmartLibgit2Ref
<git_config
>
289 CAutoConfig(CAutoRepository
&repo
)
291 git_repository_config(&m_Ref
, repo
);
294 CAutoConfig(bool init
)
303 git_config_new(&m_Ref
);
311 int GetString(const CString
&key
, CString
&value
) const
318 if ((ret
= git_config_get_string_buf(buf
, m_Ref
, CUnicodeUtils::GetUTF8(key
))))
324 value
= CUnicodeUtils::GetUnicode((CStringA
)buf
->ptr
);
329 int GetBOOL(const CString
&key
, BOOL
&b
) const
334 return git_config_get_bool(&b
, m_Ref
, CUnicodeUtils::GetUTF8(key
));
337 int GetBool(const CString
&key
, bool &b
) const
344 if ((ret
= GetBOOL(key
, value
)) < 0)
353 virtual void FreeRef()
355 git_config_free(m_Ref
);
359 class CAutoReference
: public CSmartLibgit2Ref
<git_reference
>
364 CAutoReference(git_reference
* h
)
375 virtual void FreeRef()
377 git_reference_free(m_Ref
);
381 class CAutoTreeEntry
: public CSmartLibgit2Ref
<git_tree_entry
>
390 virtual void FreeRef()
392 git_tree_entry_free(m_Ref
);
396 class CAutoDiff
: public CSmartLibgit2Ref
<git_diff
>
405 virtual void FreeRef()
407 git_diff_free(m_Ref
);
411 class CAutoPatch
: public CSmartLibgit2Ref
<git_patch
>
420 virtual void FreeRef()
422 git_patch_free(m_Ref
);
426 class CAutoDiffStats
: public CSmartLibgit2Ref
<git_diff_stats
>
435 virtual void FreeRef()
437 git_diff_stats_free(m_Ref
);
441 class CAutoIndex
: public CSmartLibgit2Ref
<git_index
>
450 virtual void FreeRef()
452 git_index_free(m_Ref
);
456 class CAutoRemote
: public CSmartLibgit2Ref
<git_remote
>
465 virtual void FreeRef()
467 git_remote_free(m_Ref
);
471 class CAutoReflog
: public CSmartLibgit2Ref
<git_reflog
>
480 virtual void FreeRef()
482 git_reflog_free(m_Ref
);
486 class CAutoRevwalk
: public CSmartLibgit2Ref
<git_revwalk
>
495 virtual void FreeRef()
497 git_revwalk_free(m_Ref
);
501 class CAutoBranchIterator
: public CSmartLibgit2Ref
<git_branch_iterator
>
504 ~CAutoBranchIterator()
510 virtual void FreeRef()
512 git_branch_iterator_free(m_Ref
);
516 class CAutoDescribeResult
: public CSmartLibgit2Ref
<git_describe_result
>
519 ~CAutoDescribeResult()
525 virtual void FreeRef()
527 git_describe_result_free(m_Ref
);
531 class CAutoStatusList
: public CSmartLibgit2Ref
<git_status_list
>
539 virtual void FreeRef()
541 git_status_list_free(m_Ref
);