Fixed issue #1499: Allow to show log if current HEAD and selected ref is orphan branc...
[TortoiseGit.git] / src / TortoiseProc / GitDiff.cpp
bloba4e07e6c9e89df37af9ba89348357e92a872ed68
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2003-2008 - TortoiseSVN
4 // Copyright (C) 2008-2012 - 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 CString dirtyList;
74 subgit.Run(_T("git.exe status --porcelain"), &dirtyList, encode);
75 dirty = !dirtyList.IsEmpty();
77 CSubmoduleDiffDlg submoduleDiffDlg;
78 submoduleDiffDlg.SetDiff(pPath->GetWinPath(), false, oldhash, oldsub, true, newhash, newsub, toOK, dirty);
79 submoduleDiffDlg.DoModal();
81 return 0;
84 if (rev1 != GIT_REV_ZERO)
85 CMessageBox::Show(NULL, _T("ls-tree output format error"), _T("TortoiseGit"), MB_OK | MB_ICONERROR);
86 else
87 CMessageBox::Show(NULL, _T("ls-files output format error"), _T("TortoiseGit"), MB_OK | MB_ICONERROR);
88 return -1;
91 int CGitDiff::DiffNull(CTGitPath *pPath, git_revnum_t rev1,bool bIsAdd)
93 CString temppath;
94 GetTempPath(temppath);
95 rev1 = g_Git.GetHash(rev1); // make sure we have a HASH here, otherwise filenames might be invalid
96 CString file1;
97 CString nullfile;
98 CString cmd;
100 if(pPath->IsDirectory())
102 git_revnum_t rev2;
103 return SubmoduleDiffNull(pPath,rev1);
106 if(rev1 != GIT_REV_ZERO )
108 TCHAR szTempName[MAX_PATH];
109 GetTempFileName(temppath, pPath->GetBaseFilename(), 0, szTempName);
110 CString temp(szTempName);
111 DeleteFile(szTempName);
112 CreateDirectory(szTempName, NULL);
113 file1.Format(_T("%s\\%s-%s%s"),
114 temp,
115 pPath->GetBaseFilename(),
116 rev1.Left(g_Git.GetShortHASHLength()),
117 pPath->GetFileExtension());
119 g_Git.GetOneFile(rev1,*pPath,file1);
121 else
123 file1=g_Git.m_CurrentDir+_T("\\")+pPath->GetWinPathString();
126 // preserve FileExtension, needed especially for diffing deleted images (detection on new filename extension)
127 CString tempfile=::GetTempFile() + pPath->GetFileExtension();
128 CStdioFile file(tempfile,CFile::modeReadWrite|CFile::modeCreate );
129 //file.WriteString();
130 file.Close();
131 ::SetFileAttributes(tempfile, FILE_ATTRIBUTE_READONLY);
133 CAppUtils::DiffFlags flags;
135 if(bIsAdd)
136 CAppUtils::StartExtDiff(tempfile,file1,
137 pPath->GetGitPathString(),
138 pPath->GetGitPathString() + _T(":") + rev1.Left(g_Git.GetShortHASHLength())
139 ,flags);
140 else
141 CAppUtils::StartExtDiff(file1,tempfile,
142 pPath->GetGitPathString() + _T(":") + rev1.Left(g_Git.GetShortHASHLength())
143 ,pPath->GetGitPathString(),flags);
145 return 0;
148 int CGitDiff::SubmoduleDiff(CTGitPath * pPath,CTGitPath * /*pPath2*/, git_revnum_t rev1, git_revnum_t rev2, bool /*blame*/, bool /*unified*/)
150 CString oldhash;
151 CString newhash;
152 bool dirty = false;
153 CString cmd;
154 bool isWorkingCopy = false;
155 if( rev2 == GIT_REV_ZERO || rev1 == GIT_REV_ZERO )
157 oldhash = GIT_REV_ZERO;
158 newhash = GIT_REV_ZERO;
160 CString rev;
161 if( rev2 != GIT_REV_ZERO )
162 rev = rev2;
163 if( rev1 != GIT_REV_ZERO )
164 rev = rev1;
166 isWorkingCopy = true;
168 cmd.Format(_T("git.exe diff %s -- \"%s\""),
169 rev,pPath->GetGitPathString());
171 CString output, err;
172 if (g_Git.Run(cmd, &output, &err, CP_UTF8))
174 CMessageBox::Show(NULL, output + L"\n" + err, _T("TortoiseGit"), MB_OK|MB_ICONERROR);
175 return -1;
177 int start =0;
178 int oldstart = output.Find(_T("-Subproject commit"),start);
179 if(oldstart<0)
181 CMessageBox::Show(NULL,_T("Subproject Diff Format error") ,_T("TortoiseGit"),MB_OK|MB_ICONERROR);
182 return -1;
184 oldhash = output.Mid(oldstart+ CString(_T("-Subproject commit")).GetLength()+1,40);
185 start = 0;
186 int newstart = output.Find(_T("+Subproject commit"),start);
187 if(oldstart<0)
189 CMessageBox::Show(NULL,_T("Subproject Diff Format error") ,_T("TortoiseGit"),MB_OK|MB_ICONERROR);
190 return -1;
192 newhash = output.Mid(newstart+ CString(_T("+Subproject commit")).GetLength()+1,40);
193 dirty = output.Mid(newstart + CString(_T("+Subproject commit")).GetLength() + 41) == _T("-dirty\n");
195 else
197 cmd.Format(_T("git.exe diff-tree -r -z %s %s -- \"%s\""),
198 rev2,rev1,pPath->GetGitPathString());
200 BYTE_VECTOR bytes, errBytes;
201 if(g_Git.Run(cmd, &bytes, &errBytes))
203 CString err;
204 g_Git.StringAppend(&err, &errBytes[0], CP_UTF8);
205 CMessageBox::Show(NULL,err,_T("TortoiseGit"),MB_OK|MB_ICONERROR);
206 return -1;
209 g_Git.StringAppend(&oldhash, &bytes[15], CP_UTF8, 40);
210 g_Git.StringAppend(&newhash, &bytes[15+41], CP_UTF8, 40);
214 CString oldsub;
215 CString newsub;
216 bool oldOK = false, newOK = false;
218 CGit subgit;
219 subgit.m_CurrentDir=g_Git.m_CurrentDir+_T("\\")+pPath->GetWinPathString();
221 if(pPath->HasAdminDir())
223 int encode=CAppUtils::GetLogOutputEncode(&subgit);
225 if(oldhash != GIT_REV_ZERO)
227 cmd.Format(_T("git log -n1 --pretty=format:\"%%s\" %s"),oldhash);
228 oldOK = !subgit.Run(cmd,&oldsub,encode);
230 if(newsub != GIT_REV_ZERO)
232 cmd.Format(_T("git log -n1 --pretty=format:\"%%s\" %s"),newhash);
233 newOK = !subgit.Run(cmd,&newsub,encode);
237 CSubmoduleDiffDlg submoduleDiffDlg;
238 submoduleDiffDlg.SetDiff(pPath->GetWinPath(), isWorkingCopy, oldhash, oldsub, oldOK, newhash, newsub, newOK, dirty);
239 submoduleDiffDlg.DoModal();
241 return 0;
244 int CGitDiff::Diff(CTGitPath * pPath,CTGitPath * pPath2, git_revnum_t rev1, git_revnum_t rev2, bool /*blame*/, bool /*unified*/)
246 CString temppath;
247 GetTempPath(temppath);
249 // make sure we have HASHes here, otherwise filenames might be invalid
250 rev1 = g_Git.GetHash(rev1);
251 rev2 = g_Git.GetHash(rev2);
253 CString file1;
254 CString title1;
255 CString cmd;
257 if(pPath->IsDirectory() || pPath2->IsDirectory())
259 return SubmoduleDiff(pPath,pPath2,rev1,rev2);
262 if(rev1 != GIT_REV_ZERO )
264 TCHAR szTempName[MAX_PATH];
265 GetTempFileName(temppath, pPath->GetBaseFilename(), 0, szTempName);
266 CString temp(szTempName);
267 DeleteFile(szTempName);
268 CreateDirectory(szTempName, NULL);
269 // use original file extension, an external diff tool might need it
270 file1.Format(_T("%s\\%s-%s-right%s"),
271 temp,
272 pPath->GetBaseFilename(),
273 rev1.Left(g_Git.GetShortHASHLength()),
274 pPath->GetFileExtension());
275 title1 = pPath->GetFileOrDirectoryName() + _T(":") + rev1.Left(g_Git.GetShortHASHLength());
276 g_Git.GetOneFile(rev1,*pPath,file1);
277 ::SetFileAttributes(file1, FILE_ATTRIBUTE_READONLY);
279 else
281 file1=g_Git.m_CurrentDir+_T("\\")+pPath->GetWinPathString();
282 title1.Format( IDS_DIFF_WCNAME, pPath->GetFileOrDirectoryName() );
283 if (!PathFileExists(file1))
285 CString sMsg;
286 sMsg.Format(IDS_PROC_DIFFERROR_FILENOTINWORKINGTREE, file1);
287 if (MessageBox(NULL, sMsg, _T("TortoiseGit"), MB_ICONEXCLAMATION | MB_OKCANCEL) == IDCANCEL)
288 return 1;
289 if (!CCommonAppUtils::FileOpenSave(file1, NULL, IDS_DIFF_WCNAME, IDS_COMMONFILEFILTER, true))
290 return 1;
291 title1.Format(IDS_DIFF_WCNAME, CTGitPath(file1).GetUIFileOrDirectoryName());
295 CString file2;
296 CString title2;
297 if(rev2 != GIT_REV_ZERO)
299 TCHAR szTempName[MAX_PATH];
300 GetTempFileName(temppath, pPath2->GetBaseFilename(), 0, szTempName);
301 CString temp(szTempName);
302 DeleteFile(szTempName);
303 CreateDirectory(szTempName, NULL);
304 CTGitPath fileName = *pPath2;
305 if (rev1 == GIT_REV_ZERO && pPath2->m_Action & CTGitPath::LOGACTIONS_REPLACED)
306 fileName = CTGitPath(pPath2->GetGitOldPathString());
308 // use original file extension, an external diff tool might need it
309 file2.Format(_T("%s\\%s-%s-left%s"),
310 temp,
311 fileName.GetBaseFilename(),
312 rev2.Left(g_Git.GetShortHASHLength()),
313 fileName.GetFileExtension());
314 title2 = fileName.GetFileOrDirectoryName() + _T(":") + rev2.Left(g_Git.GetShortHASHLength());
315 g_Git.GetOneFile(rev2, fileName, file2);
316 ::SetFileAttributes(file2, FILE_ATTRIBUTE_READONLY);
318 else
320 file2=g_Git.m_CurrentDir+_T("\\")+pPath2->GetWinPathString();
321 title2.Format( IDS_DIFF_WCNAME, pPath2->GetFileOrDirectoryName() );
324 if (pPath->m_Action == pPath->LOGACTIONS_ADDED)
326 CGitDiff::DiffNull(pPath, rev1, true);
328 else if (pPath->m_Action == pPath->LOGACTIONS_DELETED)
330 CGitDiff::DiffNull(pPath, rev2, false);
332 else
334 CAppUtils::DiffFlags flags;
335 CAppUtils::StartExtDiff(file2,file1,
336 title2,
337 title1
338 ,flags);
340 return 0;
343 int CGitDiff::DiffCommit(CTGitPath &path, GitRev *r1, GitRev *r2)
345 return DiffCommit(path, path, r1, r2);
348 int CGitDiff::DiffCommit(CTGitPath path1, CTGitPath path2, GitRev *r1, GitRev *r2)
350 if (path1.GetWinPathString().IsEmpty())
352 CFileDiffDlg dlg;
353 dlg.SetDiff(NULL, *r1, *r2);
354 dlg.DoModal();
356 else if (path1.IsDirectory())
358 CFileDiffDlg dlg;
359 dlg.SetDiff(&path1, *r1, *r2);
360 dlg.DoModal();
362 else
364 Diff(&path1, &path2, r1->m_CommitHash.ToString(), r2->m_CommitHash.ToString());
366 return 0;
369 int CGitDiff::DiffCommit(CTGitPath &path, CString r1, CString r2)
371 return DiffCommit(path, path, r1, r2);
375 int CGitDiff::DiffCommit(CTGitPath path1, CTGitPath path2, CString r1, CString r2)
377 if (path1.GetWinPathString().IsEmpty())
379 CFileDiffDlg dlg;
380 dlg.SetDiff(NULL, r1, r2);
381 dlg.DoModal();
383 else if (path1.IsDirectory())
385 CFileDiffDlg dlg;
386 dlg.SetDiff(&path1, r1, r2);
387 dlg.DoModal();
389 else
391 Diff(&path1, &path2, r1, r2);
393 return 0;