Merge branch 'libgit2-and-better-errorhandling'
[TortoiseGit.git] / src / TortoiseProc / GitDiff.cpp
blob266cc7012bb94e2d57e06fa606b081d3971179e8
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2003-2008 - TortoiseSVN
4 // Copyright (C) 2008-2013 - 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.
19 #include "stdafx.h"
20 #include "GitDiff.h"
21 #include "AppUtils.h"
22 #include "git.h"
23 #include "gittype.h"
24 #include "resource.h"
25 #include "MessageBox.h"
26 #include "FileDiffDlg.h"
27 #include "SubmoduleDiffDlg.h"
29 CGitDiff::CGitDiff(void)
33 CGitDiff::~CGitDiff(void)
36 int CGitDiff::SubmoduleDiffNull(CTGitPath *pPath, git_revnum_t &rev1)
38 CString oldhash = GIT_REV_ZERO;
39 CString oldsub ;
40 CString newsub;
41 CString newhash;
43 CString cmd;
44 if (rev1 != GIT_REV_ZERO)
45 cmd.Format(_T("git.exe ls-tree \"%s\" -- \"%s\""), rev1, pPath->GetGitPathString());
46 else
47 cmd.Format(_T("git.exe ls-files -s -- \"%s\""), pPath->GetGitPathString());
49 CString output, err;
50 if (g_Git.Run(cmd, &output, &err, CP_UTF8))
52 CMessageBox::Show(NULL, output + L"\n" + err, _T("TortoiseGit"), MB_OK|MB_ICONERROR);
53 return -1;
56 int start=0;
57 start=output.Find(_T(' '),start);
58 if(start>0)
60 if (rev1 != GIT_REV_ZERO) // in ls-files the hash is in the second column; in ls-tree it's in the third one
61 start = output.Find(_T(' '), start + 1);
62 if(start>0)
63 newhash=output.Mid(start+1, 40);
65 CGit subgit;
66 subgit.m_CurrentDir=g_Git.m_CurrentDir+_T("\\")+pPath->GetWinPathString();
67 int encode=CAppUtils::GetLogOutputEncode(&subgit);
69 cmd.Format(_T("git.exe log -n1 --pretty=format:\"%%s\" %s"),newhash);
70 bool toOK = !subgit.Run(cmd,&newsub,encode);
72 bool dirty = false;
73 if (rev1 == GIT_REV_ZERO)
75 CString dirtyList;
76 subgit.Run(_T("git.exe status --porcelain"), &dirtyList, encode);
77 dirty = !dirtyList.IsEmpty();
80 CSubmoduleDiffDlg submoduleDiffDlg;
81 submoduleDiffDlg.SetDiff(pPath->GetWinPath(), false, oldhash, oldsub, true, newhash, newsub, toOK, dirty, CSubmoduleDiffDlg::NewSubmodule);
82 submoduleDiffDlg.DoModal();
84 return 0;
87 if (rev1 != GIT_REV_ZERO)
88 CMessageBox::Show(NULL, _T("ls-tree output format error"), _T("TortoiseGit"), MB_OK | MB_ICONERROR);
89 else
90 CMessageBox::Show(NULL, _T("ls-files output format error"), _T("TortoiseGit"), MB_OK | MB_ICONERROR);
91 return -1;
94 int CGitDiff::DiffNull(CTGitPath *pPath, git_revnum_t rev1,bool bIsAdd)
96 CString temppath;
97 GetTempPath(temppath);
98 if (rev1 != GIT_REV_ZERO)
100 CGitHash rev1Hash;
101 if (g_Git.GetHash(rev1Hash, rev1)) // make sure we have a HASH here, otherwise filenames might be invalid
103 MessageBox(NULL, g_Git.GetGitLastErr(_T("Could not get hash of \"") + rev1 + _T("\".")), _T("TortoiseGit"), MB_ICONERROR);
104 return -1;
106 rev1 = rev1Hash.ToString();
108 CString file1;
109 CString nullfile;
110 CString cmd;
112 if(pPath->IsDirectory())
114 git_revnum_t rev2;
115 return SubmoduleDiffNull(pPath,rev1);
118 if(rev1 != GIT_REV_ZERO )
120 TCHAR szTempName[MAX_PATH];
121 GetTempFileName(temppath, pPath->GetBaseFilename(), 0, szTempName);
122 CString temp(szTempName);
123 DeleteFile(szTempName);
124 CreateDirectory(szTempName, NULL);
125 file1.Format(_T("%s\\%s-%s%s"),
126 temp,
127 pPath->GetBaseFilename(),
128 rev1.Left(g_Git.GetShortHASHLength()),
129 pPath->GetFileExtension());
131 g_Git.GetOneFile(rev1,*pPath,file1);
133 else
135 file1=g_Git.m_CurrentDir+_T("\\")+pPath->GetWinPathString();
138 // preserve FileExtension, needed especially for diffing deleted images (detection on new filename extension)
139 CString tempfile=::GetTempFile() + pPath->GetFileExtension();
140 CStdioFile file(tempfile,CFile::modeReadWrite|CFile::modeCreate );
141 //file.WriteString();
142 file.Close();
143 ::SetFileAttributes(tempfile, FILE_ATTRIBUTE_READONLY);
145 CAppUtils::DiffFlags flags;
147 if(bIsAdd)
148 CAppUtils::StartExtDiff(tempfile,file1,
149 pPath->GetGitPathString(),
150 pPath->GetGitPathString() + _T(":") + rev1.Left(g_Git.GetShortHASHLength()),
151 g_Git.m_CurrentDir + _T("\\") + pPath->GetWinPathString(), g_Git.m_CurrentDir + _T("\\") + pPath->GetWinPathString(),
152 git_revnum_t(GIT_REV_ZERO), rev1
153 , flags);
154 else
155 CAppUtils::StartExtDiff(file1,tempfile,
156 pPath->GetGitPathString() + _T(":") + rev1.Left(g_Git.GetShortHASHLength()),
157 pPath->GetGitPathString(),
158 g_Git.m_CurrentDir + _T("\\") + pPath->GetWinPathString(), g_Git.m_CurrentDir + _T("\\") + pPath->GetWinPathString(),
159 rev1, git_revnum_t(GIT_REV_ZERO)
160 , flags);
162 return 0;
165 int CGitDiff::SubmoduleDiff(CTGitPath * pPath,CTGitPath * /*pPath2*/, git_revnum_t rev1, git_revnum_t rev2, bool /*blame*/, bool /*unified*/)
167 CString oldhash;
168 CString newhash;
169 bool dirty = false;
170 CString cmd;
171 bool isWorkingCopy = false;
172 if( rev2 == GIT_REV_ZERO || rev1 == GIT_REV_ZERO )
174 oldhash = GIT_REV_ZERO;
175 newhash = GIT_REV_ZERO;
177 CString rev;
178 if( rev2 != GIT_REV_ZERO )
179 rev = rev2;
180 if( rev1 != GIT_REV_ZERO )
181 rev = rev1;
183 isWorkingCopy = true;
185 cmd.Format(_T("git.exe diff %s -- \"%s\""),
186 rev,pPath->GetGitPathString());
188 CString output, err;
189 if (g_Git.Run(cmd, &output, &err, CP_UTF8))
191 CMessageBox::Show(NULL, output + L"\n" + err, _T("TortoiseGit"), MB_OK|MB_ICONERROR);
192 return -1;
195 if (output.IsEmpty())
197 output.Empty();
198 err.Empty();
199 // also compare against index
200 cmd.Format(_T("git.exe diff \"%s\""), pPath->GetGitPathString());
201 if (g_Git.Run(cmd, &output, &err, CP_UTF8))
203 CMessageBox::Show(NULL, output + _T("\n") + err, _T("TortoiseGit"), MB_OK | MB_ICONERROR);
204 return -1;
207 if (output.IsEmpty())
209 CMessageBox::Show(NULL, CString(MAKEINTRESOURCE(IDS_ERR_EMPTYDIFF)), _T("TortoiseGit"), MB_OK | MB_ICONERROR);
210 return -1;
212 else if (CMessageBox::Show(NULL, CString(MAKEINTRESOURCE(IDS_SUBMODULE_EMPTYDIFF)), _T("TortoiseGit"), 1, IDI_QUESTION, CString(MAKEINTRESOURCE(IDS_MSGBOX_YES)), CString(MAKEINTRESOURCE(IDS_MSGBOX_NO))) == 1)
214 CString sCmd;
215 sCmd.Format(_T("/command:subupdate /bkpath:\"%s\""), g_Git.m_CurrentDir);
216 CAppUtils::RunTortoiseGitProc(sCmd);
218 return -1;
221 int start =0;
222 int oldstart = output.Find(_T("-Subproject commit"),start);
223 if(oldstart<0)
225 CMessageBox::Show(NULL,_T("Subproject Diff Format error") ,_T("TortoiseGit"),MB_OK|MB_ICONERROR);
226 return -1;
228 oldhash = output.Mid(oldstart+ CString(_T("-Subproject commit")).GetLength()+1,40);
229 start = 0;
230 int newstart = output.Find(_T("+Subproject commit"),start);
231 if(oldstart<0)
233 CMessageBox::Show(NULL,_T("Subproject Diff Format error") ,_T("TortoiseGit"),MB_OK|MB_ICONERROR);
234 return -1;
236 newhash = output.Mid(newstart+ CString(_T("+Subproject commit")).GetLength()+1,40);
237 dirty = output.Mid(newstart + CString(_T("+Subproject commit")).GetLength() + 41) == _T("-dirty\n");
239 else
241 cmd.Format(_T("git.exe diff-tree -r -z %s %s -- \"%s\""),
242 rev2,rev1,pPath->GetGitPathString());
244 BYTE_VECTOR bytes, errBytes;
245 if(g_Git.Run(cmd, &bytes, &errBytes))
247 CString err;
248 g_Git.StringAppend(&err, &errBytes[0], CP_UTF8);
249 CMessageBox::Show(NULL,err,_T("TortoiseGit"),MB_OK|MB_ICONERROR);
250 return -1;
253 g_Git.StringAppend(&oldhash, &bytes[15], CP_UTF8, 40);
254 g_Git.StringAppend(&newhash, &bytes[15+41], CP_UTF8, 40);
258 CString oldsub;
259 CString newsub;
260 bool oldOK = false, newOK = false;
262 CGit subgit;
263 subgit.m_CurrentDir=g_Git.m_CurrentDir+_T("\\")+pPath->GetWinPathString();
264 CSubmoduleDiffDlg::ChangeType changeType = CSubmoduleDiffDlg::Unknown;
266 if(pPath->HasAdminDir())
268 int encode=CAppUtils::GetLogOutputEncode(&subgit);
269 int oldTime = 0, newTime = 0;
271 if(oldhash != GIT_REV_ZERO)
273 CString cmdout, cmderr;
274 cmd.Format(_T("git log -n1 --pretty=format:\"%%ct %%s\" %s"), oldhash);
275 oldOK = !subgit.Run(cmd, &cmdout, &cmderr, encode);
276 if (oldOK)
278 int pos = cmdout.Find(_T(" "));
279 oldTime = _ttoi(cmdout.Left(pos));
280 oldsub = cmdout.Mid(pos + 1);
282 else
283 oldsub = cmderr;
285 if (newhash != GIT_REV_ZERO)
287 CString cmdout, cmderr;
288 cmd.Format(_T("git log -n1 --pretty=format:\"%%ct %%s\" %s"), newhash);
289 newOK = !subgit.Run(cmd, &cmdout, &cmderr, encode);
290 if (newOK)
292 int pos = cmdout.Find(_T(" "));
293 newTime = _ttoi(cmdout.Left(pos));
294 newsub = cmdout.Mid(pos + 1);
296 else
297 newsub = cmderr;
300 if (oldhash == GIT_REV_ZERO)
302 oldOK = true;
303 changeType = CSubmoduleDiffDlg::NewSubmodule;
305 else if (newhash == GIT_REV_ZERO)
307 newOK = true;
308 changeType = CSubmoduleDiffDlg::DeleteSubmodule;
310 else if (oldhash != newhash)
312 bool ffNewer = false, ffOlder = false;
313 ffNewer = subgit.IsFastForward(oldhash, newhash);
314 if (!ffNewer)
316 ffOlder = subgit.IsFastForward(newhash, oldhash);
317 if (!ffOlder)
319 if (newTime > oldTime)
320 changeType = CSubmoduleDiffDlg::NewerTime;
321 else if (newTime < oldTime)
322 changeType = CSubmoduleDiffDlg::OlderTime;
323 else
324 changeType = CSubmoduleDiffDlg::SameTime;
326 else
327 changeType = CSubmoduleDiffDlg::Rewind;
329 else
330 changeType = CSubmoduleDiffDlg::FastForward;
334 CSubmoduleDiffDlg submoduleDiffDlg;
335 submoduleDiffDlg.SetDiff(pPath->GetWinPath(), isWorkingCopy, oldhash, oldsub, oldOK, newhash, newsub, newOK, dirty, changeType);
336 submoduleDiffDlg.DoModal();
338 return 0;
341 int CGitDiff::Diff(CTGitPath * pPath,CTGitPath * pPath2, git_revnum_t rev1, git_revnum_t rev2, bool /*blame*/, bool /*unified*/)
343 CString temppath;
344 GetTempPath(temppath);
346 // make sure we have HASHes here, otherwise filenames might be invalid
347 if (rev1 != GIT_REV_ZERO)
349 CGitHash rev1Hash;
350 if (g_Git.GetHash(rev1Hash, rev1))
352 MessageBox(NULL, g_Git.GetGitLastErr(_T("Could not get hash of \"") + rev1 + _T("\".")), _T("TortoiseGit"), MB_ICONERROR);
353 return -1;
355 rev1 = rev1Hash.ToString();
357 if (rev2 != GIT_REV_ZERO)
359 CGitHash rev2Hash;
360 if (g_Git.GetHash(rev2Hash, rev2))
362 MessageBox(NULL, g_Git.GetGitLastErr(_T("Could not get hash of \"") + rev2 + _T("\".")), _T("TortoiseGit"), MB_ICONERROR);
363 return -1;
365 rev2 = rev2Hash.ToString();
368 CString file1;
369 CString title1;
370 CString cmd;
372 if(pPath->IsDirectory() || pPath2->IsDirectory())
374 return SubmoduleDiff(pPath,pPath2,rev1,rev2);
377 if(rev1 != GIT_REV_ZERO )
379 TCHAR szTempName[MAX_PATH];
380 GetTempFileName(temppath, pPath->GetBaseFilename(), 0, szTempName);
381 CString temp(szTempName);
382 DeleteFile(szTempName);
383 CreateDirectory(szTempName, NULL);
384 // use original file extension, an external diff tool might need it
385 file1.Format(_T("%s\\%s-%s-right%s"),
386 temp,
387 pPath->GetBaseFilename(),
388 rev1.Left(g_Git.GetShortHASHLength()),
389 pPath->GetFileExtension());
390 title1 = pPath->GetFileOrDirectoryName() + _T(":") + rev1.Left(g_Git.GetShortHASHLength());
391 g_Git.GetOneFile(rev1,*pPath,file1);
392 ::SetFileAttributes(file1, FILE_ATTRIBUTE_READONLY);
394 else
396 file1=g_Git.m_CurrentDir+_T("\\")+pPath->GetWinPathString();
397 title1.Format( IDS_DIFF_WCNAME, pPath->GetFileOrDirectoryName() );
398 if (!PathFileExists(file1))
400 CString sMsg;
401 sMsg.Format(IDS_PROC_DIFFERROR_FILENOTINWORKINGTREE, file1);
402 if (MessageBox(NULL, sMsg, _T("TortoiseGit"), MB_ICONEXCLAMATION | MB_YESNO) == IDNO)
403 return 1;
404 if (!CCommonAppUtils::FileOpenSave(file1, NULL, IDS_DIFF_WCNAME, IDS_COMMONFILEFILTER, true))
405 return 1;
406 title1.Format(IDS_DIFF_WCNAME, CTGitPath(file1).GetUIFileOrDirectoryName());
410 CString file2;
411 CString title2;
412 if(rev2 != GIT_REV_ZERO)
414 TCHAR szTempName[MAX_PATH];
415 GetTempFileName(temppath, pPath2->GetBaseFilename(), 0, szTempName);
416 CString temp(szTempName);
417 DeleteFile(szTempName);
418 CreateDirectory(szTempName, NULL);
419 CTGitPath fileName = *pPath2;
420 if (rev1 == GIT_REV_ZERO && pPath2->m_Action & CTGitPath::LOGACTIONS_REPLACED)
421 fileName = CTGitPath(pPath2->GetGitOldPathString());
423 // use original file extension, an external diff tool might need it
424 file2.Format(_T("%s\\%s-%s-left%s"),
425 temp,
426 fileName.GetBaseFilename(),
427 rev2.Left(g_Git.GetShortHASHLength()),
428 fileName.GetFileExtension());
429 title2 = fileName.GetFileOrDirectoryName() + _T(":") + rev2.Left(g_Git.GetShortHASHLength());
430 g_Git.GetOneFile(rev2, fileName, file2);
431 ::SetFileAttributes(file2, FILE_ATTRIBUTE_READONLY);
433 else
435 file2=g_Git.m_CurrentDir+_T("\\")+pPath2->GetWinPathString();
436 title2.Format( IDS_DIFF_WCNAME, pPath2->GetFileOrDirectoryName() );
439 if (pPath->m_Action == pPath->LOGACTIONS_ADDED)
441 CGitDiff::DiffNull(pPath, rev1, true);
443 else if (pPath->m_Action == pPath->LOGACTIONS_DELETED)
445 CGitDiff::DiffNull(pPath, rev2, false);
447 else
449 CAppUtils::DiffFlags flags;
450 CAppUtils::StartExtDiff(file2,file1,
451 title2,
452 title1,
453 g_Git.m_CurrentDir + _T("\\") + pPath2->GetWinPathString(),
454 g_Git.m_CurrentDir + _T("\\") + pPath->GetWinPathString(),
455 rev2,
456 rev1,
457 flags);
459 return 0;
462 int CGitDiff::DiffCommit(CTGitPath &path, GitRev *r1, GitRev *r2)
464 return DiffCommit(path, path, r1, r2);
467 int CGitDiff::DiffCommit(CTGitPath path1, CTGitPath path2, GitRev *r1, GitRev *r2)
469 if (path1.GetWinPathString().IsEmpty())
471 CFileDiffDlg dlg;
472 dlg.SetDiff(NULL, *r1, *r2);
473 dlg.DoModal();
475 else if (path1.IsDirectory())
477 CFileDiffDlg dlg;
478 dlg.SetDiff(&path1, *r1, *r2);
479 dlg.DoModal();
481 else
483 Diff(&path1, &path2, r1->m_CommitHash.ToString(), r2->m_CommitHash.ToString());
485 return 0;
488 int CGitDiff::DiffCommit(CTGitPath &path, CString r1, CString r2)
490 return DiffCommit(path, path, r1, r2);
494 int CGitDiff::DiffCommit(CTGitPath path1, CTGitPath path2, CString r1, CString r2)
496 if (path1.GetWinPathString().IsEmpty())
498 CFileDiffDlg dlg;
499 dlg.SetDiff(NULL, r1, r2);
500 dlg.DoModal();
502 else if (path1.IsDirectory())
504 CFileDiffDlg dlg;
505 dlg.SetDiff(&path1, r1, r2);
506 dlg.DoModal();
508 else
510 Diff(&path1, &path2, r1, r2);
512 return 0;