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"
32 #include "UnicodeUtils.h"
36 void CLogDataVector::ClearAll()
47 m_RawLogStart
.clear();
50 //CLogDataVector Class
51 int CLogDataVector::ParserFromLog(CTGitPath
*path
, int count
, int infomask
, CString
*range
)
53 // only enable --follow on files
54 if ((path
== NULL
|| path
->IsDirectory()) && (infomask
& CGit::LOG_INFO_FOLLOW
))
55 infomask
= infomask
^ CGit::LOG_INFO_FOLLOW
;
57 CString gitrange
= _T("HEAD");
60 CString cmd
= g_Git
.GetLogCmd(gitrange
, path
, count
, infomask
, true);
62 if (!g_Git
.CanParseRev(gitrange
))
67 [] { git_init(); } ();
69 catch (const char* msg
)
71 MessageBox(NULL
, _T("Could not initialize libgit.\nlibgit reports:\n") + CString(msg
), _T("TortoiseGit"), MB_ICONERROR
);
76 g_Git
.m_critGitDllSec
.Lock();
79 if (git_open_log(&handle
,CUnicodeUtils::GetMulti(cmd
, CP_UTF8
).GetBuffer()))
86 g_Git
.m_critGitDllSec
.Unlock();
87 MessageBox(NULL
, _T("Could not open log.\nlibgit reports:\n") + CString(msg
), _T("TortoiseGit"), MB_ICONERROR
);
90 g_Git
.m_critGitDllSec
.Unlock();
92 g_Git
.m_critGitDllSec
.Lock();
95 [&]{ git_get_log_firstcommit(handle
); }();
99 g_Git
.m_critGitDllSec
.Unlock();
100 MessageBox(NULL
, _T("Could not get first commit.\nlibgit reports:\n") + CString(msg
), _T("TortoiseGit"), MB_ICONERROR
);
103 g_Git
.m_critGitDllSec
.Unlock();
109 g_Git
.m_critGitDllSec
.Lock();
112 [&]{ ret
= git_get_log_nextcommit(handle
, &commit
, infomask
& CGit::LOG_INFO_FOLLOW
); }();
116 g_Git
.m_critGitDllSec
.Unlock();
117 MessageBox(NULL
, _T("Could not get next commit.\nlibgit reports:\n") + CString(msg
), _T("TortoiseGit"), MB_ICONERROR
);
120 g_Git
.m_critGitDllSec
.Unlock();
125 if (commit
.m_ignore
== 1)
127 git_free_commit(&commit
);
131 CGitHash hash
= (char*)commit
.m_hash
;
133 GitRev
*pRev
= this->m_pLogCache
->GetCacheData(hash
);
136 g_Git
.m_critGitDllSec
.Lock();
137 git_get_notes(commit
.m_hash
,¬e
);
138 g_Git
.m_critGitDllSec
.Unlock();
141 pRev
->m_Notes
.Empty();
142 g_Git
.StringAppend(&pRev
->m_Notes
,(BYTE
*)note
);
145 if((pRev
== NULL
|| !pRev
->m_IsFull
) && infomask
& CGit::LOG_INFO_FULL_DIFF
)
147 pRev
->ParserFromCommit(&commit
);
148 pRev
->ParserParentFromCommit(&commit
);
149 git_free_commit(&commit
);
150 //Must call free commit before SafeFetchFullInfo, commit parent is rewrite by log.
151 //file list will wrong if parent rewrite.
154 pRev
->SafeFetchFullInfo(&g_Git
);
156 catch (char * g_last_error
)
158 MessageBox(NULL
, _T("Could not fetch full info of a commit.\nlibgit reports:\n") + CString(g_last_error
), _T("TortoiseGit"), MB_ICONERROR
);
164 ASSERT(pRev
->m_CommitHash
== hash
);
165 pRev
->ParserFromCommit(&commit
);
166 pRev
->ParserParentFromCommit(&commit
);
167 git_free_commit(&commit
);
170 this->push_back(pRev
->m_CommitHash
);
172 m_HashMap
[pRev
->m_CommitHash
] = (int)size() - 1;
176 g_Git
.m_critGitDllSec
.Lock();
177 git_close_log(handle
);
178 g_Git
.m_critGitDllSec
.Unlock();
183 int AddTolist(unsigned char * /*osha1*/, unsigned char *nsha1
, const char * /*name*/, unsigned long /*time*/, int /*sz*/, const char *msg
, void *data
)
185 CLogDataVector
*vector
= (CLogDataVector
*)data
;
187 rev
.m_CommitHash
=(char*)nsha1
;
190 g_Git
.StringAppend(&one
, (BYTE
*)msg
);
192 int message
=one
.Find(_T(":"),0);
195 rev
.m_RefAction
=one
.Left(message
);
196 rev
.GetSubject()=one
.Mid(message
+1);
199 vector
->m_pLogCache
->m_HashMap
[rev
.m_CommitHash
]=rev
;
200 vector
->insert(vector
->begin(),rev
.m_CommitHash
);
205 int CLogDataVector::ParserFromRefLog(CString ref
)
207 if(g_Git
.m_IsUseGitDLL
)
209 git_for_each_reflog_ent(CUnicodeUtils::GetUTF8(ref
),AddTolist
,this);
210 for (size_t i
= 0; i
< size(); ++i
)
212 m_pLogCache
->m_HashMap
[at(i
)].m_Ref
.Format(_T("%s{%d}"), ref
,i
);
221 cmd
.Format(_T("git.exe reflog show %s"),ref
);
222 if (g_Git
.Run(cmd
, &out
, NULL
, CP_UTF8
))
228 CString one
=out
.Tokenize(_T("\n"),pos
);
229 int ref
=one
.Find(_T(' '),0);
235 if (g_Git
.GetHash(rev
.m_CommitHash
, one
.Left(ref
)))
237 MessageBox(NULL
, g_Git
.GetGitLastErr(_T("Could not get hash of ") + one
.Left(ref
) + _T(".")), _T("TortoiseGit"), MB_ICONERROR
);
240 int action
=one
.Find(_T(' '),ref
+1);
244 rev
.m_Ref
=one
.Mid(ref
+1,action
-ref
-2);
245 message
=one
.Find(_T(":"),action
);
248 rev
.m_RefAction
=one
.Mid(action
+1,message
-action
-1);
249 rev
.GetSubject()=one
.Right(one
.GetLength()-message
-1);
253 this->m_pLogCache
->m_HashMap
[rev
.m_CommitHash
]=rev
;
255 this->push_back(rev
.m_CommitHash
);
262 void CLogDataVector::setLane(CGitHash
& sha
)
264 Lanes
* l
= &(this->m_Lns
);
265 int i
= m_FirstFreeLane
;
267 // QVector<QByteArray> ba;
268 // const ShaString& ss = toPersistentSha(sha, ba);
269 // const ShaVect& shaVec(fh->revOrder);
271 for (int cnt
= (int)size(); i
< cnt
; ++i
) {
273 GitRev
* r
= & this->GetGitRevAt(i
);
274 CGitHash curSha
=r
->m_CommitHash
;
276 if (r
->m_Lanes
.empty())
277 updateLanes(*r
, *l
, curSha
);
282 m_FirstFreeLane
= ++i
;
285 Lanes
* l
= &(this->m_Lanes
);
286 int i
= m_FirstFreeLane
;
288 QVector
<QByteArray
> ba
;
289 const ShaString
& ss
= toPersistentSha(sha
, ba
);
290 const ShaVect
& shaVec(fh
->revOrder
);
292 for (uint cnt
= shaVec
.count(); i
< cnt
; ++i
) {
294 const ShaString
& curSha
= shaVec
[i
];
295 Rev
* r
= m_HashMap
[curSha
]const_cast<Rev
*>(revLookup(curSha
, fh
));
296 if (r
->lanes
.count() == 0)
297 updateLanes(*r
, *l
, curSha
);
302 fh
->firstFreeLane
= ++i
;
307 void CLogDataVector::updateLanes(GitRev
& c
, Lanes
& lns
, CGitHash
&sha
)
309 // we could get third argument from c.sha(), but we are in fast path here
310 // and c.sha() involves a deep copy, so we accept a little redundancy
315 bool isDiscontinuity
;
316 bool isFork
= lns
.isFork(sha
, isDiscontinuity
);
317 bool isMerge
= (c
.ParentsCount() > 1);
318 bool isInitial
= (c
.ParentsCount() == 0);
321 lns
.changeActiveLane(sha
); // uses previous isBoundary state
323 lns
.setBoundary(c
.IsBoundary() == TRUE
); // update must be here
324 TRACE(_T("%s %d"),c
.m_CommitHash
.ToString(),c
.IsBoundary());
329 lns
.setMerge(c
.m_ParentHash
);
335 lns
.getLanes(c
.m_Lanes
); // here lanes are snapshotted
339 nextSha
= c
.m_ParentHash
[0];
341 lns
.nextParent(nextSha
);
344 // lns.afterApplied();