DeleteRemoteTagDlg: Allow to delete multiple tags at once
[TortoiseGit.git] / src / TortoiseProc / LogDataVector.cpp
blob327c5ce0f58069a62913109795653c81d8c02366
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.
147 pRev->SafeFetchFullInfo(&g_Git);
149 catch (char * g_last_error)
151 MessageBox(NULL, _T("Could not fetch full info of a commit.\nlibgit reports:\n") + CString(g_last_error), _T("TortoiseGit"), MB_ICONERROR);
152 return -1;
155 else
157 ASSERT(pRev->m_CommitHash == hash);
158 pRev->ParserParentFromCommit(&commit);
159 git_free_commit(&commit);
162 this->push_back(pRev->m_CommitHash);
164 m_HashMap[rev.m_CommitHash] = (int)size() - 1;
168 git_close_log(handle);
170 return 0;
173 int AddTolist(unsigned char * /*osha1*/, unsigned char *nsha1, const char * /*name*/, unsigned long /*time*/, int /*sz*/, const char *msg, void *data)
175 CLogDataVector *vector = (CLogDataVector*)data;
176 GitRev rev;
177 rev.m_CommitHash=(char*)nsha1;
179 CString one;
180 g_Git.StringAppend(&one, (BYTE *)msg);
182 int message=one.Find(_T(":"),0);
183 if(message>0)
185 rev.m_RefAction=one.Left(message);
186 rev.GetSubject()=one.Mid(message+1);
189 vector->m_pLogCache->m_HashMap[rev.m_CommitHash]=rev;
190 vector->insert(vector->begin(),rev.m_CommitHash);
192 return 0;
195 int CLogDataVector::ParserFromRefLog(CString ref)
197 if(g_Git.m_IsUseGitDLL)
199 git_for_each_reflog_ent(CUnicodeUtils::GetUTF8(ref),AddTolist,this);
200 for(int i=0;i<size();i++)
202 m_pLogCache->m_HashMap[at(i)].m_Ref.Format(_T("%s{%d}"), ref,i);
206 else
209 CString cmd, out;
210 GitRev rev;
211 cmd.Format(_T("git.exe reflog show %s"),ref);
212 if (g_Git.Run(cmd, &out, NULL, CP_UTF8))
213 return -1;
215 int pos=0;
216 while(pos>=0)
218 CString one=out.Tokenize(_T("\n"),pos);
219 int ref=one.Find(_T(' '),0);
220 if(ref<0)
221 continue;
223 rev.Clear();
225 rev.m_CommitHash=g_Git.GetHash(one.Left(ref));
226 int action=one.Find(_T(' '),ref+1);
227 int message;
228 if(action>0)
230 rev.m_Ref=one.Mid(ref+1,action-ref-2);
231 message=one.Find(_T(":"),action);
232 if(message>0)
234 rev.m_RefAction=one.Mid(action+1,message-action-1);
235 rev.GetSubject()=one.Right(one.GetLength()-message-1);
239 this->m_pLogCache->m_HashMap[rev.m_CommitHash]=rev;
241 this->push_back(rev.m_CommitHash);
245 return 0;
248 void CLogDataVector::setLane(CGitHash& sha)
250 Lanes* l = &(this->m_Lns);
251 int i = m_FirstFreeLane;
253 // QVector<QByteArray> ba;
254 // const ShaString& ss = toPersistentSha(sha, ba);
255 // const ShaVect& shaVec(fh->revOrder);
257 for (int cnt = (int)size(); i < cnt; ++i) {
259 GitRev* r = & this->GetGitRevAt(i);
260 CGitHash curSha=r->m_CommitHash;
262 if (r->m_Lanes.empty())
263 updateLanes(*r, *l, curSha);
265 if (curSha == sha)
266 break;
268 m_FirstFreeLane = ++i;
270 #if 0
271 Lanes* l = &(this->m_Lanes);
272 int i = m_FirstFreeLane;
274 QVector<QByteArray> ba;
275 const ShaString& ss = toPersistentSha(sha, ba);
276 const ShaVect& shaVec(fh->revOrder);
278 for (uint cnt = shaVec.count(); i < cnt; ++i) {
280 const ShaString& curSha = shaVec[i];
281 Rev* r = m_HashMap[curSha]const_cast<Rev*>(revLookup(curSha, fh));
282 if (r->lanes.count() == 0)
283 updateLanes(*r, *l, curSha);
285 if (curSha == ss)
286 break;
288 fh->firstFreeLane = ++i;
289 #endif
293 void CLogDataVector::updateLanes(GitRev& c, Lanes& lns, CGitHash &sha)
295 // we could get third argument from c.sha(), but we are in fast path here
296 // and c.sha() involves a deep copy, so we accept a little redundancy
298 if (lns.isEmpty())
299 lns.init(sha);
301 bool isDiscontinuity;
302 bool isFork = lns.isFork(sha, isDiscontinuity);
303 bool isMerge = (c.ParentsCount() > 1);
304 bool isInitial = (c.ParentsCount() == 0);
306 if (isDiscontinuity)
307 lns.changeActiveLane(sha); // uses previous isBoundary state
309 lns.setBoundary(c.IsBoundary() == TRUE); // update must be here
310 TRACE(_T("%s %d"),c.m_CommitHash.ToString(),c.IsBoundary());
312 if (isFork)
313 lns.setFork(sha);
314 if (isMerge)
315 lns.setMerge(c.m_ParentHash);
316 //if (c.isApplied)
317 // lns.setApplied();
318 if (isInitial)
319 lns.setInitial();
321 lns.getLanes(c.m_Lanes); // here lanes are snapshotted
323 CGitHash nextSha;
324 if( !isInitial)
325 nextSha = c.m_ParentHash[0];
327 lns.nextParent(nextSha);
329 //if (c.isApplied)
330 // lns.afterApplied();
331 if (isMerge)
332 lns.afterMerge();
333 if (isFork)
334 lns.afterFork();
335 if (lns.isBranch())
336 lns.afterBranch();
338 // QString tmp = "", tmp2;
339 // for (uint i = 0; i < c.lanes.count(); i++) {
340 // tmp2.setNum(c.lanes[i]);
341 // tmp.append(tmp2 + "-");
342 // }
343 // qDebug("%s %s",tmp.latin1(), c.sha.latin1());