1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2003-2008 - TortoiseSVN
4 // Copyright (C) 2008-2016 - TortoiseGit
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.
25 #include "MessageBox.h"
26 #include "FileDiffDlg.h"
27 #include "SubmoduleDiffDlg.h"
30 int CGitDiff::SubmoduleDiffNull(const CTGitPath
* pPath
, const git_revnum_t
&rev1
)
32 CString oldhash
= GIT_REV_ZERO
;
38 if (rev1
!= GIT_REV_ZERO
)
39 cmd
.Format(L
"git.exe ls-tree \"%s\" -- \"%s\"", (LPCTSTR
)rev1
, (LPCTSTR
)pPath
->GetGitPathString());
41 cmd
.Format(L
"git.exe ls-files -s -- \"%s\"", (LPCTSTR
)pPath
->GetGitPathString());
44 if (g_Git
.Run(cmd
, &output
, &err
, CP_UTF8
))
46 CMessageBox::Show(nullptr, output
+ L
'\n' + err
, L
"TortoiseGit", MB_OK
| MB_ICONERROR
);
50 int start
= output
.Find(L
' ');
53 if (rev1
!= GIT_REV_ZERO
) // in ls-files the hash is in the second column; in ls-tree it's in the third one
54 start
= output
.Find(L
' ', start
+ 1);
56 newhash
=output
.Mid(start
+1, 40);
59 subgit
.m_CurrentDir
= g_Git
.CombinePath(pPath
);
60 int encode
=CAppUtils::GetLogOutputEncode(&subgit
);
62 cmd
.Format(L
"git.exe log -n1 --pretty=format:\"%%s\" %s --", (LPCTSTR
)newhash
);
63 bool toOK
= !subgit
.Run(cmd
,&newsub
,encode
);
66 if (rev1
== GIT_REV_ZERO
&& !(pPath
->m_Action
& CTGitPath::LOGACTIONS_DELETED
))
69 subgit
.Run(L
"git.exe status --porcelain", &dirtyList
, encode
);
70 dirty
= !dirtyList
.IsEmpty();
73 CSubmoduleDiffDlg submoduleDiffDlg
;
74 if (pPath
->m_Action
& CTGitPath::LOGACTIONS_DELETED
)
75 submoduleDiffDlg
.SetDiff(pPath
->GetWinPath(), false, newhash
, newsub
, toOK
, oldhash
, oldsub
, false, dirty
, DeleteSubmodule
);
77 submoduleDiffDlg
.SetDiff(pPath
->GetWinPath(), false, oldhash
, oldsub
, true, newhash
, newsub
, toOK
, dirty
, NewSubmodule
);
78 submoduleDiffDlg
.DoModal();
79 if (submoduleDiffDlg
.IsRefresh())
85 if (rev1
!= GIT_REV_ZERO
)
86 CMessageBox::Show(nullptr, L
"ls-tree output format error", L
"TortoiseGit", MB_OK
| MB_ICONERROR
);
88 CMessageBox::Show(nullptr, L
"ls-files output format error", L
"TortoiseGit", MB_OK
| MB_ICONERROR
);
92 int CGitDiff::DiffNull(const CTGitPath
*pPath
, git_revnum_t rev1
, bool bIsAdd
, int jumpToLine
, bool bAlternative
)
94 if (rev1
!= GIT_REV_ZERO
)
97 if (g_Git
.GetHash(rev1Hash
, rev1
)) // make sure we have a HASH here, otherwise filenames might be invalid
99 MessageBox(nullptr, g_Git
.GetGitLastErr(L
"Could not get hash of \"" + rev1
+ L
"\"."), L
"TortoiseGit", MB_ICONERROR
);
102 rev1
= rev1Hash
.ToString();
108 if(pPath
->IsDirectory())
111 // refresh if result = 1
112 CTGitPath path
= *pPath
;
113 while ((result
= SubmoduleDiffNull(&path
, rev1
)) == 1)
114 path
.SetFromGit(pPath
->GetGitPathString());
118 if(rev1
!= GIT_REV_ZERO
)
120 file1
= CTempFiles::Instance().GetTempFilePath(false, *pPath
, rev1
).GetWinPathString();
121 if (g_Git
.GetOneFile(rev1
, *pPath
, file1
))
124 out
.Format(IDS_STATUSLIST_CHECKOUTFILEFAILED
, (LPCTSTR
)pPath
->GetGitPathString(), (LPCTSTR
)rev1
, (LPCTSTR
)file1
);
125 CMessageBox::Show(nullptr, g_Git
.GetGitLastErr(out
, CGit::GIT_CMD_GETONEFILE
), L
"TortoiseGit", MB_OK
);
128 ::SetFileAttributes(file1
, FILE_ATTRIBUTE_READONLY
);
131 file1
= g_Git
.CombinePath(pPath
);
133 CString tempfile
= CTempFiles::Instance().GetTempFilePath(false, *pPath
, rev1
).GetWinPathString();
134 ::SetFileAttributes(tempfile
, FILE_ATTRIBUTE_READONLY
);
136 CAppUtils::DiffFlags flags
;
137 flags
.bAlternativeTool
= bAlternative
;
139 CAppUtils::StartExtDiff(tempfile
,file1
,
140 pPath
->GetGitPathString(),
141 pPath
->GetGitPathString() + L
':' + rev1
.Left(g_Git
.GetShortHASHLength()),
142 g_Git
.CombinePath(pPath
), g_Git
.CombinePath(pPath
),
143 git_revnum_t(GIT_REV_ZERO
), rev1
144 , flags
, jumpToLine
);
146 CAppUtils::StartExtDiff(file1
,tempfile
,
147 pPath
->GetGitPathString() + L
':' + rev1
.Left(g_Git
.GetShortHASHLength()),
148 pPath
->GetGitPathString(),
149 g_Git
.CombinePath(pPath
), g_Git
.CombinePath(pPath
),
150 rev1
, git_revnum_t(GIT_REV_ZERO
)
151 , flags
, jumpToLine
);
156 int CGitDiff::SubmoduleDiff(const CTGitPath
* pPath
, const CTGitPath
* /*pPath2*/, const git_revnum_t
&rev1
, const git_revnum_t
&rev2
, bool /*blame*/, bool /*unified*/)
162 bool isWorkingCopy
= false;
163 if( rev2
== GIT_REV_ZERO
|| rev1
== GIT_REV_ZERO
)
165 oldhash
= GIT_REV_ZERO
;
166 newhash
= GIT_REV_ZERO
;
169 if( rev2
!= GIT_REV_ZERO
)
171 if( rev1
!= GIT_REV_ZERO
)
174 isWorkingCopy
= true;
176 cmd
.Format(L
"git.exe diff %s -- \"%s\"",
177 (LPCTSTR
)rev
, (LPCTSTR
)pPath
->GetGitPathString());
180 if (g_Git
.Run(cmd
, &output
, &err
, CP_UTF8
))
182 CMessageBox::Show(nullptr, output
+ L
'\n' + err
, L
"TortoiseGit", MB_OK
| MB_ICONERROR
);
186 if (output
.IsEmpty())
190 // also compare against index
191 cmd
.Format(L
"git.exe diff -- \"%s\"", (LPCTSTR
)pPath
->GetGitPathString());
192 if (g_Git
.Run(cmd
, &output
, &err
, CP_UTF8
))
194 CMessageBox::Show(nullptr, output
+ L
'\n' + err
, L
"TortoiseGit", MB_OK
| MB_ICONERROR
);
198 if (output
.IsEmpty())
200 CMessageBox::Show(nullptr, IDS_ERR_EMPTYDIFF
, IDS_APPNAME
, MB_OK
| MB_ICONERROR
);
203 else if (CMessageBox::Show(nullptr, IDS_SUBMODULE_EMPTYDIFF
, IDS_APPNAME
, 1, IDI_QUESTION
, IDS_MSGBOX_YES
, IDS_MSGBOX_NO
) == 1)
206 sCmd
.Format(L
"/command:subupdate /bkpath:\"%s\"", (LPCTSTR
)g_Git
.m_CurrentDir
);
207 CAppUtils::RunTortoiseGitProc(sCmd
);
213 int oldstart
= output
.Find(L
"-Subproject commit", start
);
216 CMessageBox::Show(nullptr, L
"Subproject Diff Format error", L
"TortoiseGit", MB_OK
| MB_ICONERROR
);
219 oldhash
= output
.Mid(oldstart
+ CString(L
"-Subproject commit").GetLength() + 1, 40);
221 int newstart
= output
.Find(L
"+Subproject commit",start
);
224 CMessageBox::Show(nullptr, L
"Subproject Diff Format error", L
"TortoiseGit", MB_OK
| MB_ICONERROR
);
227 newhash
= output
.Mid(newstart
+ CString(L
"+Subproject commit").GetLength() + 1, 40);
228 dirty
= output
.Mid(newstart
+ CString(L
"+Subproject commit").GetLength() + 41) == L
"-dirty\n";
232 cmd
.Format(L
"git.exe diff-tree -r -z %s %s -- \"%s\"",
233 (LPCTSTR
)rev2
, (LPCTSTR
)rev1
, (LPCTSTR
)pPath
->GetGitPathString());
235 BYTE_VECTOR bytes
, errBytes
;
236 if(g_Git
.Run(cmd
, &bytes
, &errBytes
))
239 CGit::StringAppend(&err
, &errBytes
[0], CP_UTF8
);
240 CMessageBox::Show(nullptr, err
, L
"TortoiseGit", MB_OK
| MB_ICONERROR
);
244 if (bytes
.size() < 15 + 2 * GIT_HASH_SIZE
+ 1 + 2 * GIT_HASH_SIZE
)
246 CMessageBox::Show(nullptr, L
"git diff-tree gives invalid output", L
"TortoiseGit", MB_OK
| MB_ICONERROR
);
249 CGit::StringAppend(&oldhash
, &bytes
[15], CP_UTF8
, 2 * GIT_HASH_SIZE
);
250 CGit::StringAppend(&newhash
, &bytes
[15 + 2 * GIT_HASH_SIZE
+ 1], CP_UTF8
, 2 * GIT_HASH_SIZE
);
256 bool oldOK
= false, newOK
= false;
259 subgit
.m_CurrentDir
= g_Git
.CombinePath(pPath
);
260 ChangeType changeType
= Unknown
;
262 if (pPath
->HasAdminDir())
263 GetSubmoduleChangeType(subgit
, oldhash
, newhash
, oldOK
, newOK
, changeType
, oldsub
, newsub
);
265 CSubmoduleDiffDlg submoduleDiffDlg
;
266 submoduleDiffDlg
.SetDiff(pPath
->GetWinPath(), isWorkingCopy
, oldhash
, oldsub
, oldOK
, newhash
, newsub
, newOK
, dirty
, changeType
);
267 submoduleDiffDlg
.DoModal();
268 if (submoduleDiffDlg
.IsRefresh())
274 void CGitDiff::GetSubmoduleChangeType(CGit
& subgit
, const CString
& oldhash
, const CString
& newhash
, bool& oldOK
, bool& newOK
, ChangeType
& changeType
, CString
& oldsub
, CString
& newsub
)
277 int encode
= CAppUtils::GetLogOutputEncode(&subgit
);
278 int oldTime
= 0, newTime
= 0;
280 if (oldhash
!= GIT_REV_ZERO
)
282 CString cmdout
, cmderr
;
283 cmd
.Format(L
"git.exe log -n1 --pretty=format:\"%%ct %%s\" %s --", (LPCTSTR
)oldhash
);
284 oldOK
= !subgit
.Run(cmd
, &cmdout
, &cmderr
, encode
);
287 int pos
= cmdout
.Find(L
' ');
288 oldTime
= _wtoi(cmdout
.Left(pos
));
289 oldsub
= cmdout
.Mid(pos
+ 1);
294 if (newhash
!= GIT_REV_ZERO
)
296 CString cmdout
, cmderr
;
297 cmd
.Format(L
"git.exe log -n1 --pretty=format:\"%%ct %%s\" %s --", (LPCTSTR
)newhash
);
298 newOK
= !subgit
.Run(cmd
, &cmdout
, &cmderr
, encode
);
301 int pos
= cmdout
.Find(L
' ');
302 newTime
= _wtoi(cmdout
.Left(pos
));
303 newsub
= cmdout
.Mid(pos
+ 1);
309 if (oldhash
== GIT_REV_ZERO
)
312 changeType
= NewSubmodule
;
314 else if (newhash
== GIT_REV_ZERO
)
317 changeType
= DeleteSubmodule
;
319 else if (oldhash
!= newhash
)
321 bool ffNewer
= subgit
.IsFastForward(oldhash
, newhash
);
324 bool ffOlder
= subgit
.IsFastForward(newhash
, oldhash
);
327 if (newTime
> oldTime
)
328 changeType
= NewerTime
;
329 else if (newTime
< oldTime
)
330 changeType
= OlderTime
;
332 changeType
= SameTime
;
338 changeType
= FastForward
;
340 else if (oldhash
== newhash
)
341 changeType
= Identical
;
343 if (!oldOK
|| !newOK
)
344 changeType
= Unknown
;
347 int CGitDiff::Diff(const CTGitPath
* pPath
, const CTGitPath
* pPath2
, git_revnum_t rev1
, git_revnum_t rev2
, bool /*blame*/, bool /*unified*/, int jumpToLine
, bool bAlternativeTool
)
349 // make sure we have HASHes here, otherwise filenames might be invalid
350 if (rev1
!= GIT_REV_ZERO
)
353 if (g_Git
.GetHash(rev1Hash
, rev1
))
355 MessageBox(nullptr, g_Git
.GetGitLastErr(L
"Could not get hash of \"" + rev1
+ L
"\"."), L
"TortoiseGit", MB_ICONERROR
);
358 rev1
= rev1Hash
.ToString();
360 if (rev2
!= GIT_REV_ZERO
)
363 if (g_Git
.GetHash(rev2Hash
, rev2
))
365 MessageBox(nullptr, g_Git
.GetGitLastErr(L
"Could not get hash of \"" + rev2
+ L
"\"."), L
"TortoiseGit", MB_ICONERROR
);
368 rev2
= rev2Hash
.ToString();
375 if(pPath
->IsDirectory() || pPath2
->IsDirectory())
378 // refresh if result = 1
379 CTGitPath path
= *pPath
;
380 CTGitPath path2
= *pPath2
;
381 while ((result
= SubmoduleDiff(&path
, &path2
, rev1
, rev2
)) == 1)
383 path
.SetFromGit(pPath
->GetGitPathString());
384 path2
.SetFromGit(pPath2
->GetGitPathString());
389 if(rev1
!= GIT_REV_ZERO
)
391 // use original file extension, an external diff tool might need it
392 file1
= CTempFiles::Instance().GetTempFilePath(false, *pPath
, rev1
).GetWinPathString();
393 title1
= pPath
->GetGitPathString() + L
": " + rev1
.Left(g_Git
.GetShortHASHLength());
394 if (g_Git
.GetOneFile(rev1
, *pPath
, file1
))
397 out
.Format(IDS_STATUSLIST_CHECKOUTFILEFAILED
, (LPCTSTR
)pPath
->GetGitPathString(), (LPCTSTR
)rev1
, (LPCTSTR
)file1
);
398 CMessageBox::Show(nullptr, g_Git
.GetGitLastErr(out
, CGit::GIT_CMD_GETONEFILE
), L
"TortoiseGit", MB_OK
);
401 ::SetFileAttributes(file1
, FILE_ATTRIBUTE_READONLY
);
405 file1
= g_Git
.CombinePath(pPath
);
406 title1
.Format(IDS_DIFF_WCNAME
, (LPCTSTR
)pPath
->GetGitPathString());
407 if (!PathFileExists(file1
))
410 sMsg
.Format(IDS_PROC_DIFFERROR_FILENOTINWORKINGTREE
, (LPCTSTR
)file1
);
411 if (MessageBox(nullptr, sMsg
, L
"TortoiseGit", MB_ICONEXCLAMATION
| MB_YESNO
) != IDYES
)
413 if (!CCommonAppUtils::FileOpenSave(file1
, nullptr, IDS_SELECTFILE
, IDS_COMMONFILEFILTER
, true))
421 if(rev2
!= GIT_REV_ZERO
)
423 CTGitPath fileName
= *pPath2
;
424 if (pPath2
->m_Action
& CTGitPath::LOGACTIONS_REPLACED
)
425 fileName
= CTGitPath(pPath2
->GetGitOldPathString());
427 file2
= CTempFiles::Instance().GetTempFilePath(false, fileName
, rev2
).GetWinPathString();
428 title2
= fileName
.GetGitPathString() + L
": " + rev2
.Left(g_Git
.GetShortHASHLength());
429 if (g_Git
.GetOneFile(rev2
, fileName
, file2
))
432 out
.Format(IDS_STATUSLIST_CHECKOUTFILEFAILED
, (LPCTSTR
)pPath2
->GetGitPathString(), (LPCTSTR
)rev2
, (LPCTSTR
)file2
);
433 CMessageBox::Show(nullptr, g_Git
.GetGitLastErr(out
, CGit::GIT_CMD_GETONEFILE
), L
"TortoiseGit", MB_OK
);
436 ::SetFileAttributes(file2
, FILE_ATTRIBUTE_READONLY
);
440 file2
= g_Git
.CombinePath(pPath2
);
441 title2
.Format(IDS_DIFF_WCNAME
, pPath2
->GetGitPathString());
444 CAppUtils::DiffFlags flags
;
445 flags
.bAlternativeTool
= bAlternativeTool
;
446 CAppUtils::StartExtDiff(file2
,file1
,
449 g_Git
.CombinePath(pPath2
),
450 g_Git
.CombinePath(pPath
),
458 int CGitDiff::DiffCommit(const CTGitPath
& path
, const GitRev
* r1
, const GitRev
* r2
, bool bAlternative
)
460 return DiffCommit(path
, path
, r1
, r2
, bAlternative
);
463 int CGitDiff::DiffCommit(const CTGitPath
& path1
, const CTGitPath
& path2
, const GitRev
* r1
, const GitRev
* r2
, bool bAlternative
)
465 if (path1
.GetWinPathString().IsEmpty())
468 dlg
.SetDiff(nullptr, *r2
, *r1
);
471 else if (path1
.IsDirectory())
474 dlg
.SetDiff(&path1
, *r2
, *r1
);
478 Diff(&path1
, &path2
, r1
->m_CommitHash
.ToString(), r2
->m_CommitHash
.ToString(), false, false, 0, bAlternative
);
482 int CGitDiff::DiffCommit(const CTGitPath
& path
, const CString
& r1
, const CString
& r2
, bool bAlternative
)
484 return DiffCommit(path
, path
, r1
, r2
, bAlternative
);
487 int CGitDiff::DiffCommit(const CTGitPath
& path1
, const CTGitPath
& path2
, const CString
& r1
, const CString
& r2
, bool bAlternative
)
489 if (path1
.GetWinPathString().IsEmpty())
492 dlg
.SetDiff(nullptr, r2
, r1
);
495 else if (path1
.IsDirectory())
498 dlg
.SetDiff(&path1
, r2
, r1
);
502 Diff(&path1
, &path2
, r1
, r2
, false, false, 0, bAlternative
);