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.
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
;
44 if (rev1
!= GIT_REV_ZERO
)
45 cmd
.Format(_T("git.exe ls-tree \"%s\" -- \"%s\""), rev1
, pPath
->GetGitPathString());
47 cmd
.Format(_T("git.exe ls-files -s -- \"%s\""), pPath
->GetGitPathString());
50 if (g_Git
.Run(cmd
, &output
, &err
, CP_UTF8
))
52 CMessageBox::Show(NULL
, output
+ L
"\n" + err
, _T("TortoiseGit"), MB_OK
|MB_ICONERROR
);
57 start
=output
.Find(_T(' '),start
);
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);
63 newhash
=output
.Mid(start
+1, 40);
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
);
73 if (rev1
== GIT_REV_ZERO
)
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();
87 if (rev1
!= GIT_REV_ZERO
)
88 CMessageBox::Show(NULL
, _T("ls-tree output format error"), _T("TortoiseGit"), MB_OK
| MB_ICONERROR
);
90 CMessageBox::Show(NULL
, _T("ls-files output format error"), _T("TortoiseGit"), MB_OK
| MB_ICONERROR
);
94 int CGitDiff::DiffNull(CTGitPath
*pPath
, git_revnum_t rev1
,bool bIsAdd
)
97 GetTempPath(temppath
);
98 if (rev1
!= GIT_REV_ZERO
)
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
);
106 rev1
= rev1Hash
.ToString();
112 if(pPath
->IsDirectory())
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"),
127 pPath
->GetBaseFilename(),
128 rev1
.Left(g_Git
.GetShortHASHLength()),
129 pPath
->GetFileExtension());
131 g_Git
.GetOneFile(rev1
,*pPath
,file1
);
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();
143 ::SetFileAttributes(tempfile
, FILE_ATTRIBUTE_READONLY
);
145 CAppUtils::DiffFlags flags
;
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
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
)
165 int CGitDiff::SubmoduleDiff(CTGitPath
* pPath
,CTGitPath
* /*pPath2*/, git_revnum_t rev1
, git_revnum_t rev2
, bool /*blame*/, bool /*unified*/)
171 bool isWorkingCopy
= false;
172 if( rev2
== GIT_REV_ZERO
|| rev1
== GIT_REV_ZERO
)
174 oldhash
= GIT_REV_ZERO
;
175 newhash
= GIT_REV_ZERO
;
178 if( rev2
!= GIT_REV_ZERO
)
180 if( rev1
!= GIT_REV_ZERO
)
183 isWorkingCopy
= true;
185 cmd
.Format(_T("git.exe diff %s -- \"%s\""),
186 rev
,pPath
->GetGitPathString());
189 if (g_Git
.Run(cmd
, &output
, &err
, CP_UTF8
))
191 CMessageBox::Show(NULL
, output
+ L
"\n" + err
, _T("TortoiseGit"), MB_OK
|MB_ICONERROR
);
195 if (output
.IsEmpty())
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
);
207 if (output
.IsEmpty())
209 CMessageBox::Show(NULL
, CString(MAKEINTRESOURCE(IDS_ERR_EMPTYDIFF
)), _T("TortoiseGit"), MB_OK
| MB_ICONERROR
);
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)
215 sCmd
.Format(_T("/command:subupdate /bkpath:\"%s\""), g_Git
.m_CurrentDir
);
216 CAppUtils::RunTortoiseGitProc(sCmd
);
222 int oldstart
= output
.Find(_T("-Subproject commit"),start
);
225 CMessageBox::Show(NULL
,_T("Subproject Diff Format error") ,_T("TortoiseGit"),MB_OK
|MB_ICONERROR
);
228 oldhash
= output
.Mid(oldstart
+ CString(_T("-Subproject commit")).GetLength()+1,40);
230 int newstart
= output
.Find(_T("+Subproject commit"),start
);
233 CMessageBox::Show(NULL
,_T("Subproject Diff Format error") ,_T("TortoiseGit"),MB_OK
|MB_ICONERROR
);
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");
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
))
248 g_Git
.StringAppend(&err
, &errBytes
[0], CP_UTF8
);
249 CMessageBox::Show(NULL
,err
,_T("TortoiseGit"),MB_OK
|MB_ICONERROR
);
253 g_Git
.StringAppend(&oldhash
, &bytes
[15], CP_UTF8
, 40);
254 g_Git
.StringAppend(&newhash
, &bytes
[15+41], CP_UTF8
, 40);
260 bool oldOK
= false, newOK
= false;
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
);
278 int pos
= cmdout
.Find(_T(" "));
279 oldTime
= _ttoi(cmdout
.Left(pos
));
280 oldsub
= cmdout
.Mid(pos
+ 1);
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
);
292 int pos
= cmdout
.Find(_T(" "));
293 newTime
= _ttoi(cmdout
.Left(pos
));
294 newsub
= cmdout
.Mid(pos
+ 1);
300 if (oldhash
== GIT_REV_ZERO
)
303 changeType
= CSubmoduleDiffDlg::NewSubmodule
;
305 else if (newhash
== GIT_REV_ZERO
)
308 changeType
= CSubmoduleDiffDlg::DeleteSubmodule
;
310 else if (oldhash
!= newhash
)
312 bool ffNewer
= false, ffOlder
= false;
313 ffNewer
= subgit
.IsFastForward(oldhash
, newhash
);
316 ffOlder
= subgit
.IsFastForward(newhash
, oldhash
);
319 if (newTime
> oldTime
)
320 changeType
= CSubmoduleDiffDlg::NewerTime
;
321 else if (newTime
< oldTime
)
322 changeType
= CSubmoduleDiffDlg::OlderTime
;
324 changeType
= CSubmoduleDiffDlg::SameTime
;
327 changeType
= CSubmoduleDiffDlg::Rewind
;
330 changeType
= CSubmoduleDiffDlg::FastForward
;
334 CSubmoduleDiffDlg submoduleDiffDlg
;
335 submoduleDiffDlg
.SetDiff(pPath
->GetWinPath(), isWorkingCopy
, oldhash
, oldsub
, oldOK
, newhash
, newsub
, newOK
, dirty
, changeType
);
336 submoduleDiffDlg
.DoModal();
341 int CGitDiff::Diff(CTGitPath
* pPath
,CTGitPath
* pPath2
, git_revnum_t rev1
, git_revnum_t rev2
, bool /*blame*/, bool /*unified*/)
344 GetTempPath(temppath
);
346 // make sure we have HASHes here, otherwise filenames might be invalid
347 if (rev1
!= GIT_REV_ZERO
)
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
);
355 rev1
= rev1Hash
.ToString();
357 if (rev2
!= GIT_REV_ZERO
)
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
);
365 rev2
= rev2Hash
.ToString();
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"),
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
);
396 file1
=g_Git
.m_CurrentDir
+_T("\\")+pPath
->GetWinPathString();
397 title1
.Format( IDS_DIFF_WCNAME
, pPath
->GetFileOrDirectoryName() );
398 if (!PathFileExists(file1
))
401 sMsg
.Format(IDS_PROC_DIFFERROR_FILENOTINWORKINGTREE
, file1
);
402 if (MessageBox(NULL
, sMsg
, _T("TortoiseGit"), MB_ICONEXCLAMATION
| MB_YESNO
) == IDNO
)
404 if (!CCommonAppUtils::FileOpenSave(file1
, NULL
, IDS_DIFF_WCNAME
, IDS_COMMONFILEFILTER
, true))
406 title1
.Format(IDS_DIFF_WCNAME
, CTGitPath(file1
).GetUIFileOrDirectoryName());
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"),
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
);
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);
449 CAppUtils::DiffFlags flags
;
450 CAppUtils::StartExtDiff(file2
,file1
,
453 g_Git
.m_CurrentDir
+ _T("\\") + pPath2
->GetWinPathString(),
454 g_Git
.m_CurrentDir
+ _T("\\") + pPath
->GetWinPathString(),
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())
472 dlg
.SetDiff(NULL
, *r1
, *r2
);
475 else if (path1
.IsDirectory())
478 dlg
.SetDiff(&path1
, *r1
, *r2
);
483 Diff(&path1
, &path2
, r1
->m_CommitHash
.ToString(), r2
->m_CommitHash
.ToString());
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())
499 dlg
.SetDiff(NULL
, r1
, r2
);
502 else if (path1
.IsDirectory())
505 dlg
.SetDiff(&path1
, r1
, r2
);
510 Diff(&path1
, &path2
, r1
, r2
);