1
// TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2009-2013, 2015-2018 - 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"
34 #include "GitLogListBase.h"
35 #include "UnicodeUtils.h"
37 typedef CComCritSecLock
<CComCriticalSection
> CAutoLocker
;
39 void CLogDataVector::ClearAll()
48 m_pLogCache
->ClearAllLanes();
51 //CLogDataVector Class
52 int CLogDataVector::ParserFromLog(CTGitPath
* path
, DWORD count
, DWORD infomask
, CString
* range
)
54 ATLASSERT(m_pLogCache
);
55 // only enable --follow on files
56 if ((!path
|| path
->IsDirectory()) && (infomask
& CGit::LOG_INFO_FOLLOW
))
57 infomask
= infomask
^ CGit::LOG_INFO_FOLLOW
;
59 CString gitrange
= L
"HEAD";
64 filter
.m_NumberOfLogsScale
= CFilterData::SHOW_NO_LIMIT
;
67 filter
.m_NumberOfLogs
= count
;
68 filter
.m_NumberOfLogsScale
= CFilterData::SHOW_LAST_N_COMMITS
;
70 CString cmd
= g_Git
.GetLogCmd(gitrange
, path
, infomask
, &filter
, m_logOrderBy
);
72 if (!g_Git
.CanParseRev(gitrange
))
77 [] { git_init(); } ();
79 catch (const char* msg
)
81 MessageBox(nullptr, L
"Could not initialize libgit.\nlibgit reports:\n" + CString(msg
), L
"TortoiseGit", MB_ICONERROR
);
88 CAutoLocker
lock(g_Git
.m_critGitDllSec
);
89 if (git_open_log(&handle
, CUnicodeUtils::GetMulti(cmd
, CP_UTF8
)))
94 MessageBox(nullptr, L
"Could not open log.\nlibgit reports:\n" + CString(msg
), L
"TortoiseGit", MB_ICONERROR
);
100 CAutoLocker
lock(g_Git
.m_critGitDllSec
);
101 [&]{ git_get_log_firstcommit(handle
); }();
105 MessageBox(nullptr, L
"Could not get first commit.\nlibgit reports:\n" + CString(msg
), L
"TortoiseGit", MB_ICONERROR
);
116 CAutoLocker
lock(g_Git
.m_critGitDllSec
);
117 [&]{ ret
= git_get_log_nextcommit(handle
, &commit
, infomask
& CGit::LOG_INFO_FOLLOW
); }();
121 MessageBox(nullptr, L
"Could not get next commit.\nlibgit reports:\n" + CString(msg
), L
"TortoiseGit", MB_ICONERROR
);
127 if (ret
!= -2) // other than end of revision walking
128 MessageBox(nullptr, (L
"Could not get next commit.\nlibgit returns:" + std::to_wstring(ret
)).c_str(), L
"TortoiseGit", MB_ICONERROR
);
132 if (commit
.m_ignore
== 1)
134 git_free_commit(&commit
);
138 CGitHash
hash(commit
.m_hash
);
140 GitRevLoglist
* pRev
= this->m_pLogCache
->GetCacheData(hash
);
142 char *pNote
= nullptr;
144 CAutoLocker
lock(g_Git
.m_critGitDllSec
);
145 git_get_notes(commit
.m_hash
, &pNote
);
149 pRev
->m_Notes
= CUnicodeUtils::GetUnicode(pNote
);
154 pRev
->ParserFromCommit(&commit
);
155 pRev
->ParserParentFromCommit(&commit
);
156 git_free_commit(&commit
);
157 // Must call free commit before SafeFetchFullInfo, commit parent is rewrite by log.
158 // file list will wrong if parent rewrite.
160 if (!pRev
->m_IsFull
&& (infomask
& CGit::LOG_INFO_FULL_DIFF
))
162 if (pRev
->SafeFetchFullInfo(&g_Git
))
164 MessageBox(nullptr, pRev
->GetLastErr(), L
"TortoiseGit", MB_ICONERROR
);
169 this->push_back(pRev
->m_CommitHash
);
171 m_HashMap
[pRev
->m_CommitHash
] = (int)size() - 1;
175 CAutoLocker
lock(g_Git
.m_critGitDllSec
);
176 git_close_log(handle
);
182 struct SortByParentDate
184 bool operator()(GitRevLoglist
* pLhs
, GitRevLoglist
* pRhs
)
186 if (pLhs
->m_CommitHash
== pRhs
->m_CommitHash
)
188 if (std::any_of(pLhs
->m_ParentHash
.cbegin(), pLhs
->m_ParentHash
.cend(), [&pRhs
](auto& hash
) { return hash
== pRhs
->m_CommitHash
; }))
190 if (std::any_of(pRhs
->m_ParentHash
.cbegin(), pRhs
->m_ParentHash
.cend(), [&pLhs
](auto& hash
) { return hash
== pLhs
->m_CommitHash
; }))
192 return pLhs
->GetCommitterDate()>pRhs
->GetCommitterDate();
196 int CLogDataVector::Fill(std::unordered_set
<CGitHash
>& hashes
)
198 ATLASSERT(m_pLogCache
);
201 [] { git_init(); } ();
203 catch (const char* msg
)
205 MessageBox(nullptr, L
"Could not initialize libgit.\nlibgit reports:\n" + CString(msg
), L
"TortoiseGit", MB_ICONERROR
);
209 std::set
<GitRevLoglist
*, SortByParentDate
> revs
;
211 for (const auto& hash
: hashes
)
216 CAutoLocker
lock(g_Git
.m_critGitDllSec
);
217 if (git_get_commit_from_hash(&commit
, static_cast<const unsigned char*>(hash
)))
222 MessageBox(nullptr, L
"Could not get commit \"" + hash
.ToString() + L
"\".\nlibgit reports:\n" + CString(msg
), L
"TortoiseGit", MB_ICONERROR
);
226 GitRevLoglist
* pRev
= this->m_pLogCache
->GetCacheData(hash
);
228 // right now this code is only used by TortoiseGitBlame,
229 // as such git notes are not needed to be loaded
231 pRev
->ParserFromCommit(&commit
);
232 pRev
->ParserParentFromCommit(&commit
);
233 git_free_commit(&commit
);
238 for (const auto& pRev
: revs
)
240 this->push_back(pRev
->m_CommitHash
);
241 m_HashMap
[pRev
->m_CommitHash
] = (int)size() - 1;
247 void CLogDataVector::append(CGitHash
& sha
, bool storeInVector
)
250 this->push_back(sha
);
252 GitRevLoglist
* r
= &m_pLogCache
->m_HashMap
[sha
];
253 updateLanes(*r
, this->m_Lns
, sha
);
256 void CLogDataVector::setLane(CGitHash
& sha
)
258 Lanes
* l
= &(this->m_Lns
);
259 int i
= m_FirstFreeLane
;
261 // QVector<QByteArray> ba;
262 // const ShaString& ss = toPersistentSha(sha, ba);
263 // const ShaVect& shaVec(fh->revOrder);
265 for (int cnt
= (int)size(); i
< cnt
; ++i
) {
266 GitRevLoglist
* r
= &this->GetGitRevAt(i
);
267 CGitHash curSha
=r
->m_CommitHash
;
269 if (r
->m_Lanes
.empty())
270 updateLanes(*r
, *l
, curSha
);
275 m_FirstFreeLane
= ++i
;
278 Lanes
* l
= &(this->m_Lanes
);
279 int i
= m_FirstFreeLane
;
281 QVector
<QByteArray
> ba
;
282 const ShaString
& ss
= toPersistentSha(sha
, ba
);
283 const ShaVect
& shaVec(fh
->revOrder
);
285 for (uint cnt
= shaVec
.count(); i
< cnt
; ++i
) {
286 const ShaString
& curSha
= shaVec
[i
];
287 Rev
* r
= m_HashMap
[curSha
]const_cast<Rev
*>(revLookup(curSha
, fh
));
288 if (r
->lanes
.count() == 0)
289 updateLanes(*r
, *l
, curSha
);
294 fh
->firstFreeLane
= ++i
;
298 void CLogDataVector::updateLanes(GitRevLoglist
& c
, Lanes
& lns
, CGitHash
& sha
)
300 // we could get third argument from c.sha(), but we are in fast path here
301 // and c.sha() involves a deep copy, so we accept a little redundancy
306 bool isDiscontinuity
;
307 bool isFork
= lns
.isFork(sha
, isDiscontinuity
);
308 bool isMerge
= (c
.ParentsCount() > 1);
309 bool isInitial
= (c
.ParentsCount() == 0);
312 lns
.changeActiveLane(sha
); // uses previous isBoundary state
314 lns
.setBoundary(c
.IsBoundary() == TRUE
, isInitial
); // update must be here
315 TRACE(L
"%s %d", (LPCTSTR
)c
.m_CommitHash
.ToString(), c
.IsBoundary());
320 lns
.setMerge(c
.m_ParentHash
);
326 lns
.getLanes(c
.m_Lanes
); // here lanes are snapshotted
330 nextSha
= c
.m_ParentHash
[0];
332 lns
.nextParent(nextSha
);
335 // lns.afterApplied();