Refactored: Replace 'count' parameter of CGit::GetLogCmd() with CFilterData::m_Number...
[TortoiseGit.git] / src / TortoiseProc / LogDataVector.cpp
blobc56ad7e1d5a36220d555111edb68fdc0209313f6
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2009-2013, 2015 - TortoiseGit
4 // Copyright (C) 2007-2008 - 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 Description: start-up repository opening and reading
23 Author: Marco Costalba (C) 2005-2007
25 Copyright: See COPYING file that comes with this distribution
29 #include "stdafx.h"
30 #include "TortoiseProc.h"
31 #include "Git.h"
32 #include "GitHash.h"
33 #include "TGitPath.h"
34 #include "GitLogListBase.h"
35 #include "UnicodeUtils.h"
37 typedef CComCritSecLock<CComCriticalSection> CAutoLocker;
39 void CLogDataVector::ClearAll()
42 clear();
43 m_HashMap.clear();
44 m_Lns.clear();
46 m_FirstFreeLane=0;
47 m_Lns.clear();
48 if (m_pLogCache)
50 m_pLogCache->ClearAllLanes();
53 m_RawlogData.clear();
54 m_RawLogStart.clear();
57 //CLogDataVector Class
58 int CLogDataVector::ParserFromLog(CTGitPath *path, int count, int infomask, CString *range)
60 // only enable --follow on files
61 if ((path == NULL || path->IsDirectory()) && (infomask & CGit::LOG_INFO_FOLLOW))
62 infomask = infomask ^ CGit::LOG_INFO_FOLLOW;
64 CString gitrange = _T("HEAD");
65 if (range != nullptr)
66 gitrange = *range;
67 CFilterData filter;
68 filter.m_NumberOfLogs = count;
69 CString cmd = g_Git.GetLogCmd(gitrange, path, infomask, true, &filter);
71 if (!g_Git.CanParseRev(gitrange))
72 return 0;
74 try
76 [] { git_init(); } ();
78 catch (const char* msg)
80 MessageBox(NULL, _T("Could not initialize libgit.\nlibgit reports:\n") + CString(msg), _T("TortoiseGit"), MB_ICONERROR);
81 return -1;
84 GIT_LOG handle;
85 try
87 CAutoLocker lock(g_Git.m_critGitDllSec);
88 if (git_open_log(&handle,CUnicodeUtils::GetMulti(cmd, CP_UTF8).GetBuffer()))
90 return -1;
93 catch (char* msg)
95 MessageBox(NULL, _T("Could not open log.\nlibgit reports:\n") + CString(msg), _T("TortoiseGit"), MB_ICONERROR);
96 return -1;
99 try
101 CAutoLocker lock(g_Git.m_critGitDllSec);
102 [&]{ git_get_log_firstcommit(handle); }();
104 catch (char* msg)
106 MessageBox(NULL, _T("Could not get first commit.\nlibgit reports:\n") + CString(msg), _T("TortoiseGit"), MB_ICONERROR);
107 return -1;
110 int ret = 0;
111 while (ret == 0)
113 GIT_COMMIT commit;
117 CAutoLocker lock(g_Git.m_critGitDllSec);
118 [&]{ ret = git_get_log_nextcommit(handle, &commit, infomask & CGit::LOG_INFO_FOLLOW); }();
120 catch (char* msg)
122 MessageBox(NULL, _T("Could not get next commit.\nlibgit reports:\n") + CString(msg), _T("TortoiseGit"), MB_ICONERROR);
123 break;
126 if (ret)
128 if (ret != -2) // other than end of revision walking
129 MessageBox(nullptr, (_T("Could not get next commit.\nlibgit returns:") + std::to_wstring(ret)).c_str(), _T("TortoiseGit"), MB_ICONERROR);
130 break;
133 if (commit.m_ignore == 1)
135 git_free_commit(&commit);
136 continue;
139 CGitHash hash = (char*)commit.m_hash ;
141 GitRevLoglist* pRev = this->m_pLogCache->GetCacheData(hash);
143 char *pNote = nullptr;
145 CAutoLocker lock(g_Git.m_critGitDllSec);
146 git_get_notes(commit.m_hash, &pNote);
148 if (pNote)
150 pRev->m_Notes = CUnicodeUtils::GetUnicode(pNote);
151 free(pNote);
152 pNote = nullptr;
155 ASSERT(pRev->m_CommitHash == hash);
156 pRev->ParserFromCommit(&commit);
157 pRev->ParserParentFromCommit(&commit);
158 git_free_commit(&commit);
159 // Must call free commit before SafeFetchFullInfo, commit parent is rewrite by log.
160 // file list will wrong if parent rewrite.
162 if (!pRev->m_IsFull && (infomask & CGit::LOG_INFO_FULL_DIFF))
164 if (pRev->SafeFetchFullInfo(&g_Git))
166 MessageBox(nullptr, pRev->GetLastErr(), _T("TortoiseGit"), MB_ICONERROR);
167 return -1;
171 this->push_back(pRev->m_CommitHash);
173 m_HashMap[pRev->m_CommitHash] = (int)size() - 1;
178 CAutoLocker lock(g_Git.m_critGitDllSec);
179 git_close_log(handle);
182 return 0;
185 struct SortByParentDate
187 bool operator()(GitRevLoglist* pLhs, GitRevLoglist* pRhs)
189 if (pLhs->m_CommitHash == pRhs->m_CommitHash)
190 return false;
191 for (auto it = pLhs->m_ParentHash.begin(); it != pLhs->m_ParentHash.end(); ++it)
193 if (*it == pRhs->m_CommitHash)
194 return true;
196 for (auto it = pRhs->m_ParentHash.begin(); it != pRhs->m_ParentHash.end(); ++it)
198 if (*it == pLhs->m_CommitHash)
199 return false;
201 return pLhs->GetCommitterDate()>pRhs->GetCommitterDate();
205 int CLogDataVector::Fill(std::set<CGitHash>& hashes)
209 [] { git_init(); } ();
211 catch (const char* msg)
213 MessageBox(nullptr, _T("Could not initialize libgit.\nlibgit reports:\n") + CString(msg), _T("TortoiseGit"), MB_ICONERROR);
214 return -1;
217 std::set<GitRevLoglist*, SortByParentDate> revs;
219 for (auto it = hashes.begin(); it != hashes.end(); ++it)
221 CGitHash hash = *it;
222 GitRevLoglist* pRev = this->m_pLogCache->GetCacheData(hash);
223 ASSERT(pRev->m_CommitHash == hash);
225 GIT_COMMIT commit;
228 CAutoLocker lock(g_Git.m_critGitDllSec);
229 if (git_get_commit_from_hash(&commit, hash.m_hash))
231 return -1;
234 catch (char * msg)
236 MessageBox(nullptr, _T("Could not get commit \"") + hash.ToString() + _T("\".\nlibgit reports:\n") + CString(msg), _T("TortoiseGit"), MB_ICONERROR);
237 return -1;
240 // right now this code is only used by TortoiseGitBlame,
241 // as such git notes are not needed to be loaded
243 pRev->ParserFromCommit(&commit);
244 pRev->ParserParentFromCommit(&commit);
245 git_free_commit(&commit);
247 revs.insert(pRev);
250 for (auto it = revs.begin(); it != revs.end(); ++it)
252 GitRev *pRev = *it;
253 this->push_back(pRev->m_CommitHash);
254 m_HashMap[pRev->m_CommitHash] = (int)size() - 1;
257 return 0;
260 int AddTolist(unsigned char * /*osha1*/, unsigned char *nsha1, const char * /*name*/, unsigned long /*time*/, int /*sz*/, const char *msg, void *data)
262 CLogDataVector *vector = (CLogDataVector*)data;
263 GitRevLoglist rev;
264 rev.m_CommitHash = (char*)nsha1;
266 CString one = CUnicodeUtils::GetUnicode(msg);
268 int message = one.Find(_T(":"), 0);
269 if (message > 0)
271 rev.m_RefAction = one.Left(message);
272 rev.GetSubject() = one.Mid(message + 1);
275 vector->m_pLogCache->m_HashMap[rev.m_CommitHash] = rev;
276 vector->insert(vector->begin(),rev.m_CommitHash);
278 return 0;
281 void CLogDataVector::append(CGitHash& sha, bool storeInVector)
283 if (storeInVector)
284 this->push_back(sha);
286 GitRevLoglist* r = &m_pLogCache->m_HashMap[sha];
287 updateLanes(*r, this->m_Lns, sha);
290 void CLogDataVector::setLane(CGitHash& sha)
292 Lanes* l = &(this->m_Lns);
293 int i = m_FirstFreeLane;
295 // QVector<QByteArray> ba;
296 // const ShaString& ss = toPersistentSha(sha, ba);
297 // const ShaVect& shaVec(fh->revOrder);
299 for (int cnt = (int)size(); i < cnt; ++i) {
301 GitRevLoglist* r = &this->GetGitRevAt(i);
302 CGitHash curSha=r->m_CommitHash;
304 if (r->m_Lanes.empty())
305 updateLanes(*r, *l, curSha);
307 if (curSha == sha)
308 break;
310 m_FirstFreeLane = ++i;
312 #if 0
313 Lanes* l = &(this->m_Lanes);
314 int i = m_FirstFreeLane;
316 QVector<QByteArray> ba;
317 const ShaString& ss = toPersistentSha(sha, ba);
318 const ShaVect& shaVec(fh->revOrder);
320 for (uint cnt = shaVec.count(); i < cnt; ++i) {
322 const ShaString& curSha = shaVec[i];
323 Rev* r = m_HashMap[curSha]const_cast<Rev*>(revLookup(curSha, fh));
324 if (r->lanes.count() == 0)
325 updateLanes(*r, *l, curSha);
327 if (curSha == ss)
328 break;
330 fh->firstFreeLane = ++i;
331 #endif
335 void CLogDataVector::updateLanes(GitRevLoglist& c, Lanes& lns, CGitHash& sha)
337 // we could get third argument from c.sha(), but we are in fast path here
338 // and c.sha() involves a deep copy, so we accept a little redundancy
340 if (lns.isEmpty())
341 lns.init(sha);
343 bool isDiscontinuity;
344 bool isFork = lns.isFork(sha, isDiscontinuity);
345 bool isMerge = (c.ParentsCount() > 1);
346 bool isInitial = (c.ParentsCount() == 0);
348 if (isDiscontinuity)
349 lns.changeActiveLane(sha); // uses previous isBoundary state
351 lns.setBoundary(c.IsBoundary() == TRUE); // update must be here
352 TRACE(_T("%s %d"), (LPCTSTR)c.m_CommitHash.ToString(), c.IsBoundary());
354 if (isFork)
355 lns.setFork(sha);
356 if (isMerge)
357 lns.setMerge(c.m_ParentHash);
358 //if (c.isApplied)
359 // lns.setApplied();
360 if (isInitial)
361 lns.setInitial();
363 lns.getLanes(c.m_Lanes); // here lanes are snapshotted
365 CGitHash nextSha;
366 if( !isInitial)
367 nextSha = c.m_ParentHash[0];
369 lns.nextParent(nextSha);
371 //if (c.isApplied)
372 // lns.afterApplied();
373 if (isMerge)
374 lns.afterMerge();
375 if (isFork)
376 lns.afterFork();
377 if (lns.isBranch())
378 lns.afterBranch();