1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2003-2008 - TortoiseSVN
4 // Copyright (C) 2008-2014 - 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(const CTGitPath
* pPath
, const 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
.CombinePath(pPath
);
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();
83 if (submoduleDiffDlg
.IsRefresh())
89 if (rev1
!= GIT_REV_ZERO
)
90 CMessageBox::Show(NULL
, _T("ls-tree output format error"), _T("TortoiseGit"), MB_OK
| MB_ICONERROR
);
92 CMessageBox::Show(NULL
, _T("ls-files output format error"), _T("TortoiseGit"), MB_OK
| MB_ICONERROR
);
96 int CGitDiff::DiffNull(const CTGitPath
*pPath
, git_revnum_t rev1
, bool bIsAdd
, int jumpToLine
)
99 GetTempPath(temppath
);
100 if (rev1
!= GIT_REV_ZERO
)
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
);
108 rev1
= rev1Hash
.ToString();
114 if(pPath
->IsDirectory())
117 // refresh if result = 1
118 CTGitPath path
= *pPath
;
119 while ((result
= SubmoduleDiffNull(&path
, rev1
)) == 1)
121 path
.SetFromGit(pPath
->GetGitPathString());
126 if(rev1
!= GIT_REV_ZERO
)
128 TCHAR szTempName
[MAX_PATH
] = {0};
129 GetTempFileName(temppath
, pPath
->GetBaseFilename(), 0, szTempName
);
130 CString
temp(szTempName
);
131 DeleteFile(szTempName
);
132 CreateDirectory(szTempName
, NULL
);
133 file1
.Format(_T("%s\\%s-%s%s"),
135 pPath
->GetBaseFilename(),
136 rev1
.Left(g_Git
.GetShortHASHLength()),
137 pPath
->GetFileExtension());
139 if (g_Git
.GetOneFile(rev1
, *pPath
, file1
))
142 out
.Format(IDS_STATUSLIST_CHECKOUTFILEFAILED
, pPath
->GetGitPathString(), rev1
, file1
);
143 CMessageBox::Show(nullptr, g_Git
.GetGitLastErr(out
, CGit::GIT_CMD_GETONEFILE
), _T("TortoiseGit"), MB_OK
);
149 file1
= g_Git
.CombinePath(pPath
);
152 // preserve FileExtension, needed especially for diffing deleted images (detection on new filename extension)
153 CString tempfile
=::GetTempFile() + pPath
->GetFileExtension();
154 CStdioFile
file(tempfile
,CFile::modeReadWrite
|CFile::modeCreate
);
155 //file.WriteString();
157 ::SetFileAttributes(tempfile
, FILE_ATTRIBUTE_READONLY
);
159 CAppUtils::DiffFlags flags
;
162 CAppUtils::StartExtDiff(tempfile
,file1
,
163 pPath
->GetGitPathString(),
164 pPath
->GetGitPathString() + _T(":") + rev1
.Left(g_Git
.GetShortHASHLength()),
165 g_Git
.CombinePath(pPath
), g_Git
.CombinePath(pPath
),
166 git_revnum_t(GIT_REV_ZERO
), rev1
167 , flags
, jumpToLine
);
169 CAppUtils::StartExtDiff(file1
,tempfile
,
170 pPath
->GetGitPathString() + _T(":") + rev1
.Left(g_Git
.GetShortHASHLength()),
171 pPath
->GetGitPathString(),
172 g_Git
.CombinePath(pPath
), g_Git
.CombinePath(pPath
),
173 rev1
, git_revnum_t(GIT_REV_ZERO
)
174 , flags
, jumpToLine
);
179 int CGitDiff::SubmoduleDiff(const CTGitPath
* pPath
, const CTGitPath
* /*pPath2*/, const git_revnum_t
&rev1
, const git_revnum_t
&rev2
, bool /*blame*/, bool /*unified*/)
185 bool isWorkingCopy
= false;
186 if( rev2
== GIT_REV_ZERO
|| rev1
== GIT_REV_ZERO
)
188 oldhash
= GIT_REV_ZERO
;
189 newhash
= GIT_REV_ZERO
;
192 if( rev2
!= GIT_REV_ZERO
)
194 if( rev1
!= GIT_REV_ZERO
)
197 isWorkingCopy
= true;
199 cmd
.Format(_T("git.exe diff %s -- \"%s\""),
200 rev
,pPath
->GetGitPathString());
203 if (g_Git
.Run(cmd
, &output
, &err
, CP_UTF8
))
205 CMessageBox::Show(NULL
, output
+ L
"\n" + err
, _T("TortoiseGit"), MB_OK
|MB_ICONERROR
);
209 if (output
.IsEmpty())
213 // also compare against index
214 cmd
.Format(_T("git.exe diff -- \"%s\""), pPath
->GetGitPathString());
215 if (g_Git
.Run(cmd
, &output
, &err
, CP_UTF8
))
217 CMessageBox::Show(NULL
, output
+ _T("\n") + err
, _T("TortoiseGit"), MB_OK
| MB_ICONERROR
);
221 if (output
.IsEmpty())
223 CMessageBox::Show(NULL
, CString(MAKEINTRESOURCE(IDS_ERR_EMPTYDIFF
)), _T("TortoiseGit"), MB_OK
| MB_ICONERROR
);
226 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)
229 sCmd
.Format(_T("/command:subupdate /bkpath:\"%s\""), g_Git
.m_CurrentDir
);
230 CAppUtils::RunTortoiseGitProc(sCmd
);
236 int oldstart
= output
.Find(_T("-Subproject commit"),start
);
239 CMessageBox::Show(NULL
,_T("Subproject Diff Format error") ,_T("TortoiseGit"),MB_OK
|MB_ICONERROR
);
242 oldhash
= output
.Mid(oldstart
+ CString(_T("-Subproject commit")).GetLength()+1,40);
244 int newstart
= output
.Find(_T("+Subproject commit"),start
);
247 CMessageBox::Show(NULL
,_T("Subproject Diff Format error") ,_T("TortoiseGit"),MB_OK
|MB_ICONERROR
);
250 newhash
= output
.Mid(newstart
+ CString(_T("+Subproject commit")).GetLength()+1,40);
251 dirty
= output
.Mid(newstart
+ CString(_T("+Subproject commit")).GetLength() + 41) == _T("-dirty\n");
255 cmd
.Format(_T("git.exe diff-tree -r -z %s %s -- \"%s\""),
256 rev2
,rev1
,pPath
->GetGitPathString());
258 BYTE_VECTOR bytes
, errBytes
;
259 if(g_Git
.Run(cmd
, &bytes
, &errBytes
))
262 g_Git
.StringAppend(&err
, &errBytes
[0], CP_UTF8
);
263 CMessageBox::Show(NULL
,err
,_T("TortoiseGit"),MB_OK
|MB_ICONERROR
);
267 if (bytes
.size() < 15 + 41 + 40)
269 CMessageBox::Show(NULL
, _T("git diff-tree gives invalid output"), _T("TortoiseGit"), MB_OK
| MB_ICONERROR
);
272 g_Git
.StringAppend(&oldhash
, &bytes
[15], CP_UTF8
, 40);
273 g_Git
.StringAppend(&newhash
, &bytes
[15+41], CP_UTF8
, 40);
279 bool oldOK
= false, newOK
= false;
282 subgit
.m_CurrentDir
= g_Git
.CombinePath(pPath
);
283 CSubmoduleDiffDlg::ChangeType changeType
= CSubmoduleDiffDlg::Unknown
;
285 if(pPath
->HasAdminDir())
287 int encode
=CAppUtils::GetLogOutputEncode(&subgit
);
288 int oldTime
= 0, newTime
= 0;
290 if(oldhash
!= GIT_REV_ZERO
)
292 CString cmdout
, cmderr
;
293 cmd
.Format(_T("git.exe log -n1 --pretty=format:\"%%ct %%s\" %s --"), oldhash
);
294 oldOK
= !subgit
.Run(cmd
, &cmdout
, &cmderr
, encode
);
297 int pos
= cmdout
.Find(_T(" "));
298 oldTime
= _ttoi(cmdout
.Left(pos
));
299 oldsub
= cmdout
.Mid(pos
+ 1);
304 if (newhash
!= GIT_REV_ZERO
)
306 CString cmdout
, cmderr
;
307 cmd
.Format(_T("git.exe log -n1 --pretty=format:\"%%ct %%s\" %s --"), newhash
);
308 newOK
= !subgit
.Run(cmd
, &cmdout
, &cmderr
, encode
);
311 int pos
= cmdout
.Find(_T(" "));
312 newTime
= _ttoi(cmdout
.Left(pos
));
313 newsub
= cmdout
.Mid(pos
+ 1);
319 if (oldhash
== GIT_REV_ZERO
)
322 changeType
= CSubmoduleDiffDlg::NewSubmodule
;
324 else if (newhash
== GIT_REV_ZERO
)
327 changeType
= CSubmoduleDiffDlg::DeleteSubmodule
;
329 else if (oldhash
!= newhash
)
331 bool ffNewer
= false, ffOlder
= false;
332 ffNewer
= subgit
.IsFastForward(oldhash
, newhash
);
335 ffOlder
= subgit
.IsFastForward(newhash
, oldhash
);
338 if (newTime
> oldTime
)
339 changeType
= CSubmoduleDiffDlg::NewerTime
;
340 else if (newTime
< oldTime
)
341 changeType
= CSubmoduleDiffDlg::OlderTime
;
343 changeType
= CSubmoduleDiffDlg::SameTime
;
346 changeType
= CSubmoduleDiffDlg::Rewind
;
349 changeType
= CSubmoduleDiffDlg::FastForward
;
353 if (!oldOK
|| !newOK
)
354 changeType
= CSubmoduleDiffDlg::Unknown
;
356 CSubmoduleDiffDlg submoduleDiffDlg
;
357 submoduleDiffDlg
.SetDiff(pPath
->GetWinPath(), isWorkingCopy
, oldhash
, oldsub
, oldOK
, newhash
, newsub
, newOK
, dirty
, changeType
);
358 submoduleDiffDlg
.DoModal();
359 if (submoduleDiffDlg
.IsRefresh())
365 int CGitDiff::Diff(const CTGitPath
* pPath
, const CTGitPath
* pPath2
, git_revnum_t rev1
, git_revnum_t rev2
, bool /*blame*/, bool /*unified*/, int jumpToLine
)
368 GetTempPath(temppath
);
370 // make sure we have HASHes here, otherwise filenames might be invalid
371 if (rev1
!= GIT_REV_ZERO
)
374 if (g_Git
.GetHash(rev1Hash
, rev1
))
376 MessageBox(NULL
, g_Git
.GetGitLastErr(_T("Could not get hash of \"") + rev1
+ _T("\".")), _T("TortoiseGit"), MB_ICONERROR
);
379 rev1
= rev1Hash
.ToString();
381 if (rev2
!= GIT_REV_ZERO
)
384 if (g_Git
.GetHash(rev2Hash
, rev2
))
386 MessageBox(NULL
, g_Git
.GetGitLastErr(_T("Could not get hash of \"") + rev2
+ _T("\".")), _T("TortoiseGit"), MB_ICONERROR
);
389 rev2
= rev2Hash
.ToString();
396 if(pPath
->IsDirectory() || pPath2
->IsDirectory())
399 // refresh if result = 1
400 CTGitPath path
= *pPath
;
401 CTGitPath path2
= *pPath2
;
402 while ((result
= SubmoduleDiff(&path
, &path2
, rev1
, rev2
)) == 1)
404 path
.SetFromGit(pPath
->GetGitPathString());
405 path2
.SetFromGit(pPath2
->GetGitPathString());
410 if(rev1
!= GIT_REV_ZERO
)
412 TCHAR szTempName
[MAX_PATH
] = {0};
413 GetTempFileName(temppath
, pPath
->GetBaseFilename(), 0, szTempName
);
414 CString
temp(szTempName
);
415 DeleteFile(szTempName
);
416 CreateDirectory(szTempName
, NULL
);
417 // use original file extension, an external diff tool might need it
418 file1
.Format(_T("%s\\%s-%s-right%s"),
420 pPath
->GetBaseFilename(),
421 rev1
.Left(g_Git
.GetShortHASHLength()),
422 pPath
->GetFileExtension());
423 title1
= pPath
->GetFileOrDirectoryName() + _T(":") + rev1
.Left(g_Git
.GetShortHASHLength());
424 if (g_Git
.GetOneFile(rev1
, *pPath
, file1
))
427 out
.Format(IDS_STATUSLIST_CHECKOUTFILEFAILED
, pPath
->GetGitPathString(), rev1
, file1
);
428 CMessageBox::Show(nullptr, g_Git
.GetGitLastErr(out
, CGit::GIT_CMD_GETONEFILE
), _T("TortoiseGit"), MB_OK
);
431 ::SetFileAttributes(file1
, FILE_ATTRIBUTE_READONLY
);
435 file1
= g_Git
.CombinePath(pPath
);
436 title1
.Format( IDS_DIFF_WCNAME
, pPath
->GetFileOrDirectoryName() );
437 if (!PathFileExists(file1
))
440 sMsg
.Format(IDS_PROC_DIFFERROR_FILENOTINWORKINGTREE
, file1
);
441 if (MessageBox(NULL
, sMsg
, _T("TortoiseGit"), MB_ICONEXCLAMATION
| MB_YESNO
) == IDNO
)
443 if (!CCommonAppUtils::FileOpenSave(file1
, NULL
, IDS_SELECTFILE
, IDS_COMMONFILEFILTER
, true))
445 title1
.Format(IDS_DIFF_WCNAME
, CTGitPath(file1
).GetUIFileOrDirectoryName());
451 if(rev2
!= GIT_REV_ZERO
)
453 TCHAR szTempName
[MAX_PATH
] = {0};
454 GetTempFileName(temppath
, pPath2
->GetBaseFilename(), 0, szTempName
);
455 CString
temp(szTempName
);
456 DeleteFile(szTempName
);
457 CreateDirectory(szTempName
, NULL
);
458 CTGitPath fileName
= *pPath2
;
459 if (pPath2
->m_Action
& CTGitPath::LOGACTIONS_REPLACED
)
460 fileName
= CTGitPath(pPath2
->GetGitOldPathString());
462 // use original file extension, an external diff tool might need it
463 file2
.Format(_T("%s\\%s-%s-left%s"),
465 fileName
.GetBaseFilename(),
466 rev2
.Left(g_Git
.GetShortHASHLength()),
467 fileName
.GetFileExtension());
468 title2
= fileName
.GetFileOrDirectoryName() + _T(":") + rev2
.Left(g_Git
.GetShortHASHLength());
469 if (g_Git
.GetOneFile(rev2
, fileName
, file2
))
472 out
.Format(IDS_STATUSLIST_CHECKOUTFILEFAILED
, pPath
->GetGitPathString(), rev2
, file2
);
473 CMessageBox::Show(nullptr, g_Git
.GetGitLastErr(out
, CGit::GIT_CMD_GETONEFILE
), _T("TortoiseGit"), MB_OK
);
476 ::SetFileAttributes(file2
, FILE_ATTRIBUTE_READONLY
);
480 file2
= g_Git
.CombinePath(pPath2
);
481 title2
.Format( IDS_DIFF_WCNAME
, pPath2
->GetFileOrDirectoryName() );
484 if (pPath
->m_Action
== pPath
->LOGACTIONS_ADDED
)
486 CGitDiff::DiffNull(pPath
, rev1
, true, jumpToLine
);
488 else if (pPath
->m_Action
== pPath
->LOGACTIONS_DELETED
)
490 CGitDiff::DiffNull(pPath
, rev2
, false, jumpToLine
);
494 CAppUtils::DiffFlags flags
;
495 CAppUtils::StartExtDiff(file2
,file1
,
498 g_Git
.CombinePath(pPath2
),
499 g_Git
.CombinePath(pPath
),
507 int CGitDiff::DiffCommit(const CTGitPath
&path
, const GitRev
*r1
, const GitRev
*r2
)
509 return DiffCommit(path
, path
, r1
, r2
);
512 int CGitDiff::DiffCommit(const CTGitPath
&path1
, const CTGitPath
&path2
, const GitRev
*r1
, const GitRev
*r2
)
514 if (path1
.GetWinPathString().IsEmpty())
517 dlg
.SetDiff(NULL
, *r1
, *r2
);
520 else if (path1
.IsDirectory())
523 dlg
.SetDiff(&path1
, *r1
, *r2
);
528 Diff(&path1
, &path2
, r1
->m_CommitHash
.ToString(), r2
->m_CommitHash
.ToString());
533 int CGitDiff::DiffCommit(const CTGitPath
&path
, const CString
&r1
, const CString
&r2
)
535 return DiffCommit(path
, path
, r1
, r2
);
539 int CGitDiff::DiffCommit(const CTGitPath
&path1
, const CTGitPath
&path2
, const CString
&r1
, const CString
&r2
)
541 if (path1
.GetWinPathString().IsEmpty())
544 dlg
.SetDiff(NULL
, r1
, r2
);
547 else if (path1
.IsDirectory())
550 dlg
.SetDiff(&path1
, r1
, r2
);
555 Diff(&path1
, &path2
, r1
, r2
);