1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2003-2008 - TortoiseSVN
4 // Copyright (C) 2008-2017 - 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 CString
& rev1
)
36 if (rev1
!= GIT_REV_ZERO
)
37 cmd
.Format(L
"git.exe ls-tree \"%s\" -- \"%s\"", (LPCTSTR
)rev1
, (LPCTSTR
)pPath
->GetGitPathString());
39 cmd
.Format(L
"git.exe ls-files -s -- \"%s\"", (LPCTSTR
)pPath
->GetGitPathString());
42 if (g_Git
.Run(cmd
, &output
, &err
, CP_UTF8
))
44 CMessageBox::Show(nullptr, output
+ L
'\n' + err
, L
"TortoiseGit", MB_OK
| MB_ICONERROR
);
48 int start
= output
.Find(L
' ');
51 if (rev1
!= GIT_REV_ZERO
) // in ls-files the hash is in the second column; in ls-tree it's in the third one
52 start
= output
.Find(L
' ', start
+ 1);
54 newhash
=output
.Mid(start
+ 1, GIT_HASH_SIZE
* 2);
57 subgit
.m_CurrentDir
= g_Git
.CombinePath(pPath
);
58 int encode
=CAppUtils::GetLogOutputEncode(&subgit
);
60 cmd
.Format(L
"git.exe log -n1 --pretty=format:\"%%s\" %s --", (LPCTSTR
)newhash
);
61 bool toOK
= !subgit
.Run(cmd
,&newsub
,encode
);
64 if (rev1
== GIT_REV_ZERO
&& !(pPath
->m_Action
& CTGitPath::LOGACTIONS_DELETED
))
67 subgit
.Run(L
"git.exe status --porcelain", &dirtyList
, encode
);
68 dirty
= !dirtyList
.IsEmpty();
71 CSubmoduleDiffDlg submoduleDiffDlg
;
72 if (pPath
->m_Action
& CTGitPath::LOGACTIONS_DELETED
)
73 submoduleDiffDlg
.SetDiff(pPath
->GetWinPath(), false, newhash
, newsub
, toOK
, GIT_REV_ZERO
, L
"", false, dirty
, DeleteSubmodule
);
75 submoduleDiffDlg
.SetDiff(pPath
->GetWinPath(), false, GIT_REV_ZERO
, L
"", true, newhash
, newsub
, toOK
, dirty
, NewSubmodule
);
76 submoduleDiffDlg
.DoModal();
77 if (submoduleDiffDlg
.IsRefresh())
83 if (rev1
!= GIT_REV_ZERO
)
84 CMessageBox::Show(nullptr, L
"ls-tree output format error", L
"TortoiseGit", MB_OK
| MB_ICONERROR
);
86 CMessageBox::Show(nullptr, L
"ls-files output format error", L
"TortoiseGit", MB_OK
| MB_ICONERROR
);
90 int CGitDiff::DiffNull(const CTGitPath
* pPath
, CString rev1
, bool bIsAdd
, int jumpToLine
, bool bAlternative
)
92 if (rev1
!= GIT_REV_ZERO
)
95 if (g_Git
.GetHash(rev1Hash
, rev1
)) // make sure we have a HASH here, otherwise filenames might be invalid
97 MessageBox(nullptr, g_Git
.GetGitLastErr(L
"Could not get hash of \"" + rev1
+ L
"\"."), L
"TortoiseGit", MB_ICONERROR
);
100 rev1
= rev1Hash
.ToString();
106 if(pPath
->IsDirectory())
109 // refresh if result = 1
110 CTGitPath path
= *pPath
;
111 while ((result
= SubmoduleDiffNull(&path
, rev1
)) == 1)
112 path
.SetFromGit(pPath
->GetGitPathString());
116 if(rev1
!= GIT_REV_ZERO
)
118 file1
= CTempFiles::Instance().GetTempFilePath(false, *pPath
, rev1
).GetWinPathString();
119 if (g_Git
.GetOneFile(rev1
, *pPath
, file1
))
122 out
.Format(IDS_STATUSLIST_CHECKOUTFILEFAILED
, (LPCTSTR
)pPath
->GetGitPathString(), (LPCTSTR
)rev1
, (LPCTSTR
)file1
);
123 CMessageBox::Show(nullptr, g_Git
.GetGitLastErr(out
, CGit::GIT_CMD_GETONEFILE
), L
"TortoiseGit", MB_OK
);
126 ::SetFileAttributes(file1
, FILE_ATTRIBUTE_READONLY
);
129 file1
= g_Git
.CombinePath(pPath
);
131 CString tempfile
= CTempFiles::Instance().GetTempFilePath(false, *pPath
, rev1
).GetWinPathString();
132 ::SetFileAttributes(tempfile
, FILE_ATTRIBUTE_READONLY
);
134 CAppUtils::DiffFlags flags
;
135 flags
.bAlternativeTool
= bAlternative
;
137 CAppUtils::StartExtDiff(tempfile
,file1
,
138 pPath
->GetGitPathString(),
139 pPath
->GetGitPathString() + L
':' + rev1
.Left(g_Git
.GetShortHASHLength()),
140 g_Git
.CombinePath(pPath
), g_Git
.CombinePath(pPath
),
142 , flags
, jumpToLine
);
144 CAppUtils::StartExtDiff(file1
,tempfile
,
145 pPath
->GetGitPathString() + L
':' + rev1
.Left(g_Git
.GetShortHASHLength()),
146 pPath
->GetGitPathString(),
147 g_Git
.CombinePath(pPath
), g_Git
.CombinePath(pPath
),
149 , flags
, jumpToLine
);
154 int CGitDiff::SubmoduleDiff(const CTGitPath
* pPath
, const CTGitPath
* /*pPath2*/, const CString
& rev1
, const CString
& rev2
, bool /*blame*/, bool /*unified*/)
160 bool isWorkingCopy
= false;
161 if( rev2
== GIT_REV_ZERO
|| rev1
== GIT_REV_ZERO
)
163 oldhash
= GIT_REV_ZERO
;
164 newhash
= GIT_REV_ZERO
;
167 if( rev2
!= GIT_REV_ZERO
)
169 if( rev1
!= GIT_REV_ZERO
)
172 isWorkingCopy
= true;
174 cmd
.Format(L
"git.exe diff %s -- \"%s\"",
175 (LPCTSTR
)rev
, (LPCTSTR
)pPath
->GetGitPathString());
178 if (g_Git
.Run(cmd
, &output
, &err
, CP_UTF8
))
180 CMessageBox::Show(nullptr, output
+ L
'\n' + err
, L
"TortoiseGit", MB_OK
| MB_ICONERROR
);
184 if (output
.IsEmpty())
188 // also compare against index
189 cmd
.Format(L
"git.exe diff -- \"%s\"", (LPCTSTR
)pPath
->GetGitPathString());
190 if (g_Git
.Run(cmd
, &output
, &err
, CP_UTF8
))
192 CMessageBox::Show(nullptr, output
+ L
'\n' + err
, L
"TortoiseGit", MB_OK
| MB_ICONERROR
);
196 if (output
.IsEmpty())
198 CMessageBox::Show(nullptr, IDS_ERR_EMPTYDIFF
, IDS_APPNAME
, MB_OK
| MB_ICONERROR
);
201 else if (CMessageBox::Show(nullptr, IDS_SUBMODULE_EMPTYDIFF
, IDS_APPNAME
, 1, IDI_QUESTION
, IDS_MSGBOX_YES
, IDS_MSGBOX_NO
) == 1)
204 sCmd
.Format(L
"/command:subupdate /bkpath:\"%s\"", (LPCTSTR
)g_Git
.m_CurrentDir
);
205 CAppUtils::RunTortoiseGitProc(sCmd
);
211 int oldstart
= output
.Find(L
"-Subproject commit", start
);
214 CMessageBox::Show(nullptr, L
"Subproject Diff Format error", L
"TortoiseGit", MB_OK
| MB_ICONERROR
);
217 oldhash
= output
.Mid(oldstart
+ (int)wcslen(L
"-Subproject commit") + 1, GIT_HASH_SIZE
* 2);
219 int newstart
= output
.Find(L
"+Subproject commit",start
);
222 CMessageBox::Show(nullptr, L
"Subproject Diff Format error", L
"TortoiseGit", MB_OK
| MB_ICONERROR
);
225 newhash
= output
.Mid(newstart
+ (int)wcslen(L
"+Subproject commit") + 1, GIT_HASH_SIZE
* 2);
226 dirty
= output
.Mid(newstart
+ (int)wcslen(L
"+Subproject commit") + GIT_HASH_SIZE
* 2 + 1) == L
"-dirty\n";
230 cmd
.Format(L
"git.exe diff-tree -r -z %s %s -- \"%s\"",
231 (LPCTSTR
)rev2
, (LPCTSTR
)rev1
, (LPCTSTR
)pPath
->GetGitPathString());
233 BYTE_VECTOR bytes
, errBytes
;
234 if(g_Git
.Run(cmd
, &bytes
, &errBytes
))
237 CGit::StringAppend(&err
, &errBytes
[0], CP_UTF8
);
238 CMessageBox::Show(nullptr, err
, L
"TortoiseGit", MB_OK
| MB_ICONERROR
);
242 if (bytes
.size() < 15 + 2 * GIT_HASH_SIZE
+ 1 + 2 * GIT_HASH_SIZE
)
244 CMessageBox::Show(nullptr, L
"git diff-tree gives invalid output", L
"TortoiseGit", MB_OK
| MB_ICONERROR
);
247 CGit::StringAppend(&oldhash
, &bytes
[15], CP_UTF8
, 2 * GIT_HASH_SIZE
);
248 CGit::StringAppend(&newhash
, &bytes
[15 + 2 * GIT_HASH_SIZE
+ 1], CP_UTF8
, 2 * GIT_HASH_SIZE
);
254 bool oldOK
= false, newOK
= false;
257 subgit
.m_CurrentDir
= g_Git
.CombinePath(pPath
);
258 ChangeType changeType
= Unknown
;
260 if (pPath
->HasAdminDir())
261 GetSubmoduleChangeType(subgit
, oldhash
, newhash
, oldOK
, newOK
, changeType
, oldsub
, newsub
);
263 CSubmoduleDiffDlg submoduleDiffDlg
;
264 submoduleDiffDlg
.SetDiff(pPath
->GetWinPath(), isWorkingCopy
, oldhash
, oldsub
, oldOK
, newhash
, newsub
, newOK
, dirty
, changeType
);
265 submoduleDiffDlg
.DoModal();
266 if (submoduleDiffDlg
.IsRefresh())
272 void CGitDiff::GetSubmoduleChangeType(CGit
& subgit
, const CGitHash
& oldhash
, const CGitHash
& newhash
, bool& oldOK
, bool& newOK
, ChangeType
& changeType
, CString
& oldsub
, CString
& newsub
)
275 int encode
= CAppUtils::GetLogOutputEncode(&subgit
);
276 int oldTime
= 0, newTime
= 0;
278 if (!oldhash
.IsEmpty())
280 CString cmdout
, cmderr
;
281 cmd
.Format(L
"git.exe log -n1 --pretty=format:\"%%ct %%s\" %s --", (LPCTSTR
)oldhash
.ToString());
282 oldOK
= !subgit
.Run(cmd
, &cmdout
, &cmderr
, encode
);
285 int pos
= cmdout
.Find(L
' ');
286 oldTime
= _wtoi(cmdout
.Left(pos
));
287 oldsub
= cmdout
.Mid(pos
+ 1);
292 if (!newhash
.IsEmpty())
294 CString cmdout
, cmderr
;
295 cmd
.Format(L
"git.exe log -n1 --pretty=format:\"%%ct %%s\" %s --", (LPCTSTR
)newhash
.ToString());
296 newOK
= !subgit
.Run(cmd
, &cmdout
, &cmderr
, encode
);
299 int pos
= cmdout
.Find(L
' ');
300 newTime
= _wtoi(cmdout
.Left(pos
));
301 newsub
= cmdout
.Mid(pos
+ 1);
307 if (oldhash
.IsEmpty())
310 changeType
= NewSubmodule
;
312 else if (newhash
.IsEmpty())
315 changeType
= DeleteSubmodule
;
317 else if (oldhash
!= newhash
)
319 bool ffNewer
= subgit
.IsFastForward(oldhash
, newhash
);
322 bool ffOlder
= subgit
.IsFastForward(newhash
, oldhash
);
325 if (newTime
> oldTime
)
326 changeType
= NewerTime
;
327 else if (newTime
< oldTime
)
328 changeType
= OlderTime
;
330 changeType
= SameTime
;
336 changeType
= FastForward
;
338 else if (oldhash
== newhash
)
339 changeType
= Identical
;
341 if (!oldOK
|| !newOK
)
342 changeType
= Unknown
;
345 int CGitDiff::Diff(const CTGitPath
* pPath
, const CTGitPath
* pPath2
, CString rev1
, CString rev2
, bool /*blame*/, bool /*unified*/, int jumpToLine
, bool bAlternativeTool
, bool mustExist
)
347 // make sure we have HASHes here, otherwise filenames might be invalid
348 if (rev1
!= GIT_REV_ZERO
)
351 if (g_Git
.GetHash(rev1Hash
, rev1
))
353 MessageBox(nullptr, g_Git
.GetGitLastErr(L
"Could not get hash of \"" + rev1
+ L
"\"."), L
"TortoiseGit", MB_ICONERROR
);
356 rev1
= rev1Hash
.ToString();
358 if (rev2
!= GIT_REV_ZERO
)
361 if (g_Git
.GetHash(rev2Hash
, rev2
))
363 MessageBox(nullptr, g_Git
.GetGitLastErr(L
"Could not get hash of \"" + rev2
+ L
"\"."), L
"TortoiseGit", MB_ICONERROR
);
366 rev2
= rev2Hash
.ToString();
373 if(pPath
->IsDirectory() || pPath2
->IsDirectory())
376 // refresh if result = 1
377 CTGitPath path
= *pPath
;
378 CTGitPath path2
= *pPath2
;
379 while ((result
= SubmoduleDiff(&path
, &path2
, rev1
, rev2
)) == 1)
381 path
.SetFromGit(pPath
->GetGitPathString());
382 path2
.SetFromGit(pPath2
->GetGitPathString());
387 if(rev1
!= GIT_REV_ZERO
)
389 // use original file extension, an external diff tool might need it
390 file1
= CTempFiles::Instance().GetTempFilePath(false, *pPath
, rev1
).GetWinPathString();
391 title1
= pPath
->GetGitPathString() + L
": " + rev1
.Left(g_Git
.GetShortHASHLength());
392 auto ret
= g_Git
.GetOneFile(rev1
, *pPath
, file1
);
393 if (ret
&& !(!mustExist
&& ret
== GIT_ENOTFOUND
))
396 out
.Format(IDS_STATUSLIST_CHECKOUTFILEFAILED
, (LPCTSTR
)pPath
->GetGitPathString(), (LPCTSTR
)rev1
, (LPCTSTR
)file1
);
397 CMessageBox::Show(nullptr, g_Git
.GetGitLastErr(out
, CGit::GIT_CMD_GETONEFILE
), L
"TortoiseGit", MB_OK
);
400 ::SetFileAttributes(file1
, FILE_ATTRIBUTE_READONLY
);
404 file1
= g_Git
.CombinePath(pPath
);
405 title1
.Format(IDS_DIFF_WCNAME
, (LPCTSTR
)pPath
->GetGitPathString());
406 if (!PathFileExists(file1
))
409 sMsg
.Format(IDS_PROC_DIFFERROR_FILENOTINWORKINGTREE
, (LPCTSTR
)file1
);
410 if (MessageBox(nullptr, sMsg
, L
"TortoiseGit", MB_ICONEXCLAMATION
| MB_YESNO
) != IDYES
)
412 if (!CCommonAppUtils::FileOpenSave(file1
, nullptr, IDS_SELECTFILE
, IDS_COMMONFILEFILTER
, true))
420 if(rev2
!= GIT_REV_ZERO
)
422 CTGitPath fileName
= *pPath2
;
423 if (pPath2
->m_Action
& CTGitPath::LOGACTIONS_REPLACED
)
424 fileName
= CTGitPath(pPath2
->GetGitOldPathString());
426 file2
= CTempFiles::Instance().GetTempFilePath(false, fileName
, rev2
).GetWinPathString();
427 title2
= fileName
.GetGitPathString() + L
": " + rev2
.Left(g_Git
.GetShortHASHLength());
428 auto ret
= g_Git
.GetOneFile(rev2
, fileName
, file2
);
429 if (ret
&& !(!mustExist
&& ret
== GIT_ENOTFOUND
))
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
, (LPCTSTR
)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
);