1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2015 - TortoiseGit
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software Foundation,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include <ATLComTime.h>
25 #include "UnicodeUtils.h"
27 typedef CComCritSecLock
<CComCriticalSection
> CAutoLocker
;
39 this->m_ParentHash
.clear();
40 m_CommitterName
.Empty();
41 m_CommitterEmail
.Empty();
49 int GitRev::ParserParentFromCommit(GIT_COMMIT
*commit
)
51 this->m_ParentHash
.clear();
55 git_get_commit_first_parent(commit
,&list
);
56 while(git_get_commit_next_parent(&list
,parent
)==0)
58 m_ParentHash
.push_back(CGitHash((char *)parent
));
63 int GitRev::ParserFromCommit(GIT_COMMIT
*commit
)
67 if(commit
->m_Encode
!= 0 && commit
->m_EncodeSize
!= 0)
70 CGit::StringAppend(&str
, (BYTE
*)commit
->m_Encode
, CP_UTF8
, commit
->m_EncodeSize
);
71 encode
= CUnicodeUtils::GetCPCode(str
);
74 this->m_CommitHash
= commit
->m_hash
;
76 this->m_AuthorDate
= commit
->m_Author
.Date
;
78 this->m_AuthorEmail
.Empty();
79 CGit::StringAppend(&m_AuthorEmail
, (BYTE
*)commit
->m_Author
.Email
, encode
, commit
->m_Author
.EmailSize
);
81 this->m_AuthorName
.Empty();
82 CGit::StringAppend(&m_AuthorName
, (BYTE
*)commit
->m_Author
.Name
, encode
, commit
->m_Author
.NameSize
);
85 CGit::StringAppend(&m_Body
, (BYTE
*)commit
->m_Body
, encode
, commit
->m_BodySize
);
87 this->m_CommitterDate
= commit
->m_Committer
.Date
;
89 this->m_CommitterEmail
.Empty();
90 CGit::StringAppend(&m_CommitterEmail
, (BYTE
*)commit
->m_Committer
.Email
, encode
, commit
->m_Committer
.EmailSize
);
92 this->m_CommitterName
.Empty();
93 CGit::StringAppend(&m_CommitterName
, (BYTE
*)commit
->m_Committer
.Name
, encode
, commit
->m_Committer
.NameSize
);
95 this->m_Subject
.Empty();
96 CGit::StringAppend(&m_Subject
, (BYTE
*)commit
->m_Subject
,encode
,commit
->m_SubjectSize
);
100 void GitRev::DbgPrint()
102 ATLTRACE(_T("Commit %s\r\n"), this->m_CommitHash
.ToString());
103 for (unsigned int i
= 0; i
< this->m_ParentHash
.size(); ++i
)
105 ATLTRACE(_T("Parent %i %s"), i
, m_ParentHash
[i
].ToString());
110 int GitRev::GetParentFromHash(CGitHash
&hash
)
112 CAutoLocker
lock(g_Git
.m_critGitDllSec
);
114 g_Git
.CheckAndInitDll();
119 if (git_get_commit_from_hash(&commit
, hash
.m_hash
))
121 m_sErr
= _T("git_get_commit_from_hash failed for ") + hash
.ToString();
127 m_sErr
= _T("Could not get parents of commit \"") + hash
.ToString() + _T("\".\nlibgit reports:\n") + CString(msg
);
131 this->ParserParentFromCommit(&commit
);
132 git_free_commit(&commit
);
134 this->m_CommitHash
=hash
;
139 int GitRev::GetCommitFromHash(CGitHash
&hash
)
141 CAutoLocker
lock(g_Git
.m_critGitDllSec
);
143 g_Git
.CheckAndInitDll();
145 return GetCommitFromHash_withoutLock(hash
);
148 int GitRev::GetCommitFromHash_withoutLock(CGitHash
&hash
)
153 if (git_get_commit_from_hash(&commit
, hash
.m_hash
))
155 m_sErr
= _T("git_get_commit_from_hash failed for ") + hash
.ToString();
161 m_sErr
= _T("Could not get commit \"") + hash
.ToString() + _T("\".\nlibgit reports:\n") + CString(msg
);
165 this->ParserFromCommit(&commit
);
166 git_free_commit(&commit
);
173 int GitRev::GetCommit(const CString
& refname
)
175 CAutoLocker
lock(g_Git
.m_critGitDllSec
);
177 g_Git
.CheckAndInitDll();
179 if(refname
.GetLength() >= 8)
180 if(refname
.Find(_T("00000000")) == 0)
182 this->m_CommitHash
.Empty();
183 this->m_Subject
=_T("Working Copy");
188 rev
= CUnicodeUtils::GetUTF8(g_Git
.FixBranchName(refname
));
193 if (git_get_sha1(rev
.GetBuffer(), sha
))
195 m_sErr
= _T("Could not get SHA-1 of ref \"") + g_Git
.FixBranchName(refname
);
201 m_sErr
= _T("Could not get SHA-1 of ref \"") + g_Git
.FixBranchName(refname
) + _T("\".\nlibgit reports:\n") + CString(msg
);
205 CGitHash
hash((char*)sha
);
206 return GetCommitFromHash_withoutLock(hash
);