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();
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(CTGitPath
*pPath
, git_revnum_t rev1
,bool bIsAdd
)
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 while ((result
= SubmoduleDiffNull(pPath
, rev1
)) == 1) ;
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"),
131 pPath
->GetBaseFilename(),
132 rev1
.Left(g_Git
.GetShortHASHLength()),
133 pPath
->GetFileExtension());
135 g_Git
.GetOneFile(rev1
,*pPath
,file1
);
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();
147 ::SetFileAttributes(tempfile
, FILE_ATTRIBUTE_READONLY
);
149 CAppUtils::DiffFlags flags
;
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
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
)
169 int CGitDiff::SubmoduleDiff(CTGitPath
* pPath
,CTGitPath
* /*pPath2*/, git_revnum_t rev1
, git_revnum_t rev2
, bool /*blame*/, bool /*unified*/)
175 bool isWorkingCopy
= false;
176 if( rev2
== GIT_REV_ZERO
|| rev1
== GIT_REV_ZERO
)
178 oldhash
= GIT_REV_ZERO
;
179 newhash
= GIT_REV_ZERO
;
182 if( rev2
!= GIT_REV_ZERO
)
184 if( rev1
!= GIT_REV_ZERO
)
187 isWorkingCopy
= true;
189 cmd
.Format(_T("git.exe diff %s -- \"%s\""),
190 rev
,pPath
->GetGitPathString());
193 if (g_Git
.Run(cmd
, &output
, &err
, CP_UTF8
))
195 CMessageBox::Show(NULL
, output
+ L
"\n" + err
, _T("TortoiseGit"), MB_OK
|MB_ICONERROR
);
199 if (output
.IsEmpty())
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
);
211 if (output
.IsEmpty())
213 CMessageBox::Show(NULL
, CString(MAKEINTRESOURCE(IDS_ERR_EMPTYDIFF
)), _T("TortoiseGit"), MB_OK
| MB_ICONERROR
);
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)
219 sCmd
.Format(_T("/command:subupdate /bkpath:\"%s\""), g_Git
.m_CurrentDir
);
220 CAppUtils::RunTortoiseGitProc(sCmd
);
226 int oldstart
= output
.Find(_T("-Subproject commit"),start
);
229 CMessageBox::Show(NULL
,_T("Subproject Diff Format error") ,_T("TortoiseGit"),MB_OK
|MB_ICONERROR
);
232 oldhash
= output
.Mid(oldstart
+ CString(_T("-Subproject commit")).GetLength()+1,40);
234 int newstart
= output
.Find(_T("+Subproject commit"),start
);
237 CMessageBox::Show(NULL
,_T("Subproject Diff Format error") ,_T("TortoiseGit"),MB_OK
|MB_ICONERROR
);
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");
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
))
252 g_Git
.StringAppend(&err
, &errBytes
[0], CP_UTF8
);
253 CMessageBox::Show(NULL
,err
,_T("TortoiseGit"),MB_OK
|MB_ICONERROR
);
257 g_Git
.StringAppend(&oldhash
, &bytes
[15], CP_UTF8
, 40);
258 g_Git
.StringAppend(&newhash
, &bytes
[15+41], CP_UTF8
, 40);
264 bool oldOK
= false, newOK
= false;
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
);
282 int pos
= cmdout
.Find(_T(" "));
283 oldTime
= _ttoi(cmdout
.Left(pos
));
284 oldsub
= cmdout
.Mid(pos
+ 1);
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
);
296 int pos
= cmdout
.Find(_T(" "));
297 newTime
= _ttoi(cmdout
.Left(pos
));
298 newsub
= cmdout
.Mid(pos
+ 1);
304 if (oldhash
== GIT_REV_ZERO
)
307 changeType
= CSubmoduleDiffDlg::NewSubmodule
;
309 else if (newhash
== GIT_REV_ZERO
)
312 changeType
= CSubmoduleDiffDlg::DeleteSubmodule
;
314 else if (oldhash
!= newhash
)
316 bool ffNewer
= false, ffOlder
= false;
317 ffNewer
= subgit
.IsFastForward(oldhash
, newhash
);
320 ffOlder
= subgit
.IsFastForward(newhash
, oldhash
);
323 if (newTime
> oldTime
)
324 changeType
= CSubmoduleDiffDlg::NewerTime
;
325 else if (newTime
< oldTime
)
326 changeType
= CSubmoduleDiffDlg::OlderTime
;
328 changeType
= CSubmoduleDiffDlg::SameTime
;
331 changeType
= CSubmoduleDiffDlg::Rewind
;
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())
350 int CGitDiff::Diff(CTGitPath
* pPath
,CTGitPath
* pPath2
, git_revnum_t rev1
, git_revnum_t rev2
, bool /*blame*/, bool /*unified*/)
353 GetTempPath(temppath
);
355 // make sure we have HASHes here, otherwise filenames might be invalid
356 if (rev1
!= GIT_REV_ZERO
)
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
);
364 rev1
= rev1Hash
.ToString();
366 if (rev2
!= GIT_REV_ZERO
)
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
);
374 rev2
= rev2Hash
.ToString();
381 if(pPath
->IsDirectory() || pPath2
->IsDirectory())
384 // refresh if result = 1
385 while ((result
= SubmoduleDiff(pPath
, pPath2
, rev1
, rev2
)) == 1) ;
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"),
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
);
408 file1
=g_Git
.m_CurrentDir
+_T("\\")+pPath
->GetWinPathString();
409 title1
.Format( IDS_DIFF_WCNAME
, pPath
->GetFileOrDirectoryName() );
410 if (!PathFileExists(file1
))
413 sMsg
.Format(IDS_PROC_DIFFERROR_FILENOTINWORKINGTREE
, file1
);
414 if (MessageBox(NULL
, sMsg
, _T("TortoiseGit"), MB_ICONEXCLAMATION
| MB_YESNO
) == IDNO
)
416 if (!CCommonAppUtils::FileOpenSave(file1
, NULL
, IDS_DIFF_WCNAME
, IDS_COMMONFILEFILTER
, true))
418 title1
.Format(IDS_DIFF_WCNAME
, CTGitPath(file1
).GetUIFileOrDirectoryName());
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"),
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
);
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);
461 CAppUtils::DiffFlags flags
;
462 CAppUtils::StartExtDiff(file2
,file1
,
465 g_Git
.m_CurrentDir
+ _T("\\") + pPath2
->GetWinPathString(),
466 g_Git
.m_CurrentDir
+ _T("\\") + pPath
->GetWinPathString(),
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())
484 dlg
.SetDiff(NULL
, *r1
, *r2
);
487 else if (path1
.IsDirectory())
490 dlg
.SetDiff(&path1
, *r1
, *r2
);
495 Diff(&path1
, &path2
, r1
->m_CommitHash
.ToString(), r2
->m_CommitHash
.ToString());
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())
511 dlg
.SetDiff(NULL
, r1
, r2
);
514 else if (path1
.IsDirectory())
517 dlg
.SetDiff(&path1
, r1
, r2
);
522 Diff(&path1
, &path2
, r1
, r2
);