Fixed issue #1542: Can send pull request email
[TortoiseGit.git] / src / TortoiseProc / LogDataVector.cpp
blob1a2d3b5eb61b7aa0e896a31b3f002759898a4136
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 "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 *range)
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 gitrange = _T("HEAD");
90 if (range != nullptr)
91 gitrange = *range;
92 CString cmd = g_Git.GetLogCmd(gitrange, path, count, infomask, true);
94 if (!g_Git.CanParseRev(gitrange))
95 return 0;
97 git_init();
99 GIT_LOG handle;
102 if (git_open_log(&handle,CUnicodeUtils::GetMulti(cmd, CP_UTF8).GetBuffer()))
104 return -1;
107 catch (char * g_last_error)
109 MessageBox(NULL, CString(g_last_error), _T("TortoiseGit"), MB_ICONERROR);
110 return -1;
113 git_get_log_firstcommit(handle);
115 GIT_COMMIT commit;
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 ;
127 GitRev *pRev = this->m_pLogCache->GetCacheData(hash);
129 char *note=NULL;
130 git_get_notes(commit.m_hash,&note);
131 if(note)
133 pRev->m_Notes.Empty();
134 g_Git.StringAppend(&pRev->m_Notes,(BYTE*)note);
137 if((pRev == NULL || !pRev->m_IsFull) && infomask& CGit::LOG_INFO_FULL_DIFF)
139 pRev->ParserFromCommit(&commit);
140 pRev->ParserParentFromCommit(&commit);
141 git_free_commit(&commit);
142 //Must call free commit before SafeFetchFullInfo, commit parent is rewrite by log.
143 //file list will wrong if parent rewrite.
146 pRev->SafeFetchFullInfo(&g_Git);
148 catch (char * g_last_error)
150 MessageBox(NULL, _T("Could not fetch full info of a commit.\nlibgit reports:\n") + CString(g_last_error), _T("TortoiseGit"), MB_ICONERROR);
151 return -1;
154 else
156 ASSERT(pRev->m_CommitHash == hash);
157 pRev->ParserFromCommit(&commit);
158 pRev->ParserParentFromCommit(&commit);
159 git_free_commit(&commit);
162 this->push_back(pRev->m_CommitHash);
164 m_HashMap[pRev->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 (size_t 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 if (g_Git.GetHash(rev.m_CommitHash, one.Left(ref)))
227 MessageBox(NULL, g_Git.GetGitLastErr(_T("Could not get hash of ") + one.Left(ref) + _T(".")), _T("TortoiseGit"), MB_ICONERROR);
228 return -1;
230 int action=one.Find(_T(' '),ref+1);
231 int message;
232 if(action>0)
234 rev.m_Ref=one.Mid(ref+1,action-ref-2);
235 message=one.Find(_T(":"),action);
236 if(message>0)
238 rev.m_RefAction=one.Mid(action+1,message-action-1);
239 rev.GetSubject()=one.Right(one.GetLength()-message-1);
243 this->m_pLogCache->m_HashMap[rev.m_CommitHash]=rev;
245 this->push_back(rev.m_CommitHash);
249 return 0;
252 void CLogDataVector::setLane(CGitHash& sha)
254 Lanes* l = &(this->m_Lns);
255 int i = m_FirstFreeLane;
257 // QVector<QByteArray> ba;
258 // const ShaString& ss = toPersistentSha(sha, ba);
259 // const ShaVect& shaVec(fh->revOrder);
261 for (int cnt = (int)size(); i < cnt; ++i) {
263 GitRev* r = & this->GetGitRevAt(i);
264 CGitHash curSha=r->m_CommitHash;
266 if (r->m_Lanes.empty())
267 updateLanes(*r, *l, curSha);
269 if (curSha == sha)
270 break;
272 m_FirstFreeLane = ++i;
274 #if 0
275 Lanes* l = &(this->m_Lanes);
276 int i = m_FirstFreeLane;
278 QVector<QByteArray> ba;
279 const ShaString& ss = toPersistentSha(sha, ba);
280 const ShaVect& shaVec(fh->revOrder);
282 for (uint cnt = shaVec.count(); i < cnt; ++i) {
284 const ShaString& curSha = shaVec[i];
285 Rev* r = m_HashMap[curSha]const_cast<Rev*>(revLookup(curSha, fh));
286 if (r->lanes.count() == 0)
287 updateLanes(*r, *l, curSha);
289 if (curSha == ss)
290 break;
292 fh->firstFreeLane = ++i;
293 #endif
297 void CLogDataVector::updateLanes(GitRev& c, Lanes& lns, CGitHash &sha)
299 // we could get third argument from c.sha(), but we are in fast path here
300 // and c.sha() involves a deep copy, so we accept a little redundancy
302 if (lns.isEmpty())
303 lns.init(sha);
305 bool isDiscontinuity;
306 bool isFork = lns.isFork(sha, isDiscontinuity);
307 bool isMerge = (c.ParentsCount() > 1);
308 bool isInitial = (c.ParentsCount() == 0);
310 if (isDiscontinuity)
311 lns.changeActiveLane(sha); // uses previous isBoundary state
313 lns.setBoundary(c.IsBoundary() == TRUE); // update must be here
314 TRACE(_T("%s %d"),c.m_CommitHash.ToString(),c.IsBoundary());
316 if (isFork)
317 lns.setFork(sha);
318 if (isMerge)
319 lns.setMerge(c.m_ParentHash);
320 //if (c.isApplied)
321 // lns.setApplied();
322 if (isInitial)
323 lns.setInitial();
325 lns.getLanes(c.m_Lanes); // here lanes are snapshotted
327 CGitHash nextSha;
328 if( !isInitial)
329 nextSha = c.m_ParentHash[0];
331 lns.nextParent(nextSha);
333 //if (c.isApplied)
334 // lns.afterApplied();
335 if (isMerge)
336 lns.afterMerge();
337 if (isFork)
338 lns.afterFork();
339 if (lns.isBranch())
340 lns.afterBranch();
342 // QString tmp = "", tmp2;
343 // for (uint i = 0; i < c.lanes.count(); i++) {
344 // tmp2.setNum(c.lanes[i]);
345 // tmp.append(tmp2 + "-");
346 // }
347 // qDebug("%s %s",tmp.latin1(), c.sha.latin1());