Fixed issue #1220: ext/CrashServer/CommonLibs/Zlib/Zlib.vcproj immediate dir Win32...
[TortoiseGit.git] / src / TortoiseGitBlame / TortoiseGitBlameDoc.cpp
blob9d5dcca592a78280e383baa362de7e1a0363be69
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;
57 CTortoiseGitBlameDoc::~CTortoiseGitBlameDoc()
61 BOOL CTortoiseGitBlameDoc::OnNewDocument()
63 return TRUE;
65 BOOL CTortoiseGitBlameDoc::OnOpenDocument(LPCTSTR lpszPathName)
67 CCmdLineParser parser(AfxGetApp()->m_lpCmdLine);
68 if(parser.HasVal(_T("rev")) && m_bFirstStartup)
70 m_Rev=parser.GetVal(_T("rev"));
71 m_bFirstStartup = false;
73 else
75 m_Rev.Empty();
78 return OnOpenDocument(lpszPathName,m_Rev);
81 BOOL CTortoiseGitBlameDoc::OnOpenDocument(LPCTSTR lpszPathName,CString Rev)
83 if(Rev.IsEmpty())
84 Rev = _T("HEAD");
86 // enable blame for files which do not exist in current working tree
87 if (!PathFileExists(lpszPathName) && Rev != _T("HEAD"))
89 if (!CDocument::OnOpenDocument(GetTempFile()))
90 return FALSE;
92 else
94 if (!CDocument::OnOpenDocument(lpszPathName))
95 return FALSE;
98 m_CurrentFileName = lpszPathName;
100 m_Rev=Rev;
102 // (SDI documents will reuse this document)
103 if(!g_Git.CheckMsysGitDir())
105 CCommonAppUtils::RunTortoiseProc(_T(" /command:settings"));
106 return FALSE;
108 GitAdminDir admindir;
109 CString topdir;
110 if(!admindir.HasAdminDir(m_CurrentFileName, &topdir))
112 CString temp;
113 temp.Format(IDS_CANNOTBLAMENOGIT, CString(m_CurrentFileName));
114 CMessageBox::Show(NULL, temp, _T("TortoiseGitBlame"), MB_OK);
116 else
118 GitAdminDir lastAdminDir;
119 CString oldTopDir;
120 if (topdir != g_Git.m_CurrentDir && CTGitPath(g_Git.m_CurrentDir).HasAdminDir(&oldTopDir) && oldTopDir != topdir)
122 CString sMsg;
123 sMsg.Format(IDS_ERR_DIFFENERTPREPO, oldTopDir, topdir);
124 MessageBox(NULL, sMsg, _T("TortoiseGitBlame"), MB_OK | MB_ICONERROR);
125 return FALSE;
128 m_IsGitFile=TRUE;
129 sOrigCWD = g_Git.m_CurrentDir = topdir;
131 CString PathName = m_CurrentFileName;
132 if(topdir[topdir.GetLength()-1] == _T('\\') ||
133 topdir[topdir.GetLength()-1] == _T('/'))
134 PathName=PathName.Right(PathName.GetLength()-g_Git.m_CurrentDir.GetLength());
135 else
136 PathName=PathName.Right(PathName.GetLength()-g_Git.m_CurrentDir.GetLength()-1);
138 CTGitPath path;
139 path.SetFromWin(PathName);
141 if(!g_Git.m_CurrentDir.IsEmpty())
142 SetCurrentDirectory(g_Git.m_CurrentDir);
144 m_GitPath = path;
145 if (GetMainFrame()->m_wndOutput.LoadHistory(path.GetGitPathString(), m_Rev, (theApp.GetInt(_T("FollowRenames"), 0) == 1)))
146 return FALSE;
148 CString cmd;
150 cmd.Format(_T("git.exe blame -s -l %s -- \"%s\""),Rev,path.GetGitPathString());
151 m_BlameData.clear();
152 BYTE_VECTOR err;
153 if(g_Git.Run(cmd, &m_BlameData, &err))
155 CString str;
156 if (m_BlameData.size() > 0)
157 g_Git.StringAppend(&str, &m_BlameData[0], CP_UTF8);
158 if (err.size() > 0)
159 g_Git.StringAppend(&str, &err[0], CP_UTF8);
160 MessageBox(NULL, CString(MAKEINTRESOURCE(IDS_BLAMEERROR)) + _T("\n\n") + str, _T("TortoiseGitBlame"), MB_OK);
162 return FALSE;
165 if(!m_TempFileName.IsEmpty())
167 ::DeleteFile(m_TempFileName);
168 m_TempFileName.Empty();
171 m_TempFileName=GetTempFile();
173 if(Rev.IsEmpty())
174 Rev=_T("HEAD");
176 cmd.Format(_T("git.exe cat-file blob %s:\"%s\""),Rev,path.GetGitPathString());
178 if(g_Git.RunLogFile(cmd, m_TempFileName))
180 CString str;
181 str.Format(IDS_CHECKOUTFAILED, path.GetGitPathString());
182 MessageBox(NULL, CString(MAKEINTRESOURCE(IDS_BLAMEERROR)) + _T("\n\n") + str, _T("TortoiseGitBlame"), MB_OK);
185 CTortoiseGitBlameView *pView=DYNAMIC_DOWNCAST(CTortoiseGitBlameView,GetMainFrame()->GetActiveView());
186 if(pView == NULL)
188 CWnd* pWnd = GetMainFrame()->GetDescendantWindow(AFX_IDW_PANE_FIRST, TRUE);
189 if (pWnd != NULL && pWnd->IsKindOf(RUNTIME_CLASS(CTortoiseGitBlameView)))
191 pView = (CTortoiseGitBlameView*)pWnd;
193 else
195 return FALSE;
198 pView->UpdateInfo();
201 return TRUE;
204 void CTortoiseGitBlameDoc::SetPathName(LPCTSTR lpszPathName, BOOL bAddToMRU)
206 CDocument::SetPathName(lpszPathName,bAddToMRU);
208 CString title;
209 if(m_Rev.IsEmpty())
210 title=CString(lpszPathName)+_T(":HEAD");
211 else
212 title=CString(lpszPathName)+_T(":")+m_Rev;
214 this->SetTitle(title);
217 // CTortoiseGitBlameDoc serialization
219 void CTortoiseGitBlameDoc::Serialize(CArchive& ar)
221 if (ar.IsStoring())
223 // TODO: add storing code here
225 else
227 // TODO: add loading code here
232 // CTortoiseGitBlameDoc diagnostics
234 #ifdef _DEBUG
235 void CTortoiseGitBlameDoc::AssertValid() const
237 CDocument::AssertValid();
240 void CTortoiseGitBlameDoc::Dump(CDumpContext& dc) const
242 CDocument::Dump(dc);
244 #endif //_DEBUG
247 // CTortoiseGitBlameDoc commands