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()
63 if (!CDocument::OnNewDocument())
66 // (SDI documents will reuse this document)
69 BOOL
CTortoiseGitBlameDoc::OnOpenDocument(LPCTSTR lpszPathName
)
71 CCmdLineParser
parser(AfxGetApp()->m_lpCmdLine
);
72 if(parser
.HasVal(_T("rev")) && m_bFirstStartup
)
74 m_Rev
=parser
.GetVal(_T("rev"));
75 m_bFirstStartup
= false;
82 return OnOpenDocument(lpszPathName
,m_Rev
);
85 BOOL
CTortoiseGitBlameDoc::OnOpenDocument(LPCTSTR lpszPathName
,CString Rev
)
87 if (!CDocument::OnOpenDocument(lpszPathName
))
90 m_CurrentFileName
=lpszPathName
;
93 // (SDI documents will reuse this document)
94 if(!g_Git
.CheckMsysGitDir())
96 CCommonAppUtils::RunTortoiseProc(_T(" /command:settings"));
100 GitAdminDir admindir
;
102 if(!admindir
.HasAdminDir(lpszPathName
,&topdir
))
105 temp
.Format(IDS_CANNOTBLAMENOGIT
, CString(lpszPathName
));
106 CMessageBox::Show(NULL
, temp
, _T("TortoiseGitBlame"), MB_OK
);
111 sOrigCWD
= g_Git
.m_CurrentDir
= topdir
;
113 CString PathName
=lpszPathName
;
114 if(topdir
[topdir
.GetLength()-1] == _T('\\') ||
115 topdir
[topdir
.GetLength()-1] == _T('/'))
116 PathName
=PathName
.Right(PathName
.GetLength()-g_Git
.m_CurrentDir
.GetLength());
118 PathName
=PathName
.Right(PathName
.GetLength()-g_Git
.m_CurrentDir
.GetLength()-1);
121 path
.SetFromWin(PathName
);
123 if(!g_Git
.m_CurrentDir
.IsEmpty())
124 SetCurrentDirectory(g_Git
.m_CurrentDir
);
127 GetMainFrame()->m_wndOutput
.LoadHistory(path
.GetGitPathString(), (theApp
.GetInt(_T("FollowRenames"), 0) == 1));
134 cmd
.Format(_T("git.exe blame -s -l %s -- \"%s\""),Rev
,path
.GetGitPathString());
137 if(g_Git
.Run(cmd
, &m_BlameData
, &err
))
140 if (m_BlameData
.size() > 0)
141 g_Git
.StringAppend(&str
, &m_BlameData
[0], CP_UTF8
);
143 g_Git
.StringAppend(&str
, &err
[0], CP_UTF8
);
144 MessageBox(NULL
, CString(MAKEINTRESOURCE(IDS_BLAMEERROR
)) + _T("\n\n") + str
, _T("TortoiseGitBlame"), MB_OK
);
148 if(!m_TempFileName
.IsEmpty())
150 ::DeleteFile(m_TempFileName
);
151 m_TempFileName
.Empty();
154 m_TempFileName
=GetTempFile();
159 cmd
.Format(_T("git.exe cat-file blob %s:\"%s\""),Rev
,path
.GetGitPathString());
161 if(g_Git
.RunLogFile(cmd
, m_TempFileName
))
164 str
.Format(IDS_CHECKOUTFAILED
, path
.GetGitPathString());
165 MessageBox(NULL
, CString(MAKEINTRESOURCE(IDS_BLAMEERROR
)) + _T("\n\n") + str
, _T("TortoiseGitBlame"), MB_OK
);
168 CTortoiseGitBlameView
*pView
=DYNAMIC_DOWNCAST(CTortoiseGitBlameView
,GetMainFrame()->GetActiveView());
171 CWnd
* pWnd
= GetMainFrame()->GetDescendantWindow(AFX_IDW_PANE_FIRST
, TRUE
);
172 if (pWnd
!= NULL
&& pWnd
->IsKindOf(RUNTIME_CLASS(CTortoiseGitBlameView
)))
174 pView
= (CTortoiseGitBlameView
*)pWnd
;
187 void CTortoiseGitBlameDoc::SetPathName(LPCTSTR lpszPathName
, BOOL bAddToMRU
)
189 CDocument::SetPathName(lpszPathName
,bAddToMRU
);
193 title
=CString(lpszPathName
)+_T(":HEAD");
195 title
=CString(lpszPathName
)+_T(":")+m_Rev
;
197 this->SetTitle(title
);
200 // CTortoiseGitBlameDoc serialization
202 void CTortoiseGitBlameDoc::Serialize(CArchive
& ar
)
206 // TODO: add storing code here
210 // TODO: add loading code here
215 // CTortoiseGitBlameDoc diagnostics
218 void CTortoiseGitBlameDoc::AssertValid() const
220 CDocument::AssertValid();
223 void CTortoiseGitBlameDoc::Dump(CDumpContext
& dc
) const
230 // CTortoiseGitBlameDoc commands