Automatically remove tempfiles if not needed any more
[TortoiseGit.git] / src / TortoiseGitBlame / TortoiseGitBlameDoc.cpp
blob6f2047a235f641037e47fb2a10ed471b501df5d2
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2016 - 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.
21 // TortoiseGitBlameDoc.cpp : implementation of the CTortoiseGitBlameDoc class
24 #include "stdafx.h"
25 #include "TortoiseGitBlame.h"
27 #include "TortoiseGitBlameDoc.h"
28 #include "GitAdminDir.h"
29 #include "Git.h"
30 #include "MainFrm.h"
31 #include "TGitPath.h"
32 #include "TortoiseGitBlameView.h"
33 #include "CmdLineParser.h"
34 #include "CommonAppUtils.h"
35 #include "BlameDetectMovedOrCopiedLines.h"
36 #include "TempFile.h"
38 #ifdef _DEBUG
39 #define new DEBUG_NEW
40 #endif
43 // CTortoiseGitBlameDoc
45 IMPLEMENT_DYNCREATE(CTortoiseGitBlameDoc, CDocument)
47 BEGIN_MESSAGE_MAP(CTortoiseGitBlameDoc, CDocument)
48 END_MESSAGE_MAP()
51 // CTortoiseGitBlameDoc construction/destruction
53 CTortoiseGitBlameDoc::CTortoiseGitBlameDoc()
55 m_bFirstStartup = true;
56 m_IsGitFile = FALSE;
57 m_lLine = 1;
60 CTortoiseGitBlameDoc::~CTortoiseGitBlameDoc()
64 BOOL CTortoiseGitBlameDoc::OnNewDocument()
66 return TRUE;
68 BOOL CTortoiseGitBlameDoc::OnOpenDocument(LPCTSTR lpszPathName)
70 CCmdLineParser parser(AfxGetApp()->m_lpCmdLine);
71 if (m_bFirstStartup)
73 m_Rev = parser.GetVal(L"rev");
74 m_lLine = (int)parser.GetLongVal(L"line");
75 m_bFirstStartup = false;
77 else
79 m_Rev.Empty();
80 m_lLine = 1;
83 return OnOpenDocument(lpszPathName,m_Rev);
86 BOOL CTortoiseGitBlameDoc::OnOpenDocument(LPCTSTR lpszPathName,CString Rev)
88 if(Rev.IsEmpty())
89 Rev = L"HEAD";
91 // enable blame for files which do not exist in current working tree
92 if (!PathFileExists(lpszPathName) && Rev != L"HEAD")
94 if (!CDocument::OnOpenDocument(CTempFiles::Instance().GetTempFilePath(true).GetWinPathString()))
95 return FALSE;
97 else
99 if (!CDocument::OnOpenDocument(lpszPathName))
100 return FALSE;
103 m_CurrentFileName = lpszPathName;
105 m_Rev=Rev;
107 // (SDI documents will reuse this document)
108 if(!g_Git.CheckMsysGitDir())
110 CCommonAppUtils::RunTortoiseGitProc(L" /command:settings");
111 return FALSE;
113 CString topdir;
114 if (!GitAdminDir::HasAdminDir(m_CurrentFileName, &topdir))
116 CString temp;
117 temp.Format(IDS_CANNOTBLAMENOGIT, (LPCTSTR)m_CurrentFileName);
118 MessageBox(nullptr, temp, L"TortoiseGitBlame", MB_OK | MB_ICONERROR);
119 return FALSE;
121 else
123 m_IsGitFile=TRUE;
124 sOrigCWD = g_Git.m_CurrentDir = topdir;
126 CString PathName = m_CurrentFileName;
127 if (topdir[topdir.GetLength() - 1] == L'\\' || topdir[topdir.GetLength() - 1] == L'/')
128 PathName=PathName.Right(PathName.GetLength()-g_Git.m_CurrentDir.GetLength());
129 else
130 PathName=PathName.Right(PathName.GetLength()-g_Git.m_CurrentDir.GetLength()-1);
132 CTGitPath path;
133 path.SetFromWin(PathName);
135 if(!g_Git.m_CurrentDir.IsEmpty())
136 SetCurrentDirectory(g_Git.m_CurrentDir);
140 // make sure all config files are read in order to check that none contains an error
141 g_Git.GetConfigValue(L"doesnot.exist");
143 catch (char * libgiterr)
145 MessageBox(nullptr, CString(libgiterr), L"TortoiseGitBlame", MB_ICONERROR);
146 return FALSE;
149 CString cmd, option;
150 int dwDetectMovedOrCopiedLines = theApp.GetInt(L"DetectMovedOrCopiedLines", BLAME_DETECT_MOVED_OR_COPIED_LINES_DISABLED);
151 int dwDetectMovedOrCopiedLinesNumCharactersWithinFile = theApp.GetInt(L"DetectMovedOrCopiedLinesNumCharactersWithinFile", BLAME_DETECT_MOVED_OR_COPIED_LINES_NUM_CHARACTERS_WITHIN_FILE_DEFAULT);
152 int dwDetectMovedOrCopiedLinesNumCharactersFromFiles = theApp.GetInt(L"DetectMovedOrCopiedLinesNumCharactersFromFiles", BLAME_DETECT_MOVED_OR_COPIED_LINES_NUM_CHARACTERS_FROM_FILES_DEFAULT);
153 switch(dwDetectMovedOrCopiedLines)
155 default:
156 case BLAME_DETECT_MOVED_OR_COPIED_LINES_DISABLED:
157 option.Empty();
158 break;
159 case BLAME_DETECT_MOVED_OR_COPIED_LINES_WITHIN_FILE:
160 option.Format(L"-M%d", dwDetectMovedOrCopiedLinesNumCharactersWithinFile);
161 break;
162 case BLAME_DETECT_MOVED_OR_COPIED_LINES_FROM_MODIFIED_FILES:
163 option.Format(L"-C%d", dwDetectMovedOrCopiedLinesNumCharactersFromFiles);
164 break;
165 case BLAME_DETECT_MOVED_OR_COPIED_LINES_FROM_EXISTING_FILES_AT_FILE_CREATION:
166 option.Format(L"-C -C%d", dwDetectMovedOrCopiedLinesNumCharactersFromFiles);
167 break;
168 case BLAME_DETECT_MOVED_OR_COPIED_LINES_FROM_EXISTING_FILES:
169 option.Format(L"-C -C -C%d", dwDetectMovedOrCopiedLinesNumCharactersFromFiles);
170 break;
173 if (theApp.GetInt(L"IgnoreWhitespace", 0) == 1)
174 option += L" -w";
176 cmd.Format(L"git.exe blame -p %s %s -- \"%s\"", (LPCTSTR)option, (LPCTSTR)Rev, (LPCTSTR)path.GetGitPathString());
177 m_BlameData.clear();
178 BYTE_VECTOR err;
179 if(g_Git.Run(cmd, &m_BlameData, &err))
181 CString str;
182 if (!m_BlameData.empty())
183 CGit::StringAppend(&str, &m_BlameData[0], CP_UTF8);
184 if (!err.empty())
185 CGit::StringAppend(&str, &err[0], CP_UTF8);
186 MessageBox(nullptr, CString(MAKEINTRESOURCE(IDS_BLAMEERROR)) + L"\n\n" + str, L"TortoiseGitBlame", MB_OK | MB_ICONERROR);
188 return FALSE;
191 #ifdef USE_TEMPFILENAME
192 m_TempFileName = CTempFiles::Instance().GetTempFilePath(true).GetWinPathString();
194 cmd.Format(L"git.exe cat-file blob %s:\"%s\"", (LPCTSTR)Rev, (LPCTSTR)path.GetGitPathString());
196 if(g_Git.RunLogFile(cmd, m_TempFileName))
198 CString str;
199 str.Format(IDS_CHECKOUTFAILED, (LPCTSTR)path.GetGitPathString());
200 MessageBox(nullptr, CString(MAKEINTRESOURCE(IDS_BLAMEERROR)) + L"\n\n" + str, L"TortoiseGitBlame", MB_OK | MB_ICONERROR);
201 return FALSE;
203 #endif
204 m_GitPath = path;
206 CTortoiseGitBlameView *pView=DYNAMIC_DOWNCAST(CTortoiseGitBlameView,GetMainFrame()->GetActiveView());
207 if (!pView)
209 CWnd* pWnd = GetMainFrame()->GetDescendantWindow(AFX_IDW_PANE_FIRST, TRUE);
210 if (pWnd && pWnd->IsKindOf(RUNTIME_CLASS(CTortoiseGitBlameView)))
211 pView = static_cast<CTortoiseGitBlameView*>(pWnd);
212 else
213 return FALSE;
215 pView->ParseBlame();
217 BOOL bShowCompleteLog = (theApp.GetInt(L"ShowCompleteLog", 1) == 1);
218 if (bShowCompleteLog && BlameIsLimitedToOneFilename(dwDetectMovedOrCopiedLines))
220 if (GetMainFrame()->m_wndOutput.LoadHistory(path.GetGitPathString(), m_Rev, (theApp.GetInt(L"FollowRenames", 0) == 1)))
221 return FALSE;
223 else
225 std::set<CGitHash> hashes;
226 pView->m_data.GetHashes(hashes);
227 if (GetMainFrame()->m_wndOutput.LoadHistory(hashes))
228 return FALSE;
231 pView->MapLineToLogIndex();
232 pView->UpdateInfo();
233 if (m_lLine > 0)
234 pView->GotoLine(m_lLine);
236 SetPathName(m_CurrentFileName, FALSE);
239 return TRUE;
242 void CTortoiseGitBlameDoc::SetPathName(LPCTSTR lpszPathName, BOOL bAddToMRU)
244 CDocument::SetPathName(lpszPathName, bAddToMRU && (m_Rev == L"HEAD"));
246 this->SetTitle(CString(lpszPathName) + L':' + m_Rev);
249 // CTortoiseGitBlameDoc diagnostics
251 #ifdef _DEBUG
252 void CTortoiseGitBlameDoc::AssertValid() const
254 CDocument::AssertValid();
257 void CTortoiseGitBlameDoc::Dump(CDumpContext& dc) const
259 CDocument::Dump(dc);
261 #endif //_DEBUG
264 // CTortoiseGitBlameDoc commands