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.
21 #include "DiffCommand.h"
22 #include "PathUtils.h"
24 #include "ChangedDlg.h"
26 #include "../TGitCache/CacheInterface.h"
27 #include "UnicodeUtils.h"
29 bool DiffCommand::Execute()
32 CString path2
= CPathUtils::GetLongPathname(parser
.GetVal(L
"path2"));
33 bool bAlternativeTool
= !!parser
.HasKey(L
"alternative");
34 // bool bBlame = !!parser.HasKey(L"blame");
37 if (this->orgCmdLinePath
.IsDirectory())
40 dlg
.m_pathList
= CTGitPathList(cmdLinePath
);
46 if (cmdLinePath
.IsEmpty())
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
"startrev"), cmdLinePath
, parser
.GetVal(L
"endrev"), bAlternativeTool
);
54 bRet
= !!CGitDiff::Diff(&cmdLinePath
, &cmdLinePath
, parser
.GetVal(L
"endrev"), parser
.GetVal(L
"startrev"), false, parser
.HasKey(L
"unified") == TRUE
, parser
.GetLongVal(L
"line"), bAlternativeTool
, false);
58 // check if it is a newly added (but uncommitted) file
59 unsigned int status_flags
= 0;
60 CAutoRepository
repo(g_Git
.GetGitRepository());
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
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
);
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
);
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
);
99 bRet
= !!CGitDiff::Diff(&cmdLinePath
, &cmdLinePath
, GIT_REV_ZERO
, L
"HEAD", false, parser
.HasKey(L
"unified") == TRUE
, parser
.GetLongVal(L
"line"), bAlternativeTool
);
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
"endrev"), parser
.GetVal(L
"startrev"), false, parser
.HasKey(L
"unified") == TRUE
, parser
.GetLongVal(L
"line"), bAlternativeTool
);
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"));