Don't delete files which contain of one line without a line ending
[TortoiseGit.git] / src / TortoiseProc / GitDiff.cpp
blob8d9aae309a3c32a0f0b164b94be1200c318ce898
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 if (!oldOK || !newOK)
335 changeType = CSubmoduleDiffDlg::Unknown;
337 CSubmoduleDiffDlg submoduleDiffDlg;
338 submoduleDiffDlg.SetDiff(pPath->GetWinPath(), isWorkingCopy, oldhash, oldsub, oldOK, newhash, newsub, newOK, dirty, changeType);
339 submoduleDiffDlg.DoModal();
341 return 0;
344 int CGitDiff::Diff(CTGitPath * pPath,CTGitPath * pPath2, git_revnum_t rev1, git_revnum_t rev2, bool /*blame*/, bool /*unified*/)
346 CString temppath;
347 GetTempPath(temppath);
349 // make sure we have HASHes here, otherwise filenames might be invalid
350 if (rev1 != GIT_REV_ZERO)
352 CGitHash rev1Hash;
353 if (g_Git.GetHash(rev1Hash, rev1))
355 MessageBox(NULL, g_Git.GetGitLastErr(_T("Could not get hash of \"") + rev1 + _T("\".")), _T("TortoiseGit"), MB_ICONERROR);
356 return -1;
358 rev1 = rev1Hash.ToString();
360 if (rev2 != GIT_REV_ZERO)
362 CGitHash rev2Hash;
363 if (g_Git.GetHash(rev2Hash, rev2))
365 MessageBox(NULL, g_Git.GetGitLastErr(_T("Could not get hash of \"") + rev2 + _T("\".")), _T("TortoiseGit"), MB_ICONERROR);
366 return -1;
368 rev2 = rev2Hash.ToString();
371 CString file1;
372 CString title1;
373 CString cmd;
375 if(pPath->IsDirectory() || pPath2->IsDirectory())
377 return SubmoduleDiff(pPath,pPath2,rev1,rev2);
380 if(rev1 != GIT_REV_ZERO )
382 TCHAR szTempName[MAX_PATH];
383 GetTempFileName(temppath, pPath->GetBaseFilename(), 0, szTempName);
384 CString temp(szTempName);
385 DeleteFile(szTempName);
386 CreateDirectory(szTempName, NULL);
387 // use original file extension, an external diff tool might need it
388 file1.Format(_T("%s\\%s-%s-right%s"),
389 temp,
390 pPath->GetBaseFilename(),
391 rev1.Left(g_Git.GetShortHASHLength()),
392 pPath->GetFileExtension());
393 title1 = pPath->GetFileOrDirectoryName() + _T(":") + rev1.Left(g_Git.GetShortHASHLength());
394 g_Git.GetOneFile(rev1,*pPath,file1);
395 ::SetFileAttributes(file1, FILE_ATTRIBUTE_READONLY);
397 else
399 file1=g_Git.m_CurrentDir+_T("\\")+pPath->GetWinPathString();
400 title1.Format( IDS_DIFF_WCNAME, pPath->GetFileOrDirectoryName() );
401 if (!PathFileExists(file1))
403 CString sMsg;
404 sMsg.Format(IDS_PROC_DIFFERROR_FILENOTINWORKINGTREE, file1);
405 if (MessageBox(NULL, sMsg, _T("TortoiseGit"), MB_ICONEXCLAMATION | MB_YESNO) == IDNO)
406 return 1;
407 if (!CCommonAppUtils::FileOpenSave(file1, NULL, IDS_DIFF_WCNAME, IDS_COMMONFILEFILTER, true))
408 return 1;
409 title1.Format(IDS_DIFF_WCNAME, CTGitPath(file1).GetUIFileOrDirectoryName());
413 CString file2;
414 CString title2;
415 if(rev2 != GIT_REV_ZERO)
417 TCHAR szTempName[MAX_PATH];
418 GetTempFileName(temppath, pPath2->GetBaseFilename(), 0, szTempName);
419 CString temp(szTempName);
420 DeleteFile(szTempName);
421 CreateDirectory(szTempName, NULL);
422 CTGitPath fileName = *pPath2;
423 if (rev1 == GIT_REV_ZERO && pPath2->m_Action & CTGitPath::LOGACTIONS_REPLACED)
424 fileName = CTGitPath(pPath2->GetGitOldPathString());
426 // use original file extension, an external diff tool might need it
427 file2.Format(_T("%s\\%s-%s-left%s"),
428 temp,
429 fileName.GetBaseFilename(),
430 rev2.Left(g_Git.GetShortHASHLength()),
431 fileName.GetFileExtension());
432 title2 = fileName.GetFileOrDirectoryName() + _T(":") + rev2.Left(g_Git.GetShortHASHLength());
433 g_Git.GetOneFile(rev2, fileName, file2);
434 ::SetFileAttributes(file2, FILE_ATTRIBUTE_READONLY);
436 else
438 file2=g_Git.m_CurrentDir+_T("\\")+pPath2->GetWinPathString();
439 title2.Format( IDS_DIFF_WCNAME, pPath2->GetFileOrDirectoryName() );
442 if (pPath->m_Action == pPath->LOGACTIONS_ADDED)
444 CGitDiff::DiffNull(pPath, rev1, true);
446 else if (pPath->m_Action == pPath->LOGACTIONS_DELETED)
448 CGitDiff::DiffNull(pPath, rev2, false);
450 else
452 CAppUtils::DiffFlags flags;
453 CAppUtils::StartExtDiff(file2,file1,
454 title2,
455 title1,
456 g_Git.m_CurrentDir + _T("\\") + pPath2->GetWinPathString(),
457 g_Git.m_CurrentDir + _T("\\") + pPath->GetWinPathString(),
458 rev2,
459 rev1,
460 flags);
462 return 0;
465 int CGitDiff::DiffCommit(CTGitPath &path, GitRev *r1, GitRev *r2)
467 return DiffCommit(path, path, r1, r2);
470 int CGitDiff::DiffCommit(CTGitPath path1, CTGitPath path2, GitRev *r1, GitRev *r2)
472 if (path1.GetWinPathString().IsEmpty())
474 CFileDiffDlg dlg;
475 dlg.SetDiff(NULL, *r1, *r2);
476 dlg.DoModal();
478 else if (path1.IsDirectory())
480 CFileDiffDlg dlg;
481 dlg.SetDiff(&path1, *r1, *r2);
482 dlg.DoModal();
484 else
486 Diff(&path1, &path2, r1->m_CommitHash.ToString(), r2->m_CommitHash.ToString());
488 return 0;
491 int CGitDiff::DiffCommit(CTGitPath &path, CString r1, CString r2)
493 return DiffCommit(path, path, r1, r2);
497 int CGitDiff::DiffCommit(CTGitPath path1, CTGitPath path2, CString r1, CString r2)
499 if (path1.GetWinPathString().IsEmpty())
501 CFileDiffDlg dlg;
502 dlg.SetDiff(NULL, r1, r2);
503 dlg.DoModal();
505 else if (path1.IsDirectory())
507 CFileDiffDlg dlg;
508 dlg.SetDiff(&path1, r1, r2);
509 dlg.DoModal();
511 else
513 Diff(&path1, &path2, r1, r2);
515 return 0;