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
30 #include "TortoiseProc.h"
31 #include "GitLogListBase.h"
37 #include "GITProgressDlg.h"
38 #include "ProgressDlg.h"
40 #include "MessageBox.h"
43 #include "PathUtils.h"
44 #include "StringUtils.h"
45 #include "UnicodeUtils.h"
48 #include "GitStatus.h"
49 #include "FileDiffDlg.h"
53 void CLogDataVector::ClearAll()
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");
77 CString cmd
= g_Git
.GetLogCmd(gitrange
, path
, count
, infomask
, true);
79 if (!g_Git
.CanParseRev(gitrange
))
87 if (git_open_log(&handle
,CUnicodeUtils::GetMulti(cmd
, CP_UTF8
).GetBuffer()))
92 catch (char * g_last_error
)
94 MessageBox(NULL
, CString(g_last_error
), _T("TortoiseGit"), MB_ICONERROR
);
98 git_get_log_firstcommit(handle
);
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
);
110 CGitHash hash
= (char*)commit
.m_hash
;
112 GitRev
*pRev
= this->m_pLogCache
->GetCacheData(hash
);
115 git_get_notes(commit
.m_hash
,¬e
);
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
);
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
);
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
;
162 rev
.m_CommitHash
=(char*)nsha1
;
165 g_Git
.StringAppend(&one
, (BYTE
*)msg
);
167 int message
=one
.Find(_T(":"),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
);
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
);
196 cmd
.Format(_T("git.exe reflog show %s"),ref
);
197 if (g_Git
.Run(cmd
, &out
, NULL
, CP_UTF8
))
203 CString one
=out
.Tokenize(_T("\n"),pos
);
204 int ref
=one
.Find(_T(' '),0);
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
);
215 int action
=one
.Find(_T(' '),ref
+1);
219 rev
.m_Ref
=one
.Mid(ref
+1,action
-ref
-2);
220 message
=one
.Find(_T(":"),action
);
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
);
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
);
257 m_FirstFreeLane
= ++i
;
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
);
277 fh
->firstFreeLane
= ++i
;
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
290 bool isDiscontinuity
;
291 bool isFork
= lns
.isFork(sha
, isDiscontinuity
);
292 bool isMerge
= (c
.ParentsCount() > 1);
293 bool isInitial
= (c
.ParentsCount() == 0);
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());
304 lns
.setMerge(c
.m_ParentHash
);
310 lns
.getLanes(c
.m_Lanes
); // here lanes are snapshotted
314 nextSha
= c
.m_ParentHash
[0];
316 lns
.nextParent(nextSha
);
319 // lns.afterApplied();