1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2009,2011-2014 - TortoiseGit
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software Foundation,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "CatCommand.h"
21 #include "UnicodeUtils.h"
22 #include "PathUtils.h"
24 #include "MessageBox.h"
26 bool CatCommand::Execute()
28 CString savepath
= CPathUtils::GetLongPathname(parser
.GetVal(_T("savepath")));
29 CString revision
= parser
.GetVal(_T("revision"));
31 if (g_Git
.UsingLibGit2(CGit::GIT_CMD_GETONEFILE
))
33 git_repository
*repo
= nullptr;
34 if (git_repository_open(&repo
, CUnicodeUtils::GetUTF8(CTGitPath(g_Git
.m_CurrentDir
).GetGitPathString())))
36 ::DeleteFile(savepath
);
37 CMessageBox::Show(hwndExplorer
, g_Git
.GetLibGit2LastErr(L
"Could not open repository."), L
"TortoiseGit", MB_ICONERROR
);
41 git_object
*obj
= nullptr;
42 if (git_revparse_single(&obj
, repo
, CUnicodeUtils::GetUTF8(revision
)))
44 ::DeleteFile(savepath
);
45 git_repository_free(repo
);
46 CMessageBox::Show(hwndExplorer
, g_Git
.GetLibGit2LastErr(L
"Could not parse revision."), L
"TortoiseGit", MB_ICONERROR
);
50 if (git_object_type(obj
) == GIT_OBJ_BLOB
)
53 _tfopen_s(&file
, savepath
, _T("w"));
56 ::DeleteFile(savepath
);
57 CMessageBox::Show(hwndExplorer
, L
"Could not open file for writing.", L
"TortoiseGit", MB_ICONERROR
);
59 git_repository_free(repo
);
62 if (fwrite(git_blob_rawcontent((const git_blob
*)obj
), 1, git_blob_rawsize((const git_blob
*)obj
), file
) != (size_t)git_blob_rawsize((const git_blob
*)obj
)) // TODO: need to apply git_blob_filtered_content?
64 ::DeleteFile(savepath
);
65 CString err
= CFormatMessageWrapper();
66 CMessageBox::Show(hwndExplorer
, _T("Could not write to file: ") + err
, L
"TortoiseGit", MB_ICONERROR
);
69 git_repository_free(repo
);
74 git_repository_free(repo
);
79 git_repository_free(repo
);
81 if (g_Git
.GetOneFile(revision
, cmdLinePath
, savepath
))
83 CMessageBox::Show(hwndExplorer
, g_Git
.GetGitLastErr(L
"Could get file.", CGit::GIT_CMD_GETONEFILE
), L
"TortoiseGit", MB_ICONERROR
);
89 CString cmd
, output
, err
;
90 cmd
.Format(_T("git.exe cat-file -t %s"),revision
);
92 if (g_Git
.Run(cmd
, &output
, &err
, CP_UTF8
))
94 ::DeleteFile(savepath
);
95 CMessageBox::Show(NULL
, output
+ L
"\n" + err
, _T("TortoiseGit"), MB_ICONERROR
);
99 if(output
.Find(_T("blob")) == 0)
101 cmd
.Format(_T("git.exe cat-file -p %s"),revision
);
105 cmd
.Format(_T("git.exe show %s -- \"%s\""),revision
,this->cmdLinePath
);
108 if (g_Git
.RunLogFile(cmd
, savepath
, &err
))
110 ::DeleteFile(savepath
);
111 CMessageBox::Show(hwndExplorer
, _T("Cat file failed:\n") + err
, _T("TortoiseGit"), MB_ICONERROR
);