Can dynamically set show/hide checkboxes in GitStatusListCtrl
[TortoiseGit.git] / src / TortoiseGitBlame / TortoiseGitBlameDoc.cpp
bloba7d1cd2834944c30631061b492b88805f914572c
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;
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);
116 return FALSE;
118 else
120 GitAdminDir lastAdminDir;
121 CString oldTopDir;
122 if (topdir != g_Git.m_CurrentDir && CTGitPath(g_Git.m_CurrentDir).HasAdminDir(&oldTopDir) && oldTopDir != topdir)
124 CString sMsg;
125 sMsg.Format(IDS_ERR_DIFFENERTPREPO, oldTopDir, topdir);
126 MessageBox(NULL, sMsg, _T("TortoiseGitBlame"), MB_OK | MB_ICONERROR);
127 return FALSE;
130 m_IsGitFile=TRUE;
131 sOrigCWD = g_Git.m_CurrentDir = topdir;
133 CString PathName = m_CurrentFileName;
134 if(topdir[topdir.GetLength()-1] == _T('\\') ||
135 topdir[topdir.GetLength()-1] == _T('/'))
136 PathName=PathName.Right(PathName.GetLength()-g_Git.m_CurrentDir.GetLength());
137 else
138 PathName=PathName.Right(PathName.GetLength()-g_Git.m_CurrentDir.GetLength()-1);
140 CTGitPath path;
141 path.SetFromWin(PathName);
143 if(!g_Git.m_CurrentDir.IsEmpty())
144 SetCurrentDirectory(g_Git.m_CurrentDir);
148 // make sure all config files are read in order to check that none contains an error
149 g_Git.GetConfigValue(_T("doesnot.exist"));
151 catch (char * libgiterr)
153 MessageBox(NULL, CString(libgiterr), _T("TortoiseGitBlame"), MB_ICONERROR);
154 return FALSE;
157 m_GitPath = path;
158 if (GetMainFrame()->m_wndOutput.LoadHistory(path.GetGitPathString(), m_Rev, (theApp.GetInt(_T("FollowRenames"), 0) == 1)))
159 return FALSE;
161 CString cmd;
163 cmd.Format(_T("git.exe blame -s -l %s -- \"%s\""),Rev,path.GetGitPathString());
164 m_BlameData.clear();
165 BYTE_VECTOR err;
166 if(g_Git.Run(cmd, &m_BlameData, &err))
168 CString str;
169 if (!m_BlameData.empty())
170 g_Git.StringAppend(&str, &m_BlameData[0], CP_UTF8);
171 if (!err.empty())
172 g_Git.StringAppend(&str, &err[0], CP_UTF8);
173 MessageBox(NULL, CString(MAKEINTRESOURCE(IDS_BLAMEERROR)) + _T("\n\n") + str, _T("TortoiseGitBlame"), MB_OK);
175 return FALSE;
178 if(!m_TempFileName.IsEmpty())
180 ::DeleteFile(m_TempFileName);
181 m_TempFileName.Empty();
184 m_TempFileName=GetTempFile();
186 cmd.Format(_T("git.exe cat-file blob %s:\"%s\""),Rev,path.GetGitPathString());
188 if(g_Git.RunLogFile(cmd, m_TempFileName))
190 CString str;
191 str.Format(IDS_CHECKOUTFAILED, path.GetGitPathString());
192 MessageBox(NULL, CString(MAKEINTRESOURCE(IDS_BLAMEERROR)) + _T("\n\n") + str, _T("TortoiseGitBlame"), MB_OK);
193 return FALSE;
196 CTortoiseGitBlameView *pView=DYNAMIC_DOWNCAST(CTortoiseGitBlameView,GetMainFrame()->GetActiveView());
197 if(pView == NULL)
199 CWnd* pWnd = GetMainFrame()->GetDescendantWindow(AFX_IDW_PANE_FIRST, TRUE);
200 if (pWnd != NULL && pWnd->IsKindOf(RUNTIME_CLASS(CTortoiseGitBlameView)))
202 pView = (CTortoiseGitBlameView*)pWnd;
204 else
206 return FALSE;
209 pView->UpdateInfo();
212 return TRUE;
215 void CTortoiseGitBlameDoc::SetPathName(LPCTSTR lpszPathName, BOOL bAddToMRU)
217 CDocument::SetPathName(lpszPathName, bAddToMRU && (m_Rev == _T("HEAD")));
219 this->SetTitle(CString(lpszPathName) + _T(":") + m_Rev);
222 // CTortoiseGitBlameDoc diagnostics
224 #ifdef _DEBUG
225 void CTortoiseGitBlameDoc::AssertValid() const
227 CDocument::AssertValid();
230 void CTortoiseGitBlameDoc::Dump(CDumpContext& dc) const
232 CDocument::Dump(dc);
234 #endif //_DEBUG
237 // CTortoiseGitBlameDoc commands