pulled latest translations from Transifex
[TortoiseGit.git] / src / TortoiseGitBlame / TortoiseGitBlameDoc.cpp
blobe65dddb7433fb6f90e162b62ef44a830ecdb0ec7
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
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;
57 CTortoiseGitBlameDoc::~CTortoiseGitBlameDoc()
61 BOOL CTortoiseGitBlameDoc::OnNewDocument()
63 if (!CDocument::OnNewDocument())
64 return FALSE;
66 // (SDI documents will reuse this document)
67 return TRUE;
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;
77 else
79 m_Rev.Empty();
82 return OnOpenDocument(lpszPathName,m_Rev);
85 BOOL CTortoiseGitBlameDoc::OnOpenDocument(LPCTSTR lpszPathName,CString Rev)
87 if (!CDocument::OnOpenDocument(lpszPathName))
88 return FALSE;
90 m_CurrentFileName=lpszPathName;
91 m_Rev=Rev;
93 // (SDI documents will reuse this document)
94 if(!g_Git.CheckMsysGitDir())
96 CCommonAppUtils::RunTortoiseProc(_T(" /command:settings"));
97 return FALSE;
100 GitAdminDir admindir;
101 CString topdir;
102 if(!admindir.HasAdminDir(lpszPathName,&topdir))
104 CString temp;
105 temp.Format(IDS_CANNOTBLAMENOGIT, CString(lpszPathName));
106 CMessageBox::Show(NULL, temp, _T("TortoiseGitBlame"), MB_OK);
108 else
110 m_IsGitFile=TRUE;
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());
117 else
118 PathName=PathName.Right(PathName.GetLength()-g_Git.m_CurrentDir.GetLength()-1);
120 CTGitPath path;
121 path.SetFromWin(PathName);
123 if(!g_Git.m_CurrentDir.IsEmpty())
124 SetCurrentDirectory(g_Git.m_CurrentDir);
126 m_GitPath = path;
127 GetMainFrame()->m_wndOutput.LoadHistory(path.GetGitPathString(), (theApp.GetInt(_T("FollowRenames"), 0) == 1));
129 CString cmd;
131 if(Rev.IsEmpty())
132 Rev=_T("HEAD");
134 cmd.Format(_T("git.exe blame -s -l %s -- \"%s\""),Rev,path.GetGitPathString());
135 m_BlameData.clear();
136 BYTE_VECTOR err;
137 if(g_Git.Run(cmd, &m_BlameData, &err))
139 CString str;
140 if (m_BlameData.size() > 0)
141 g_Git.StringAppend(&str, &m_BlameData[0], CP_UTF8);
142 if (err.size() > 0)
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();
156 if(Rev.IsEmpty())
157 Rev=_T("HEAD");
159 cmd.Format(_T("git.exe cat-file blob %s:\"%s\""),Rev,path.GetGitPathString());
161 if(g_Git.RunLogFile(cmd, m_TempFileName))
163 CString str;
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());
169 if(pView == NULL)
171 CWnd* pWnd = GetMainFrame()->GetDescendantWindow(AFX_IDW_PANE_FIRST, TRUE);
172 if (pWnd != NULL && pWnd->IsKindOf(RUNTIME_CLASS(CTortoiseGitBlameView)))
174 pView = (CTortoiseGitBlameView*)pWnd;
176 else
178 return FALSE;
181 pView->UpdateInfo();
184 return TRUE;
187 void CTortoiseGitBlameDoc::SetPathName(LPCTSTR lpszPathName, BOOL bAddToMRU)
189 CDocument::SetPathName(lpszPathName,bAddToMRU);
191 CString title;
192 if(m_Rev.IsEmpty())
193 title=CString(lpszPathName)+_T(":HEAD");
194 else
195 title=CString(lpszPathName)+_T(":")+m_Rev;
197 this->SetTitle(title);
200 // CTortoiseGitBlameDoc serialization
202 void CTortoiseGitBlameDoc::Serialize(CArchive& ar)
204 if (ar.IsStoring())
206 // TODO: add storing code here
208 else
210 // TODO: add loading code here
215 // CTortoiseGitBlameDoc diagnostics
217 #ifdef _DEBUG
218 void CTortoiseGitBlameDoc::AssertValid() const
220 CDocument::AssertValid();
223 void CTortoiseGitBlameDoc::Dump(CDumpContext& dc) const
225 CDocument::Dump(dc);
227 #endif //_DEBUG
230 // CTortoiseGitBlameDoc commands