Fix diff-xls.js error reported from http://tortoisesvn.tigris.org/ds/viewMessage...
[TortoiseGit.git] / src / TortoiseGitBlame / TortoiseGitBlameDoc.cpp
blob279eff650a0da1156fc3b252c5cc0bb03b313a78
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2013 - 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;
56 m_lLine = 1;
59 CTortoiseGitBlameDoc::~CTortoiseGitBlameDoc()
63 BOOL CTortoiseGitBlameDoc::OnNewDocument()
65 return TRUE;
67 BOOL CTortoiseGitBlameDoc::OnOpenDocument(LPCTSTR lpszPathName)
69 CCmdLineParser parser(AfxGetApp()->m_lpCmdLine);
70 if (m_bFirstStartup)
72 m_Rev=parser.GetVal(_T("rev"));
73 m_lLine = parser.GetLongVal(_T("line"));
74 m_bFirstStartup = false;
76 else
78 m_Rev.Empty();
79 m_lLine = 1;
82 return OnOpenDocument(lpszPathName,m_Rev);
85 BOOL CTortoiseGitBlameDoc::OnOpenDocument(LPCTSTR lpszPathName,CString Rev)
87 if(Rev.IsEmpty())
88 Rev = _T("HEAD");
90 // enable blame for files which do not exist in current working tree
91 if (!PathFileExists(lpszPathName) && Rev != _T("HEAD"))
93 if (!CDocument::OnOpenDocument(GetTempFile()))
94 return FALSE;
96 else
98 if (!CDocument::OnOpenDocument(lpszPathName))
99 return FALSE;
102 m_CurrentFileName = lpszPathName;
104 m_Rev=Rev;
106 // (SDI documents will reuse this document)
107 if(!g_Git.CheckMsysGitDir())
109 CCommonAppUtils::RunTortoiseGitProc(_T(" /command:settings"));
110 return FALSE;
112 GitAdminDir admindir;
113 CString topdir;
114 if(!admindir.HasAdminDir(m_CurrentFileName, &topdir))
116 CString temp;
117 temp.Format(IDS_CANNOTBLAMENOGIT, CString(m_CurrentFileName));
118 CMessageBox::Show(NULL, temp, _T("TortoiseGitBlame"), MB_OK);
119 return FALSE;
121 else
123 GitAdminDir lastAdminDir;
124 CString oldTopDir;
125 if (topdir != g_Git.m_CurrentDir && CTGitPath(g_Git.m_CurrentDir).HasAdminDir(&oldTopDir) && oldTopDir != topdir)
127 CString sMsg;
128 sMsg.Format(IDS_ERR_DIFFENERTPREPO, oldTopDir, topdir);
129 MessageBox(NULL, sMsg, _T("TortoiseGitBlame"), MB_OK | MB_ICONERROR);
130 return FALSE;
133 m_IsGitFile=TRUE;
134 sOrigCWD = g_Git.m_CurrentDir = topdir;
136 CString PathName = m_CurrentFileName;
137 if(topdir[topdir.GetLength()-1] == _T('\\') ||
138 topdir[topdir.GetLength()-1] == _T('/'))
139 PathName=PathName.Right(PathName.GetLength()-g_Git.m_CurrentDir.GetLength());
140 else
141 PathName=PathName.Right(PathName.GetLength()-g_Git.m_CurrentDir.GetLength()-1);
143 CTGitPath path;
144 path.SetFromWin(PathName);
146 if(!g_Git.m_CurrentDir.IsEmpty())
147 SetCurrentDirectory(g_Git.m_CurrentDir);
151 // make sure all config files are read in order to check that none contains an error
152 g_Git.GetConfigValue(_T("doesnot.exist"));
154 catch (char * libgiterr)
156 MessageBox(NULL, CString(libgiterr), _T("TortoiseGitBlame"), MB_ICONERROR);
157 return FALSE;
160 CString cmd;
161 cmd.Format(_T("git.exe blame -s -l %s -- \"%s\""),Rev,path.GetGitPathString());
162 m_BlameData.clear();
163 BYTE_VECTOR err;
164 if(g_Git.Run(cmd, &m_BlameData, &err))
166 CString str;
167 if (!m_BlameData.empty())
168 g_Git.StringAppend(&str, &m_BlameData[0], CP_UTF8);
169 if (!err.empty())
170 g_Git.StringAppend(&str, &err[0], CP_UTF8);
171 MessageBox(NULL, CString(MAKEINTRESOURCE(IDS_BLAMEERROR)) + _T("\n\n") + str, _T("TortoiseGitBlame"), MB_OK);
173 return FALSE;
176 if(!m_TempFileName.IsEmpty())
178 ::DeleteFile(m_TempFileName);
179 m_TempFileName.Empty();
182 m_TempFileName=GetTempFile();
184 cmd.Format(_T("git.exe cat-file blob %s:\"%s\""),Rev,path.GetGitPathString());
186 if(g_Git.RunLogFile(cmd, m_TempFileName))
188 CString str;
189 str.Format(IDS_CHECKOUTFAILED, path.GetGitPathString());
190 MessageBox(NULL, CString(MAKEINTRESOURCE(IDS_BLAMEERROR)) + _T("\n\n") + str, _T("TortoiseGitBlame"), MB_OK);
191 return FALSE;
194 m_GitPath = path;
195 if (GetMainFrame()->m_wndOutput.LoadHistory(path.GetGitPathString(), m_Rev, (theApp.GetInt(_T("FollowRenames"), 0) == 1)))
196 return FALSE;
198 CTortoiseGitBlameView *pView=DYNAMIC_DOWNCAST(CTortoiseGitBlameView,GetMainFrame()->GetActiveView());
199 if(pView == NULL)
201 CWnd* pWnd = GetMainFrame()->GetDescendantWindow(AFX_IDW_PANE_FIRST, TRUE);
202 if (pWnd != NULL && pWnd->IsKindOf(RUNTIME_CLASS(CTortoiseGitBlameView)))
204 pView = (CTortoiseGitBlameView*)pWnd;
206 else
208 return FALSE;
211 pView->UpdateInfo();
212 if (m_lLine > 0)
213 pView->GotoLine(m_lLine);
216 return TRUE;
219 void CTortoiseGitBlameDoc::SetPathName(LPCTSTR lpszPathName, BOOL bAddToMRU)
221 CDocument::SetPathName(lpszPathName, bAddToMRU && (m_Rev == _T("HEAD")));
223 this->SetTitle(CString(lpszPathName) + _T(":") + m_Rev);
226 // CTortoiseGitBlameDoc diagnostics
228 #ifdef _DEBUG
229 void CTortoiseGitBlameDoc::AssertValid() const
231 CDocument::AssertValid();
234 void CTortoiseGitBlameDoc::Dump(CDumpContext& dc) const
236 CDocument::Dump(dc);
238 #endif //_DEBUG
241 // CTortoiseGitBlameDoc commands