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
25 #include "TortoiseGitBlame.h"
27 #include "TortoiseGitBlameDoc.h"
28 #include "GitAdminDir.h"
29 #include "MessageBox.h"
33 #include "TortoiseGitBlameView.h"
34 #include "CmdLineParser.h"
35 #include "CommonAppUtils.h"
42 // CTortoiseGitBlameDoc
44 IMPLEMENT_DYNCREATE(CTortoiseGitBlameDoc
, CDocument
)
46 BEGIN_MESSAGE_MAP(CTortoiseGitBlameDoc
, CDocument
)
50 // CTortoiseGitBlameDoc construction/destruction
52 CTortoiseGitBlameDoc::CTortoiseGitBlameDoc()
54 m_bFirstStartup
= true;
57 CTortoiseGitBlameDoc::~CTortoiseGitBlameDoc()
61 BOOL
CTortoiseGitBlameDoc::OnNewDocument()
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;
78 return OnOpenDocument(lpszPathName
,m_Rev
);
81 BOOL
CTortoiseGitBlameDoc::OnOpenDocument(LPCTSTR lpszPathName
,CString Rev
)
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()))
94 if (!CDocument::OnOpenDocument(lpszPathName
))
98 m_CurrentFileName
= lpszPathName
;
102 // (SDI documents will reuse this document)
103 if(!g_Git
.CheckMsysGitDir())
105 CCommonAppUtils::RunTortoiseProc(_T(" /command:settings"));
108 GitAdminDir admindir
;
110 if(!admindir
.HasAdminDir(m_CurrentFileName
, &topdir
))
113 temp
.Format(IDS_CANNOTBLAMENOGIT
, CString(m_CurrentFileName
));
114 CMessageBox::Show(NULL
, temp
, _T("TortoiseGitBlame"), MB_OK
);
118 GitAdminDir lastAdminDir
;
120 if (topdir
!= g_Git
.m_CurrentDir
&& CTGitPath(g_Git
.m_CurrentDir
).HasAdminDir(&oldTopDir
) && oldTopDir
!= topdir
)
123 sMsg
.Format(IDS_ERR_DIFFENERTPREPO
, oldTopDir
, topdir
);
124 MessageBox(NULL
, sMsg
, _T("TortoiseGitBlame"), MB_OK
| MB_ICONERROR
);
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());
136 PathName
=PathName
.Right(PathName
.GetLength()-g_Git
.m_CurrentDir
.GetLength()-1);
139 path
.SetFromWin(PathName
);
141 if(!g_Git
.m_CurrentDir
.IsEmpty())
142 SetCurrentDirectory(g_Git
.m_CurrentDir
);
145 if (GetMainFrame()->m_wndOutput
.LoadHistory(path
.GetGitPathString(), m_Rev
, (theApp
.GetInt(_T("FollowRenames"), 0) == 1)))
150 cmd
.Format(_T("git.exe blame -s -l %s -- \"%s\""),Rev
,path
.GetGitPathString());
153 if(g_Git
.Run(cmd
, &m_BlameData
, &err
))
156 if (!m_BlameData
.empty())
157 g_Git
.StringAppend(&str
, &m_BlameData
[0], CP_UTF8
);
159 g_Git
.StringAppend(&str
, &err
[0], CP_UTF8
);
160 MessageBox(NULL
, CString(MAKEINTRESOURCE(IDS_BLAMEERROR
)) + _T("\n\n") + str
, _T("TortoiseGitBlame"), MB_OK
);
165 if(!m_TempFileName
.IsEmpty())
167 ::DeleteFile(m_TempFileName
);
168 m_TempFileName
.Empty();
171 m_TempFileName
=GetTempFile();
176 cmd
.Format(_T("git.exe cat-file blob %s:\"%s\""),Rev
,path
.GetGitPathString());
178 if(g_Git
.RunLogFile(cmd
, m_TempFileName
))
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());
188 CWnd
* pWnd
= GetMainFrame()->GetDescendantWindow(AFX_IDW_PANE_FIRST
, TRUE
);
189 if (pWnd
!= NULL
&& pWnd
->IsKindOf(RUNTIME_CLASS(CTortoiseGitBlameView
)))
191 pView
= (CTortoiseGitBlameView
*)pWnd
;
204 void CTortoiseGitBlameDoc::SetPathName(LPCTSTR lpszPathName
, BOOL bAddToMRU
)
206 CDocument::SetPathName(lpszPathName
,bAddToMRU
);
210 title
=CString(lpszPathName
)+_T(":HEAD");
212 title
=CString(lpszPathName
)+_T(":")+m_Rev
;
214 this->SetTitle(title
);
217 // CTortoiseGitBlameDoc serialization
219 void CTortoiseGitBlameDoc::Serialize(CArchive
& ar
)
223 // TODO: add storing code here
227 // TODO: add loading code here
232 // CTortoiseGitBlameDoc diagnostics
235 void CTortoiseGitBlameDoc::AssertValid() const
237 CDocument::AssertValid();
240 void CTortoiseGitBlameDoc::Dump(CDumpContext
& dc
) const
247 // CTortoiseGitBlameDoc commands