Fixed issue #3028: Error when using TortoiseGit cli diff command with new added file...
[TortoiseGit.git] / src / TortoiseProc / GitDiff.cpp
blob110f40e2a00da2ce2f2e3b8d7a2076601d356122
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.
20 #include "stdafx.h"
21 #include "GitDiff.h"
22 #include "AppUtils.h"
23 #include "gittype.h"
24 #include "resource.h"
25 #include "MessageBox.h"
26 #include "FileDiffDlg.h"
27 #include "SubmoduleDiffDlg.h"
28 #include "TempFile.h"
30 int CGitDiff::SubmoduleDiffNull(const CTGitPath* pPath, const CString& rev1)
32 CString newsub;
33 CString newhash;
35 CString cmd;
36 if (rev1 != GIT_REV_ZERO)
37 cmd.Format(L"git.exe ls-tree \"%s\" -- \"%s\"", (LPCTSTR)rev1, (LPCTSTR)pPath->GetGitPathString());
38 else
39 cmd.Format(L"git.exe ls-files -s -- \"%s\"", (LPCTSTR)pPath->GetGitPathString());
41 CString output, err;
42 if (g_Git.Run(cmd, &output, &err, CP_UTF8))
44 CMessageBox::Show(nullptr, output + L'\n' + err, L"TortoiseGit", MB_OK | MB_ICONERROR);
45 return -1;
48 int start = output.Find(L' ');
49 if(start>0)
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);
53 if(start>0)
54 newhash=output.Mid(start + 1, GIT_HASH_SIZE * 2);
56 CGit subgit;
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);
63 bool dirty = false;
64 if (rev1 == GIT_REV_ZERO && !(pPath->m_Action & CTGitPath::LOGACTIONS_DELETED))
66 CString dirtyList;
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);
74 else
75 submoduleDiffDlg.SetDiff(pPath->GetWinPath(), false, GIT_REV_ZERO, L"", true, newhash, newsub, toOK, dirty, NewSubmodule);
76 submoduleDiffDlg.DoModal();
77 if (submoduleDiffDlg.IsRefresh())
78 return 1;
80 return 0;
83 if (rev1 != GIT_REV_ZERO)
84 CMessageBox::Show(nullptr, L"ls-tree output format error", L"TortoiseGit", MB_OK | MB_ICONERROR);
85 else
86 CMessageBox::Show(nullptr, L"ls-files output format error", L"TortoiseGit", MB_OK | MB_ICONERROR);
87 return -1;
90 int CGitDiff::DiffNull(const CTGitPath* pPath, CString rev1, bool bIsAdd, int jumpToLine, bool bAlternative)
92 if (rev1 != GIT_REV_ZERO)
94 CGitHash rev1Hash;
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);
98 return -1;
100 rev1 = rev1Hash.ToString();
102 CString file1;
103 CString nullfile;
104 CString cmd;
106 if(pPath->IsDirectory())
108 int result;
109 // refresh if result = 1
110 CTGitPath path = *pPath;
111 while ((result = SubmoduleDiffNull(&path, rev1)) == 1)
112 path.SetFromGit(pPath->GetGitPathString());
113 return result;
116 if(rev1 != GIT_REV_ZERO )
118 file1 = CTempFiles::Instance().GetTempFilePath(false, *pPath, rev1).GetWinPathString();
119 if (g_Git.GetOneFile(rev1, *pPath, file1))
121 CString out;
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);
124 return -1;
126 ::SetFileAttributes(file1, FILE_ATTRIBUTE_READONLY);
128 else
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;
136 if(bIsAdd)
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),
141 GIT_REV_ZERO, rev1
142 , flags, jumpToLine);
143 else
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),
148 rev1, GIT_REV_ZERO
149 , flags, jumpToLine);
151 return 0;
154 int CGitDiff::SubmoduleDiff(const CTGitPath* pPath, const CTGitPath* /*pPath2*/, const CString& rev1, const CString& rev2, bool /*blame*/, bool /*unified*/)
156 CString oldhash;
157 CString newhash;
158 bool dirty = false;
159 CString cmd;
160 bool isWorkingCopy = false;
161 if( rev2 == GIT_REV_ZERO || rev1 == GIT_REV_ZERO )
163 oldhash = GIT_REV_ZERO;
164 newhash = GIT_REV_ZERO;
166 CString rev;
167 if( rev2 != GIT_REV_ZERO )
168 rev = rev2;
169 if( rev1 != GIT_REV_ZERO )
170 rev = rev1;
172 isWorkingCopy = true;
174 cmd.Format(L"git.exe diff %s -- \"%s\"",
175 (LPCTSTR)rev, (LPCTSTR)pPath->GetGitPathString());
177 CString output, err;
178 if (g_Git.Run(cmd, &output, &err, CP_UTF8))
180 CMessageBox::Show(nullptr, output + L'\n' + err, L"TortoiseGit", MB_OK | MB_ICONERROR);
181 return -1;
184 if (output.IsEmpty())
186 output.Empty();
187 err.Empty();
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);
193 return -1;
196 if (output.IsEmpty())
198 CMessageBox::Show(nullptr, IDS_ERR_EMPTYDIFF, IDS_APPNAME, MB_OK | MB_ICONERROR);
199 return -1;
201 else if (CMessageBox::Show(nullptr, IDS_SUBMODULE_EMPTYDIFF, IDS_APPNAME, 1, IDI_QUESTION, IDS_MSGBOX_YES, IDS_MSGBOX_NO) == 1)
203 CString sCmd;
204 sCmd.Format(L"/command:subupdate /bkpath:\"%s\"", (LPCTSTR)g_Git.m_CurrentDir);
205 CAppUtils::RunTortoiseGitProc(sCmd);
207 return -1;
210 int start =0;
211 int oldstart = output.Find(L"-Subproject commit", start);
212 if(oldstart<0)
214 CMessageBox::Show(nullptr, L"Subproject Diff Format error", L"TortoiseGit", MB_OK | MB_ICONERROR);
215 return -1;
217 oldhash = output.Mid(oldstart + (int)wcslen(L"-Subproject commit") + 1, GIT_HASH_SIZE * 2);
218 start = 0;
219 int newstart = output.Find(L"+Subproject commit",start);
220 if (newstart < 0)
222 CMessageBox::Show(nullptr, L"Subproject Diff Format error", L"TortoiseGit", MB_OK | MB_ICONERROR);
223 return -1;
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";
228 else
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))
236 CString err;
237 CGit::StringAppend(&err, &errBytes[0], CP_UTF8);
238 CMessageBox::Show(nullptr, err, L"TortoiseGit", MB_OK | MB_ICONERROR);
239 return -1;
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);
245 return -1;
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);
252 CString oldsub;
253 CString newsub;
254 bool oldOK = false, newOK = false;
256 CGit subgit;
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())
267 return 1;
269 return 0;
272 void CGitDiff::GetSubmoduleChangeType(CGit& subgit, const CGitHash& oldhash, const CGitHash& newhash, bool& oldOK, bool& newOK, ChangeType& changeType, CString& oldsub, CString& newsub)
274 CString cmd;
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);
283 if (oldOK)
285 int pos = cmdout.Find(L' ');
286 oldTime = _wtoi(cmdout.Left(pos));
287 oldsub = cmdout.Mid(pos + 1);
289 else
290 oldsub = cmderr;
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);
297 if (newOK)
299 int pos = cmdout.Find(L' ');
300 newTime = _wtoi(cmdout.Left(pos));
301 newsub = cmdout.Mid(pos + 1);
303 else
304 newsub = cmderr;
307 if (oldhash.IsEmpty())
309 oldOK = true;
310 changeType = NewSubmodule;
312 else if (newhash.IsEmpty())
314 newOK = true;
315 changeType = DeleteSubmodule;
317 else if (oldhash != newhash)
319 bool ffNewer = subgit.IsFastForward(oldhash, newhash);
320 if (!ffNewer)
322 bool ffOlder = subgit.IsFastForward(newhash, oldhash);
323 if (!ffOlder)
325 if (newTime > oldTime)
326 changeType = NewerTime;
327 else if (newTime < oldTime)
328 changeType = OlderTime;
329 else
330 changeType = SameTime;
332 else
333 changeType = Rewind;
335 else
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)
350 CGitHash rev1Hash;
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);
354 return -1;
356 rev1 = rev1Hash.ToString();
358 if (rev2 != GIT_REV_ZERO)
360 CGitHash rev2Hash;
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);
364 return -1;
366 rev2 = rev2Hash.ToString();
369 CString file1;
370 CString title1;
371 CString cmd;
373 if(pPath->IsDirectory() || pPath2->IsDirectory())
375 int result;
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());
384 return result;
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))
395 CString out;
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);
398 return -1;
400 ::SetFileAttributes(file1, FILE_ATTRIBUTE_READONLY);
402 else
404 file1 = g_Git.CombinePath(pPath);
405 title1.Format(IDS_DIFF_WCNAME, (LPCTSTR)pPath->GetGitPathString());
406 if (!PathFileExists(file1))
408 CString sMsg;
409 sMsg.Format(IDS_PROC_DIFFERROR_FILENOTINWORKINGTREE, (LPCTSTR)file1);
410 if (MessageBox(nullptr, sMsg, L"TortoiseGit", MB_ICONEXCLAMATION | MB_YESNO) != IDYES)
411 return 1;
412 if (!CCommonAppUtils::FileOpenSave(file1, nullptr, IDS_SELECTFILE, IDS_COMMONFILEFILTER, true))
413 return 1;
414 title1 = file1;
418 CString file2;
419 CString title2;
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))
431 CString out;
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);
434 return -1;
436 ::SetFileAttributes(file2, FILE_ATTRIBUTE_READONLY);
438 else
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,
447 title2,
448 title1,
449 g_Git.CombinePath(pPath2),
450 g_Git.CombinePath(pPath),
451 rev2,
452 rev1,
453 flags, jumpToLine);
455 return 0;
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())
467 CFileDiffDlg dlg;
468 dlg.SetDiff(nullptr, *r2, *r1);
469 dlg.DoModal();
471 else if (path1.IsDirectory())
473 CFileDiffDlg dlg;
474 dlg.SetDiff(&path1, *r2, *r1);
475 dlg.DoModal();
477 else
478 Diff(&path1, &path2, r1->m_CommitHash.ToString(), r2->m_CommitHash.ToString(), false, false, 0, bAlternative);
479 return 0;
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())
491 CFileDiffDlg dlg;
492 dlg.SetDiff(nullptr, r2, r1);
493 dlg.DoModal();
495 else if (path1.IsDirectory())
497 CFileDiffDlg dlg;
498 dlg.SetDiff(&path1, r2, r1);
499 dlg.DoModal();
501 else
502 Diff(&path1, &path2, r1, r2, false, false, 0, bAlternative);
503 return 0;