Drop unused msysgit dialog of installer
[TortoiseGit.git] / src / TortoiseProc / LogDataVector.cpp
blob8172df92b21cf77311f6696c5287b7492e45e18e
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2009-2013 - 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 "GitLogListBase.h"
32 #include "UnicodeUtils.h"
34 typedef CComCritSecLock<CComCriticalSection> CAutoLocker;
36 void CLogDataVector::ClearAll()
39 clear();
40 m_HashMap.clear();
41 m_Lns.clear();
43 m_FirstFreeLane=0;
44 m_Lns.clear();
45 if (m_pLogCache)
47 m_pLogCache->ClearAllLanes();
50 m_RawlogData.clear();
51 m_RawLogStart.clear();
54 //CLogDataVector Class
55 int CLogDataVector::ParserFromLog(CTGitPath *path, int count, int infomask, CString *range)
57 // only enable --follow on files
58 if ((path == NULL || path->IsDirectory()) && (infomask & CGit::LOG_INFO_FOLLOW))
59 infomask = infomask ^ CGit::LOG_INFO_FOLLOW;
61 CString gitrange = _T("HEAD");
62 if (range != nullptr)
63 gitrange = *range;
64 CString cmd = g_Git.GetLogCmd(gitrange, path, count, infomask, true);
66 if (!g_Git.CanParseRev(gitrange))
67 return 0;
69 try
71 [] { git_init(); } ();
73 catch (const char* msg)
75 MessageBox(NULL, _T("Could not initialize libgit.\nlibgit reports:\n") + CString(msg), _T("TortoiseGit"), MB_ICONERROR);
76 return -1;
79 GIT_LOG handle;
80 try
82 CAutoLocker lock(g_Git.m_critGitDllSec);
83 if (git_open_log(&handle,CUnicodeUtils::GetMulti(cmd, CP_UTF8).GetBuffer()))
85 return -1;
88 catch (char* msg)
90 MessageBox(NULL, _T("Could not open log.\nlibgit reports:\n") + CString(msg), _T("TortoiseGit"), MB_ICONERROR);
91 return -1;
94 try
96 CAutoLocker lock(g_Git.m_critGitDllSec);
97 [&]{ git_get_log_firstcommit(handle); }();
99 catch (char* msg)
101 MessageBox(NULL, _T("Could not get first commit.\nlibgit reports:\n") + CString(msg), _T("TortoiseGit"), MB_ICONERROR);
102 return -1;
105 int ret = 0;
106 while (ret == 0)
108 GIT_COMMIT commit;
112 CAutoLocker lock(g_Git.m_critGitDllSec);
113 [&]{ ret = git_get_log_nextcommit(handle, &commit, infomask & CGit::LOG_INFO_FOLLOW); }();
115 catch (char* msg)
117 MessageBox(NULL, _T("Could not get next commit.\nlibgit reports:\n") + CString(msg), _T("TortoiseGit"), MB_ICONERROR);
118 break;
121 if (ret)
122 break;
124 if (commit.m_ignore == 1)
126 git_free_commit(&commit);
127 continue;
130 CGitHash hash = (char*)commit.m_hash ;
132 GitRev *pRev = this->m_pLogCache->GetCacheData(hash);
134 char *pNote = nullptr;
136 CAutoLocker lock(g_Git.m_critGitDllSec);
137 git_get_notes(commit.m_hash, &pNote);
139 if (pNote)
141 pRev->m_Notes.Empty();
142 g_Git.StringAppend(&pRev->m_Notes,(BYTE*)pNote);
143 free(pNote);
144 pNote = nullptr;
147 ASSERT(pRev->m_CommitHash == hash);
148 pRev->ParserFromCommit(&commit);
149 pRev->ParserParentFromCommit(&commit);
150 git_free_commit(&commit);
151 // Must call free commit before SafeFetchFullInfo, commit parent is rewrite by log.
152 // file list will wrong if parent rewrite.
154 if (!pRev->m_IsFull && (infomask & CGit::LOG_INFO_FULL_DIFF))
158 pRev->SafeFetchFullInfo(&g_Git);
160 catch (char * g_last_error)
162 MessageBox(NULL, _T("Could not fetch full info of a commit.\nlibgit reports:\n") + CString(g_last_error), _T("TortoiseGit"), MB_ICONERROR);
163 return -1;
167 this->push_back(pRev->m_CommitHash);
169 m_HashMap[pRev->m_CommitHash] = (int)size() - 1;
174 CAutoLocker lock(g_Git.m_critGitDllSec);
175 git_close_log(handle);
178 return 0;
181 struct SortByParentDate
183 bool operator()(GitRev *pLhs, GitRev *pRhs)
185 if (pLhs->m_CommitHash == pRhs->m_CommitHash)
186 return false;
187 for (auto it = pLhs->m_ParentHash.begin(); it != pLhs->m_ParentHash.end(); ++it)
189 if (*it == pRhs->m_CommitHash)
190 return true;
192 for (auto it = pRhs->m_ParentHash.begin(); it != pRhs->m_ParentHash.end(); ++it)
194 if (*it == pLhs->m_CommitHash)
195 return false;
197 return pLhs->GetCommitterDate()>pRhs->GetCommitterDate();
201 int CLogDataVector::Fill(std::set<CGitHash>& hashes)
205 [] { git_init(); } ();
207 catch (const char* msg)
209 MessageBox(nullptr, _T("Could not initialize libgit.\nlibgit reports:\n") + CString(msg), _T("TortoiseGit"), MB_ICONERROR);
210 return -1;
213 std::set<GitRev*, SortByParentDate> revs;
215 for (auto it = hashes.begin(); it != hashes.end(); ++it)
217 CGitHash hash = *it;
218 GitRev *pRev = this->m_pLogCache->GetCacheData(hash);
219 ASSERT(pRev->m_CommitHash == hash);
221 GIT_COMMIT commit;
224 CAutoLocker lock(g_Git.m_critGitDllSec);
225 if (git_get_commit_from_hash(&commit, hash.m_hash))
227 return -1;
230 catch (char * msg)
232 MessageBox(nullptr, _T("Could not get commit \"") + hash.ToString() + _T("\".\nlibgit reports:\n") + CString(msg), _T("TortoiseGit"), MB_ICONERROR);
233 return -1;
236 // right now this code is only used by TortoiseGitBlame,
237 // as such git notes are not needed to be loaded
239 pRev->ParserFromCommit(&commit);
240 pRev->ParserParentFromCommit(&commit);
241 git_free_commit(&commit);
243 revs.insert(pRev);
246 for (auto it = revs.begin(); it != revs.end(); ++it)
248 GitRev *pRev = *it;
249 this->push_back(pRev->m_CommitHash);
250 m_HashMap[pRev->m_CommitHash] = (int)size() - 1;
253 return 0;
256 int AddTolist(unsigned char * /*osha1*/, unsigned char *nsha1, const char * /*name*/, unsigned long /*time*/, int /*sz*/, const char *msg, void *data)
258 CLogDataVector *vector = (CLogDataVector*)data;
259 GitRev rev;
260 rev.m_CommitHash = (char*)nsha1;
262 CString one;
263 g_Git.StringAppend(&one, (BYTE *)msg);
265 int message = one.Find(_T(":"), 0);
266 if (message > 0)
268 rev.m_RefAction = one.Left(message);
269 rev.GetSubject() = one.Mid(message + 1);
272 vector->m_pLogCache->m_HashMap[rev.m_CommitHash] = rev;
273 vector->insert(vector->begin(),rev.m_CommitHash);
275 return 0;
278 void CLogDataVector::append(CGitHash& sha, bool storeInVector)
280 if (storeInVector)
281 this->push_back(sha);
283 GitRev* r = &m_pLogCache->m_HashMap[sha];
284 updateLanes(*r, this->m_Lns, sha);
287 void CLogDataVector::setLane(CGitHash& sha)
289 Lanes* l = &(this->m_Lns);
290 int i = m_FirstFreeLane;
292 // QVector<QByteArray> ba;
293 // const ShaString& ss = toPersistentSha(sha, ba);
294 // const ShaVect& shaVec(fh->revOrder);
296 for (int cnt = (int)size(); i < cnt; ++i) {
298 GitRev* r = & this->GetGitRevAt(i);
299 CGitHash curSha=r->m_CommitHash;
301 if (r->m_Lanes.empty())
302 updateLanes(*r, *l, curSha);
304 if (curSha == sha)
305 break;
307 m_FirstFreeLane = ++i;
309 #if 0
310 Lanes* l = &(this->m_Lanes);
311 int i = m_FirstFreeLane;
313 QVector<QByteArray> ba;
314 const ShaString& ss = toPersistentSha(sha, ba);
315 const ShaVect& shaVec(fh->revOrder);
317 for (uint cnt = shaVec.count(); i < cnt; ++i) {
319 const ShaString& curSha = shaVec[i];
320 Rev* r = m_HashMap[curSha]const_cast<Rev*>(revLookup(curSha, fh));
321 if (r->lanes.count() == 0)
322 updateLanes(*r, *l, curSha);
324 if (curSha == ss)
325 break;
327 fh->firstFreeLane = ++i;
328 #endif
332 void CLogDataVector::updateLanes(GitRev& c, Lanes& lns, CGitHash &sha)
334 // we could get third argument from c.sha(), but we are in fast path here
335 // and c.sha() involves a deep copy, so we accept a little redundancy
337 if (lns.isEmpty())
338 lns.init(sha);
340 bool isDiscontinuity;
341 bool isFork = lns.isFork(sha, isDiscontinuity);
342 bool isMerge = (c.ParentsCount() > 1);
343 bool isInitial = (c.ParentsCount() == 0);
345 if (isDiscontinuity)
346 lns.changeActiveLane(sha); // uses previous isBoundary state
348 lns.setBoundary(c.IsBoundary() == TRUE); // update must be here
349 TRACE(_T("%s %d"),c.m_CommitHash.ToString(),c.IsBoundary());
351 if (isFork)
352 lns.setFork(sha);
353 if (isMerge)
354 lns.setMerge(c.m_ParentHash);
355 //if (c.isApplied)
356 // lns.setApplied();
357 if (isInitial)
358 lns.setInitial();
360 lns.getLanes(c.m_Lanes); // here lanes are snapshotted
362 CGitHash nextSha;
363 if( !isInitial)
364 nextSha = c.m_ParentHash[0];
366 lns.nextParent(nextSha);
368 //if (c.isApplied)
369 // lns.afterApplied();
370 if (isMerge)
371 lns.afterMerge();
372 if (isFork)
373 lns.afterFork();
374 if (lns.isBranch())
375 lns.afterBranch();