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->()
59 void Free(git_buf
* ref
)
72 void Free(git_strarray
* ref
)
74 git_strarray_free(ref
);
82 typedef CSmartBuffer
<git_buf
, CFreeBuf
> CAutoBuf
;
83 typedef CSmartBuffer
<git_strarray
, CFreeStrArray
> CAutoStrArray
;
85 template <typename ReferenceType
>
86 class CSmartLibgit2Ref
94 void Swap(CSmartLibgit2Ref
& tmp
)
108 ReferenceType
* Detach()
118 operator ReferenceType
*()
124 * Free the wrapped object and return a pointer to the wrapped object ReferenceType*
126 ReferenceType
** GetPointer()
139 return m_Ref
!= nullptr;
143 virtual void FreeRef() = 0;
154 ReferenceType
* m_Ref
;
157 class CAutoRepository
: public CSmartLibgit2Ref
<git_repository
>
160 CAutoRepository() {};
162 CAutoRepository(git_repository
* h
)
167 CAutoRepository(const CString
& gitDir
)
172 CAutoRepository(const CStringA
& gitDirA
)
177 int Open(const CString
& gitDir
)
179 return Open(CUnicodeUtils::GetUTF8(gitDir
));
182 int Open(const CStringA
& gitDirA
)
185 return git_repository_open(GetPointer(), gitDirA
);
194 virtual void FreeRef()
196 git_repository_free(m_Ref
);
200 class CAutoCommit
: public CSmartLibgit2Ref
<git_commit
>
205 CAutoCommit(git_commit
* h
)
215 virtual void FreeRef()
217 git_commit_free(m_Ref
);
221 class CAutoTree
: public CSmartLibgit2Ref
<git_tree
>
224 git_tree
* operator=(git_tree
* h
)
240 virtual void FreeRef()
242 git_tree_free(m_Ref
);
246 class CAutoObject
: public CSmartLibgit2Ref
<git_object
>
255 virtual void FreeRef()
257 git_object_free(m_Ref
);
261 class CAutoBlob
: public CSmartLibgit2Ref
<git_blob
>
270 virtual void FreeRef()
272 git_blob_free(m_Ref
);
276 class CAutoConfig
: public CSmartLibgit2Ref
<git_config
>
281 CAutoConfig(CAutoRepository
&repo
)
283 git_repository_config(&m_Ref
, repo
);
286 CAutoConfig(bool init
)
295 git_config_new(&m_Ref
);
303 int GetString(const CString
&key
, CString
&value
) const
310 if ((ret
= git_config_get_string_buf(buf
, m_Ref
, CUnicodeUtils::GetUTF8(key
))))
316 value
= CUnicodeUtils::GetUnicode((CStringA
)buf
->ptr
);
321 int GetBOOL(const CString
&key
, BOOL
&b
) const
326 return git_config_get_bool(&b
, m_Ref
, CUnicodeUtils::GetUTF8(key
));
329 int GetBool(const CString
&key
, bool &b
) const
336 if ((ret
= GetBOOL(key
, value
)) < 0)
345 virtual void FreeRef()
347 git_config_free(m_Ref
);
351 class CAutoReference
: public CSmartLibgit2Ref
<git_reference
>
356 CAutoReference(git_reference
* h
)
367 virtual void FreeRef()
369 git_reference_free(m_Ref
);
373 class CAutoTreeEntry
: public CSmartLibgit2Ref
<git_tree_entry
>
382 virtual void FreeRef()
384 git_tree_entry_free(m_Ref
);
388 class CAutoDiff
: public CSmartLibgit2Ref
<git_diff
>
397 virtual void FreeRef()
399 git_diff_free(m_Ref
);
403 class CAutoPatch
: public CSmartLibgit2Ref
<git_patch
>
412 virtual void FreeRef()
414 git_patch_free(m_Ref
);
418 class CAutoDiffStats
: public CSmartLibgit2Ref
<git_diff_stats
>
427 virtual void FreeRef()
429 git_diff_stats_free(m_Ref
);
433 class CAutoIndex
: public CSmartLibgit2Ref
<git_index
>
442 virtual void FreeRef()
444 git_index_free(m_Ref
);
448 class CAutoRemote
: public CSmartLibgit2Ref
<git_remote
>
457 virtual void FreeRef()
459 git_remote_free(m_Ref
);
463 class CAutoReflog
: public CSmartLibgit2Ref
<git_reflog
>
472 virtual void FreeRef()
474 git_reflog_free(m_Ref
);
478 class CAutoRevwalk
: public CSmartLibgit2Ref
<git_revwalk
>
487 virtual void FreeRef()
489 git_revwalk_free(m_Ref
);
493 class CAutoBranchIterator
: public CSmartLibgit2Ref
<git_branch_iterator
>
496 ~CAutoBranchIterator()
502 virtual void FreeRef()
504 git_branch_iterator_free(m_Ref
);
508 class CAutoDescribeResult
: public CSmartLibgit2Ref
<git_describe_result
>
511 ~CAutoDescribeResult()
517 virtual void FreeRef()
519 git_describe_result_free(m_Ref
);
523 class CAutoStatusList
: public CSmartLibgit2Ref
<git_status_list
>
531 virtual void FreeRef()
533 git_status_list_free(m_Ref
);