Fix compilation warnings
[TortoiseGit.git] / src / TortoiseProc / LogDataVector.cpp
blob14d2f8d50d19beeb5689e3945823647f3c53d0df
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 "GitRev.h"
33 #include "IconMenu.h"
34 // CGitLogList
35 #include "cursor.h"
36 #include "InputDlg.h"
37 #include "GITProgressDlg.h"
38 #include "ProgressDlg.h"
39 #include "Logdlg.h"
40 #include "MessageBox.h"
41 #include "registry.h"
42 #include "AppUtils.h"
43 #include "PathUtils.h"
44 #include "StringUtils.h"
45 #include "UnicodeUtils.h"
46 #include "TempFile.h"
47 #include "IconMenu.h"
48 #include "GitStatus.h"
49 #include "FileDiffDlg.h"
50 #include "GitHash.h"
51 CGitHashMap a;
53 void CLogDataVector::ClearAll()
56 clear();
57 m_HashMap.clear();
58 m_Lns.clear();
60 m_FirstFreeLane=0;
61 m_Lns.clear();
63 m_RawlogData.clear();
64 m_RawLogStart.clear();
67 //CLogDataVector Class
68 int CLogDataVector::ParserFromLog(CTGitPath *path, int count, int infomask, CString *range)
70 // only enable --follow on files
71 if ((path == NULL || path->IsDirectory()) && (infomask & CGit::LOG_INFO_FOLLOW))
72 infomask = infomask ^ CGit::LOG_INFO_FOLLOW;
74 CString gitrange = _T("HEAD");
75 if (range != nullptr)
76 gitrange = *range;
77 CString cmd = g_Git.GetLogCmd(gitrange, path, count, infomask, true);
79 if (!g_Git.CanParseRev(gitrange))
80 return 0;
82 git_init();
84 GIT_LOG handle;
85 try
87 if (git_open_log(&handle,CUnicodeUtils::GetMulti(cmd, CP_UTF8).GetBuffer()))
89 return -1;
92 catch (char * g_last_error)
94 MessageBox(NULL, CString(g_last_error), _T("TortoiseGit"), MB_ICONERROR);
95 return -1;
98 git_get_log_firstcommit(handle);
100 GIT_COMMIT commit;
102 while (git_get_log_nextcommit(handle, &commit, infomask & CGit::LOG_INFO_FOLLOW) == 0)
104 if (commit.m_ignore == 1)
106 git_free_commit(&commit);
107 continue;
110 CGitHash hash = (char*)commit.m_hash ;
112 GitRev *pRev = this->m_pLogCache->GetCacheData(hash);
114 char *note=NULL;
115 git_get_notes(commit.m_hash,&note);
116 if(note)
118 pRev->m_Notes.Empty();
119 g_Git.StringAppend(&pRev->m_Notes,(BYTE*)note);
122 if((pRev == NULL || !pRev->m_IsFull) && infomask& CGit::LOG_INFO_FULL_DIFF)
124 pRev->ParserFromCommit(&commit);
125 pRev->ParserParentFromCommit(&commit);
126 git_free_commit(&commit);
127 //Must call free commit before SafeFetchFullInfo, commit parent is rewrite by log.
128 //file list will wrong if parent rewrite.
131 pRev->SafeFetchFullInfo(&g_Git);
133 catch (char * g_last_error)
135 MessageBox(NULL, _T("Could not fetch full info of a commit.\nlibgit reports:\n") + CString(g_last_error), _T("TortoiseGit"), MB_ICONERROR);
136 return -1;
139 else
141 ASSERT(pRev->m_CommitHash == hash);
142 pRev->ParserFromCommit(&commit);
143 pRev->ParserParentFromCommit(&commit);
144 git_free_commit(&commit);
147 this->push_back(pRev->m_CommitHash);
149 m_HashMap[pRev->m_CommitHash] = (int)size() - 1;
153 git_close_log(handle);
155 return 0;
158 int AddTolist(unsigned char * /*osha1*/, unsigned char *nsha1, const char * /*name*/, unsigned long /*time*/, int /*sz*/, const char *msg, void *data)
160 CLogDataVector *vector = (CLogDataVector*)data;
161 GitRev rev;
162 rev.m_CommitHash=(char*)nsha1;
164 CString one;
165 g_Git.StringAppend(&one, (BYTE *)msg);
167 int message=one.Find(_T(":"),0);
168 if(message>0)
170 rev.m_RefAction=one.Left(message);
171 rev.GetSubject()=one.Mid(message+1);
174 vector->m_pLogCache->m_HashMap[rev.m_CommitHash]=rev;
175 vector->insert(vector->begin(),rev.m_CommitHash);
177 return 0;
180 int CLogDataVector::ParserFromRefLog(CString ref)
182 if(g_Git.m_IsUseGitDLL)
184 git_for_each_reflog_ent(CUnicodeUtils::GetUTF8(ref),AddTolist,this);
185 for (size_t i = 0; i < size(); ++i)
187 m_pLogCache->m_HashMap[at(i)].m_Ref.Format(_T("%s{%d}"), ref,i);
191 else
194 CString cmd, out;
195 GitRev rev;
196 cmd.Format(_T("git.exe reflog show %s"),ref);
197 if (g_Git.Run(cmd, &out, NULL, CP_UTF8))
198 return -1;
200 int pos=0;
201 while(pos>=0)
203 CString one=out.Tokenize(_T("\n"),pos);
204 int ref=one.Find(_T(' '),0);
205 if(ref<0)
206 continue;
208 rev.Clear();
210 if (g_Git.GetHash(rev.m_CommitHash, one.Left(ref)))
212 MessageBox(NULL, g_Git.GetGitLastErr(_T("Could not get hash of ") + one.Left(ref) + _T(".")), _T("TortoiseGit"), MB_ICONERROR);
213 return -1;
215 int action=one.Find(_T(' '),ref+1);
216 int message;
217 if(action>0)
219 rev.m_Ref=one.Mid(ref+1,action-ref-2);
220 message=one.Find(_T(":"),action);
221 if(message>0)
223 rev.m_RefAction=one.Mid(action+1,message-action-1);
224 rev.GetSubject()=one.Right(one.GetLength()-message-1);
228 this->m_pLogCache->m_HashMap[rev.m_CommitHash]=rev;
230 this->push_back(rev.m_CommitHash);
234 return 0;
237 void CLogDataVector::setLane(CGitHash& sha)
239 Lanes* l = &(this->m_Lns);
240 int i = m_FirstFreeLane;
242 // QVector<QByteArray> ba;
243 // const ShaString& ss = toPersistentSha(sha, ba);
244 // const ShaVect& shaVec(fh->revOrder);
246 for (int cnt = (int)size(); i < cnt; ++i) {
248 GitRev* r = & this->GetGitRevAt(i);
249 CGitHash curSha=r->m_CommitHash;
251 if (r->m_Lanes.empty())
252 updateLanes(*r, *l, curSha);
254 if (curSha == sha)
255 break;
257 m_FirstFreeLane = ++i;
259 #if 0
260 Lanes* l = &(this->m_Lanes);
261 int i = m_FirstFreeLane;
263 QVector<QByteArray> ba;
264 const ShaString& ss = toPersistentSha(sha, ba);
265 const ShaVect& shaVec(fh->revOrder);
267 for (uint cnt = shaVec.count(); i < cnt; ++i) {
269 const ShaString& curSha = shaVec[i];
270 Rev* r = m_HashMap[curSha]const_cast<Rev*>(revLookup(curSha, fh));
271 if (r->lanes.count() == 0)
272 updateLanes(*r, *l, curSha);
274 if (curSha == ss)
275 break;
277 fh->firstFreeLane = ++i;
278 #endif
282 void CLogDataVector::updateLanes(GitRev& c, Lanes& lns, CGitHash &sha)
284 // we could get third argument from c.sha(), but we are in fast path here
285 // and c.sha() involves a deep copy, so we accept a little redundancy
287 if (lns.isEmpty())
288 lns.init(sha);
290 bool isDiscontinuity;
291 bool isFork = lns.isFork(sha, isDiscontinuity);
292 bool isMerge = (c.ParentsCount() > 1);
293 bool isInitial = (c.ParentsCount() == 0);
295 if (isDiscontinuity)
296 lns.changeActiveLane(sha); // uses previous isBoundary state
298 lns.setBoundary(c.IsBoundary() == TRUE); // update must be here
299 TRACE(_T("%s %d"),c.m_CommitHash.ToString(),c.IsBoundary());
301 if (isFork)
302 lns.setFork(sha);
303 if (isMerge)
304 lns.setMerge(c.m_ParentHash);
305 //if (c.isApplied)
306 // lns.setApplied();
307 if (isInitial)
308 lns.setInitial();
310 lns.getLanes(c.m_Lanes); // here lanes are snapshotted
312 CGitHash nextSha;
313 if( !isInitial)
314 nextSha = c.m_ParentHash[0];
316 lns.nextParent(nextSha);
318 //if (c.isApplied)
319 // lns.afterApplied();
320 if (isMerge)
321 lns.afterMerge();
322 if (isFork)
323 lns.afterFork();
324 if (lns.isBranch())
325 lns.afterBranch();