Code cleanup
[TortoiseGit.git] / src / Git / GitRev.cpp
blobaf02b23dec903afb6fb5bb5b904fb32712a89373
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.
20 #include "stdafx.h"
21 #include <ATLComTime.h>
22 #include "GitRev.h"
23 #include "Git.h"
24 #include "gitdll.h"
25 #include "UnicodeUtils.h"
27 typedef CComCritSecLock<CComCriticalSection> CAutoLocker;
29 GitRev::GitRev(void)
33 GitRev::~GitRev(void)
37 void GitRev::Clear()
39 this->m_ParentHash.clear();
40 m_CommitterName.Empty();
41 m_CommitterEmail.Empty();
42 m_Body.Empty();
43 m_Subject.Empty();
44 m_CommitHash.Empty();
45 m_sErr.Empty();
49 int GitRev::ParserParentFromCommit(GIT_COMMIT *commit)
51 this->m_ParentHash.clear();
52 GIT_COMMIT_LIST list;
53 GIT_HASH parent;
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));
60 return 0;
63 int GitRev::ParserFromCommit(GIT_COMMIT *commit)
65 int encode =CP_UTF8;
67 if(commit->m_Encode != 0 && commit->m_EncodeSize != 0)
69 CString str;
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);
84 this->m_Body.Empty();
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);
98 return 0;
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());
107 ATLTRACE(_T("\n"));
110 int GitRev::GetParentFromHash(CGitHash &hash)
112 CAutoLocker lock(g_Git.m_critGitDllSec);
114 g_Git.CheckAndInitDll();
116 GIT_COMMIT commit;
119 if (git_get_commit_from_hash(&commit, hash.m_hash))
121 m_sErr = _T("git_get_commit_from_hash failed for ") + hash.ToString();
122 return -1;
125 catch (char* msg)
127 m_sErr = _T("Could not get parents of commit \"") + hash.ToString() + _T("\".\nlibgit reports:\n") + CString(msg);
128 return -1;
131 this->ParserParentFromCommit(&commit);
132 git_free_commit(&commit);
134 this->m_CommitHash=hash;
136 return 0;
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)
150 GIT_COMMIT commit;
153 if (git_get_commit_from_hash(&commit, hash.m_hash))
155 m_sErr = _T("git_get_commit_from_hash failed for ") + hash.ToString();
156 return -1;
159 catch (char * msg)
161 m_sErr = _T("Could not get commit \"") + hash.ToString() + _T("\".\nlibgit reports:\n") + CString(msg);
162 return -1;
165 this->ParserFromCommit(&commit);
166 git_free_commit(&commit);
168 m_sErr.Empty();
170 return 0;
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");
184 m_sErr.Empty();
185 return 0;
187 CStringA rev;
188 rev= CUnicodeUtils::GetUTF8(g_Git.FixBranchName(refname));
189 GIT_HASH sha;
193 if (git_get_sha1(rev.GetBuffer(), sha))
195 m_sErr = _T("Could not get SHA-1 of ref \"") + g_Git.FixBranchName(refname);
196 return -1;
199 catch (char * msg)
201 m_sErr = _T("Could not get SHA-1 of ref \"") + g_Git.FixBranchName(refname) + _T("\".\nlibgit reports:\n") + CString(msg);
202 return -1;
205 CGitHash hash((char*)sha);
206 return GetCommitFromHash_withoutLock(hash);