1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2003-2008 - TortoiseSVN
4 // Copyright (C) 2008-2018 - 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.
22 #include "TortoiseProc.h"
26 #include "MessageBox.h"
27 #include "FileDiffDlg.h"
28 #include "SubmoduleDiffDlg.h"
31 int CGitDiff::SubmoduleDiffNull(HWND hWnd
, const CTGitPath
* pPath
, const CString
& rev1
)
37 if (rev1
!= GIT_REV_ZERO
)
38 cmd
.Format(L
"git.exe ls-tree \"%s\" -- \"%s\"", (LPCTSTR
)rev1
, (LPCTSTR
)pPath
->GetGitPathString());
40 cmd
.Format(L
"git.exe ls-files -s -- \"%s\"", (LPCTSTR
)pPath
->GetGitPathString());
43 if (g_Git
.Run(cmd
, &output
, &err
, CP_UTF8
))
45 CMessageBox::Show(hWnd
, output
+ L
'\n' + err
, L
"TortoiseGit", MB_OK
| MB_ICONERROR
);
49 int start
= output
.Find(L
' ');
52 if (rev1
!= GIT_REV_ZERO
) // in ls-files the hash is in the second column; in ls-tree it's in the third one
53 start
= output
.Find(L
' ', start
+ 1);
55 newhash
=output
.Mid(start
+ 1, GIT_HASH_SIZE
* 2);
58 subgit
.m_CurrentDir
= g_Git
.CombinePath(pPath
);
59 int encode
=CAppUtils::GetLogOutputEncode(&subgit
);
61 cmd
.Format(L
"git.exe log -n1 --pretty=format:\"%%s\" %s --", (LPCTSTR
)newhash
);
62 bool toOK
= !subgit
.Run(cmd
,&newsub
,encode
);
65 if (rev1
== GIT_REV_ZERO
&& !(pPath
->m_Action
& CTGitPath::LOGACTIONS_DELETED
))
68 subgit
.Run(L
"git.exe status --porcelain", &dirtyList
, encode
);
69 dirty
= !dirtyList
.IsEmpty();
72 CSubmoduleDiffDlg
submoduleDiffDlg(GetExplorerHWND() == hWnd
? nullptr : CWnd::FromHandle(hWnd
));
73 if (pPath
->m_Action
& CTGitPath::LOGACTIONS_DELETED
)
74 submoduleDiffDlg
.SetDiff(pPath
->GetWinPath(), false, newhash
, newsub
, toOK
, GIT_REV_ZERO
, L
"", false, dirty
, DeleteSubmodule
);
76 submoduleDiffDlg
.SetDiff(pPath
->GetWinPath(), false, GIT_REV_ZERO
, L
"", true, newhash
, newsub
, toOK
, dirty
, NewSubmodule
);
77 submoduleDiffDlg
.DoModal();
78 if (submoduleDiffDlg
.IsRefresh())
84 if (rev1
!= GIT_REV_ZERO
)
85 CMessageBox::Show(hWnd
, L
"ls-tree output format error", L
"TortoiseGit", MB_OK
| MB_ICONERROR
);
87 CMessageBox::Show(hWnd
, L
"ls-files output format error", L
"TortoiseGit", MB_OK
| MB_ICONERROR
);
91 int CGitDiff::DiffNull(HWND hWnd
, const CTGitPath
* pPath
, CString rev1
, bool bIsAdd
, int jumpToLine
, bool bAlternative
)
93 if (rev1
!= GIT_REV_ZERO
)
96 if (g_Git
.GetHash(rev1Hash
, rev1
)) // make sure we have a HASH here, otherwise filenames might be invalid
98 MessageBox(hWnd
, g_Git
.GetGitLastErr(L
"Could not get hash of \"" + rev1
+ L
"\"."), L
"TortoiseGit", MB_ICONERROR
);
101 rev1
= rev1Hash
.ToString();
107 if(pPath
->IsDirectory())
110 // refresh if result = 1
111 CTGitPath path
= *pPath
;
112 while ((result
= SubmoduleDiffNull(hWnd
, &path
, rev1
)) == 1)
113 path
.SetFromGit(pPath
->GetGitPathString());
117 if(rev1
!= GIT_REV_ZERO
)
119 file1
= CTempFiles::Instance().GetTempFilePath(false, *pPath
, rev1
).GetWinPathString();
120 if (g_Git
.GetOneFile(rev1
, *pPath
, file1
))
123 out
.Format(IDS_STATUSLIST_CHECKOUTFILEFAILED
, (LPCTSTR
)pPath
->GetGitPathString(), (LPCTSTR
)rev1
, (LPCTSTR
)file1
);
124 CMessageBox::Show(hWnd
, g_Git
.GetGitLastErr(out
, CGit::GIT_CMD_GETONEFILE
), L
"TortoiseGit", MB_OK
);
127 ::SetFileAttributes(file1
, FILE_ATTRIBUTE_READONLY
);
130 file1
= g_Git
.CombinePath(pPath
);
132 CString tempfile
= CTempFiles::Instance().GetTempFilePath(false, *pPath
, rev1
).GetWinPathString();
133 ::SetFileAttributes(tempfile
, FILE_ATTRIBUTE_READONLY
);
135 CAppUtils::DiffFlags flags
;
136 flags
.bAlternativeTool
= bAlternative
;
138 CAppUtils::StartExtDiff(tempfile
,file1
,
139 pPath
->GetGitPathString(),
140 pPath
->GetGitPathString() + L
':' + rev1
.Left(g_Git
.GetShortHASHLength()),
141 g_Git
.CombinePath(pPath
), g_Git
.CombinePath(pPath
),
143 , flags
, jumpToLine
);
145 CAppUtils::StartExtDiff(file1
,tempfile
,
146 pPath
->GetGitPathString() + L
':' + rev1
.Left(g_Git
.GetShortHASHLength()),
147 pPath
->GetGitPathString(),
148 g_Git
.CombinePath(pPath
), g_Git
.CombinePath(pPath
),
150 , flags
, jumpToLine
);
155 int CGitDiff::SubmoduleDiff(HWND hWnd
, const CTGitPath
* pPath
, const CTGitPath
* /*pPath2*/, const CString
& rev1
, const CString
& rev2
, bool /*blame*/, bool /*unified*/)
161 bool isWorkingCopy
= false;
162 if( rev2
== GIT_REV_ZERO
|| rev1
== GIT_REV_ZERO
)
164 oldhash
= GIT_REV_ZERO
;
165 newhash
= GIT_REV_ZERO
;
168 if( rev2
!= GIT_REV_ZERO
)
170 if( rev1
!= GIT_REV_ZERO
)
173 isWorkingCopy
= true;
175 cmd
.Format(L
"git.exe diff %s -- \"%s\"",
176 (LPCTSTR
)rev
, (LPCTSTR
)pPath
->GetGitPathString());
179 if (g_Git
.Run(cmd
, &output
, &err
, CP_UTF8
))
181 CMessageBox::Show(hWnd
, output
+ L
'\n' + err
, L
"TortoiseGit", MB_OK
| MB_ICONERROR
);
185 if (output
.IsEmpty())
189 // also compare against index
190 cmd
.Format(L
"git.exe diff -- \"%s\"", (LPCTSTR
)pPath
->GetGitPathString());
191 if (g_Git
.Run(cmd
, &output
, &err
, CP_UTF8
))
193 CMessageBox::Show(hWnd
, output
+ L
'\n' + err
, L
"TortoiseGit", MB_OK
| MB_ICONERROR
);
197 if (output
.IsEmpty())
199 CMessageBox::Show(hWnd
, IDS_ERR_EMPTYDIFF
, IDS_APPNAME
, MB_OK
| MB_ICONERROR
);
202 else if (CMessageBox::Show(hWnd
, IDS_SUBMODULE_EMPTYDIFF
, IDS_APPNAME
, 1, IDI_QUESTION
, IDS_MSGBOX_YES
, IDS_MSGBOX_NO
) == 1)
205 sCmd
.Format(L
"/command:subupdate /bkpath:\"%s\"", (LPCTSTR
)g_Git
.m_CurrentDir
);
206 CAppUtils::RunTortoiseGitProc(sCmd
);
212 int oldstart
= output
.Find(L
"-Subproject commit", start
);
215 CMessageBox::Show(hWnd
, L
"Subproject Diff Format error", L
"TortoiseGit", MB_OK
| MB_ICONERROR
);
218 oldhash
= output
.Mid(oldstart
+ (int)wcslen(L
"-Subproject commit") + 1, GIT_HASH_SIZE
* 2);
220 int newstart
= output
.Find(L
"+Subproject commit",start
);
223 CMessageBox::Show(hWnd
, L
"Subproject Diff Format error", L
"TortoiseGit", MB_OK
| MB_ICONERROR
);
226 newhash
= output
.Mid(newstart
+ (int)wcslen(L
"+Subproject commit") + 1, GIT_HASH_SIZE
* 2);
227 dirty
= output
.Mid(newstart
+ (int)wcslen(L
"+Subproject commit") + GIT_HASH_SIZE
* 2 + 1) == L
"-dirty\n";
231 cmd
.Format(L
"git.exe diff-tree -r -z %s %s -- \"%s\"",
232 (LPCTSTR
)rev2
, (LPCTSTR
)rev1
, (LPCTSTR
)pPath
->GetGitPathString());
234 BYTE_VECTOR bytes
, errBytes
;
235 if(g_Git
.Run(cmd
, &bytes
, &errBytes
))
238 CGit::StringAppend(&err
, &errBytes
[0], CP_UTF8
);
239 CMessageBox::Show(hWnd
, err
, L
"TortoiseGit", MB_OK
| MB_ICONERROR
);
243 if (bytes
.size() < 15 + 2 * GIT_HASH_SIZE
+ 1 + 2 * GIT_HASH_SIZE
)
245 CMessageBox::Show(hWnd
, L
"git diff-tree gives invalid output", L
"TortoiseGit", MB_OK
| MB_ICONERROR
);
248 CGit::StringAppend(&oldhash
, &bytes
[15], CP_UTF8
, 2 * GIT_HASH_SIZE
);
249 CGit::StringAppend(&newhash
, &bytes
[15 + 2 * GIT_HASH_SIZE
+ 1], CP_UTF8
, 2 * GIT_HASH_SIZE
);
255 bool oldOK
= false, newOK
= false;
258 subgit
.m_CurrentDir
= g_Git
.CombinePath(pPath
);
259 ChangeType changeType
= Unknown
;
261 if (pPath
->HasAdminDir())
262 GetSubmoduleChangeType(subgit
, oldhash
, newhash
, oldOK
, newOK
, changeType
, oldsub
, newsub
);
264 CSubmoduleDiffDlg
submoduleDiffDlg(GetExplorerHWND() == hWnd
? nullptr : CWnd::FromHandle(hWnd
));
265 submoduleDiffDlg
.SetDiff(pPath
->GetWinPath(), isWorkingCopy
, oldhash
, oldsub
, oldOK
, newhash
, newsub
, newOK
, dirty
, changeType
);
266 submoduleDiffDlg
.DoModal();
267 if (submoduleDiffDlg
.IsRefresh())
273 void CGitDiff::GetSubmoduleChangeType(CGit
& subgit
, const CGitHash
& oldhash
, const CGitHash
& newhash
, bool& oldOK
, bool& newOK
, ChangeType
& changeType
, CString
& oldsub
, CString
& newsub
)
276 int encode
= CAppUtils::GetLogOutputEncode(&subgit
);
277 int oldTime
= 0, newTime
= 0;
279 if (!oldhash
.IsEmpty())
281 CString cmdout
, cmderr
;
282 cmd
.Format(L
"git.exe log -n1 --pretty=format:\"%%ct %%s\" %s --", (LPCTSTR
)oldhash
.ToString());
283 oldOK
= !subgit
.Run(cmd
, &cmdout
, &cmderr
, encode
);
286 int pos
= cmdout
.Find(L
' ');
287 oldTime
= _wtoi(cmdout
.Left(pos
));
288 oldsub
= cmdout
.Mid(pos
+ 1);
293 if (!newhash
.IsEmpty())
295 CString cmdout
, cmderr
;
296 cmd
.Format(L
"git.exe log -n1 --pretty=format:\"%%ct %%s\" %s --", (LPCTSTR
)newhash
.ToString());
297 newOK
= !subgit
.Run(cmd
, &cmdout
, &cmderr
, encode
);
300 int pos
= cmdout
.Find(L
' ');
301 newTime
= _wtoi(cmdout
.Left(pos
));
302 newsub
= cmdout
.Mid(pos
+ 1);
308 if (oldhash
.IsEmpty())
311 changeType
= NewSubmodule
;
313 else if (newhash
.IsEmpty())
316 changeType
= DeleteSubmodule
;
318 else if (oldhash
!= newhash
)
320 bool ffNewer
= subgit
.IsFastForward(oldhash
, newhash
);
323 bool ffOlder
= subgit
.IsFastForward(newhash
, oldhash
);
326 if (newTime
> oldTime
)
327 changeType
= NewerTime
;
328 else if (newTime
< oldTime
)
329 changeType
= OlderTime
;
331 changeType
= SameTime
;
337 changeType
= FastForward
;
339 else if (oldhash
== newhash
)
340 changeType
= Identical
;
342 if (!oldOK
|| !newOK
)
343 changeType
= Unknown
;
346 int CGitDiff::Diff(HWND hWnd
, const CTGitPath
* pPath
, const CTGitPath
* pPath2
, CString rev1
, CString rev2
, bool /*blame*/, bool /*unified*/, int jumpToLine
, bool bAlternativeTool
, bool mustExist
)
348 // make sure we have HASHes here, otherwise filenames might be invalid
349 if (rev1
!= GIT_REV_ZERO
)
352 if (g_Git
.GetHash(rev1Hash
, rev1
))
354 MessageBox(hWnd
, g_Git
.GetGitLastErr(L
"Could not get hash of \"" + rev1
+ L
"\"."), L
"TortoiseGit", MB_ICONERROR
);
357 rev1
= rev1Hash
.ToString();
359 if (rev2
!= GIT_REV_ZERO
)
362 if (g_Git
.GetHash(rev2Hash
, rev2
))
364 MessageBox(hWnd
, g_Git
.GetGitLastErr(L
"Could not get hash of \"" + rev2
+ L
"\"."), L
"TortoiseGit", MB_ICONERROR
);
367 rev2
= rev2Hash
.ToString();
374 if(pPath
->IsDirectory() || pPath2
->IsDirectory())
377 // refresh if result = 1
378 CTGitPath path
= *pPath
;
379 CTGitPath path2
= *pPath2
;
380 while ((result
= SubmoduleDiff(hWnd
, &path
, &path2
, rev1
, rev2
)) == 1)
382 path
.SetFromGit(pPath
->GetGitPathString());
383 path2
.SetFromGit(pPath2
->GetGitPathString());
388 if(rev1
!= GIT_REV_ZERO
)
390 // use original file extension, an external diff tool might need it
391 file1
= CTempFiles::Instance().GetTempFilePath(false, *pPath
, rev1
).GetWinPathString();
392 title1
= pPath
->GetGitPathString() + L
": " + rev1
.Left(g_Git
.GetShortHASHLength());
393 auto ret
= g_Git
.GetOneFile(rev1
, *pPath
, file1
);
394 if (ret
&& !(!mustExist
&& ret
== GIT_ENOTFOUND
))
397 out
.Format(IDS_STATUSLIST_CHECKOUTFILEFAILED
, (LPCTSTR
)pPath
->GetGitPathString(), (LPCTSTR
)rev1
, (LPCTSTR
)file1
);
398 CMessageBox::Show(hWnd
, 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(hWnd
, 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 auto ret
= g_Git
.GetOneFile(rev2
, fileName
, file2
);
430 if (ret
&& !(!mustExist
&& ret
== GIT_ENOTFOUND
))
433 out
.Format(IDS_STATUSLIST_CHECKOUTFILEFAILED
, (LPCTSTR
)pPath2
->GetGitPathString(), (LPCTSTR
)rev2
, (LPCTSTR
)file2
);
434 CMessageBox::Show(hWnd
, g_Git
.GetGitLastErr(out
, CGit::GIT_CMD_GETONEFILE
), L
"TortoiseGit", MB_OK
);
437 ::SetFileAttributes(file2
, FILE_ATTRIBUTE_READONLY
);
441 file2
= g_Git
.CombinePath(pPath2
);
442 title2
.Format(IDS_DIFF_WCNAME
, (LPCTSTR
)pPath2
->GetGitPathString());
445 CAppUtils::DiffFlags flags
;
446 flags
.bAlternativeTool
= bAlternativeTool
;
447 CAppUtils::StartExtDiff(file2
,file1
,
450 g_Git
.CombinePath(pPath2
),
451 g_Git
.CombinePath(pPath
),
459 int CGitDiff::DiffCommit(HWND hWnd
, const CTGitPath
& path
, const GitRev
* r1
, const GitRev
* r2
, bool bAlternative
)
461 return DiffCommit(hWnd
, path
, path
, r1
, r2
, bAlternative
);
464 int CGitDiff::DiffCommit(HWND hWnd
, const CTGitPath
& path1
, const CTGitPath
& path2
, const GitRev
* r1
, const GitRev
* r2
, bool bAlternative
)
466 if (path1
.GetWinPathString().IsEmpty())
468 CFileDiffDlg
dlg(GetExplorerHWND() == hWnd
? nullptr : CWnd::FromHandle(hWnd
));
469 dlg
.SetDiff(nullptr, *r2
, *r1
);
472 else if (path1
.IsDirectory())
474 CFileDiffDlg
dlg(GetExplorerHWND() == hWnd
? nullptr : CWnd::FromHandle(hWnd
));
475 dlg
.SetDiff(&path1
, *r2
, *r1
);
479 Diff(hWnd
, &path1
, &path2
, r1
->m_CommitHash
.ToString(), r2
->m_CommitHash
.ToString(), false, false, 0, bAlternative
);
483 int CGitDiff::DiffCommit(HWND hWnd
, const CTGitPath
& path
, const CString
& r1
, const CString
& r2
, bool bAlternative
)
485 return DiffCommit(hWnd
, path
, path
, r1
, r2
, bAlternative
);
488 int CGitDiff::DiffCommit(HWND hWnd
, const CTGitPath
& path1
, const CTGitPath
& path2
, const CString
& r1
, const CString
& r2
, bool bAlternative
)
490 if (path1
.GetWinPathString().IsEmpty())
492 CFileDiffDlg
dlg(GetExplorerHWND() == hWnd
? nullptr : CWnd::FromHandle(hWnd
));
493 dlg
.SetDiff(nullptr, r2
, r1
);
496 else if (path1
.IsDirectory())
498 CFileDiffDlg
dlg(GetExplorerHWND() == hWnd
? nullptr : CWnd::FromHandle(hWnd
));
499 dlg
.SetDiff(&path1
, r2
, r1
);
503 Diff(hWnd
, &path1
, &path2
, r1
, r2
, false, false, 0, bAlternative
);