added missing GPL header
[TortoiseGit.git] / src / TortoiseGitBlame / TortoiseGitBlameDoc.cpp
blobad0ffeb859de32af8837791bc1e96420c7665a3a
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
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"
36 #ifdef _DEBUG
37 #define new DEBUG_NEW
38 #endif
41 // CTortoiseGitBlameDoc
43 IMPLEMENT_DYNCREATE(CTortoiseGitBlameDoc, CDocument)
45 BEGIN_MESSAGE_MAP(CTortoiseGitBlameDoc, CDocument)
46 END_MESSAGE_MAP()
49 // CTortoiseGitBlameDoc construction/destruction
51 CTortoiseGitBlameDoc::CTortoiseGitBlameDoc()
53 m_bFirstStartup = true;
56 CTortoiseGitBlameDoc::~CTortoiseGitBlameDoc()
60 BOOL CTortoiseGitBlameDoc::OnNewDocument()
62 if (!CDocument::OnNewDocument())
63 return FALSE;
65 // (SDI documents will reuse this document)
66 return TRUE;
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;
75 }else
77 m_Rev.Empty();
80 return OnOpenDocument(lpszPathName,m_Rev);
83 BOOL CTortoiseGitBlameDoc::OnOpenDocument(LPCTSTR lpszPathName,CString Rev)
85 if (!CDocument::OnOpenDocument(lpszPathName))
86 return FALSE;
88 m_CurrentFileName=lpszPathName;
89 m_Rev=Rev;
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);
95 return FALSE;
98 GitAdminDir admindir;
99 CString topdir;
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);
103 }else
105 m_IsGitFile=TRUE;
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());
112 else
113 PathName=PathName.Right(PathName.GetLength()-g_Git.m_CurrentDir.GetLength()-1);
115 CTGitPath path;
116 path.SetFromWin(PathName);
118 if(!g_Git.m_CurrentDir.IsEmpty())
119 SetCurrentDirectory(g_Git.m_CurrentDir);
121 m_GitPath = path;
122 GetMainFrame()->m_wndOutput.LoadHistory(path.GetGitPathString());
124 CString cmd;
126 if(Rev.IsEmpty())
127 Rev=_T("HEAD");
129 cmd.Format(_T("git.exe blame -s -l %s -- \"%s\""),Rev,path.GetGitPathString());
130 m_BlameData.clear();
131 if(g_Git.Run(cmd,&m_BlameData))
133 CString str;
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();
147 if(Rev.IsEmpty())
148 Rev=_T("HEAD");
150 cmd.Format(_T("git.exe cat-file blob %s:\"%s\""),Rev,path.GetGitPathString());
152 if(g_Git.RunLogFile(cmd, m_TempFileName))
154 CString str;
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());
160 if(pView == NULL)
162 CWnd* pWnd = GetMainFrame()->GetDescendantWindow(AFX_IDW_PANE_FIRST, TRUE);
163 if (pWnd != NULL && pWnd->IsKindOf(RUNTIME_CLASS(CTortoiseGitBlameView)))
165 pView = (CTortoiseGitBlameView*)pWnd;
167 else
169 return FALSE;
172 pView->UpdateInfo();
175 return TRUE;
178 void CTortoiseGitBlameDoc::SetPathName(LPCTSTR lpszPathName, BOOL bAddToMRU)
180 CDocument::SetPathName(lpszPathName,bAddToMRU);
182 CString title;
183 if(m_Rev.IsEmpty())
184 title=CString(lpszPathName)+_T(":HEAD");
185 else
186 title=CString(lpszPathName)+_T(":")+m_Rev;
188 this->SetTitle(title);
191 // CTortoiseGitBlameDoc serialization
193 void CTortoiseGitBlameDoc::Serialize(CArchive& ar)
195 if (ar.IsStoring())
197 // TODO: add storing code here
199 else
201 // TODO: add loading code here
206 // CTortoiseGitBlameDoc diagnostics
208 #ifdef _DEBUG
209 void CTortoiseGitBlameDoc::AssertValid() const
211 CDocument::AssertValid();
214 void CTortoiseGitBlameDoc::Dump(CDumpContext& dc) const
216 CDocument::Dump(dc);
218 #endif //_DEBUG
221 // CTortoiseGitBlameDoc commands