OSDN Git Service

61930cdd266c1b7810dfa6044c5606c15ebd48b3
[tortoisegit/TortoiseGitJp.git] / Git / Git.cpp
1 #include "StdAfx.h"\r
2 #include "Git.h"\r
3 #include "atlconv.h"\r
4 #include "afx.h"\r
5 \r
6 #define MAX_DIRBUFFER 1000\r
7 CGit g_Git;\r
8 CGit::CGit(void)\r
9 {\r
10         GetCurrentDirectory(MAX_DIRBUFFER,m_CurrentDir.GetBuffer(MAX_DIRBUFFER));\r
11 }\r
12 \r
13 CGit::~CGit(void)\r
14 {\r
15 }\r
16 \r
17 char buffer[4096];\r
18 \r
19 int CGit::Run(CString cmd, CString* output)\r
20 {\r
21         SECURITY_ATTRIBUTES sa;\r
22         HANDLE hRead, hWrite;\r
23 \r
24         sa.nLength = sizeof(SECURITY_ATTRIBUTES);\r
25         sa.lpSecurityDescriptor=NULL;\r
26         sa.bInheritHandle=TRUE;\r
27         if(!CreatePipe(&hRead,&hWrite,&sa,0))\r
28         {\r
29                 return GIT_ERROR_OPEN_PIP;\r
30         }\r
31         \r
32         STARTUPINFO si;\r
33         PROCESS_INFORMATION pi;\r
34         si.cb=sizeof(STARTUPINFO);\r
35         GetStartupInfo(&si);\r
36 \r
37         si.hStdError=hWrite;\r
38         si.hStdOutput=hWrite;\r
39         si.wShowWindow=SW_HIDE;\r
40         si.dwFlags=STARTF_USESTDHANDLES|STARTF_USESHOWWINDOW;\r
41 \r
42         \r
43         if(!CreateProcess(NULL,(LPWSTR)cmd.GetString(), NULL,NULL,TRUE,NULL,NULL,(LPWSTR)m_CurrentDir.GetString(),&si,&pi))\r
44         {\r
45                 LPVOID lpMsgBuf;\r
46                 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,\r
47                         NULL,GetLastError(),MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),\r
48                         (LPTSTR)&lpMsgBuf,\r
49                         0,NULL);\r
50                 return GIT_ERROR_CREATE_PROCESS;\r
51         }\r
52         \r
53         DWORD readnumber;\r
54         while(ReadFile(hRead,buffer,4090,&readnumber,NULL))\r
55         {\r
56                 buffer[readnumber]=0;\r
57                 USES_CONVERSION;\r
58                 output->Append(A2W(buffer));\r
59                 if(readnumber<4090)\r
60                         break;\r
61         }\r
62 \r
63         \r
64         CloseHandle(pi.hThread);\r
65         CloseHandle(pi.hProcess);\r
66         CloseHandle(hWrite);\r
67         CloseHandle(hRead);\r
68         return GIT_SUCCESS;\r
69 }\r
70 \r
71 CString CGit::GetUserName(void)\r
72 {\r
73         CString UserName;\r
74         Run(_T("git.cmd config user.name"),&UserName);\r
75         return UserName;\r
76 }\r
77 CString CGit::GetUserEmail(void)\r
78 {\r
79         CString UserName;\r
80         Run(_T("git.cmd config user.email"),&UserName);\r
81         return UserName;\r
82 }\r
83 \r
84 CString CGit::GetCurrentBranch(void)\r
85 {\r
86         CString branch;\r
87         Run(_T("git.cmd branch"),&branch);\r
88         if(branch.GetLength()>0)\r
89         {\r
90                 branch.Replace(_T('*'),_T(' '));\r
91                 branch.TrimLeft();\r
92                 return branch;\r
93         }\r
94         return CString("");\r
95 }\r