OSDN Git Service

Fix error in call to GetTimeZoneInformation().
[tortoisegit/TortoiseGitJp.git] / src / Git / Git.cpp
1 #include "StdAfx.h"\r
2 #include "Git.h"\r
3 #include "atlconv.h"\r
4 #include "GitRev.h"\r
5 #include "registry.h"\r
6 #include "GitConfig.h"\r
7 \r
8 #define MAX_DIRBUFFER 1000\r
9 CString CGit::ms_LastMsysGitDir;\r
10 CGit g_Git;\r
11 CGit::CGit(void)\r
12 {\r
13         GetCurrentDirectory(MAX_DIRBUFFER,m_CurrentDir.GetBuffer(MAX_DIRBUFFER));\r
14 }\r
15 \r
16 CGit::~CGit(void)\r
17 {\r
18 }\r
19 \r
20 static char g_Buffer[4096];\r
21 \r
22 int CGit::RunAsync(CString cmd,PROCESS_INFORMATION *piOut,HANDLE *hReadOut,CString *StdioFile)\r
23 {\r
24         SECURITY_ATTRIBUTES sa;\r
25         HANDLE hRead, hWrite;\r
26         HANDLE hStdioFile = NULL;\r
27 \r
28         sa.nLength = sizeof(SECURITY_ATTRIBUTES);\r
29         sa.lpSecurityDescriptor=NULL;\r
30         sa.bInheritHandle=TRUE;\r
31         if(!CreatePipe(&hRead,&hWrite,&sa,0))\r
32         {\r
33                 return GIT_ERROR_OPEN_PIP;\r
34         }\r
35         \r
36         if(StdioFile)\r
37         {\r
38                 hStdioFile=CreateFile(*StdioFile,GENERIC_WRITE,FILE_SHARE_READ   |   FILE_SHARE_WRITE,   \r
39                         &sa,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);  \r
40         }\r
41 \r
42         STARTUPINFO si;\r
43         PROCESS_INFORMATION pi;\r
44         si.cb=sizeof(STARTUPINFO);\r
45         GetStartupInfo(&si);\r
46 \r
47         si.hStdError=hWrite;\r
48         if(StdioFile)\r
49                 si.hStdOutput=hStdioFile;\r
50         else\r
51                 si.hStdOutput=hWrite;\r
52 \r
53         si.wShowWindow=SW_HIDE;\r
54         si.dwFlags=STARTF_USESTDHANDLES|STARTF_USESHOWWINDOW;\r
55 \r
56         if(!CreateProcess(NULL,(LPWSTR)cmd.GetString(), NULL,NULL,TRUE,NULL,NULL,(LPWSTR)m_CurrentDir.GetString(),&si,&pi))\r
57         {\r
58                 LPVOID lpMsgBuf;\r
59                 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,\r
60                         NULL,GetLastError(),MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),\r
61                         (LPTSTR)&lpMsgBuf,\r
62                         0,NULL);\r
63                 return GIT_ERROR_CREATE_PROCESS;\r
64         }\r
65         \r
66         CloseHandle(hWrite);\r
67         if(piOut)\r
68                 *piOut=pi;\r
69         if(hReadOut)\r
70                 *hReadOut=hRead;\r
71         \r
72         return 0;\r
73 \r
74 }\r
75 //Must use sperate function to convert ANSI str to union code string\r
76 //Becuase A2W use stack as internal convert buffer. \r
77 void CGit::StringAppend(CString *str,BYTE *p,int code,int length)\r
78 {\r
79      //USES_CONVERSION;\r
80          //str->Append(A2W_CP((LPCSTR)p,code));\r
81         WCHAR * buf;\r
82 \r
83         int len ;\r
84         if(length<0)\r
85                 len= strlen((const char*)p);\r
86         else\r
87                 len=length;\r
88         //if (len==0)\r
89         //      return ;\r
90         //buf = new WCHAR[len*4 + 1];\r
91         buf = str->GetBuffer(len*4+1+str->GetLength())+str->GetLength();\r
92         SecureZeroMemory(buf, (len*4 + 1)*sizeof(WCHAR));\r
93         MultiByteToWideChar(code, 0, (LPCSTR)p, len, buf, len*4);\r
94         str->ReleaseBuffer();\r
95         //str->Append(buf);\r
96         //delete buf;\r
97 }       \r
98 BOOL CGit::IsInitRepos()\r
99 {\r
100         CString cmdout;\r
101         cmdout.Empty();\r
102         if(g_Git.Run(_T("git.exe rev-parse --revs-only HEAD"),&cmdout,CP_UTF8))\r
103         {\r
104         //      CMessageBox::Show(NULL,cmdout,_T("TortoiseGit"),MB_OK);\r
105                 return TRUE;\r
106         }\r
107         if(cmdout.IsEmpty())\r
108                 return TRUE;\r
109 \r
110         return FALSE;\r
111 }\r
112 int CGit::Run(CString cmd,BYTE_VECTOR *vector)\r
113 {\r
114         PROCESS_INFORMATION pi;\r
115         HANDLE hRead;\r
116         if(RunAsync(cmd,&pi,&hRead))\r
117                 return GIT_ERROR_CREATE_PROCESS;\r
118 \r
119         DWORD readnumber;\r
120         BYTE data;\r
121         while(ReadFile(hRead,&data,1,&readnumber,NULL))\r
122         {\r
123                 //g_Buffer[readnumber]=0;\r
124                 vector->push_back(data);\r
125 //              StringAppend(output,g_Buffer,codes);\r
126         }\r
127 \r
128         \r
129         CloseHandle(pi.hThread);\r
130 \r
131         WaitForSingleObject(pi.hProcess, INFINITE);\r
132         DWORD exitcode =0;\r
133 \r
134         if(!GetExitCodeProcess(pi.hProcess,&exitcode))\r
135         {\r
136                 return GIT_ERROR_GET_EXIT_CODE;\r
137         }\r
138 \r
139         CloseHandle(pi.hProcess);\r
140 \r
141         CloseHandle(hRead);\r
142         return exitcode;\r
143 \r
144 }\r
145 int CGit::Run(CString cmd, CString* output,int code)\r
146 {\r
147         BYTE_VECTOR vector;\r
148         int ret;\r
149         ret=Run(cmd,&vector);\r
150 \r
151         if(ret)\r
152                 return ret;\r
153         \r
154         vector.push_back(0);\r
155         \r
156         StringAppend(output,&(vector[0]),code);\r
157         return 0;\r
158 }\r
159 \r
160 CString CGit::GetUserName(void)\r
161 {\r
162         CString UserName;\r
163         Run(_T("git.exe config user.name"),&UserName,CP_UTF8);\r
164         return UserName;\r
165 }\r
166 CString CGit::GetUserEmail(void)\r
167 {\r
168         CString UserName;\r
169         Run(_T("git.exe config user.email"),&UserName,CP_UTF8);\r
170         return UserName;\r
171 }\r
172 \r
173 CString CGit::GetCurrentBranch(void)\r
174 {\r
175         CString output;\r
176         //Run(_T("git.exe branch"),&branch);\r
177 \r
178         int ret=g_Git.Run(_T("git.exe branch"),&output,CP_UTF8);\r
179         if(!ret)\r
180         {               \r
181                 int pos=0;\r
182                 CString one;\r
183                 while( pos>=0 )\r
184                 {\r
185                         //i++;\r
186                         one=output.Tokenize(_T("\n"),pos);\r
187                         //list.push_back(one.Right(one.GetLength()-2));\r
188                         if(one[0] == _T('*'))\r
189                                 return one.Right(one.GetLength()-2);\r
190                 }\r
191         }\r
192         return CString("");\r
193 }\r
194 \r
195 int CGit::BuildOutputFormat(CString &format,bool IsFull)\r
196 {\r
197         CString log;\r
198         log.Format(_T("#<%c>%%x00"),LOG_REV_ITEM_BEGIN);\r
199         format += log;\r
200         if(IsFull)\r
201         {\r
202                 log.Format(_T("#<%c>%%an%%x00"),LOG_REV_AUTHOR_NAME);\r
203                 format += log;\r
204                 log.Format(_T("#<%c>%%ae%%x00"),LOG_REV_AUTHOR_EMAIL);\r
205                 format += log;\r
206                 log.Format(_T("#<%c>%%ai%%x00"),LOG_REV_AUTHOR_DATE);\r
207                 format += log;\r
208                 log.Format(_T("#<%c>%%cn%%x00"),LOG_REV_COMMIT_NAME);\r
209                 format += log;\r
210                 log.Format(_T("#<%c>%%ce%%x00"),LOG_REV_COMMIT_EMAIL);\r
211                 format += log;\r
212                 log.Format(_T("#<%c>%%ci%%x00"),LOG_REV_COMMIT_DATE);\r
213                 format += log;\r
214                 log.Format(_T("#<%c>%%s%%x00"),LOG_REV_COMMIT_SUBJECT);\r
215                 format += log;\r
216                 log.Format(_T("#<%c>%%b%%x00"),LOG_REV_COMMIT_BODY);\r
217                 format += log;\r
218         }\r
219         log.Format(_T("#<%c>%%m%%H%%x00"),LOG_REV_COMMIT_HASH);\r
220         format += log;\r
221         log.Format(_T("#<%c>%%P%%x00"),LOG_REV_COMMIT_PARENT);\r
222         format += log;\r
223 \r
224         if(IsFull)\r
225         {\r
226                 log.Format(_T("#<%c>%%x00"),LOG_REV_COMMIT_FILE);\r
227                 format += log;\r
228         }\r
229         return 0;\r
230 }\r
231 \r
232 int CGit::GetLog(BYTE_VECTOR& logOut, CString &hash,  CTGitPath *path ,int count,int mask)\r
233 {\r
234 \r
235         CString cmd;\r
236         CString log;\r
237         CString num;\r
238         CString since;\r
239 \r
240         CString file;\r
241 \r
242         if(path)\r
243                 file.Format(_T(" -- \"%s\""),path->GetGitPathString());\r
244         \r
245         if(count>0)\r
246                 num.Format(_T("-n%d"),count);\r
247 \r
248         CString param;\r
249 \r
250         if(mask& LOG_INFO_STAT )\r
251                 param += _T(" --numstat ");\r
252         if(mask& LOG_INFO_FILESTATE)\r
253                 param += _T(" --raw ");\r
254 \r
255         if(mask& LOG_INFO_FULLHISTORY)\r
256                 param += _T(" --full-history ");\r
257 \r
258         if(mask& LOG_INFO_BOUNDARY)\r
259                 param += _T(" --left-right --boundary ");\r
260 \r
261         if(mask& CGit::LOG_INFO_ALL_BRANCH)\r
262                 param += _T(" --all ");\r
263 \r
264         if(mask& CGit::LOG_INFO_DETECT_COPYRENAME)\r
265                 param += _T(" -C ");\r
266         \r
267         if(mask& CGit::LOG_INFO_DETECT_RENAME )\r
268                 param += _T(" -M ");\r
269 \r
270         if(mask& CGit::LOG_INFO_FIRST_PARENT )\r
271                 param += _T(" --first-parent ");\r
272         \r
273         if(mask& CGit::LOG_INFO_NO_MERGE )\r
274                 param += _T(" --no-merges ");\r
275 \r
276         if(mask& CGit::LOG_INFO_FOLLOW)\r
277                 param += _T(" --follow ");\r
278 \r
279         param+=hash;\r
280 \r
281         cmd.Format(_T("git.exe log %s -z --topo-order %s --parents --pretty=format:\""),\r
282                                 num,param);\r
283 \r
284         BuildOutputFormat(log,!(mask&CGit::LOG_INFO_ONLY_HASH));\r
285 \r
286         cmd += log;\r
287         cmd += CString(_T("\"  "))+hash+file;\r
288 \r
289         return Run(cmd,&logOut);\r
290 }\r
291 \r
292 #if 0\r
293 int CGit::GetShortLog(CString &logOut,CTGitPath * path, int count)\r
294 {\r
295         CString cmd;\r
296         CString log;\r
297         int n;\r
298         if(count<0)\r
299                 n=100;\r
300         else\r
301                 n=count;\r
302         cmd.Format(_T("git.exe log --left-right --boundary --topo-order -n%d --pretty=format:\""),n);\r
303         BuildOutputFormat(log,false);\r
304         cmd += log+_T("\"");\r
305         if (path)\r
306                 cmd+= _T("  -- \"")+path->GetGitPathString()+_T("\"");\r
307         //cmd += CString(_T("\" HEAD~40..HEAD"));\r
308         return Run(cmd,&logOut);\r
309 }\r
310 #endif\r
311 \r
312 #define BUFSIZE 512\r
313 void GetTempPath(CString &path)\r
314 {\r
315         TCHAR lpPathBuffer[BUFSIZE];\r
316         DWORD dwRetVal;\r
317         DWORD dwBufSize=BUFSIZE;\r
318         dwRetVal = GetTempPath(dwBufSize,     // length of the buffer\r
319                            lpPathBuffer); // buffer for path \r
320     if (dwRetVal > dwBufSize || (dwRetVal == 0))\r
321     {\r
322         path=_T("");\r
323     }\r
324         path.Format(_T("%s"),lpPathBuffer);\r
325 }\r
326 CString GetTempFile()\r
327 {\r
328         TCHAR lpPathBuffer[BUFSIZE];\r
329         DWORD dwRetVal;\r
330     DWORD dwBufSize=BUFSIZE;\r
331         TCHAR szTempName[BUFSIZE];  \r
332         UINT uRetVal;\r
333 \r
334         dwRetVal = GetTempPath(dwBufSize,     // length of the buffer\r
335                            lpPathBuffer); // buffer for path \r
336     if (dwRetVal > dwBufSize || (dwRetVal == 0))\r
337     {\r
338         return _T("");\r
339     }\r
340          // Create a temporary file. \r
341     uRetVal = GetTempFileName(lpPathBuffer, // directory for tmp files\r
342                               TEXT("Patch"),  // temp file name prefix \r
343                               0,            // create unique name \r
344                               szTempName);  // buffer for name \r
345 \r
346 \r
347     if (uRetVal == 0)\r
348     {\r
349         return _T("");\r
350     }\r
351 \r
352         return CString(szTempName);\r
353 \r
354 }\r
355 \r
356 int CGit::RunLogFile(CString cmd,CString &filename)\r
357 {\r
358         STARTUPINFO si;\r
359         PROCESS_INFORMATION pi;\r
360         si.cb=sizeof(STARTUPINFO);\r
361         GetStartupInfo(&si);\r
362 \r
363         SECURITY_ATTRIBUTES   psa={sizeof(psa),NULL,TRUE};;   \r
364         psa.bInheritHandle=TRUE;   \r
365     \r
366         HANDLE   houtfile=CreateFile(filename,GENERIC_WRITE,FILE_SHARE_READ   |   FILE_SHARE_WRITE,   \r
367                         &psa,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);   \r
368 \r
369 \r
370         si.wShowWindow=SW_HIDE;\r
371         si.dwFlags=STARTF_USESTDHANDLES|STARTF_USESHOWWINDOW;\r
372         si.hStdOutput   =   houtfile; \r
373         \r
374         if(!CreateProcess(NULL,(LPWSTR)cmd.GetString(), NULL,NULL,TRUE,NULL,NULL,(LPWSTR)m_CurrentDir.GetString(),&si,&pi))\r
375         {\r
376                 LPVOID lpMsgBuf;\r
377                 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,\r
378                         NULL,GetLastError(),MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),\r
379                         (LPTSTR)&lpMsgBuf,\r
380                         0,NULL);\r
381                 return GIT_ERROR_CREATE_PROCESS;\r
382         }\r
383         \r
384         WaitForSingleObject(pi.hProcess,INFINITE);   \r
385         \r
386         CloseHandle(pi.hThread);\r
387         CloseHandle(pi.hProcess);\r
388         CloseHandle(houtfile);\r
389         return GIT_SUCCESS;\r
390 //      return 0;\r
391 }\r
392 \r
393 git_revnum_t CGit::GetHash(CString &friendname)\r
394 {\r
395         CString cmd;\r
396         CString out;\r
397         cmd.Format(_T("git.exe rev-parse %s" ),friendname);\r
398         Run(cmd,&out,CP_UTF8);\r
399         int pos=out.ReverseFind(_T('\n'));\r
400         if(pos>0)\r
401                 return out.Left(pos);\r
402         return out;\r
403 }\r
404 \r
405 int CGit::GetTagList(STRING_VECTOR &list)\r
406 {\r
407         int ret;\r
408         CString cmd,output;\r
409         cmd=_T("git.exe tag -l");\r
410         int i=0;\r
411         ret=g_Git.Run(cmd,&output,CP_UTF8);\r
412         if(!ret)\r
413         {               \r
414                 int pos=0;\r
415                 CString one;\r
416                 while( pos>=0 )\r
417                 {\r
418                         i++;\r
419                         one=output.Tokenize(_T("\n"),pos);\r
420                         list.push_back(one);\r
421                 }\r
422         }\r
423         return ret;\r
424 }\r
425 \r
426 int CGit::GetBranchList(STRING_VECTOR &list,int *current,BRANCH_TYPE type)\r
427 {\r
428         int ret;\r
429         CString cmd,output;\r
430         cmd=_T("git.exe branch");\r
431 \r
432         if(type==(BRANCH_LOCAL|BRANCH_REMOTE))\r
433                 cmd+=_T(" -a");\r
434         else if(type==BRANCH_REMOTE)\r
435                 cmd+=_T(" -r");\r
436 \r
437         int i=0;\r
438         ret=g_Git.Run(cmd,&output,CP_UTF8);\r
439         if(!ret)\r
440         {               \r
441                 int pos=0;\r
442                 CString one;\r
443                 while( pos>=0 )\r
444                 {\r
445                         i++;\r
446                         one=output.Tokenize(_T("\n"),pos);\r
447                         list.push_back(one.Right(one.GetLength()-2));\r
448                         if(one[0] == _T('*'))\r
449                                 if(current)\r
450                                         *current=i;\r
451                 }\r
452         }\r
453         return ret;\r
454 }\r
455 \r
456 int CGit::GetRemoteList(STRING_VECTOR &list)\r
457 {\r
458         int ret;\r
459         CString cmd,output;\r
460         cmd=_T("git.exe config  --get-regexp remote.*.url");\r
461         ret=g_Git.Run(cmd,&output,CP_UTF8);\r
462         if(!ret)\r
463         {\r
464                 int pos=0;\r
465                 CString one;\r
466                 while( pos>=0 )\r
467                 {\r
468                         one=output.Tokenize(_T("\n"),pos);\r
469                         int start=one.Find(_T("."),0);\r
470                         if(start>0)\r
471                         {\r
472                                 CString url;\r
473                                 url=one.Right(one.GetLength()-start-1);\r
474                                 one=url;\r
475                                 one=one.Left(one.Find(_T("."),0));\r
476                                 list.push_back(one);\r
477                         }\r
478                 }\r
479         }\r
480         return ret;\r
481 }\r
482 \r
483 int CGit::GetMapHashToFriendName(MAP_HASH_NAME &map)\r
484 {\r
485         int ret;\r
486         CString cmd,output;\r
487         cmd=_T("git show-ref -d");\r
488         ret=g_Git.Run(cmd,&output,CP_UTF8);\r
489         if(!ret)\r
490         {\r
491                 int pos=0;\r
492                 CString one;\r
493                 while( pos>=0 )\r
494                 {\r
495                         one=output.Tokenize(_T("\n"),pos);\r
496                         int start=one.Find(_T(" "),0);\r
497                         if(start>0)\r
498                         {\r
499                                 CString name;\r
500                                 name=one.Right(one.GetLength()-start-1);\r
501 \r
502                                 CString hash;\r
503                                 hash=one.Left(start);\r
504 \r
505                                 map[hash].push_back(name);\r
506                         }\r
507                 }\r
508         }\r
509         return ret;\r
510 }\r
511 \r
512 BOOL CGit::CheckMsysGitDir()\r
513 {\r
514         CRegString msysdir=CRegString(REG_MSYSGIT_PATH,_T(""),FALSE,HKEY_LOCAL_MACHINE);\r
515         CString str=msysdir;\r
516         if(str.IsEmpty())\r
517         {\r
518                 CRegString msysinstalldir=CRegString(REG_MSYSGIT_INSTALL,_T(""),FALSE,HKEY_LOCAL_MACHINE);\r
519                 str=msysinstalldir;\r
520         // check it has a trailing blank\r
521         if (str.Right(1) != _T("\\"))\r
522         {\r
523             str += _T("\\");\r
524         }\r
525                 str+=_T("bin");\r
526                 msysdir=str;\r
527                 msysdir.write();\r
528 \r
529         }\r
530         //CGit::m_MsysGitPath=str;\r
531 \r
532         TCHAR *oldpath,*home;\r
533         size_t size;\r
534 \r
535         _tdupenv_s(&home,&size,_T("HOME")); \r
536         \r
537         if(home == NULL)\r
538         {               \r
539                 _tdupenv_s(&home,&size,_T("USERPROFILE")); \r
540                 _tputenv_s(_T("HOME"),home);\r
541         }\r
542         free(home);\r
543         \r
544         //set path\r
545         _tdupenv_s(&oldpath,&size,_T("PATH")); \r
546 \r
547         CString path;\r
548         CString unterminated_path = str;        // path to msysgit without semicolon\r
549         CString oldpath_s = oldpath;\r
550         path.Format(_T("%s;"),str);\r
551         // check msysgit not already in path\r
552         if ( oldpath_s.Find( path ) < 0  &&  oldpath_s.Right( unterminated_path.GetLength() ) != unterminated_path )\r
553         {\r
554                 // not already there, see if we have to take out one we added last time\r
555                 if ( ms_LastMsysGitDir != _T("") )\r
556                 {\r
557                         // we have added one so take it out\r
558                         int index = oldpath_s.Find( ms_LastMsysGitDir );\r
559                         if ( index >= 0 )\r
560                         {\r
561                                 oldpath_s = oldpath_s.Left( index ) + \r
562                                         oldpath_s.Right( oldpath_s.GetLength() - (index+ms_LastMsysGitDir.GetLength()) );\r
563                         }\r
564                 }\r
565                 // save the new msysdir path that we are about to add\r
566                 ms_LastMsysGitDir = path;\r
567                 // add the new one on the front of the existing path\r
568                 path+=oldpath_s;\r
569                 _tputenv_s(_T("PATH"),path);\r
570         }\r
571         free(oldpath);\r
572 \r
573         CString cmd,out;\r
574         cmd=_T("git.exe --version");\r
575         if(g_Git.Run(cmd,&out,CP_UTF8))\r
576         {\r
577                 return false;\r
578         }\r
579         else\r
580                 return true;\r
581 \r
582 }