Use libgit2 for cat command
[TortoiseGit.git] / src / TortoiseProc / Commands / CatCommand.cpp
blobd9e582e6b80e836e0bc18cdfe0a8c71dabd84ace
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.
19 #include "stdafx.h"
20 #include "CatCommand.h"
21 #include "UnicodeUtils.h"
22 #include "PathUtils.h"
23 #include "Git.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);
38 return false;
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);
47 return false;
50 if (git_object_type(obj) == GIT_OBJ_BLOB)
52 FILE *file = nullptr;
53 _tfopen_s(&file, savepath, _T("w"));
54 if (file == nullptr)
56 ::DeleteFile(savepath);
57 CMessageBox::Show(hwndExplorer, L"Could not open file for writing.", L"TortoiseGit", MB_ICONERROR);
58 git_object_free(obj);
59 git_repository_free(repo);
60 return false;
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);
67 fclose(file);
68 git_object_free(obj);
69 git_repository_free(repo);
70 return false;
72 fclose(file);
73 git_object_free(obj);
74 git_repository_free(repo);
75 return true;
78 git_object_free(obj);
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);
84 return false;
86 return true;
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);
96 return false;
99 if(output.Find(_T("blob")) == 0)
101 cmd.Format(_T("git.exe cat-file -p %s"),revision);
103 else
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);
112 return false;
114 return true;