1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2011 - 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"
41 // CTortoiseGitBlameDoc
43 IMPLEMENT_DYNCREATE(CTortoiseGitBlameDoc
, CDocument
)
45 BEGIN_MESSAGE_MAP(CTortoiseGitBlameDoc
, CDocument
)
49 // CTortoiseGitBlameDoc construction/destruction
51 CTortoiseGitBlameDoc::CTortoiseGitBlameDoc()
53 m_bFirstStartup
= true;
56 CTortoiseGitBlameDoc::~CTortoiseGitBlameDoc()
60 BOOL
CTortoiseGitBlameDoc::OnNewDocument()
62 if (!CDocument::OnNewDocument())
65 // (SDI documents will reuse this document)
68 BOOL
CTortoiseGitBlameDoc::OnOpenDocument(LPCTSTR lpszPathName
)
70 CCmdLineParser
parser(AfxGetApp()->m_lpCmdLine
);
71 if(parser
.HasVal(_T("rev")) && m_bFirstStartup
)
73 m_Rev
=parser
.GetVal(_T("rev"));
74 m_bFirstStartup
= false;
80 return OnOpenDocument(lpszPathName
,m_Rev
);
83 BOOL
CTortoiseGitBlameDoc::OnOpenDocument(LPCTSTR lpszPathName
,CString Rev
)
85 if (!CDocument::OnOpenDocument(lpszPathName
))
88 m_CurrentFileName
=lpszPathName
;
91 // (SDI documents will reuse this document)
92 if(!g_Git
.CheckMsysGitDir())
94 CMessageBox::Show(NULL
,_T("MsysGit have not install or config fail"),_T("TortoiseGitBlame"),MB_OK
);
100 if(!admindir
.HasAdminDir(lpszPathName
,&topdir
))
102 CMessageBox::Show(NULL
,CString(lpszPathName
)+_T(" is not controled by git\nJust Show File Context"),_T("TortoiseGitBlame"),MB_OK
);
106 g_Git
.m_CurrentDir
=topdir
;
108 CString PathName
=lpszPathName
;
109 if(topdir
[topdir
.GetLength()-1] == _T('\\') ||
110 topdir
[topdir
.GetLength()-1] == _T('/'))
111 PathName
=PathName
.Right(PathName
.GetLength()-g_Git
.m_CurrentDir
.GetLength());
113 PathName
=PathName
.Right(PathName
.GetLength()-g_Git
.m_CurrentDir
.GetLength()-1);
116 path
.SetFromWin(PathName
);
118 if(!g_Git
.m_CurrentDir
.IsEmpty())
119 SetCurrentDirectory(g_Git
.m_CurrentDir
);
122 GetMainFrame()->m_wndOutput
.LoadHistory(path
.GetGitPathString());
129 cmd
.Format(_T("git.exe blame -s -l %s -- \"%s\""),Rev
,path
.GetGitPathString());
131 if(g_Git
.Run(cmd
,&m_BlameData
))
134 g_Git
.StringAppend(&str
, &m_BlameData
[0], CP_ACP
);
135 CMessageBox::Show(NULL
,CString(_T("Blame Error")) + str
,_T("TortoiseGitBlame"),MB_OK
);
139 if(!m_TempFileName
.IsEmpty())
141 ::DeleteFile(m_TempFileName
);
142 m_TempFileName
.Empty();
145 m_TempFileName
=GetTempFile();
150 cmd
.Format(_T("git.exe cat-file blob %s:\"%s\""),Rev
,path
.GetGitPathString());
152 if(g_Git
.RunLogFile(cmd
, m_TempFileName
))
155 str
.Format(_T("Fail get file %s"), path
.GetGitPathString());
156 CMessageBox::Show(NULL
,CString(_T("Blame Error")) + str
,_T("TortoiseGitBlame"),MB_OK
);
159 CTortoiseGitBlameView
*pView
=DYNAMIC_DOWNCAST(CTortoiseGitBlameView
,GetMainFrame()->GetActiveView());
162 CWnd
* pWnd
= GetMainFrame()->GetDescendantWindow(AFX_IDW_PANE_FIRST
, TRUE
);
163 if (pWnd
!= NULL
&& pWnd
->IsKindOf(RUNTIME_CLASS(CTortoiseGitBlameView
)))
165 pView
= (CTortoiseGitBlameView
*)pWnd
;
178 void CTortoiseGitBlameDoc::SetPathName(LPCTSTR lpszPathName
, BOOL bAddToMRU
)
180 CDocument::SetPathName(lpszPathName
,bAddToMRU
);
184 title
=CString(lpszPathName
)+_T(":HEAD");
186 title
=CString(lpszPathName
)+_T(":")+m_Rev
;
188 this->SetTitle(title
);
191 // CTortoiseGitBlameDoc serialization
193 void CTortoiseGitBlameDoc::Serialize(CArchive
& ar
)
197 // TODO: add storing code here
201 // TODO: add loading code here
206 // CTortoiseGitBlameDoc diagnostics
209 void CTortoiseGitBlameDoc::AssertValid() const
211 CDocument::AssertValid();
214 void CTortoiseGitBlameDoc::Dump(CDumpContext
& dc
) const
221 // CTortoiseGitBlameDoc commands