1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2014-2016 - 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
>
36 operator HandleType
*()
41 HandleType
* operator->()
48 FreeFunction::Free(&m_Ref
);
52 CSmartBuffer(const CSmartBuffer
&) = delete;
53 CSmartBuffer
& operator=(const CSmartBuffer
&) = delete;
61 static void Free(git_buf
* ref
)
69 static void Free(git_strarray
* ref
)
71 git_strarray_free(ref
);
75 typedef CSmartBuffer
<git_buf
, CFreeBuf
> CAutoBuf
;
76 typedef CSmartBuffer
<git_strarray
, CFreeStrArray
> CAutoStrArray
;
78 template <typename ReferenceType
>
79 class CSmartLibgit2Ref
87 void Swap(CSmartLibgit2Ref
& tmp
)
101 ReferenceType
* Detach()
103 ReferenceType
* p
= m_Ref
;
109 operator ReferenceType
*() const
115 * Free the wrapped object and return a pointer to the wrapped object ReferenceType*
117 ReferenceType
** GetPointer()
123 operator bool() const
130 return m_Ref
!= nullptr;
134 CSmartLibgit2Ref(const CSmartLibgit2Ref
&) = delete;
135 CSmartLibgit2Ref
& operator=(const CSmartLibgit2Ref
&) = delete;
138 virtual void FreeRef() = 0;
149 ReferenceType
* m_Ref
;
152 class CAutoRepository
: public CSmartLibgit2Ref
<git_repository
>
155 CAutoRepository() {};
157 CAutoRepository(git_repository
* h
)
162 CAutoRepository(const CString
& gitDir
)
167 CAutoRepository(const CStringA
& gitDirA
)
172 CAutoRepository(CAutoRepository
&& that
)
174 m_Ref
= that
.Detach();
177 int Open(const CString
& gitDir
)
179 return Open(CUnicodeUtils::GetUTF8(gitDir
));
188 CAutoRepository(const CAutoRepository
&) = delete;
189 CAutoRepository
& operator=(const CAutoRepository
&) = delete;
192 int Open(const CStringA
& gitDirA
)
194 return git_repository_open(GetPointer(), gitDirA
);
197 virtual void FreeRef()
199 git_repository_free(m_Ref
);
203 class CAutoSubmodule
: public CSmartLibgit2Ref
<git_submodule
>
214 CAutoSubmodule(const CAutoSubmodule
&) = delete;
215 CAutoSubmodule
& operator=(const CAutoSubmodule
&) = delete;
218 virtual void FreeRef()
220 git_submodule_free(m_Ref
);
224 class CAutoCommit
: public CSmartLibgit2Ref
<git_commit
>
229 CAutoCommit(git_commit
* h
)
240 CAutoCommit(const CAutoCommit
&) = delete;
241 CAutoCommit
& operator=(const CAutoCommit
&) = delete;
244 virtual void FreeRef()
246 git_commit_free(m_Ref
);
250 class CAutoTree
: public CSmartLibgit2Ref
<git_tree
>
255 git_tree
* operator=(git_tree
* h
)
271 CAutoTree(const CAutoTree
&) = delete;
272 CAutoTree
& operator=(const CAutoTree
&) = delete;
275 virtual void FreeRef()
277 git_tree_free(m_Ref
);
281 class CAutoObject
: public CSmartLibgit2Ref
<git_object
>
292 CAutoObject(const CAutoObject
&) = delete;
293 CAutoObject
& operator=(const CAutoObject
&) = delete;
296 virtual void FreeRef()
298 git_object_free(m_Ref
);
302 class CAutoBlob
: public CSmartLibgit2Ref
<git_blob
>
313 CAutoBlob(const CAutoBlob
&) = delete;
314 CAutoBlob
& operator=(const CAutoBlob
&) = delete;
317 virtual void FreeRef()
319 git_blob_free(m_Ref
);
323 class CAutoConfig
: public CSmartLibgit2Ref
<git_config
>
328 CAutoConfig(CAutoRepository
&repo
)
330 git_repository_config(&m_Ref
, repo
);
333 CAutoConfig(bool init
)
342 git_config_new(&m_Ref
);
350 int GetString(const CString
&key
, CString
&value
) const
357 if ((ret
= git_config_get_string_buf(buf
, m_Ref
, CUnicodeUtils::GetUTF8(key
))))
363 value
= CUnicodeUtils::GetUnicode((CStringA
)buf
->ptr
);
368 int GetBOOL(const CString
&key
, BOOL
&b
) const
373 return git_config_get_bool(&b
, m_Ref
, CUnicodeUtils::GetUTF8(key
));
376 int GetBool(const CString
&key
, bool &b
) const
383 if ((ret
= GetBOOL(key
, value
)) < 0)
392 CAutoConfig(const CAutoConfig
&) = delete;
393 CAutoConfig
& operator=(const CAutoConfig
&) = delete;
396 virtual void FreeRef()
398 git_config_free(m_Ref
);
402 class CAutoReference
: public CSmartLibgit2Ref
<git_reference
>
407 CAutoReference(git_reference
* h
)
418 CAutoReference(const CAutoReference
&) = delete;
419 CAutoReference
& operator=(const CAutoReference
&) = delete;
422 virtual void FreeRef()
424 git_reference_free(m_Ref
);
428 class CAutoTag
: public CSmartLibgit2Ref
<git_tag
>
444 CAutoTag(const CAutoTag
&) = delete;
445 CAutoTag
& operator=(const CAutoTag
&) = delete;
448 virtual void FreeRef()
454 class CAutoTreeEntry
: public CSmartLibgit2Ref
<git_tree_entry
>
465 CAutoTreeEntry(const CAutoTreeEntry
&) = delete;
466 CAutoTreeEntry
& operator=(const CAutoTreeEntry
&) = delete;
469 virtual void FreeRef()
471 git_tree_entry_free(m_Ref
);
475 class CAutoDiff
: public CSmartLibgit2Ref
<git_diff
>
486 CAutoDiff(const CAutoDiff
&) = delete;
487 CAutoDiff
& operator=(const CAutoDiff
&) = delete;
490 virtual void FreeRef()
492 git_diff_free(m_Ref
);
496 class CAutoPatch
: public CSmartLibgit2Ref
<git_patch
>
507 CAutoPatch(const CAutoPatch
&) = delete;
508 CAutoPatch
& operator=(const CAutoPatch
&) = delete;
511 virtual void FreeRef()
513 git_patch_free(m_Ref
);
517 class CAutoDiffStats
: public CSmartLibgit2Ref
<git_diff_stats
>
528 CAutoDiffStats(const CAutoDiffStats
&) = delete;
529 CAutoDiffStats
& operator=(const CAutoDiffStats
&) = delete;
532 virtual void FreeRef()
534 git_diff_stats_free(m_Ref
);
538 class CAutoIndex
: public CSmartLibgit2Ref
<git_index
>
549 CAutoIndex(const CAutoIndex
& that
);
550 CAutoIndex
& operator=(const CAutoIndex
& that
);
553 virtual void FreeRef()
555 git_index_free(m_Ref
);
559 class CAutoRemote
: public CSmartLibgit2Ref
<git_remote
>
570 CAutoRemote(const CAutoRemote
&) = delete;
571 CAutoRemote
& operator=(const CAutoRemote
&) = delete;
574 virtual void FreeRef()
576 git_remote_free(m_Ref
);
580 class CAutoReflog
: public CSmartLibgit2Ref
<git_reflog
>
591 CAutoReflog(const CAutoReflog
&) = delete;
592 CAutoReflog
& operator=(const CAutoReflog
&) = delete;
595 virtual void FreeRef()
597 git_reflog_free(m_Ref
);
601 class CAutoRevwalk
: public CSmartLibgit2Ref
<git_revwalk
>
612 CAutoRevwalk(const CAutoRevwalk
&) = delete;
613 CAutoRevwalk
& operator=(const CAutoRevwalk
&) = delete;
616 virtual void FreeRef()
618 git_revwalk_free(m_Ref
);
622 class CAutoBranchIterator
: public CSmartLibgit2Ref
<git_branch_iterator
>
625 CAutoBranchIterator() {};
627 ~CAutoBranchIterator()
633 CAutoBranchIterator(const CAutoBranchIterator
&) = delete;
634 CAutoBranchIterator
& operator=(const CAutoBranchIterator
&) = delete;
637 virtual void FreeRef()
639 git_branch_iterator_free(m_Ref
);
643 class CAutoReferenceIterator
: public CSmartLibgit2Ref
<git_reference_iterator
>
646 CAutoReferenceIterator() {};
648 ~CAutoReferenceIterator()
654 CAutoReferenceIterator(const CAutoReferenceIterator
&) = delete;
655 CAutoReferenceIterator
& operator=(const CAutoReferenceIterator
&) = delete;
658 virtual void FreeRef()
660 git_reference_iterator_free(m_Ref
);
664 class CAutoDescribeResult
: public CSmartLibgit2Ref
<git_describe_result
>
667 CAutoDescribeResult() {};
669 ~CAutoDescribeResult()
675 CAutoDescribeResult(const CAutoDescribeResult
&) = delete;
676 CAutoDescribeResult
& operator=(const CAutoDescribeResult
&) = delete;
679 virtual void FreeRef()
681 git_describe_result_free(m_Ref
);
685 class CAutoStatusList
: public CSmartLibgit2Ref
<git_status_list
>
688 CAutoStatusList() {};
696 CAutoStatusList(const CAutoStatusList
&) = delete;
697 CAutoStatusList
& operator=(const CAutoStatusList
&) = delete;
700 virtual void FreeRef()
702 git_status_list_free(m_Ref
);