OSDN Git Service

Fixed issue #190: Access violation in Blame and wrong path name when root dir is...
[tortoisegit/TortoiseGitJp.git] / src / TortoiseGitBlame / TortoiseGitBlameDoc.cpp
1 \r
2 // TortoiseGitBlameDoc.cpp : implementation of the CTortoiseGitBlameDoc class\r
3 //\r
4 \r
5 #include "stdafx.h"\r
6 #include "TortoiseGitBlame.h"\r
7 \r
8 #include "TortoiseGitBlameDoc.h"\r
9 #include "GitAdminDir.h"\r
10 #include "MessageBox.h"\r
11 #include "Git.h"\r
12 #include "MainFrm.h"\r
13 #include "TGitPath.h"\r
14 #include "TortoiseGitBlameView.h"\r
15 #include "CmdLineParser.h"\r
16 \r
17 #ifdef _DEBUG\r
18 #define new DEBUG_NEW\r
19 #endif\r
20 \r
21 \r
22 // CTortoiseGitBlameDoc\r
23 \r
24 IMPLEMENT_DYNCREATE(CTortoiseGitBlameDoc, CDocument)\r
25 \r
26 BEGIN_MESSAGE_MAP(CTortoiseGitBlameDoc, CDocument)\r
27 END_MESSAGE_MAP()\r
28 \r
29 \r
30 // CTortoiseGitBlameDoc construction/destruction\r
31 \r
32 CTortoiseGitBlameDoc::CTortoiseGitBlameDoc()\r
33 {\r
34         // TODO: add one-time construction code here\r
35 \r
36 }\r
37 \r
38 CTortoiseGitBlameDoc::~CTortoiseGitBlameDoc()\r
39 {\r
40 }\r
41 \r
42 BOOL CTortoiseGitBlameDoc::OnNewDocument()\r
43 {\r
44         if (!CDocument::OnNewDocument())\r
45                 return FALSE;\r
46 \r
47         // TODO: add reinitialization code here\r
48         // (SDI documents will reuse this document)\r
49 \r
50         return TRUE;\r
51 }\r
52 BOOL CTortoiseGitBlameDoc::OnOpenDocument(LPCTSTR lpszPathName)\r
53 {\r
54         CCmdLineParser parser(AfxGetApp()->m_lpCmdLine);\r
55         if(parser.HasVal(_T("rev")))\r
56         {\r
57                 m_Rev=parser.GetVal(_T("rev"));\r
58         }else\r
59         {\r
60                 m_Rev.Empty();\r
61         }\r
62 \r
63         return OnOpenDocument(lpszPathName,m_Rev);\r
64 }\r
65 \r
66 BOOL CTortoiseGitBlameDoc::OnOpenDocument(LPCTSTR lpszPathName,CString Rev)\r
67 {\r
68         if (!CDocument::OnOpenDocument(lpszPathName))\r
69                 return FALSE;\r
70 \r
71         m_CurrentFileName=lpszPathName;\r
72         m_Rev=Rev;\r
73 \r
74         // TODO: add reinitialization code here\r
75         // (SDI documents will reuse this document)\r
76         if(!CGit::CheckMsysGitDir())\r
77         {\r
78                 CMessageBox::Show(NULL,_T("MsysGit have not install or config fail"),_T("TortoiseGitBlame"),MB_OK);\r
79                 return FALSE;\r
80         }\r
81 \r
82         GitAdminDir admindir;\r
83         CString topdir;\r
84         if(!admindir.HasAdminDir(lpszPathName,&topdir))\r
85         {\r
86                 CMessageBox::Show(NULL,CString(lpszPathName)+_T(" is not controled by git\nJust Show File Context"),_T("TortoiseGitBlame"),MB_OK);\r
87         }else\r
88         {\r
89                 m_IsGitFile=TRUE;\r
90                 g_Git.m_CurrentDir=topdir;\r
91 \r
92                 CString PathName=lpszPathName;\r
93                 if(topdir[topdir.GetLength()-1] == _T('\\'))\r
94                         PathName=PathName.Right(PathName.GetLength()-g_Git.m_CurrentDir.GetLength());\r
95                 else\r
96                         PathName=PathName.Right(PathName.GetLength()-g_Git.m_CurrentDir.GetLength()-1);\r
97 \r
98                 CTGitPath path;\r
99                 path.SetFromWin(PathName);\r
100 \r
101                 if(!g_Git.m_CurrentDir.IsEmpty())\r
102                         SetCurrentDirectory(g_Git.m_CurrentDir);\r
103                 \r
104                 m_GitPath = path;\r
105                 GetMainFrame()->m_wndOutput.LoadHistory(path.GetGitPathString());\r
106         \r
107                 CString cmd;\r
108                 \r
109                 cmd.Format(_T("git.exe blame -s -l %s -- \"%s\""),Rev,path.GetGitPathString());\r
110                 m_BlameData.Empty();\r
111                 if(g_Git.Run(cmd,&m_BlameData,CP_UTF8))\r
112                 {\r
113                         CMessageBox::Show(NULL,CString(_T("Blame Error"))+m_BlameData,_T("TortoiseGitBlame"),MB_OK);\r
114 \r
115                 }\r
116                 CTortoiseGitBlameView *pView=DYNAMIC_DOWNCAST(CTortoiseGitBlameView,GetMainFrame()->GetActiveView());\r
117                 if(pView == NULL)\r
118                 {\r
119                         CWnd* pWnd = GetMainFrame()->GetDescendantWindow(AFX_IDW_PANE_FIRST, TRUE);\r
120                         if (pWnd != NULL && pWnd->IsKindOf(RUNTIME_CLASS(CTortoiseGitBlameView)))\r
121                         {\r
122                                 pView = (CTortoiseGitBlameView*)pWnd;\r
123                         }\r
124                         else\r
125                         {\r
126                                 return FALSE;\r
127                         }\r
128                 }\r
129                 pView->UpdateInfo();            \r
130         }\r
131 \r
132         return TRUE;\r
133 }\r
134 \r
135 void CTortoiseGitBlameDoc::SetPathName(LPCTSTR lpszPathName, BOOL bAddToMRU)\r
136 {\r
137         CDocument::SetPathName(lpszPathName,bAddToMRU);\r
138 \r
139         CString title;\r
140         if(m_Rev.IsEmpty())\r
141                 title=CString(lpszPathName)+_T(":HEAD");\r
142         else\r
143                 title=CString(lpszPathName)+_T(":")+m_Rev;\r
144 \r
145         this->SetTitle(title);\r
146 }\r
147 \r
148 // CTortoiseGitBlameDoc serialization\r
149 \r
150 void CTortoiseGitBlameDoc::Serialize(CArchive& ar)\r
151 {\r
152         if (ar.IsStoring())\r
153         {\r
154                 // TODO: add storing code here\r
155         }\r
156         else\r
157         {\r
158                 // TODO: add loading code here\r
159         }\r
160 }\r
161 \r
162 \r
163 // CTortoiseGitBlameDoc diagnostics\r
164 \r
165 #ifdef _DEBUG\r
166 void CTortoiseGitBlameDoc::AssertValid() const\r
167 {\r
168         CDocument::AssertValid();\r
169 }\r
170 \r
171 void CTortoiseGitBlameDoc::Dump(CDumpContext& dc) const\r
172 {\r
173         CDocument::Dump(dc);\r
174 }\r
175 #endif //_DEBUG\r
176 \r
177 \r
178 // CTortoiseGitBlameDoc commands\r