TGitBlame: Dot try to add non-existing files to MRU list
[TortoiseGit.git] / src / TortoiseGitBlame / TortoiseGitBlameDoc.cpp
blob238c933500189c4bab54dd1fdc77a9e46958855f
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2012 - 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 "MessageBox.h"
30 #include "Git.h"
31 #include "MainFrm.h"
32 #include "TGitPath.h"
33 #include "TortoiseGitBlameView.h"
34 #include "CmdLineParser.h"
35 #include "CommonAppUtils.h"
37 #ifdef _DEBUG
38 #define new DEBUG_NEW
39 #endif
42 // CTortoiseGitBlameDoc
44 IMPLEMENT_DYNCREATE(CTortoiseGitBlameDoc, CDocument)
46 BEGIN_MESSAGE_MAP(CTortoiseGitBlameDoc, CDocument)
47 END_MESSAGE_MAP()
50 // CTortoiseGitBlameDoc construction/destruction
52 CTortoiseGitBlameDoc::CTortoiseGitBlameDoc()
54 m_bFirstStartup = true;
55 m_IsGitFile = FALSE;
58 CTortoiseGitBlameDoc::~CTortoiseGitBlameDoc()
62 BOOL CTortoiseGitBlameDoc::OnNewDocument()
64 return TRUE;
66 BOOL CTortoiseGitBlameDoc::OnOpenDocument(LPCTSTR lpszPathName)
68 CCmdLineParser parser(AfxGetApp()->m_lpCmdLine);
69 if(parser.HasVal(_T("rev")) && m_bFirstStartup)
71 m_Rev=parser.GetVal(_T("rev"));
72 m_bFirstStartup = false;
74 else
76 m_Rev.Empty();
79 return OnOpenDocument(lpszPathName,m_Rev);
82 BOOL CTortoiseGitBlameDoc::OnOpenDocument(LPCTSTR lpszPathName,CString Rev)
84 if(Rev.IsEmpty())
85 Rev = _T("HEAD");
87 // enable blame for files which do not exist in current working tree
88 if (!PathFileExists(lpszPathName) && Rev != _T("HEAD"))
90 if (!CDocument::OnOpenDocument(GetTempFile()))
91 return FALSE;
93 else
95 if (!CDocument::OnOpenDocument(lpszPathName))
96 return FALSE;
99 m_CurrentFileName = lpszPathName;
101 m_Rev=Rev;
103 // (SDI documents will reuse this document)
104 if(!g_Git.CheckMsysGitDir())
106 CCommonAppUtils::RunTortoiseGitProc(_T(" /command:settings"));
107 return FALSE;
109 GitAdminDir admindir;
110 CString topdir;
111 if(!admindir.HasAdminDir(m_CurrentFileName, &topdir))
113 CString temp;
114 temp.Format(IDS_CANNOTBLAMENOGIT, CString(m_CurrentFileName));
115 CMessageBox::Show(NULL, temp, _T("TortoiseGitBlame"), MB_OK);
117 else
119 GitAdminDir lastAdminDir;
120 CString oldTopDir;
121 if (topdir != g_Git.m_CurrentDir && CTGitPath(g_Git.m_CurrentDir).HasAdminDir(&oldTopDir) && oldTopDir != topdir)
123 CString sMsg;
124 sMsg.Format(IDS_ERR_DIFFENERTPREPO, oldTopDir, topdir);
125 MessageBox(NULL, sMsg, _T("TortoiseGitBlame"), MB_OK | MB_ICONERROR);
126 return FALSE;
129 m_IsGitFile=TRUE;
130 sOrigCWD = g_Git.m_CurrentDir = topdir;
132 CString PathName = m_CurrentFileName;
133 if(topdir[topdir.GetLength()-1] == _T('\\') ||
134 topdir[topdir.GetLength()-1] == _T('/'))
135 PathName=PathName.Right(PathName.GetLength()-g_Git.m_CurrentDir.GetLength());
136 else
137 PathName=PathName.Right(PathName.GetLength()-g_Git.m_CurrentDir.GetLength()-1);
139 CTGitPath path;
140 path.SetFromWin(PathName);
142 if(!g_Git.m_CurrentDir.IsEmpty())
143 SetCurrentDirectory(g_Git.m_CurrentDir);
145 m_GitPath = path;
146 if (GetMainFrame()->m_wndOutput.LoadHistory(path.GetGitPathString(), m_Rev, (theApp.GetInt(_T("FollowRenames"), 0) == 1)))
147 return FALSE;
149 CString cmd;
151 cmd.Format(_T("git.exe blame -s -l %s -- \"%s\""),Rev,path.GetGitPathString());
152 m_BlameData.clear();
153 BYTE_VECTOR err;
154 if(g_Git.Run(cmd, &m_BlameData, &err))
156 CString str;
157 if (!m_BlameData.empty())
158 g_Git.StringAppend(&str, &m_BlameData[0], CP_UTF8);
159 if (!err.empty())
160 g_Git.StringAppend(&str, &err[0], CP_UTF8);
161 MessageBox(NULL, CString(MAKEINTRESOURCE(IDS_BLAMEERROR)) + _T("\n\n") + str, _T("TortoiseGitBlame"), MB_OK);
163 return FALSE;
166 if(!m_TempFileName.IsEmpty())
168 ::DeleteFile(m_TempFileName);
169 m_TempFileName.Empty();
172 m_TempFileName=GetTempFile();
174 if(Rev.IsEmpty())
175 Rev=_T("HEAD");
177 cmd.Format(_T("git.exe cat-file blob %s:\"%s\""),Rev,path.GetGitPathString());
179 if(g_Git.RunLogFile(cmd, m_TempFileName))
181 CString str;
182 str.Format(IDS_CHECKOUTFAILED, path.GetGitPathString());
183 MessageBox(NULL, CString(MAKEINTRESOURCE(IDS_BLAMEERROR)) + _T("\n\n") + str, _T("TortoiseGitBlame"), MB_OK);
186 CTortoiseGitBlameView *pView=DYNAMIC_DOWNCAST(CTortoiseGitBlameView,GetMainFrame()->GetActiveView());
187 if(pView == NULL)
189 CWnd* pWnd = GetMainFrame()->GetDescendantWindow(AFX_IDW_PANE_FIRST, TRUE);
190 if (pWnd != NULL && pWnd->IsKindOf(RUNTIME_CLASS(CTortoiseGitBlameView)))
192 pView = (CTortoiseGitBlameView*)pWnd;
194 else
196 return FALSE;
199 pView->UpdateInfo();
202 return TRUE;
205 void CTortoiseGitBlameDoc::SetPathName(LPCTSTR lpszPathName, BOOL bAddToMRU)
207 CDocument::SetPathName(lpszPathName, bAddToMRU && (m_Rev == _T("HEAD")));
209 CString title;
210 if(m_Rev.IsEmpty())
211 title=CString(lpszPathName)+_T(":HEAD");
212 else
213 title=CString(lpszPathName)+_T(":")+m_Rev;
215 this->SetTitle(title);
218 // CTortoiseGitBlameDoc serialization
220 void CTortoiseGitBlameDoc::Serialize(CArchive& ar)
222 if (ar.IsStoring())
224 // TODO: add storing code here
226 else
228 // TODO: add loading code here
233 // CTortoiseGitBlameDoc diagnostics
235 #ifdef _DEBUG
236 void CTortoiseGitBlameDoc::AssertValid() const
238 CDocument::AssertValid();
241 void CTortoiseGitBlameDoc::Dump(CDumpContext& dc) const
243 CDocument::Dump(dc);
245 #endif //_DEBUG
248 // CTortoiseGitBlameDoc commands