doc: updated images
[TortoiseGit.git] / src / TortoiseProc / LogDataVector.cpp
blobc362e0b93503be0936f15fda24dc78e64146baee
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2009-2012 - 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 "VssStyle.h"
34 #include "IconMenu.h"
35 // CGitLogList
36 #include "cursor.h"
37 #include "InputDlg.h"
38 #include "GITProgressDlg.h"
39 #include "ProgressDlg.h"
40 //#include "RepositoryBrowser.h"
41 //#include "CopyDlg.h"
42 //#include "StatGraphDlg.h"
43 #include "Logdlg.h"
44 #include "MessageBox.h"
45 #include "Registry.h"
46 #include "AppUtils.h"
47 #include "PathUtils.h"
48 #include "StringUtils.h"
49 #include "UnicodeUtils.h"
50 #include "TempFile.h"
51 //#include "GitInfo.h"
52 //#include "GitDiff.h"
53 #include "IconMenu.h"
54 //#include "RevisionRangeDlg.h"
55 //#include "BrowseFolder.h"
56 //#include "BlameDlg.h"
57 //#include "Blame.h"
58 //#include "GitHelpers.h"
59 #include "GitStatus.h"
60 //#include "LogDlgHelper.h"
61 //#include "CachedLogInfo.h"
62 //#include "RepositoryInfo.h"
63 //#include "EditPropertiesDlg.h"
64 #include "FileDiffDlg.h"
65 #include "GitHash.h"
66 CGitHashMap a;
68 void CLogDataVector::ClearAll()
71 clear();
72 m_HashMap.clear();
73 m_Lns.clear();
75 m_FirstFreeLane=0;
76 m_Lns.clear();
78 m_RawlogData.clear();
79 m_RawLogStart.clear();
82 //CLogDataVector Class
83 int CLogDataVector::ParserFromLog(CTGitPath *path ,int count ,int infomask,CString *from,CString *to)
85 // only enable --follow on files
86 if ((path == NULL || path->IsDirectory()) && (infomask & CGit::LOG_INFO_FOLLOW))
87 infomask = infomask ^ CGit::LOG_INFO_FOLLOW;
89 CString hash;
90 CString cmd=g_Git.GetLogCmd(hash,path,count,infomask,from,to,true);
92 if(g_Git.IsInitRepos())
93 return 0;
95 git_init();
97 GIT_LOG handle;
98 try
100 if (git_open_log(&handle,CUnicodeUtils::GetMulti(cmd, CP_UTF8).GetBuffer()))
102 return -1;
105 catch (char * g_last_error)
107 MessageBox(NULL, CString(g_last_error), _T("TortoiseGit"), MB_ICONERROR);
108 return -1;
111 git_get_log_firstcommit(handle);
113 GIT_COMMIT commit;
115 GitRev rev;
117 while (git_get_log_nextcommit(handle, &commit, infomask & CGit::LOG_INFO_FOLLOW) == 0)
119 if (commit.m_ignore == 1)
121 git_free_commit(&commit);
122 continue;
125 CGitHash hash = (char*)commit.m_hash ;
126 rev.Clear();
128 GitRev *pRev = this->m_pLogCache->GetCacheData(hash);
130 char *note=NULL;
131 git_get_notes(commit.m_hash,&note);
132 if(note)
134 pRev->m_Notes.Empty();
135 g_Git.StringAppend(&pRev->m_Notes,(BYTE*)note);
138 if(pRev == NULL || !pRev->m_IsFull)
140 pRev->ParserFromCommit(&commit);
141 pRev->ParserParentFromCommit(&commit);
142 git_free_commit(&commit);
143 //Must call free commit before SafeFetchFullInfo, commit parent is rewrite by log.
144 //file list will wrong if parent rewrite.
145 pRev->SafeFetchFullInfo(&g_Git);
148 else
150 ASSERT(pRev->m_CommitHash == hash);
151 pRev->ParserParentFromCommit(&commit);
152 git_free_commit(&commit);
155 this->push_back(pRev->m_CommitHash);
157 m_HashMap[rev.m_CommitHash]=size()-1;
161 git_close_log(handle);
163 return 0;
166 int AddTolist(unsigned char * /*osha1*/, unsigned char *nsha1, const char * /*name*/, unsigned long /*time*/, int /*sz*/, const char *msg, void *data)
168 CLogDataVector *vector = (CLogDataVector*)data;
169 GitRev rev;
170 rev.m_CommitHash=(char*)nsha1;
172 CString one;
173 g_Git.StringAppend(&one, (BYTE *)msg);
175 int message=one.Find(_T(":"),0);
176 if(message>0)
178 rev.m_RefAction=one.Left(message);
179 rev.GetSubject()=one.Mid(message+1);
182 vector->m_pLogCache->m_HashMap[rev.m_CommitHash]=rev;
183 vector->insert(vector->begin(),rev.m_CommitHash);
185 return 0;
188 int CLogDataVector::ParserFromRefLog(CString ref)
190 if(g_Git.m_IsUseGitDLL)
192 git_for_each_reflog_ent(CUnicodeUtils::GetUTF8(ref),AddTolist,this);
193 for(int i=0;i<size();i++)
195 m_pLogCache->m_HashMap[at(i)].m_Ref.Format(_T("%s{%d}"), ref,i);
199 else
202 CString cmd, out;
203 GitRev rev;
204 cmd.Format(_T("git.exe reflog show %s"),ref);
205 if (g_Git.Run(cmd, &out, NULL, CP_UTF8))
206 return -1;
208 int pos=0;
209 while(pos>=0)
211 CString one=out.Tokenize(_T("\n"),pos);
212 int ref=one.Find(_T(' '),0);
213 if(ref<0)
214 continue;
216 rev.Clear();
218 rev.m_CommitHash=g_Git.GetHash(one.Left(ref));
219 int action=one.Find(_T(' '),ref+1);
220 int message;
221 if(action>0)
223 rev.m_Ref=one.Mid(ref+1,action-ref-2);
224 message=one.Find(_T(":"),action);
225 if(message>0)
227 rev.m_RefAction=one.Mid(action+1,message-action-1);
228 rev.GetSubject()=one.Right(one.GetLength()-message-1);
232 this->m_pLogCache->m_HashMap[rev.m_CommitHash]=rev;
234 this->push_back(rev.m_CommitHash);
238 return 0;
241 void CLogDataVector::setLane(CGitHash& sha)
243 Lanes* l = &(this->m_Lns);
244 int i = m_FirstFreeLane;
246 // QVector<QByteArray> ba;
247 // const ShaString& ss = toPersistentSha(sha, ba);
248 // const ShaVect& shaVec(fh->revOrder);
250 for (int cnt = size(); i < cnt; ++i) {
252 GitRev* r = & this->GetGitRevAt(i);
253 CGitHash curSha=r->m_CommitHash;
255 if (r->m_Lanes.size() == 0)
256 updateLanes(*r, *l, curSha);
258 if (curSha == sha)
259 break;
261 m_FirstFreeLane = ++i;
263 #if 0
264 Lanes* l = &(this->m_Lanes);
265 int i = m_FirstFreeLane;
267 QVector<QByteArray> ba;
268 const ShaString& ss = toPersistentSha(sha, ba);
269 const ShaVect& shaVec(fh->revOrder);
271 for (uint cnt = shaVec.count(); i < cnt; ++i) {
273 const ShaString& curSha = shaVec[i];
274 Rev* r = m_HashMap[curSha]const_cast<Rev*>(revLookup(curSha, fh));
275 if (r->lanes.count() == 0)
276 updateLanes(*r, *l, curSha);
278 if (curSha == ss)
279 break;
281 fh->firstFreeLane = ++i;
282 #endif
286 void CLogDataVector::updateLanes(GitRev& c, Lanes& lns, CGitHash &sha)
288 // we could get third argument from c.sha(), but we are in fast path here
289 // and c.sha() involves a deep copy, so we accept a little redundancy
291 if (lns.isEmpty())
292 lns.init(sha);
294 bool isDiscontinuity;
295 bool isFork = lns.isFork(sha, isDiscontinuity);
296 bool isMerge = (c.ParentsCount() > 1);
297 bool isInitial = (c.ParentsCount() == 0);
299 if (isDiscontinuity)
300 lns.changeActiveLane(sha); // uses previous isBoundary state
302 lns.setBoundary(c.IsBoundary() == TRUE); // update must be here
303 TRACE(_T("%s %d"),c.m_CommitHash.ToString(),c.IsBoundary());
305 if (isFork)
306 lns.setFork(sha);
307 if (isMerge)
308 lns.setMerge(c.m_ParentHash);
309 //if (c.isApplied)
310 // lns.setApplied();
311 if (isInitial)
312 lns.setInitial();
314 lns.getLanes(c.m_Lanes); // here lanes are snapshotted
316 CGitHash nextSha;
317 if( !isInitial)
318 nextSha = c.m_ParentHash[0];
320 lns.nextParent(nextSha);
322 //if (c.isApplied)
323 // lns.afterApplied();
324 if (isMerge)
325 lns.afterMerge();
326 if (isFork)
327 lns.afterFork();
328 if (lns.isBranch())
329 lns.afterBranch();
331 // QString tmp = "", tmp2;
332 // for (uint i = 0; i < c.lanes.count(); i++) {
333 // tmp2.setNum(c.lanes[i]);
334 // tmp.append(tmp2 + "-");
335 // }
336 // qDebug("%s %s",tmp.latin1(), c.sha.latin1());