Fixed issue #1542: Can send pull request email
[TortoiseGit.git] / src / TortoiseProc / GitDiff.cpp
blob9d4866871c67daa785f122c3ed1db9c088e9a182
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();
83 if (submoduleDiffDlg.IsRefresh())
84 return 1;
86 return 0;
89 if (rev1 != GIT_REV_ZERO)
90 CMessageBox::Show(NULL, _T("ls-tree output format error"), _T("TortoiseGit"), MB_OK | MB_ICONERROR);
91 else
92 CMessageBox::Show(NULL, _T("ls-files output format error"), _T("TortoiseGit"), MB_OK | MB_ICONERROR);
93 return -1;
96 int CGitDiff::DiffNull(CTGitPath *pPath, git_revnum_t rev1,bool bIsAdd)
98 CString temppath;
99 GetTempPath(temppath);
100 if (rev1 != GIT_REV_ZERO)
102 CGitHash rev1Hash;
103 if (g_Git.GetHash(rev1Hash, rev1)) // make sure we have a HASH here, otherwise filenames might be invalid
105 MessageBox(NULL, g_Git.GetGitLastErr(_T("Could not get hash of \"") + rev1 + _T("\".")), _T("TortoiseGit"), MB_ICONERROR);
106 return -1;
108 rev1 = rev1Hash.ToString();
110 CString file1;
111 CString nullfile;
112 CString cmd;
114 if(pPath->IsDirectory())
116 int result;
117 // refresh if result = 1
118 while ((result = SubmoduleDiffNull(pPath, rev1)) == 1) ;
119 return result;
122 if(rev1 != GIT_REV_ZERO )
124 TCHAR szTempName[MAX_PATH];
125 GetTempFileName(temppath, pPath->GetBaseFilename(), 0, szTempName);
126 CString temp(szTempName);
127 DeleteFile(szTempName);
128 CreateDirectory(szTempName, NULL);
129 file1.Format(_T("%s\\%s-%s%s"),
130 temp,
131 pPath->GetBaseFilename(),
132 rev1.Left(g_Git.GetShortHASHLength()),
133 pPath->GetFileExtension());
135 g_Git.GetOneFile(rev1,*pPath,file1);
137 else
139 file1=g_Git.m_CurrentDir+_T("\\")+pPath->GetWinPathString();
142 // preserve FileExtension, needed especially for diffing deleted images (detection on new filename extension)
143 CString tempfile=::GetTempFile() + pPath->GetFileExtension();
144 CStdioFile file(tempfile,CFile::modeReadWrite|CFile::modeCreate );
145 //file.WriteString();
146 file.Close();
147 ::SetFileAttributes(tempfile, FILE_ATTRIBUTE_READONLY);
149 CAppUtils::DiffFlags flags;
151 if(bIsAdd)
152 CAppUtils::StartExtDiff(tempfile,file1,
153 pPath->GetGitPathString(),
154 pPath->GetGitPathString() + _T(":") + rev1.Left(g_Git.GetShortHASHLength()),
155 g_Git.m_CurrentDir + _T("\\") + pPath->GetWinPathString(), g_Git.m_CurrentDir + _T("\\") + pPath->GetWinPathString(),
156 git_revnum_t(GIT_REV_ZERO), rev1
157 , flags);
158 else
159 CAppUtils::StartExtDiff(file1,tempfile,
160 pPath->GetGitPathString() + _T(":") + rev1.Left(g_Git.GetShortHASHLength()),
161 pPath->GetGitPathString(),
162 g_Git.m_CurrentDir + _T("\\") + pPath->GetWinPathString(), g_Git.m_CurrentDir + _T("\\") + pPath->GetWinPathString(),
163 rev1, git_revnum_t(GIT_REV_ZERO)
164 , flags);
166 return 0;
169 int CGitDiff::SubmoduleDiff(CTGitPath * pPath,CTGitPath * /*pPath2*/, git_revnum_t rev1, git_revnum_t rev2, bool /*blame*/, bool /*unified*/)
171 CString oldhash;
172 CString newhash;
173 bool dirty = false;
174 CString cmd;
175 bool isWorkingCopy = false;
176 if( rev2 == GIT_REV_ZERO || rev1 == GIT_REV_ZERO )
178 oldhash = GIT_REV_ZERO;
179 newhash = GIT_REV_ZERO;
181 CString rev;
182 if( rev2 != GIT_REV_ZERO )
183 rev = rev2;
184 if( rev1 != GIT_REV_ZERO )
185 rev = rev1;
187 isWorkingCopy = true;
189 cmd.Format(_T("git.exe diff %s -- \"%s\""),
190 rev,pPath->GetGitPathString());
192 CString output, err;
193 if (g_Git.Run(cmd, &output, &err, CP_UTF8))
195 CMessageBox::Show(NULL, output + L"\n" + err, _T("TortoiseGit"), MB_OK|MB_ICONERROR);
196 return -1;
199 if (output.IsEmpty())
201 output.Empty();
202 err.Empty();
203 // also compare against index
204 cmd.Format(_T("git.exe diff \"%s\""), pPath->GetGitPathString());
205 if (g_Git.Run(cmd, &output, &err, CP_UTF8))
207 CMessageBox::Show(NULL, output + _T("\n") + err, _T("TortoiseGit"), MB_OK | MB_ICONERROR);
208 return -1;
211 if (output.IsEmpty())
213 CMessageBox::Show(NULL, CString(MAKEINTRESOURCE(IDS_ERR_EMPTYDIFF)), _T("TortoiseGit"), MB_OK | MB_ICONERROR);
214 return -1;
216 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)
218 CString sCmd;
219 sCmd.Format(_T("/command:subupdate /bkpath:\"%s\""), g_Git.m_CurrentDir);
220 CAppUtils::RunTortoiseGitProc(sCmd);
222 return -1;
225 int start =0;
226 int oldstart = output.Find(_T("-Subproject commit"),start);
227 if(oldstart<0)
229 CMessageBox::Show(NULL,_T("Subproject Diff Format error") ,_T("TortoiseGit"),MB_OK|MB_ICONERROR);
230 return -1;
232 oldhash = output.Mid(oldstart+ CString(_T("-Subproject commit")).GetLength()+1,40);
233 start = 0;
234 int newstart = output.Find(_T("+Subproject commit"),start);
235 if(oldstart<0)
237 CMessageBox::Show(NULL,_T("Subproject Diff Format error") ,_T("TortoiseGit"),MB_OK|MB_ICONERROR);
238 return -1;
240 newhash = output.Mid(newstart+ CString(_T("+Subproject commit")).GetLength()+1,40);
241 dirty = output.Mid(newstart + CString(_T("+Subproject commit")).GetLength() + 41) == _T("-dirty\n");
243 else
245 cmd.Format(_T("git.exe diff-tree -r -z %s %s -- \"%s\""),
246 rev2,rev1,pPath->GetGitPathString());
248 BYTE_VECTOR bytes, errBytes;
249 if(g_Git.Run(cmd, &bytes, &errBytes))
251 CString err;
252 g_Git.StringAppend(&err, &errBytes[0], CP_UTF8);
253 CMessageBox::Show(NULL,err,_T("TortoiseGit"),MB_OK|MB_ICONERROR);
254 return -1;
257 g_Git.StringAppend(&oldhash, &bytes[15], CP_UTF8, 40);
258 g_Git.StringAppend(&newhash, &bytes[15+41], CP_UTF8, 40);
262 CString oldsub;
263 CString newsub;
264 bool oldOK = false, newOK = false;
266 CGit subgit;
267 subgit.m_CurrentDir=g_Git.m_CurrentDir+_T("\\")+pPath->GetWinPathString();
268 CSubmoduleDiffDlg::ChangeType changeType = CSubmoduleDiffDlg::Unknown;
270 if(pPath->HasAdminDir())
272 int encode=CAppUtils::GetLogOutputEncode(&subgit);
273 int oldTime = 0, newTime = 0;
275 if(oldhash != GIT_REV_ZERO)
277 CString cmdout, cmderr;
278 cmd.Format(_T("git log -n1 --pretty=format:\"%%ct %%s\" %s"), oldhash);
279 oldOK = !subgit.Run(cmd, &cmdout, &cmderr, encode);
280 if (oldOK)
282 int pos = cmdout.Find(_T(" "));
283 oldTime = _ttoi(cmdout.Left(pos));
284 oldsub = cmdout.Mid(pos + 1);
286 else
287 oldsub = cmderr;
289 if (newhash != GIT_REV_ZERO)
291 CString cmdout, cmderr;
292 cmd.Format(_T("git log -n1 --pretty=format:\"%%ct %%s\" %s"), newhash);
293 newOK = !subgit.Run(cmd, &cmdout, &cmderr, encode);
294 if (newOK)
296 int pos = cmdout.Find(_T(" "));
297 newTime = _ttoi(cmdout.Left(pos));
298 newsub = cmdout.Mid(pos + 1);
300 else
301 newsub = cmderr;
304 if (oldhash == GIT_REV_ZERO)
306 oldOK = true;
307 changeType = CSubmoduleDiffDlg::NewSubmodule;
309 else if (newhash == GIT_REV_ZERO)
311 newOK = true;
312 changeType = CSubmoduleDiffDlg::DeleteSubmodule;
314 else if (oldhash != newhash)
316 bool ffNewer = false, ffOlder = false;
317 ffNewer = subgit.IsFastForward(oldhash, newhash);
318 if (!ffNewer)
320 ffOlder = subgit.IsFastForward(newhash, oldhash);
321 if (!ffOlder)
323 if (newTime > oldTime)
324 changeType = CSubmoduleDiffDlg::NewerTime;
325 else if (newTime < oldTime)
326 changeType = CSubmoduleDiffDlg::OlderTime;
327 else
328 changeType = CSubmoduleDiffDlg::SameTime;
330 else
331 changeType = CSubmoduleDiffDlg::Rewind;
333 else
334 changeType = CSubmoduleDiffDlg::FastForward;
338 if (!oldOK || !newOK)
339 changeType = CSubmoduleDiffDlg::Unknown;
341 CSubmoduleDiffDlg submoduleDiffDlg;
342 submoduleDiffDlg.SetDiff(pPath->GetWinPath(), isWorkingCopy, oldhash, oldsub, oldOK, newhash, newsub, newOK, dirty, changeType);
343 submoduleDiffDlg.DoModal();
344 if (submoduleDiffDlg.IsRefresh())
345 return 1;
347 return 0;
350 int CGitDiff::Diff(CTGitPath * pPath,CTGitPath * pPath2, git_revnum_t rev1, git_revnum_t rev2, bool /*blame*/, bool /*unified*/)
352 CString temppath;
353 GetTempPath(temppath);
355 // make sure we have HASHes here, otherwise filenames might be invalid
356 if (rev1 != GIT_REV_ZERO)
358 CGitHash rev1Hash;
359 if (g_Git.GetHash(rev1Hash, rev1))
361 MessageBox(NULL, g_Git.GetGitLastErr(_T("Could not get hash of \"") + rev1 + _T("\".")), _T("TortoiseGit"), MB_ICONERROR);
362 return -1;
364 rev1 = rev1Hash.ToString();
366 if (rev2 != GIT_REV_ZERO)
368 CGitHash rev2Hash;
369 if (g_Git.GetHash(rev2Hash, rev2))
371 MessageBox(NULL, g_Git.GetGitLastErr(_T("Could not get hash of \"") + rev2 + _T("\".")), _T("TortoiseGit"), MB_ICONERROR);
372 return -1;
374 rev2 = rev2Hash.ToString();
377 CString file1;
378 CString title1;
379 CString cmd;
381 if(pPath->IsDirectory() || pPath2->IsDirectory())
383 int result;
384 // refresh if result = 1
385 while ((result = SubmoduleDiff(pPath, pPath2, rev1, rev2)) == 1) ;
386 return result;
389 if(rev1 != GIT_REV_ZERO )
391 TCHAR szTempName[MAX_PATH];
392 GetTempFileName(temppath, pPath->GetBaseFilename(), 0, szTempName);
393 CString temp(szTempName);
394 DeleteFile(szTempName);
395 CreateDirectory(szTempName, NULL);
396 // use original file extension, an external diff tool might need it
397 file1.Format(_T("%s\\%s-%s-right%s"),
398 temp,
399 pPath->GetBaseFilename(),
400 rev1.Left(g_Git.GetShortHASHLength()),
401 pPath->GetFileExtension());
402 title1 = pPath->GetFileOrDirectoryName() + _T(":") + rev1.Left(g_Git.GetShortHASHLength());
403 g_Git.GetOneFile(rev1,*pPath,file1);
404 ::SetFileAttributes(file1, FILE_ATTRIBUTE_READONLY);
406 else
408 file1=g_Git.m_CurrentDir+_T("\\")+pPath->GetWinPathString();
409 title1.Format( IDS_DIFF_WCNAME, pPath->GetFileOrDirectoryName() );
410 if (!PathFileExists(file1))
412 CString sMsg;
413 sMsg.Format(IDS_PROC_DIFFERROR_FILENOTINWORKINGTREE, file1);
414 if (MessageBox(NULL, sMsg, _T("TortoiseGit"), MB_ICONEXCLAMATION | MB_YESNO) == IDNO)
415 return 1;
416 if (!CCommonAppUtils::FileOpenSave(file1, NULL, IDS_DIFF_WCNAME, IDS_COMMONFILEFILTER, true))
417 return 1;
418 title1.Format(IDS_DIFF_WCNAME, CTGitPath(file1).GetUIFileOrDirectoryName());
422 CString file2;
423 CString title2;
424 if(rev2 != GIT_REV_ZERO)
426 TCHAR szTempName[MAX_PATH];
427 GetTempFileName(temppath, pPath2->GetBaseFilename(), 0, szTempName);
428 CString temp(szTempName);
429 DeleteFile(szTempName);
430 CreateDirectory(szTempName, NULL);
431 CTGitPath fileName = *pPath2;
432 if (rev1 == GIT_REV_ZERO && pPath2->m_Action & CTGitPath::LOGACTIONS_REPLACED)
433 fileName = CTGitPath(pPath2->GetGitOldPathString());
435 // use original file extension, an external diff tool might need it
436 file2.Format(_T("%s\\%s-%s-left%s"),
437 temp,
438 fileName.GetBaseFilename(),
439 rev2.Left(g_Git.GetShortHASHLength()),
440 fileName.GetFileExtension());
441 title2 = fileName.GetFileOrDirectoryName() + _T(":") + rev2.Left(g_Git.GetShortHASHLength());
442 g_Git.GetOneFile(rev2, fileName, file2);
443 ::SetFileAttributes(file2, FILE_ATTRIBUTE_READONLY);
445 else
447 file2=g_Git.m_CurrentDir+_T("\\")+pPath2->GetWinPathString();
448 title2.Format( IDS_DIFF_WCNAME, pPath2->GetFileOrDirectoryName() );
451 if (pPath->m_Action == pPath->LOGACTIONS_ADDED)
453 CGitDiff::DiffNull(pPath, rev1, true);
455 else if (pPath->m_Action == pPath->LOGACTIONS_DELETED)
457 CGitDiff::DiffNull(pPath, rev2, false);
459 else
461 CAppUtils::DiffFlags flags;
462 CAppUtils::StartExtDiff(file2,file1,
463 title2,
464 title1,
465 g_Git.m_CurrentDir + _T("\\") + pPath2->GetWinPathString(),
466 g_Git.m_CurrentDir + _T("\\") + pPath->GetWinPathString(),
467 rev2,
468 rev1,
469 flags);
471 return 0;
474 int CGitDiff::DiffCommit(CTGitPath &path, GitRev *r1, GitRev *r2)
476 return DiffCommit(path, path, r1, r2);
479 int CGitDiff::DiffCommit(CTGitPath path1, CTGitPath path2, GitRev *r1, GitRev *r2)
481 if (path1.GetWinPathString().IsEmpty())
483 CFileDiffDlg dlg;
484 dlg.SetDiff(NULL, *r1, *r2);
485 dlg.DoModal();
487 else if (path1.IsDirectory())
489 CFileDiffDlg dlg;
490 dlg.SetDiff(&path1, *r1, *r2);
491 dlg.DoModal();
493 else
495 Diff(&path1, &path2, r1->m_CommitHash.ToString(), r2->m_CommitHash.ToString());
497 return 0;
500 int CGitDiff::DiffCommit(CTGitPath &path, CString r1, CString r2)
502 return DiffCommit(path, path, r1, r2);
506 int CGitDiff::DiffCommit(CTGitPath path1, CTGitPath path2, CString r1, CString r2)
508 if (path1.GetWinPathString().IsEmpty())
510 CFileDiffDlg dlg;
511 dlg.SetDiff(NULL, r1, r2);
512 dlg.DoModal();
514 else if (path1.IsDirectory())
516 CFileDiffDlg dlg;
517 dlg.SetDiff(&path1, r1, r2);
518 dlg.DoModal();
520 else
522 Diff(&path1, &path2, r1, r2);
524 return 0;