Add support for "project" config level
[TortoiseGit.git] / src / TortoiseProc / LogDataVector.cpp
blob54f10053f2dcbbb8075f059af2b7ff86abae734b
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 CGitHashMap a;
36 void CLogDataVector::ClearAll()
39 clear();
40 m_HashMap.clear();
41 m_Lns.clear();
43 m_FirstFreeLane=0;
44 m_Lns.clear();
46 m_RawlogData.clear();
47 m_RawLogStart.clear();
50 //CLogDataVector Class
51 int CLogDataVector::ParserFromLog(CTGitPath *path, int count, int infomask, CString *range)
53 // only enable --follow on files
54 if ((path == NULL || path->IsDirectory()) && (infomask & CGit::LOG_INFO_FOLLOW))
55 infomask = infomask ^ CGit::LOG_INFO_FOLLOW;
57 CString gitrange = _T("HEAD");
58 if (range != nullptr)
59 gitrange = *range;
60 CString cmd = g_Git.GetLogCmd(gitrange, path, count, infomask, true);
62 if (!g_Git.CanParseRev(gitrange))
63 return 0;
65 try
67 [] { git_init(); } ();
69 catch (const char* msg)
71 MessageBox(NULL, _T("Could not initialize libgit.\nlibgit reports:\n") + CString(msg), _T("TortoiseGit"), MB_ICONERROR);
72 return -1;
75 GIT_LOG handle;
76 g_Git.m_critGitDllSec.Lock();
77 try
79 if (git_open_log(&handle,CUnicodeUtils::GetMulti(cmd, CP_UTF8).GetBuffer()))
81 return -1;
84 catch (char* msg)
86 g_Git.m_critGitDllSec.Unlock();
87 MessageBox(NULL, _T("Could not open log.\nlibgit reports:\n") + CString(msg), _T("TortoiseGit"), MB_ICONERROR);
88 return -1;
90 g_Git.m_critGitDllSec.Unlock();
92 g_Git.m_critGitDllSec.Lock();
93 try
95 [&]{ git_get_log_firstcommit(handle); }();
97 catch (char* msg)
99 g_Git.m_critGitDllSec.Unlock();
100 MessageBox(NULL, _T("Could not get first commit.\nlibgit reports:\n") + CString(msg), _T("TortoiseGit"), MB_ICONERROR);
101 return -1;
103 g_Git.m_critGitDllSec.Unlock();
105 int ret = 0;
106 while (ret == 0)
108 GIT_COMMIT commit;
109 g_Git.m_critGitDllSec.Lock();
112 [&]{ ret = git_get_log_nextcommit(handle, &commit, infomask & CGit::LOG_INFO_FOLLOW); }();
114 catch (char* msg)
116 g_Git.m_critGitDllSec.Unlock();
117 MessageBox(NULL, _T("Could not get next commit.\nlibgit reports:\n") + CString(msg), _T("TortoiseGit"), MB_ICONERROR);
118 break;
120 g_Git.m_critGitDllSec.Unlock();
122 if (ret)
123 break;
125 if (commit.m_ignore == 1)
127 git_free_commit(&commit);
128 continue;
131 CGitHash hash = (char*)commit.m_hash ;
133 GitRev *pRev = this->m_pLogCache->GetCacheData(hash);
135 char *note=NULL;
136 g_Git.m_critGitDllSec.Lock();
137 git_get_notes(commit.m_hash,&note);
138 g_Git.m_critGitDllSec.Unlock();
139 if(note)
141 pRev->m_Notes.Empty();
142 g_Git.StringAppend(&pRev->m_Notes,(BYTE*)note);
145 if((pRev == NULL || !pRev->m_IsFull) && infomask& CGit::LOG_INFO_FULL_DIFF)
147 pRev->ParserFromCommit(&commit);
148 pRev->ParserParentFromCommit(&commit);
149 git_free_commit(&commit);
150 //Must call free commit before SafeFetchFullInfo, commit parent is rewrite by log.
151 //file list will wrong if parent rewrite.
154 pRev->SafeFetchFullInfo(&g_Git);
156 catch (char * g_last_error)
158 MessageBox(NULL, _T("Could not fetch full info of a commit.\nlibgit reports:\n") + CString(g_last_error), _T("TortoiseGit"), MB_ICONERROR);
159 return -1;
162 else
164 ASSERT(pRev->m_CommitHash == hash);
165 pRev->ParserFromCommit(&commit);
166 pRev->ParserParentFromCommit(&commit);
167 git_free_commit(&commit);
170 this->push_back(pRev->m_CommitHash);
172 m_HashMap[pRev->m_CommitHash] = (int)size() - 1;
176 g_Git.m_critGitDllSec.Lock();
177 git_close_log(handle);
178 g_Git.m_critGitDllSec.Unlock();
180 return 0;
183 int AddTolist(unsigned char * /*osha1*/, unsigned char *nsha1, const char * /*name*/, unsigned long /*time*/, int /*sz*/, const char *msg, void *data)
185 CLogDataVector *vector = (CLogDataVector*)data;
186 GitRev rev;
187 rev.m_CommitHash=(char*)nsha1;
189 CString one;
190 g_Git.StringAppend(&one, (BYTE *)msg);
192 int message=one.Find(_T(":"),0);
193 if(message>0)
195 rev.m_RefAction=one.Left(message);
196 rev.GetSubject()=one.Mid(message+1);
199 vector->m_pLogCache->m_HashMap[rev.m_CommitHash]=rev;
200 vector->insert(vector->begin(),rev.m_CommitHash);
202 return 0;
205 int CLogDataVector::ParserFromRefLog(CString ref)
207 if(g_Git.m_IsUseGitDLL)
209 git_for_each_reflog_ent(CUnicodeUtils::GetUTF8(ref),AddTolist,this);
210 for (size_t i = 0; i < size(); ++i)
212 m_pLogCache->m_HashMap[at(i)].m_Ref.Format(_T("%s{%d}"), ref,i);
216 else
219 CString cmd, out;
220 GitRev rev;
221 cmd.Format(_T("git.exe reflog show %s"),ref);
222 if (g_Git.Run(cmd, &out, NULL, CP_UTF8))
223 return -1;
225 int pos=0;
226 while(pos>=0)
228 CString one=out.Tokenize(_T("\n"),pos);
229 int ref=one.Find(_T(' '),0);
230 if(ref<0)
231 continue;
233 rev.Clear();
235 if (g_Git.GetHash(rev.m_CommitHash, one.Left(ref)))
237 MessageBox(NULL, g_Git.GetGitLastErr(_T("Could not get hash of ") + one.Left(ref) + _T(".")), _T("TortoiseGit"), MB_ICONERROR);
238 return -1;
240 int action=one.Find(_T(' '),ref+1);
241 int message;
242 if(action>0)
244 rev.m_Ref=one.Mid(ref+1,action-ref-2);
245 message=one.Find(_T(":"),action);
246 if(message>0)
248 rev.m_RefAction=one.Mid(action+1,message-action-1);
249 rev.GetSubject()=one.Right(one.GetLength()-message-1);
253 this->m_pLogCache->m_HashMap[rev.m_CommitHash]=rev;
255 this->push_back(rev.m_CommitHash);
259 return 0;
262 void CLogDataVector::setLane(CGitHash& sha)
264 Lanes* l = &(this->m_Lns);
265 int i = m_FirstFreeLane;
267 // QVector<QByteArray> ba;
268 // const ShaString& ss = toPersistentSha(sha, ba);
269 // const ShaVect& shaVec(fh->revOrder);
271 for (int cnt = (int)size(); i < cnt; ++i) {
273 GitRev* r = & this->GetGitRevAt(i);
274 CGitHash curSha=r->m_CommitHash;
276 if (r->m_Lanes.empty())
277 updateLanes(*r, *l, curSha);
279 if (curSha == sha)
280 break;
282 m_FirstFreeLane = ++i;
284 #if 0
285 Lanes* l = &(this->m_Lanes);
286 int i = m_FirstFreeLane;
288 QVector<QByteArray> ba;
289 const ShaString& ss = toPersistentSha(sha, ba);
290 const ShaVect& shaVec(fh->revOrder);
292 for (uint cnt = shaVec.count(); i < cnt; ++i) {
294 const ShaString& curSha = shaVec[i];
295 Rev* r = m_HashMap[curSha]const_cast<Rev*>(revLookup(curSha, fh));
296 if (r->lanes.count() == 0)
297 updateLanes(*r, *l, curSha);
299 if (curSha == ss)
300 break;
302 fh->firstFreeLane = ++i;
303 #endif
307 void CLogDataVector::updateLanes(GitRev& c, Lanes& lns, CGitHash &sha)
309 // we could get third argument from c.sha(), but we are in fast path here
310 // and c.sha() involves a deep copy, so we accept a little redundancy
312 if (lns.isEmpty())
313 lns.init(sha);
315 bool isDiscontinuity;
316 bool isFork = lns.isFork(sha, isDiscontinuity);
317 bool isMerge = (c.ParentsCount() > 1);
318 bool isInitial = (c.ParentsCount() == 0);
320 if (isDiscontinuity)
321 lns.changeActiveLane(sha); // uses previous isBoundary state
323 lns.setBoundary(c.IsBoundary() == TRUE); // update must be here
324 TRACE(_T("%s %d"),c.m_CommitHash.ToString(),c.IsBoundary());
326 if (isFork)
327 lns.setFork(sha);
328 if (isMerge)
329 lns.setMerge(c.m_ParentHash);
330 //if (c.isApplied)
331 // lns.setApplied();
332 if (isInitial)
333 lns.setInitial();
335 lns.getLanes(c.m_Lanes); // here lanes are snapshotted
337 CGitHash nextSha;
338 if( !isInitial)
339 nextSha = c.m_ParentHash[0];
341 lns.nextParent(nextSha);
343 //if (c.isApplied)
344 // lns.afterApplied();
345 if (isMerge)
346 lns.afterMerge();
347 if (isFork)
348 lns.afterFork();
349 if (lns.isBranch())
350 lns.afterBranch();