Refactor rebase commit list: Don't fill list again and again when reordering commits
[TortoiseGit.git] / src / TortoiseProc / GitDiff.cpp
blobf7617d8992ea986f9c647a28c389af09994f3554
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.
20 #include "stdafx.h"
21 #include "GitDiff.h"
22 #include "TortoiseProc.h"
23 #include "AppUtils.h"
24 #include "gittype.h"
25 #include "resource.h"
26 #include "MessageBox.h"
27 #include "FileDiffDlg.h"
28 #include "SubmoduleDiffDlg.h"
29 #include "TempFile.h"
31 int CGitDiff::SubmoduleDiffNull(HWND hWnd, const CTGitPath* pPath, const CString& rev1)
33 CString newsub;
34 CString newhash;
36 CString cmd;
37 if (rev1 != GIT_REV_ZERO)
38 cmd.Format(L"git.exe ls-tree \"%s\" -- \"%s\"", (LPCTSTR)rev1, (LPCTSTR)pPath->GetGitPathString());
39 else
40 cmd.Format(L"git.exe ls-files -s -- \"%s\"", (LPCTSTR)pPath->GetGitPathString());
42 CString output, err;
43 if (g_Git.Run(cmd, &output, &err, CP_UTF8))
45 CMessageBox::Show(hWnd, output + L'\n' + err, L"TortoiseGit", MB_OK | MB_ICONERROR);
46 return -1;
49 int start = output.Find(L' ');
50 if(start>0)
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);
54 if(start>0)
55 newhash=output.Mid(start + 1, GIT_HASH_SIZE * 2);
57 CGit subgit;
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);
64 bool dirty = false;
65 if (rev1 == GIT_REV_ZERO && !(pPath->m_Action & CTGitPath::LOGACTIONS_DELETED))
67 CString dirtyList;
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);
75 else
76 submoduleDiffDlg.SetDiff(pPath->GetWinPath(), false, GIT_REV_ZERO, L"", true, newhash, newsub, toOK, dirty, NewSubmodule);
77 submoduleDiffDlg.DoModal();
78 if (submoduleDiffDlg.IsRefresh())
79 return 1;
81 return 0;
84 if (rev1 != GIT_REV_ZERO)
85 CMessageBox::Show(hWnd, L"ls-tree output format error", L"TortoiseGit", MB_OK | MB_ICONERROR);
86 else
87 CMessageBox::Show(hWnd, L"ls-files output format error", L"TortoiseGit", MB_OK | MB_ICONERROR);
88 return -1;
91 int CGitDiff::DiffNull(HWND hWnd, const CTGitPath* pPath, CString rev1, bool bIsAdd, int jumpToLine, bool bAlternative)
93 if (rev1 != GIT_REV_ZERO)
95 CGitHash rev1Hash;
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);
99 return -1;
101 rev1 = rev1Hash.ToString();
103 CString file1;
104 CString nullfile;
105 CString cmd;
107 if(pPath->IsDirectory())
109 int result;
110 // refresh if result = 1
111 CTGitPath path = *pPath;
112 while ((result = SubmoduleDiffNull(hWnd, &path, rev1)) == 1)
113 path.SetFromGit(pPath->GetGitPathString());
114 return result;
117 if(rev1 != GIT_REV_ZERO )
119 file1 = CTempFiles::Instance().GetTempFilePath(false, *pPath, rev1).GetWinPathString();
120 if (g_Git.GetOneFile(rev1, *pPath, file1))
122 CString out;
123 out.FormatMessage(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);
125 return -1;
127 ::SetFileAttributes(file1, FILE_ATTRIBUTE_READONLY);
129 else
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;
137 if(bIsAdd)
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),
142 GIT_REV_ZERO, rev1
143 , flags, jumpToLine);
144 else
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),
149 rev1, GIT_REV_ZERO
150 , flags, jumpToLine);
152 return 0;
155 int CGitDiff::SubmoduleDiff(HWND hWnd, const CTGitPath* pPath, const CTGitPath* /*pPath2*/, const CString& rev1, const CString& rev2, bool /*blame*/, bool /*unified*/)
157 CString oldhash;
158 CString newhash;
159 bool dirty = false;
160 CString cmd;
161 bool isWorkingCopy = false;
162 if( rev2 == GIT_REV_ZERO || rev1 == GIT_REV_ZERO )
164 oldhash = GIT_REV_ZERO;
165 newhash = GIT_REV_ZERO;
167 CString rev;
168 if( rev2 != GIT_REV_ZERO )
169 rev = rev2;
170 if( rev1 != GIT_REV_ZERO )
171 rev = rev1;
173 isWorkingCopy = true;
175 cmd.Format(L"git.exe diff --submodule=short %s -- \"%s\"",
176 (LPCTSTR)rev, (LPCTSTR)pPath->GetGitPathString());
178 CString output, err;
179 if (g_Git.Run(cmd, &output, &err, CP_UTF8))
181 CMessageBox::Show(hWnd, output + L'\n' + err, L"TortoiseGit", MB_OK | MB_ICONERROR);
182 return -1;
185 if (output.IsEmpty())
187 output.Empty();
188 err.Empty();
189 // also compare against index
190 cmd.Format(L"git.exe diff --submodule=short -- \"%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);
194 return -1;
197 if (output.IsEmpty())
199 CMessageBox::Show(hWnd, IDS_ERR_EMPTYDIFF, IDS_APPNAME, MB_OK | MB_ICONERROR);
200 return -1;
202 else if (CMessageBox::Show(hWnd, IDS_SUBMODULE_EMPTYDIFF, IDS_APPNAME, 1, IDI_QUESTION, IDS_MSGBOX_YES, IDS_MSGBOX_NO) == 1)
204 CString sCmd;
205 sCmd.Format(L"/command:subupdate /bkpath:\"%s\"", (LPCTSTR)g_Git.m_CurrentDir);
206 CAppUtils::RunTortoiseGitProc(sCmd);
208 return -1;
211 int start =0;
212 int oldstart = output.Find(L"-Subproject commit", start);
213 if(oldstart<0)
215 CMessageBox::Show(hWnd, L"Subproject Diff Format error", L"TortoiseGit", MB_OK | MB_ICONERROR);
216 return -1;
218 oldhash = output.Mid(oldstart + (int)wcslen(L"-Subproject commit") + 1, GIT_HASH_SIZE * 2);
219 start = 0;
220 int newstart = output.Find(L"+Subproject commit",start);
221 if (newstart < 0)
223 CMessageBox::Show(hWnd, L"Subproject Diff Format error", L"TortoiseGit", MB_OK | MB_ICONERROR);
224 return -1;
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";
229 else
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))
237 CString err;
238 CGit::StringAppend(&err, &errBytes[0], CP_UTF8);
239 CMessageBox::Show(hWnd, err, L"TortoiseGit", MB_OK | MB_ICONERROR);
240 return -1;
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);
246 return -1;
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);
253 CString oldsub;
254 CString newsub;
255 bool oldOK = false, newOK = false;
257 CGit subgit;
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())
268 return 1;
270 return 0;
273 void CGitDiff::GetSubmoduleChangeType(CGit& subgit, const CGitHash& oldhash, const CGitHash& newhash, bool& oldOK, bool& newOK, ChangeType& changeType, CString& oldsub, CString& newsub)
275 CString cmd;
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);
284 if (oldOK)
286 int pos = cmdout.Find(L' ');
287 oldTime = _wtoi(cmdout.Left(pos));
288 oldsub = cmdout.Mid(pos + 1);
290 else
291 oldsub = cmderr;
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);
298 if (newOK)
300 int pos = cmdout.Find(L' ');
301 newTime = _wtoi(cmdout.Left(pos));
302 newsub = cmdout.Mid(pos + 1);
304 else
305 newsub = cmderr;
308 if (oldhash.IsEmpty())
310 oldOK = true;
311 changeType = NewSubmodule;
313 else if (newhash.IsEmpty())
315 newOK = true;
316 changeType = DeleteSubmodule;
318 else if (oldhash != newhash)
320 bool ffNewer = subgit.IsFastForward(oldhash.ToString(), newhash.ToString());
321 if (!ffNewer)
323 bool ffOlder = subgit.IsFastForward(newhash.ToString(), oldhash.ToString());
324 if (!ffOlder)
326 if (newTime > oldTime)
327 changeType = NewerTime;
328 else if (newTime < oldTime)
329 changeType = OlderTime;
330 else
331 changeType = SameTime;
333 else
334 changeType = Rewind;
336 else
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)
351 CGitHash rev1Hash;
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);
355 return -1;
357 rev1 = rev1Hash.ToString();
359 if (rev2 != GIT_REV_ZERO)
361 CGitHash rev2Hash;
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);
365 return -1;
367 rev2 = rev2Hash.ToString();
370 CString file1;
371 CString title1;
372 CString cmd;
374 if(pPath->IsDirectory() || pPath2->IsDirectory())
376 int result;
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());
385 return result;
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))
396 CString out;
397 out.FormatMessage(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);
399 return -1;
401 ::SetFileAttributes(file1, FILE_ATTRIBUTE_READONLY);
403 else
405 file1 = g_Git.CombinePath(pPath);
406 title1.Format(IDS_DIFF_WCNAME, (LPCTSTR)pPath->GetGitPathString());
407 if (!PathFileExists(file1))
409 CString sMsg;
410 sMsg.Format(IDS_PROC_DIFFERROR_FILENOTINWORKINGTREE, (LPCTSTR)file1);
411 if (MessageBox(hWnd, sMsg, L"TortoiseGit", MB_ICONEXCLAMATION | MB_YESNO) != IDYES)
412 return 1;
413 if (!CCommonAppUtils::FileOpenSave(file1, nullptr, IDS_SELECTFILE, IDS_COMMONFILEFILTER, true))
414 return 1;
415 title1 = file1;
419 CString file2;
420 CString title2;
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))
432 CString out;
433 out.FormatMessage(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);
435 return -1;
437 ::SetFileAttributes(file2, FILE_ATTRIBUTE_READONLY);
439 else
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,
448 title2,
449 title1,
450 g_Git.CombinePath(pPath2),
451 g_Git.CombinePath(pPath),
452 rev2,
453 rev1,
454 flags, jumpToLine);
456 return 0;
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);
470 dlg.DoModal();
472 else if (path1.IsDirectory())
474 CFileDiffDlg dlg(GetExplorerHWND() == hWnd ? nullptr : CWnd::FromHandle(hWnd));
475 dlg.SetDiff(&path1, *r2, *r1);
476 dlg.DoModal();
478 else
479 Diff(hWnd, &path1, &path2, r1->m_CommitHash.ToString(), r2->m_CommitHash.ToString(), false, false, 0, bAlternative);
480 return 0;
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);
494 dlg.DoModal();
496 else if (path1.IsDirectory())
498 CFileDiffDlg dlg(GetExplorerHWND() == hWnd ? nullptr : CWnd::FromHandle(hWnd));
499 dlg.SetDiff(&path1, r2, r1);
500 dlg.DoModal();
502 else
503 Diff(hWnd, &path1, &path2, r1, r2, false, false, 0, bAlternative);
504 return 0;