Code cleanup
[TortoiseGit.git] / src / Git / GitRevLoglist.cpp
blobf2db4561d574097f470c95fcb6c904ff17876f8d
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 "GitRevLoglist.h"
23 #include "Git.h"
24 #include "gitdll.h"
25 #include "UnicodeUtils.h"
27 typedef CComCritSecLock<CComCriticalSection> CAutoLocker;
29 GitRevLoglist::GitRevLoglist(void)
31 GitRev();
32 m_Action = 0;
33 m_RebaseAction = 0;
34 m_IsFull = FALSE;
35 m_IsUpdateing = FALSE;
36 m_IsCommitParsed = FALSE;
37 m_IsDiffFiles = FALSE;
38 m_CallDiffAsync = nullptr;
39 m_IsSimpleListReady = FALSE;
40 m_Mark = 0;
42 SecureZeroMemory(&m_GitCommit, sizeof(GIT_COMMIT));
45 GitRevLoglist::~GitRevLoglist(void)
47 if (!m_IsCommitParsed && m_GitCommit.m_pGitCommit)
48 git_free_commit(&m_GitCommit);
51 void GitRevLoglist::Clear()
53 GitRev::Clear();
54 m_Action = 0;
55 m_Files.Clear();
56 m_Ref.Empty();
57 m_RefAction.Empty();
58 m_Mark = 0;
62 int GitRevLoglist::SafeGetSimpleList(CGit* git)
64 if (InterlockedExchange(&m_IsUpdateing, TRUE) == TRUE)
65 return 0;
67 m_SimpleFileList.clear();
68 git->CheckAndInitDll();
69 GIT_COMMIT commit = { 0 };
70 GIT_COMMIT_LIST list;
71 GIT_HASH parent;
73 CAutoLocker lock(g_Git.m_critGitDllSec);
75 try
77 if (git_get_commit_from_hash(&commit, m_CommitHash.m_hash))
78 return -1;
80 catch (char *)
82 return -1;
85 int i = 0;
86 bool isRoot = m_ParentHash.empty();
87 git_get_commit_first_parent(&commit, &list);
88 while (git_get_commit_next_parent(&list, parent) == 0 || isRoot)
90 GIT_FILE file = 0;
91 int count = 0;
92 try
94 if (isRoot)
95 git_root_diff(git->GetGitSimpleListDiff(), commit.m_hash, &file, &count, 0);
96 else
97 git_do_diff(git->GetGitSimpleListDiff(), parent, commit.m_hash, &file, &count, 0);
99 catch (char *)
101 return -1;
104 isRoot = false;
106 for (int j = 0; j < count; ++j)
108 char* newname;
109 char* oldname;
110 int mode, IsBin, inc, dec;
114 git_get_diff_file(git->GetGitSimpleListDiff(), file, j, &newname, &oldname, &mode, &IsBin, &inc, &dec);
116 catch (char *)
118 return -1;
121 m_SimpleFileList.push_back(CUnicodeUtils::GetUnicode(newname));
124 git_diff_flush(git->GetGitSimpleListDiff());
125 ++i;
128 InterlockedExchange(&m_IsUpdateing, FALSE);
129 InterlockedExchange(&m_IsSimpleListReady, TRUE);
130 git_free_commit(&commit);
132 return 0;
135 int GitRevLoglist::SafeFetchFullInfo(CGit* git)
137 if (InterlockedExchange(&m_IsUpdateing, TRUE) == TRUE)
138 return 0;
140 m_Files.Clear();
141 git->CheckAndInitDll();
142 GIT_COMMIT commit = { 0 };
143 GIT_COMMIT_LIST list;
144 GIT_HASH parent;
146 CAutoLocker lock(g_Git.m_critGitDllSec);
150 if (git_get_commit_from_hash(&commit, m_CommitHash.m_hash))
151 return -1;
153 catch (char *)
155 return -1;
158 int i = 0;
159 git_get_commit_first_parent(&commit, &list);
160 bool isRoot = (list == nullptr);
162 while (git_get_commit_next_parent(&list, parent) == 0 || isRoot)
164 GIT_FILE file = 0;
165 int count = 0;
169 if (isRoot)
170 git_root_diff(git->GetGitDiff(), m_CommitHash.m_hash, &file, &count, 1);
171 else
172 git_do_diff(git->GetGitDiff(), parent, commit.m_hash, &file, &count, 1);
174 catch (char*)
176 git_free_commit(&commit);
177 return -1;
179 isRoot = false;
181 CTGitPath path;
182 CString strnewname;
183 CString stroldname;
185 for (int j = 0; j < count; ++j)
187 path.Reset();
188 char* newname;
189 char* oldname;
191 strnewname.Empty();
192 stroldname.Empty();
194 int mode, IsBin, inc, dec;
195 git_get_diff_file(git->GetGitDiff(), file, j, &newname, &oldname, &mode, &IsBin, &inc, &dec);
197 git->StringAppend(&strnewname, (BYTE*)newname, CP_UTF8);
198 git->StringAppend(&stroldname, (BYTE*)oldname, CP_UTF8);
200 path.SetFromGit(strnewname, &stroldname);
201 path.ParserAction((BYTE)mode);
202 path.m_ParentNo = i;
204 m_Action |= path.m_Action;
206 if (IsBin)
208 path.m_StatAdd = _T("-");
209 path.m_StatDel = _T("-");
211 else
213 path.m_StatAdd.Format(_T("%d"), inc);
214 path.m_StatDel.Format(_T("%d"), dec);
216 m_Files.AddPath(path);
218 git_diff_flush(git->GetGitDiff());
219 ++i;
222 InterlockedExchange(&m_IsUpdateing, FALSE);
223 InterlockedExchange(&m_IsFull, TRUE);
224 git_free_commit(&commit);
226 return 0;