Use libgit2 to check whether a file is newly added or not
[TortoiseGit.git] / src / TortoiseProc / Commands / DiffCommand.cpp
blobb672e286a97093d199c07f1916b36228e5121297
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2007-2008 - TortoiseSVN
4 // Copyright (C) 2008-2017 - 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.
20 #include "stdafx.h"
21 #include "DiffCommand.h"
22 #include "PathUtils.h"
23 #include "AppUtils.h"
24 #include "ChangedDlg.h"
25 #include "GitDiff.h"
26 #include "../TGitCache/CacheInterface.h"
27 #include "UnicodeUtils.h"
29 bool DiffCommand::Execute()
31 bool bRet = false;
32 CString path2 = CPathUtils::GetLongPathname(parser.GetVal(L"path2"));
33 bool bAlternativeTool = !!parser.HasKey(L"alternative");
34 // bool bBlame = !!parser.HasKey(L"blame");
35 if (path2.IsEmpty())
37 if (this->orgCmdLinePath.IsDirectory())
39 CChangedDlg dlg;
40 dlg.m_pathList = CTGitPathList(cmdLinePath);
41 dlg.DoModal();
42 bRet = true;
44 else
46 if (cmdLinePath.IsEmpty())
47 return false;
48 //diff.SetAlternativeTool(bAlternativeTool);
49 if (parser.HasKey(L"startrev") && parser.HasKey(L"endrev"))
51 if (parser.HasKey(L"unified"))
52 bRet = !!CAppUtils::StartShowUnifiedDiff(nullptr, cmdLinePath, parser.GetVal(L"endrev"), cmdLinePath, parser.GetVal(L"startrev"), bAlternativeTool);
53 else
54 bRet = !!CGitDiff::Diff(&cmdLinePath, &cmdLinePath, parser.GetVal(L"startrev"), parser.GetVal(L"endrev"), false, parser.HasKey(L"unified") == TRUE, parser.GetLongVal(L"line"), bAlternativeTool);
56 else
58 // check if it is a newly added (but uncommitted) file
59 unsigned int status_flags = 0;
60 CAutoRepository repo(g_Git.GetGitRepository());
61 if (repo)
62 git_status_file(&status_flags, repo, CUnicodeUtils::GetUTF8(cmdLinePath.GetWinPathString()));
63 if (status_flags == GIT_STATUS_INDEX_NEW)
65 if (!g_Git.IsInitRepos())
67 // this might be a rename, try to find original name
68 BYTE_VECTOR cmdout;
69 g_Git.Run(L"git.exe diff-index --raw HEAD -M -C -z --", &cmdout);
70 CTGitPathList changedFiles;
71 changedFiles.ParserFromLog(cmdout);
72 for (int i = 0; i < changedFiles.GetCount(); ++i)
74 if (changedFiles[i].GetGitPathString() == cmdLinePath.GetGitPathString())
76 if (!changedFiles[i].GetGitOldPathString().IsEmpty())
78 CTGitPath oldPath(changedFiles[i].GetGitOldPathString());
79 if (parser.HasKey(L"unified"))
80 return !!CAppUtils::StartShowUnifiedDiff(nullptr, cmdLinePath, L"HEAD", cmdLinePath, GIT_REV_ZERO, bAlternativeTool);
81 return !!CGitDiff::Diff(&cmdLinePath, &oldPath, GIT_REV_ZERO, L"HEAD", false, parser.HasKey(L"unified") == TRUE, parser.GetLongVal(L"line"), bAlternativeTool);
83 break;
87 if (parser.HasKey(L"unified"))
89 cmdLinePath.m_Action = cmdLinePath.LOGACTIONS_ADDED;
90 return !!CAppUtils::StartShowUnifiedDiff(nullptr, cmdLinePath, L"HEAD", cmdLinePath, GIT_REV_ZERO, bAlternativeTool);
92 else
93 return !!CGitDiff::DiffNull(&cmdLinePath, GIT_REV_ZERO, true, parser.GetLongVal(L"line"), bAlternativeTool);
96 if (parser.HasKey(L"unified"))
97 bRet = !!CAppUtils::StartShowUnifiedDiff(nullptr, cmdLinePath, L"HEAD", cmdLinePath, GIT_REV_ZERO, bAlternativeTool);
98 else
99 bRet = !!CGitDiff::Diff(&cmdLinePath, &cmdLinePath, GIT_REV_ZERO, L"HEAD", false, parser.HasKey(L"unified") == TRUE, parser.GetLongVal(L"line"), bAlternativeTool);
103 else
105 if (parser.HasKey(L"startrev") && parser.HasKey(L"endrev") && CStringUtils::StartsWith(path2, g_Git.m_CurrentDir + L"\\"))
107 CTGitPath tgitPath2 = path2.Mid(g_Git.m_CurrentDir.GetLength() + 1);
108 bRet = !!CGitDiff::Diff(&tgitPath2, &cmdLinePath, parser.GetVal(L"startrev"), parser.GetVal(L"endrev"), false, parser.HasKey(L"unified") == TRUE, parser.GetLongVal(L"line"), bAlternativeTool);
110 else
112 bRet = CAppUtils::StartExtDiff(
113 path2, orgCmdLinePath.GetWinPathString(), CString(), CString(),
114 CString(), CString(), GIT_REV_ZERO, GIT_REV_ZERO,
115 CAppUtils::DiffFlags().AlternativeTool(bAlternativeTool), parser.GetLongVal(L"line"));
119 return bRet;