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 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();
344 int CGitDiff::Diff(CTGitPath
* pPath
,CTGitPath
* pPath2
, git_revnum_t rev1
, git_revnum_t rev2
, bool /*blame*/, bool /*unified*/)
347 GetTempPath(temppath
);
349 // make sure we have HASHes here, otherwise filenames might be invalid
350 if (rev1
!= GIT_REV_ZERO
)
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
);
358 rev1
= rev1Hash
.ToString();
360 if (rev2
!= GIT_REV_ZERO
)
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
);
368 rev2
= rev2Hash
.ToString();
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"),
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
);
399 file1
=g_Git
.m_CurrentDir
+_T("\\")+pPath
->GetWinPathString();
400 title1
.Format( IDS_DIFF_WCNAME
, pPath
->GetFileOrDirectoryName() );
401 if (!PathFileExists(file1
))
404 sMsg
.Format(IDS_PROC_DIFFERROR_FILENOTINWORKINGTREE
, file1
);
405 if (MessageBox(NULL
, sMsg
, _T("TortoiseGit"), MB_ICONEXCLAMATION
| MB_YESNO
) == IDNO
)
407 if (!CCommonAppUtils::FileOpenSave(file1
, NULL
, IDS_DIFF_WCNAME
, IDS_COMMONFILEFILTER
, true))
409 title1
.Format(IDS_DIFF_WCNAME
, CTGitPath(file1
).GetUIFileOrDirectoryName());
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"),
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
);
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);
452 CAppUtils::DiffFlags flags
;
453 CAppUtils::StartExtDiff(file2
,file1
,
456 g_Git
.m_CurrentDir
+ _T("\\") + pPath2
->GetWinPathString(),
457 g_Git
.m_CurrentDir
+ _T("\\") + pPath
->GetWinPathString(),
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())
475 dlg
.SetDiff(NULL
, *r1
, *r2
);
478 else if (path1
.IsDirectory())
481 dlg
.SetDiff(&path1
, *r1
, *r2
);
486 Diff(&path1
, &path2
, r1
->m_CommitHash
.ToString(), r2
->m_CommitHash
.ToString());
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())
502 dlg
.SetDiff(NULL
, r1
, r2
);
505 else if (path1
.IsDirectory())
508 dlg
.SetDiff(&path1
, r1
, r2
);
513 Diff(&path1
, &path2
, r1
, r2
);