OSDN Git Service

591878c0188b0d0ee65f5442c2c5cef3b7df519e
[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 #include <map>\r
8 #include "UnicodeUtils.h"\r
9 \r
10 int CGit::m_LogEncode=CP_UTF8;\r
11 \r
12 static LPTSTR nextpath(LPCTSTR src, LPTSTR dst, UINT maxlen)\r
13 {\r
14         LPCTSTR orgsrc;\r
15 \r
16         while (*src == _T(';'))\r
17                 src++;\r
18 \r
19         orgsrc = src;\r
20 \r
21         if (!--maxlen)\r
22                 goto nullterm;\r
23 \r
24         while (*src && *src != _T(';'))\r
25         {\r
26                 if (*src != _T('"'))\r
27                 {\r
28                         *dst++ = *src++;\r
29                         if (!--maxlen)\r
30                         {\r
31                                 orgsrc = src;\r
32                                 goto nullterm;\r
33                         }\r
34                 }\r
35                 else\r
36                 {\r
37                         src++;\r
38                         while (*src && *src != _T('"'))\r
39                         {\r
40                                 *dst++ = *src++;\r
41                                 if (!--maxlen)\r
42                                 {\r
43                                         orgsrc = src;\r
44                                         goto nullterm;\r
45                                 }\r
46                         }\r
47 \r
48                         if (*src)\r
49                                 src++;\r
50                 }\r
51         }\r
52 \r
53         while (*src == _T(';'))\r
54                 src++;\r
55 \r
56 nullterm:\r
57 \r
58         *dst = 0;\r
59 \r
60         return (orgsrc != src) ? (LPTSTR)src : NULL;\r
61 }\r
62 \r
63 static inline BOOL FileExists(LPCTSTR lpszFileName)\r
64 {\r
65         struct _stat st;\r
66         return _tstat(lpszFileName, &st) == 0;\r
67 }\r
68 \r
69 static BOOL FindGitPath()\r
70 {\r
71         size_t size;\r
72         _tgetenv_s(&size, NULL, 0, _T("PATH"));\r
73 \r
74         if (!size)\r
75         {\r
76                 return FALSE;\r
77         }\r
78 \r
79         TCHAR *env = (TCHAR*)alloca(size * sizeof(TCHAR));\r
80         _tgetenv_s(&size, env, size, _T("PATH"));\r
81 \r
82         TCHAR buf[_MAX_PATH];\r
83 \r
84         // search in all paths defined in PATH\r
85         while ((env = nextpath(env, buf, _MAX_PATH-1)) && *buf)\r
86         {\r
87                 TCHAR *pfin = buf + _tcslen(buf)-1;\r
88 \r
89                 // ensure trailing slash\r
90                 if (*pfin != _T('/') && *pfin != _T('\\'))\r
91                         _tcscpy(++pfin, _T("\\"));\r
92 \r
93                 const int len = _tcslen(buf);\r
94 \r
95                 if ((len + 7) < _MAX_PATH)\r
96                         _tcscpy(pfin+1, _T("git.exe"));\r
97                 else\r
98                         break;\r
99 \r
100                 if ( FileExists(buf) )\r
101                 {\r
102                         // dir found\r
103                         pfin[1] = 0;\r
104                         CGit::ms_LastMsysGitDir = buf;\r
105                         return TRUE;\r
106                 }\r
107         }\r
108 \r
109         return FALSE;\r
110 }\r
111 \r
112 \r
113 #define MAX_DIRBUFFER 1000\r
114 #define CALL_OUTPUT_READ_CHUNK_SIZE 1024\r
115 \r
116 CString CGit::ms_LastMsysGitDir;\r
117 CGit g_Git;\r
118 \r
119 // contains system environment that should be used by child processes (RunAsync)\r
120 // initialized by CheckMsysGitDir\r
121 static LPTSTR l_processEnv = NULL;\r
122 \r
123 \r
124 \r
125 CGit::CGit(void)\r
126 {\r
127         GetCurrentDirectory(MAX_DIRBUFFER,m_CurrentDir.GetBuffer(MAX_DIRBUFFER));\r
128         m_CurrentDir.ReleaseBuffer();\r
129 \r
130         CheckMsysGitDir();\r
131 }\r
132 \r
133 CGit::~CGit(void)\r
134 {\r
135 }\r
136 \r
137 static char g_Buffer[4096];\r
138 \r
139 int CGit::RunAsync(CString cmd,PROCESS_INFORMATION *piOut,HANDLE *hReadOut,CString *StdioFile)\r
140 {\r
141         SECURITY_ATTRIBUTES sa;\r
142         HANDLE hRead, hWrite;\r
143         HANDLE hStdioFile = NULL;\r
144 \r
145         sa.nLength = sizeof(SECURITY_ATTRIBUTES);\r
146         sa.lpSecurityDescriptor=NULL;\r
147         sa.bInheritHandle=TRUE;\r
148         if(!CreatePipe(&hRead,&hWrite,&sa,0))\r
149         {\r
150                 return GIT_ERROR_OPEN_PIP;\r
151         }\r
152         \r
153         if(StdioFile)\r
154         {\r
155                 hStdioFile=CreateFile(*StdioFile,GENERIC_WRITE,FILE_SHARE_READ   |   FILE_SHARE_WRITE,   \r
156                         &sa,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);  \r
157         }\r
158 \r
159         STARTUPINFO si;\r
160         PROCESS_INFORMATION pi;\r
161         si.cb=sizeof(STARTUPINFO);\r
162         GetStartupInfo(&si);\r
163 \r
164         si.hStdError=hWrite;\r
165         if(StdioFile)\r
166                 si.hStdOutput=hStdioFile;\r
167         else\r
168                 si.hStdOutput=hWrite;\r
169 \r
170         si.wShowWindow=SW_HIDE;\r
171         si.dwFlags=STARTF_USESTDHANDLES|STARTF_USESHOWWINDOW;\r
172 \r
173         LPTSTR pEnv = l_processEnv;\r
174         DWORD dwFlags = pEnv ? CREATE_UNICODE_ENVIRONMENT : 0;\r
175         \r
176         //DETACHED_PROCESS make ssh recognize that it has no console to launch askpass to input password. \r
177         dwFlags |= DETACHED_PROCESS | CREATE_NEW_PROCESS_GROUP; \r
178 \r
179         memset(&this->m_CurrentGitPi,0,sizeof(PROCESS_INFORMATION));\r
180 \r
181         if(!CreateProcess(NULL,(LPWSTR)cmd.GetString(), NULL,NULL,TRUE,dwFlags,pEnv,(LPWSTR)m_CurrentDir.GetString(),&si,&pi))\r
182         {\r
183                 LPVOID lpMsgBuf;\r
184                 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,\r
185                         NULL,GetLastError(),MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),\r
186                         (LPTSTR)&lpMsgBuf,\r
187                         0,NULL);\r
188                 return GIT_ERROR_CREATE_PROCESS;\r
189         }\r
190         \r
191         m_CurrentGitPi = pi;\r
192         \r
193         CloseHandle(hWrite);\r
194         if(piOut)\r
195                 *piOut=pi;\r
196         if(hReadOut)\r
197                 *hReadOut=hRead;\r
198         \r
199         return 0;\r
200 \r
201 }\r
202 //Must use sperate function to convert ANSI str to union code string\r
203 //Becuase A2W use stack as internal convert buffer. \r
204 void CGit::StringAppend(CString *str,BYTE *p,int code,int length)\r
205 {\r
206      //USES_CONVERSION;\r
207          //str->Append(A2W_CP((LPCSTR)p,code));\r
208         if(str == NULL)\r
209                 return ;\r
210 \r
211         WCHAR * buf;\r
212 \r
213         int len ;\r
214         if(length<0)\r
215                 len= strlen((const char*)p);\r
216         else\r
217                 len=length;\r
218         //if (len==0)\r
219         //      return ;\r
220         //buf = new WCHAR[len*4 + 1];\r
221         buf = str->GetBuffer(len*4+1+str->GetLength())+str->GetLength();\r
222         SecureZeroMemory(buf, (len*4 + 1)*sizeof(WCHAR));\r
223         MultiByteToWideChar(code, 0, (LPCSTR)p, len, buf, len*4);\r
224         str->ReleaseBuffer();\r
225         //str->Append(buf);\r
226         //delete buf;\r
227 }       \r
228 BOOL CGit::IsInitRepos()\r
229 {\r
230         CString cmdout;\r
231         cmdout.Empty();\r
232         if(g_Git.Run(_T("git.exe rev-parse --revs-only HEAD"),&cmdout,CP_UTF8))\r
233         {\r
234         //      CMessageBox::Show(NULL,cmdout,_T("TortoiseGit"),MB_OK);\r
235                 return TRUE;\r
236         }\r
237         if(cmdout.IsEmpty())\r
238                 return TRUE;\r
239 \r
240         return FALSE;\r
241 }\r
242 int CGit::Run(CGitCall* pcall)\r
243 {\r
244         PROCESS_INFORMATION pi;\r
245         HANDLE hRead;\r
246         if(RunAsync(pcall->GetCmd(),&pi,&hRead))\r
247                 return GIT_ERROR_CREATE_PROCESS;\r
248 \r
249         DWORD readnumber;\r
250         BYTE data[CALL_OUTPUT_READ_CHUNK_SIZE];\r
251         bool bAborted=false;\r
252         while(ReadFile(hRead,data,CALL_OUTPUT_READ_CHUNK_SIZE,&readnumber,NULL))\r
253         {\r
254                 //Todo: when OnOutputData() returns 'true', abort git-command. Send CTRL-C signal?\r
255                 if(!bAborted)//For now, flush output when command aborted.\r
256                         if(pcall->OnOutputData(data,readnumber))\r
257                                 bAborted=true;\r
258         }\r
259         if(!bAborted)\r
260                 pcall->OnEnd();\r
261 \r
262         \r
263         CloseHandle(pi.hThread);\r
264 \r
265         WaitForSingleObject(pi.hProcess, INFINITE);\r
266         DWORD exitcode =0;\r
267 \r
268         if(!GetExitCodeProcess(pi.hProcess,&exitcode))\r
269         {\r
270                 return GIT_ERROR_GET_EXIT_CODE;\r
271         }\r
272 \r
273         CloseHandle(pi.hProcess);\r
274 \r
275         CloseHandle(hRead);\r
276         return exitcode;\r
277 }\r
278 class CGitCall_ByteVector : public CGitCall\r
279 {\r
280 public:\r
281         CGitCall_ByteVector(CString cmd,BYTE_VECTOR* pvector):CGitCall(cmd),m_pvector(pvector){}\r
282         virtual bool OnOutputData(const BYTE* data, size_t size)\r
283         {\r
284                 size_t oldsize=m_pvector->size();\r
285                 m_pvector->resize(m_pvector->size()+size);\r
286                 memcpy(&*(m_pvector->begin()+oldsize),data,size);\r
287                 return false;\r
288         }\r
289         BYTE_VECTOR* m_pvector;\r
290 \r
291 };\r
292 int CGit::Run(CString cmd,BYTE_VECTOR *vector)\r
293 {\r
294         CGitCall_ByteVector call(cmd,vector);\r
295         return Run(&call);\r
296 }\r
297 int CGit::Run(CString cmd, CString* output,int code)\r
298 {\r
299         BYTE_VECTOR vector;\r
300         int ret;\r
301         ret=Run(cmd,&vector);\r
302 \r
303         vector.push_back(0);\r
304         \r
305         StringAppend(output,&(vector[0]),code);\r
306         return ret;\r
307 }\r
308 \r
309 CString CGit::GetUserName(void)\r
310 {\r
311         return GetConfigValue(L"user.name");\r
312 }\r
313 CString CGit::GetUserEmail(void)\r
314 {\r
315         return GetConfigValue(L"user.email");\r
316 }\r
317 \r
318 CString CGit::GetConfigValue(CString name)\r
319 {\r
320         CString configValue;\r
321         CString cmd;\r
322         cmd.Format(L"git.exe config %s", name);\r
323         Run(cmd,&configValue,CP_UTF8);\r
324         int start = 0;\r
325         return configValue.Tokenize(_T("\n"),start);\r
326 }\r
327 \r
328 \r
329 CString CGit::GetCurrentBranch(void)\r
330 {\r
331         CString output;\r
332         //Run(_T("git.exe branch"),&branch);\r
333 \r
334         int ret=g_Git.Run(_T("git.exe branch --no-color"),&output,CP_UTF8);\r
335         if(!ret)\r
336         {               \r
337                 int pos=0;\r
338                 CString one;\r
339                 while( pos>=0 )\r
340                 {\r
341                         //i++;\r
342                         one=output.Tokenize(_T("\n"),pos);\r
343                         //list.push_back(one.Right(one.GetLength()-2));\r
344                         if(one[0] == _T('*'))\r
345                                 return one.Right(one.GetLength()-2);\r
346                 }\r
347         }\r
348         return CString("");\r
349 }\r
350 \r
351 CString CGit::GetSymbolicRef(const wchar_t* symbolicRefName, bool bStripRefsHeads)\r
352 {\r
353         CString refName;\r
354         CString cmd;\r
355         cmd.Format(L"git symbolic-ref %s", symbolicRefName);\r
356         if(Run(cmd, &refName, CP_UTF8) != 0)\r
357                 return CString();//Error\r
358         int iStart = 0;\r
359         refName = refName.Tokenize(L"\n", iStart);\r
360         if(bStripRefsHeads)\r
361                 refName = StripRefName(refName);\r
362         return refName;\r
363 }\r
364 \r
365 CString CGit::GetFullRefName(CString shortRefName)\r
366 {\r
367         CString refName;\r
368         CString cmd;\r
369         cmd.Format(L"git rev-parse --symbolic-full-name %s", shortRefName);\r
370         if(Run(cmd, &refName, CP_UTF8) != 0)\r
371                 return CString();//Error\r
372         int iStart = 0;\r
373         return refName.Tokenize(L"\n", iStart);\r
374 }\r
375 \r
376 CString CGit::StripRefName(CString refName)\r
377 {\r
378         if(wcsncmp(refName, L"refs/heads/", 11) == 0)\r
379                 refName = refName.Mid(11);\r
380         else if(wcsncmp(refName, L"refs/", 5) == 0)\r
381                 refName = refName.Mid(5);\r
382         return refName;\r
383 }\r
384 \r
385 int CGit::GetCurrentBranchFromFile(const CString &sProjectRoot, CString &sBranchOut)\r
386 {\r
387         // read current branch name like git-gui does, by parsing the .git/HEAD file directly\r
388 \r
389         if ( sProjectRoot.IsEmpty() )\r
390                 return -1;\r
391 \r
392         CString sHeadFile = sProjectRoot + _T("\\") + g_GitAdminDir.GetAdminDirName() + _T("\\HEAD");\r
393 \r
394         FILE *pFile;\r
395         _tfopen_s(&pFile, sHeadFile.GetString(), _T("r"));\r
396 \r
397         if (!pFile)\r
398         {\r
399                 return -1;\r
400         }\r
401 \r
402         char s[256] = {0};\r
403     fgets(s, sizeof(s), pFile);\r
404 \r
405         fclose(pFile);\r
406 \r
407         const char *pfx = "ref: refs/heads/";\r
408         const int len = 16;//strlen(pfx)\r
409 \r
410         if ( !strncmp(s, pfx, len) )\r
411         {\r
412                 //# We're on a branch.  It might not exist.  But\r
413                 //# HEAD looks good enough to be a branch.\r
414                 sBranchOut = s + len;\r
415                 sBranchOut.TrimRight(_T(" \r\n\t"));\r
416 \r
417                 if ( sBranchOut.IsEmpty() )\r
418                         return -1;\r
419         }\r
420         else\r
421         {\r
422                 //# Assume this is a detached head.\r
423                 sBranchOut = "HEAD";\r
424 \r
425                 return 1;\r
426         }\r
427 \r
428         return 0;\r
429 }\r
430 \r
431 int CGit::BuildOutputFormat(CString &format,bool IsFull)\r
432 {\r
433         CString log;\r
434         log.Format(_T("#<%c>%%x00"),LOG_REV_ITEM_BEGIN);\r
435         format += log;\r
436         if(IsFull)\r
437         {\r
438                 log.Format(_T("#<%c>%%an%%x00"),LOG_REV_AUTHOR_NAME);\r
439                 format += log;\r
440                 log.Format(_T("#<%c>%%ae%%x00"),LOG_REV_AUTHOR_EMAIL);\r
441                 format += log;\r
442                 log.Format(_T("#<%c>%%ai%%x00"),LOG_REV_AUTHOR_DATE);\r
443                 format += log;\r
444                 log.Format(_T("#<%c>%%cn%%x00"),LOG_REV_COMMIT_NAME);\r
445                 format += log;\r
446                 log.Format(_T("#<%c>%%ce%%x00"),LOG_REV_COMMIT_EMAIL);\r
447                 format += log;\r
448                 log.Format(_T("#<%c>%%ci%%x00"),LOG_REV_COMMIT_DATE);\r
449                 format += log;\r
450                 log.Format(_T("#<%c>%%b%%x00"),LOG_REV_COMMIT_BODY);\r
451                 format += log;\r
452         }\r
453         \r
454         log.Format(_T("#<%c>%%m%%H%%x00"),LOG_REV_COMMIT_HASH);\r
455         format += log;\r
456         log.Format(_T("#<%c>%%P%%x00"),LOG_REV_COMMIT_PARENT);\r
457         format += log;\r
458         log.Format(_T("#<%c>%%s%%x00"),LOG_REV_COMMIT_SUBJECT);\r
459         format += log;\r
460 \r
461         if(IsFull)\r
462         {\r
463                 log.Format(_T("#<%c>%%x00"),LOG_REV_COMMIT_FILE);\r
464                 format += log;\r
465         }\r
466         return 0;\r
467 }\r
468 \r
469 int CGit::GetLog(BYTE_VECTOR& logOut, CString &hash,  CTGitPath *path ,int count,int mask,CString *from,CString *to)\r
470 {\r
471         CGitCall_ByteVector gitCall(CString(),&logOut);\r
472         return GetLog(&gitCall,hash,path,count,mask,from,to);\r
473 }\r
474 \r
475 //int CGit::GetLog(CGitCall* pgitCall, CString &hash,  CTGitPath *path ,int count,int mask)\r
476 int CGit::GetLog(CGitCall* pgitCall, CString &hash, CTGitPath *path, int count, int mask,CString *from,CString *to)\r
477 {\r
478 \r
479         CString cmd;\r
480         CString log;\r
481         CString num;\r
482         CString since;\r
483 \r
484         CString file;\r
485 \r
486         if(path)\r
487                 file.Format(_T(" -- \"%s\""),path->GetGitPathString());\r
488         \r
489         if(count>0)\r
490                 num.Format(_T("-n%d"),count);\r
491 \r
492         CString param;\r
493 \r
494         if(mask& LOG_INFO_STAT )\r
495                 param += _T(" --numstat ");\r
496         if(mask& LOG_INFO_FILESTATE)\r
497                 param += _T(" --raw ");\r
498 \r
499         if(mask& LOG_INFO_FULLHISTORY)\r
500                 param += _T(" --full-history ");\r
501 \r
502         if(mask& LOG_INFO_BOUNDARY)\r
503                 param += _T(" --left-right --boundary ");\r
504 \r
505         if(mask& CGit::LOG_INFO_ALL_BRANCH)\r
506                 param += _T(" --all ");\r
507 \r
508         if(mask& CGit::LOG_INFO_DETECT_COPYRENAME)\r
509                 param += _T(" -C ");\r
510         \r
511         if(mask& CGit::LOG_INFO_DETECT_RENAME )\r
512                 param += _T(" -M ");\r
513 \r
514         if(mask& CGit::LOG_INFO_FIRST_PARENT )\r
515                 param += _T(" --first-parent ");\r
516         \r
517         if(mask& CGit::LOG_INFO_NO_MERGE )\r
518                 param += _T(" --no-merges ");\r
519 \r
520         if(mask& CGit::LOG_INFO_FOLLOW)\r
521                 param += _T(" --follow ");\r
522 \r
523         if(mask& CGit::LOG_INFO_SHOW_MERGEDFILE)\r
524                 param += _T(" -c ");\r
525 \r
526         if(mask& CGit::LOG_INFO_FULL_DIFF)\r
527                 param += _T(" --full-diff ");\r
528 \r
529         if(from != NULL && to != NULL)\r
530         {\r
531                 CString range;\r
532                 range.Format(_T(" %s..%s "),*from,*to);\r
533                 param += range;\r
534         }\r
535         param+=hash;\r
536 \r
537         cmd.Format(_T("git.exe log %s -z --topo-order %s --parents --pretty=format:\""),\r
538                                 num,param);\r
539 \r
540         BuildOutputFormat(log,!(mask&CGit::LOG_INFO_ONLY_HASH));\r
541 \r
542         cmd += log;\r
543         cmd += CString(_T("\"  "))+hash+file;\r
544 \r
545         pgitCall->SetCmd(cmd);\r
546 \r
547         return Run(pgitCall);\r
548 //      return Run(cmd,&logOut);\r
549 }\r
550 \r
551 #if 0\r
552 int CGit::GetShortLog(CString &logOut,CTGitPath * path, int count)\r
553 {\r
554         CString cmd;\r
555         CString log;\r
556         int n;\r
557         if(count<0)\r
558                 n=100;\r
559         else\r
560                 n=count;\r
561         cmd.Format(_T("git.exe log --left-right --boundary --topo-order -n%d --pretty=format:\""),n);\r
562         BuildOutputFormat(log,false);\r
563         cmd += log+_T("\"");\r
564         if (path)\r
565                 cmd+= _T("  -- \"")+path->GetGitPathString()+_T("\"");\r
566         //cmd += CString(_T("\" HEAD~40..HEAD"));\r
567         return Run(cmd,&logOut);\r
568 }\r
569 #endif\r
570 \r
571 #define BUFSIZE 512\r
572 void GetTempPath(CString &path)\r
573 {\r
574         TCHAR lpPathBuffer[BUFSIZE];\r
575         DWORD dwRetVal;\r
576         DWORD dwBufSize=BUFSIZE;\r
577         dwRetVal = GetTempPath(dwBufSize,     // length of the buffer\r
578                            lpPathBuffer); // buffer for path \r
579     if (dwRetVal > dwBufSize || (dwRetVal == 0))\r
580     {\r
581         path=_T("");\r
582     }\r
583         path.Format(_T("%s"),lpPathBuffer);\r
584 }\r
585 CString GetTempFile()\r
586 {\r
587         TCHAR lpPathBuffer[BUFSIZE];\r
588         DWORD dwRetVal;\r
589     DWORD dwBufSize=BUFSIZE;\r
590         TCHAR szTempName[BUFSIZE];  \r
591         UINT uRetVal;\r
592 \r
593         dwRetVal = GetTempPath(dwBufSize,     // length of the buffer\r
594                            lpPathBuffer); // buffer for path \r
595     if (dwRetVal > dwBufSize || (dwRetVal == 0))\r
596     {\r
597         return _T("");\r
598     }\r
599          // Create a temporary file. \r
600     uRetVal = GetTempFileName(lpPathBuffer, // directory for tmp files\r
601                               TEXT("Patch"),  // temp file name prefix \r
602                               0,            // create unique name \r
603                               szTempName);  // buffer for name \r
604 \r
605 \r
606     if (uRetVal == 0)\r
607     {\r
608         return _T("");\r
609     }\r
610 \r
611         return CString(szTempName);\r
612 \r
613 }\r
614 \r
615 int CGit::RunLogFile(CString cmd,CString &filename)\r
616 {\r
617         STARTUPINFO si;\r
618         PROCESS_INFORMATION pi;\r
619         si.cb=sizeof(STARTUPINFO);\r
620         GetStartupInfo(&si);\r
621 \r
622         SECURITY_ATTRIBUTES   psa={sizeof(psa),NULL,TRUE};;   \r
623         psa.bInheritHandle=TRUE;   \r
624     \r
625         HANDLE   houtfile=CreateFile(filename,GENERIC_WRITE,FILE_SHARE_READ   |   FILE_SHARE_WRITE,   \r
626                         &psa,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);   \r
627 \r
628 \r
629         si.wShowWindow=SW_HIDE;\r
630         si.dwFlags=STARTF_USESTDHANDLES|STARTF_USESHOWWINDOW;\r
631         si.hStdOutput   =   houtfile; \r
632         \r
633         if(!CreateProcess(NULL,(LPWSTR)cmd.GetString(), NULL,NULL,TRUE,NULL,NULL,(LPWSTR)m_CurrentDir.GetString(),&si,&pi))\r
634         {\r
635                 LPVOID lpMsgBuf;\r
636                 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,\r
637                         NULL,GetLastError(),MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),\r
638                         (LPTSTR)&lpMsgBuf,\r
639                         0,NULL);\r
640                 return GIT_ERROR_CREATE_PROCESS;\r
641         }\r
642         \r
643         WaitForSingleObject(pi.hProcess,INFINITE);   \r
644         \r
645         CloseHandle(pi.hThread);\r
646         CloseHandle(pi.hProcess);\r
647         CloseHandle(houtfile);\r
648         return GIT_SUCCESS;\r
649 //      return 0;\r
650 }\r
651 \r
652 git_revnum_t CGit::GetHash(const CString &friendname)\r
653 {\r
654         CString cmd;\r
655         CString out;\r
656         cmd.Format(_T("git.exe rev-parse %s" ),friendname);\r
657         Run(cmd,&out,CP_UTF8);\r
658 //      int pos=out.ReverseFind(_T('\n'));\r
659         int pos=out.FindOneOf(_T("\r\n"));\r
660         if(pos>0)\r
661                 return out.Left(pos);\r
662         return out;\r
663 }\r
664 \r
665 int CGit::GetCommitDiffList(CString &rev1,CString &rev2,CTGitPathList &outputlist)\r
666 {\r
667         CString cmd;\r
668         \r
669         if(rev1 == GIT_REV_ZERO || rev2 == GIT_REV_ZERO)\r
670         {\r
671                 //rev1=+_T("");\r
672                 if(rev1 == GIT_REV_ZERO)\r
673                         cmd.Format(_T("git.exe diff -r --raw -C -M --numstat -z %s"),rev2);\r
674                 else\r
675                         cmd.Format(_T("git.exe diff -r -R --raw -C -M --numstat -z %s"),rev1);\r
676         }else\r
677         {\r
678                 cmd.Format(_T("git.exe diff-tree -r --raw -C -M --numstat -z %s %s"),rev2,rev1);\r
679         }\r
680 \r
681         BYTE_VECTOR out;\r
682         if(g_Git.Run(cmd,&out))\r
683                 return -1;\r
684 \r
685         outputlist.ParserFromLog(out);\r
686 \r
687 }\r
688 \r
689 int CGit::GetTagList(STRING_VECTOR &list)\r
690 {\r
691         int ret;\r
692         CString cmd,output;\r
693         cmd=_T("git.exe tag -l");\r
694         int i=0;\r
695         ret=g_Git.Run(cmd,&output,CP_UTF8);\r
696         if(!ret)\r
697         {               \r
698                 int pos=0;\r
699                 CString one;\r
700                 while( pos>=0 )\r
701                 {\r
702                         i++;\r
703                         one=output.Tokenize(_T("\n"),pos);\r
704                         list.push_back(one);\r
705                 }\r
706         }\r
707         return ret;\r
708 }\r
709 \r
710 int CGit::GetBranchList(STRING_VECTOR &list,int *current,BRANCH_TYPE type)\r
711 {\r
712         int ret;\r
713         CString cmd,output;\r
714         cmd=_T("git.exe branch --no-color");\r
715 \r
716         if(type==(BRANCH_LOCAL|BRANCH_REMOTE))\r
717                 cmd+=_T(" -a");\r
718         else if(type==BRANCH_REMOTE)\r
719                 cmd+=_T(" -r");\r
720 \r
721         int i=0;\r
722         ret=g_Git.Run(cmd,&output,CP_UTF8);\r
723         if(!ret)\r
724         {               \r
725                 int pos=0;\r
726                 CString one;\r
727                 while( pos>=0 )\r
728                 {\r
729                         one=output.Tokenize(_T("\n"),pos);\r
730                         one.Trim(L" \r\n\t");\r
731                         if(one.Find(L" -> ") >= 0 || one.IsEmpty())\r
732                                 continue; // skip something like: refs/origin/HEAD -> refs/origin/master\r
733                         if(one[0] == _T('*'))\r
734                         {\r
735                                 if(current)\r
736                                         *current=i;\r
737                                 one = one.Mid(2);\r
738                         }\r
739                         list.push_back(one);\r
740                         i++;\r
741                 }\r
742         }\r
743         return ret;\r
744 }\r
745 \r
746 int CGit::GetRemoteList(STRING_VECTOR &list)\r
747 {\r
748         int ret;\r
749         CString cmd,output;\r
750         cmd=_T("git.exe config  --get-regexp remote.*.url");\r
751         ret=g_Git.Run(cmd,&output,CP_UTF8);\r
752         if(!ret)\r
753         {\r
754                 int pos=0;\r
755                 CString one;\r
756                 while( pos>=0 )\r
757                 {\r
758                         one=output.Tokenize(_T("\n"),pos);\r
759                         int start=one.Find(_T("."),0);\r
760                         if(start>0)\r
761                         {\r
762                                 CString url;\r
763                                 url=one.Right(one.GetLength()-start-1);\r
764                                 one=url;\r
765                                 one=one.Left(one.Find(_T("."),0));\r
766                                 list.push_back(one);\r
767                         }\r
768                 }\r
769         }\r
770         return ret;\r
771 }\r
772 \r
773 int CGit::GetRefList(STRING_VECTOR &list)\r
774 {\r
775         int ret;\r
776         CString cmd,output;\r
777         cmd=_T("git show-ref -d");\r
778         ret=g_Git.Run(cmd,&output,CP_UTF8);\r
779         if(!ret)\r
780         {\r
781                 int pos=0;\r
782                 CString one;\r
783                 while( pos>=0 )\r
784                 {\r
785                         one=output.Tokenize(_T("\n"),pos);\r
786                         int start=one.Find(_T(" "),0);\r
787                         if(start>0)\r
788                         {\r
789                                 CString name;\r
790                                 name=one.Right(one.GetLength()-start-1);\r
791                                 list.push_back(name);\r
792                         }\r
793                 }\r
794         }\r
795         return ret;\r
796 }\r
797 int CGit::GetMapHashToFriendName(MAP_HASH_NAME &map)\r
798 {\r
799         int ret;\r
800         CString cmd,output;\r
801         cmd=_T("git show-ref -d");\r
802         ret=g_Git.Run(cmd,&output,CP_UTF8);\r
803         if(!ret)\r
804         {\r
805                 int pos=0;\r
806                 CString one;\r
807                 while( pos>=0 )\r
808                 {\r
809                         one=output.Tokenize(_T("\n"),pos);\r
810                         int start=one.Find(_T(" "),0);\r
811                         if(start>0)\r
812                         {\r
813                                 CString name;\r
814                                 name=one.Right(one.GetLength()-start-1);\r
815 \r
816                                 CString hash;\r
817                                 hash=one.Left(start);\r
818 \r
819                                 map[hash].push_back(name);\r
820                         }\r
821                 }\r
822         }\r
823         return ret;\r
824 }\r
825 \r
826 BOOL CGit::CheckMsysGitDir()\r
827 {\r
828         static BOOL bInitialized = FALSE;\r
829 \r
830         if (bInitialized)\r
831         {\r
832                 return TRUE;\r
833         }\r
834 \r
835         TCHAR *oldpath,*home;\r
836         size_t homesize,size,httpsize;\r
837 \r
838         // set HOME if not set already\r
839         _tgetenv_s(&homesize, NULL, 0, _T("HOME"));\r
840         if (!homesize)\r
841         {\r
842                 _tdupenv_s(&home,&size,_T("USERPROFILE")); \r
843                 _tputenv_s(_T("HOME"),home);\r
844                 free(home);\r
845         }\r
846         CString str;\r
847 \r
848 #ifndef _TORTOISESHELL\r
849         //set http_proxy\r
850         _tgetenv_s(&httpsize, NULL, 0, _T("http_proxy"));\r
851         if (!httpsize)\r
852         {\r
853                 CString regServeraddress_copy = CRegString(_T("Software\\TortoiseGit\\Servers\\global\\http-proxy-host"), _T(""));\r
854                 CString regServerport_copy = CRegString(_T("Software\\TortoiseGit\\Servers\\global\\http-proxy-port"), _T(""));\r
855                 CString regUsername_copy = CRegString(_T("Software\\TortoiseGit\\Servers\\global\\http-proxy-username"), _T(""));\r
856                 CString regPassword_copy = CRegString(_T("Software\\TortoiseGit\\Servers\\global\\http-proxy-password"), _T(""));\r
857                 CString regTimeout_copy = CRegString(_T("Software\\TortoiseGit\\Servers\\global\\http-proxy-timeout"), _T(""));\r
858                 CString regExceptions_copy = CRegString(_T("Software\\TortoiseGit\\Servers\\global\\http-proxy-exceptions"), _T(""));\r
859 \r
860                 CString http_proxy;\r
861                 if(!regServeraddress_copy.IsEmpty())\r
862                 {\r
863                         if(regServeraddress_copy.Left(4) != _T("http"))\r
864                                 http_proxy=_T("http://");\r
865 \r
866                         if(!regUsername_copy.IsEmpty())\r
867                         {\r
868                                 http_proxy += regUsername_copy;\r
869                                 http_proxy += _T(":")+regPassword_copy;\r
870                                 http_proxy += _T("@");\r
871                         }\r
872                         http_proxy+=regServeraddress_copy;\r
873                         if(!regServerport_copy.IsEmpty())\r
874                         {\r
875                                 http_proxy +=_T(":")+regServerport_copy;\r
876                         }\r
877                         _tputenv_s(_T("http_proxy"),http_proxy);\r
878                 }\r
879         }\r
880         //setup ssh client\r
881         CString sshclient=CRegString(_T("Software\\TortoiseGit\\SSH"));\r
882 \r
883         if(!sshclient.IsEmpty())\r
884         {\r
885                 _tputenv_s(_T("GIT_SSH"),sshclient);\r
886                 \r
887                 //Setup SVN_SSH\r
888                 CString ssh=sshclient;\r
889                 ssh.Replace(_T("/"),_T("\\"));\r
890                 ssh.Replace(_T("\\"),_T("\\\\"));\r
891                 ssh=CString(_T("\""))+ssh+_T('\"');\r
892                 _tputenv_s(_T("SVN_SSH"),ssh);\r
893 \r
894         }else\r
895         {\r
896                 TCHAR sPlink[MAX_PATH];\r
897                 GetModuleFileName(NULL, sPlink, _countof(sPlink));\r
898                 LPTSTR ptr = _tcsrchr(sPlink, _T('\\'));\r
899                 if (ptr) {\r
900                         _tcscpy(ptr + 1, _T("TortoisePlink.exe"));\r
901                         _tputenv_s(_T("GIT_SSH"), sPlink);\r
902 \r
903                         //Setup SVN_SSH\r
904                         CString ssh=sPlink;\r
905                         ssh.Replace(_T("/"),_T("\\"));\r
906                         ssh.Replace(_T("\\"),_T("\\\\"));\r
907                         ssh=CString(_T("\""))+ssh+_T('\"');\r
908                         _tputenv_s(_T("SVN_SSH"),ssh);\r
909                 }\r
910         }\r
911 \r
912         {\r
913                 TCHAR sAskPass[MAX_PATH];\r
914                 GetModuleFileName(NULL, sAskPass, _countof(sAskPass));\r
915                 LPTSTR ptr = _tcsrchr(sAskPass, _T('\\'));\r
916                 if (ptr) \r
917                 {\r
918                         _tcscpy(ptr + 1, _T("SshAskPass.exe"));\r
919                         _tputenv_s(_T("DISPLAY"),_T(":9999"));\r
920                         _tputenv_s(_T("SSH_ASKPASS"),sAskPass);\r
921                 }\r
922         }\r
923         // search PATH if git/bin directory is alredy present\r
924         if ( FindGitPath() )\r
925         {\r
926                 bInitialized = TRUE;\r
927                 return TRUE;\r
928         }\r
929 \r
930         // add git/bin path to PATH\r
931 \r
932         CRegString msysdir=CRegString(REG_MSYSGIT_PATH,_T(""),FALSE);\r
933         str=msysdir;\r
934         if(str.IsEmpty())\r
935         {\r
936                 CRegString msysinstalldir=CRegString(REG_MSYSGIT_INSTALL,_T(""),FALSE,HKEY_LOCAL_MACHINE);\r
937                 str=msysinstalldir;\r
938                 if ( !str.IsEmpty() )\r
939                 {\r
940                         str += (str[str.GetLength()-1] != '\\') ? "\\bin" : "bin";\r
941                         msysdir=str;\r
942                         msysdir.write();\r
943                 }\r
944                 else\r
945                 {\r
946                         return false;\r
947                 }\r
948         }\r
949 #endif\r
950         //CGit::m_MsysGitPath=str;\r
951 \r
952         //set path\r
953 \r
954         _tdupenv_s(&oldpath,&size,_T("PATH")); \r
955 \r
956         CString path;\r
957         path.Format(_T("%s;%s"),oldpath,str);\r
958 \r
959         _tputenv_s(_T("PATH"),path);\r
960 \r
961         CString sOldPath = oldpath;\r
962         free(oldpath);\r
963 \r
964 \r
965     if( !FindGitPath() )\r
966         {\r
967                 if(!homesize)\r
968                 {\r
969                         _tputenv_s(_T("HOME"),_T(""));\r
970                 }\r
971                 return false;\r
972         }\r
973         else\r
974         {\r
975 #ifdef _TORTOISESHELL\r
976                 l_processEnv = GetEnvironmentStrings();\r
977                 // updated environment is now duplicated for use in CreateProcess, restore original PATH for current process\r
978                 _tputenv_s(_T("PATH"),sOldPath);\r
979                 if(!homesize)\r
980                 {\r
981                         _tputenv_s(_T("HOME"),_T(""));\r
982                 }\r
983 #endif\r
984 \r
985                 bInitialized = TRUE;\r
986                 return true;\r
987         }\r
988 }\r
989 \r
990 \r
991 class CGitCall_EnumFiles : public CGitCall\r
992 {\r
993 public:\r
994         CGitCall_EnumFiles(const TCHAR *pszProjectPath, const TCHAR *pszSubPath, unsigned int nFlags, WGENUMFILECB *pEnumCb, void *pUserData)\r
995         :       m_pszProjectPath(pszProjectPath),\r
996                 m_pszSubPath(pszSubPath),\r
997                 m_nFlags(nFlags),\r
998                 m_pEnumCb(pEnumCb),\r
999                 m_pUserData(pUserData)\r
1000         {\r
1001         }\r
1002 \r
1003         typedef std::map<CStringA,char> TStrCharMap;\r
1004 \r
1005         const TCHAR *   m_pszProjectPath;\r
1006         const TCHAR *   m_pszSubPath;\r
1007         unsigned int    m_nFlags;\r
1008         WGENUMFILECB *  m_pEnumCb;\r
1009         void *                  m_pUserData;\r
1010 \r
1011         BYTE_VECTOR             m_DataCollector;\r
1012 \r
1013         virtual bool    OnOutputData(const BYTE* data, size_t size)\r
1014         {\r
1015                 m_DataCollector.append(data,size);\r
1016                 while(true)\r
1017                 {\r
1018                         // lines from igit.exe are 0 terminated\r
1019                         int found=m_DataCollector.findData((const BYTE*)"",1);\r
1020                         if(found<0)\r
1021                                 return false;\r
1022                         OnSingleLine( (LPCSTR)&*m_DataCollector.begin() );\r
1023                         m_DataCollector.erase(m_DataCollector.begin(), m_DataCollector.begin()+found+1);\r
1024                 }\r
1025                 return false;//Should never reach this\r
1026         }\r
1027         virtual void    OnEnd()\r
1028         {\r
1029         }\r
1030 \r
1031         BYTE HexChar(char ch)\r
1032         {\r
1033                 if (ch >= '0' && ch <= '9')\r
1034                         return (UINT)(ch - '0');\r
1035                 else if (ch >= 'A' && ch <= 'F')\r
1036                         return (UINT)(ch - 'A') + 10;\r
1037                 else if (ch >= 'a' && ch <= 'f')\r
1038                         return (UINT)(ch - 'a') + 10;\r
1039                 else\r
1040                         return 0;\r
1041         }\r
1042 \r
1043         bool OnSingleLine(LPCSTR line)\r
1044         {\r
1045                 //Parse single line\r
1046 \r
1047                 wgFile_s fileStatus;\r
1048 \r
1049                 // file/dir type\r
1050 \r
1051                 fileStatus.nFlags = 0;\r
1052                 if (*line == 'D')\r
1053                         fileStatus.nFlags |= WGFF_Directory;\r
1054                 else if (*line != 'F')\r
1055                         // parse error\r
1056                         return false;\r
1057                 line += 2;\r
1058 \r
1059                 // status\r
1060 \r
1061                 fileStatus.nStatus = WGFS_Unknown;\r
1062                 switch (*line)\r
1063                 {\r
1064                 case 'N': fileStatus.nStatus = WGFS_Normal; break;\r
1065                 case 'M': fileStatus.nStatus = WGFS_Modified; break;\r
1066                 case 'S': fileStatus.nStatus = WGFS_Staged; break;\r
1067                 case 'A': fileStatus.nStatus = WGFS_Added; break;\r
1068                 case 'C': fileStatus.nStatus = WGFS_Conflicted; break;\r
1069                 case 'D': fileStatus.nStatus = WGFS_Deleted; break;\r
1070                 case 'I': fileStatus.nStatus = WGFS_Ignored; break;\r
1071                 case 'U': fileStatus.nStatus = WGFS_Unversioned; break;\r
1072                 case 'E': fileStatus.nStatus = WGFS_Empty; break;\r
1073                 case '?': fileStatus.nStatus = WGFS_Unknown; break;\r
1074                 default:\r
1075                         // parse error\r
1076                         return false;\r
1077                 }\r
1078                 line += 2;\r
1079 \r
1080                 // file sha1\r
1081 \r
1082                 BYTE sha1[20];\r
1083                 fileStatus.sha1 = NULL;\r
1084                 if ( !(fileStatus.nFlags & WGFF_Directory) )\r
1085                 {\r
1086                         for (int i=0; i<20; i++)\r
1087                         {\r
1088                                 sha1[i]  = (HexChar(line[0])<<4)&0xF0;\r
1089                                 sha1[i] |= HexChar(line[1])&0xF;\r
1090 \r
1091                                 line += 2;\r
1092                         }\r
1093 \r
1094                         line++;\r
1095                 }\r
1096 \r
1097                 // filename\r
1098                 int len = strlen(line);\r
1099                 if (len && len < 2048)\r
1100                 {\r
1101                         WCHAR *buf = (WCHAR*)alloca((len*4+2)*sizeof(WCHAR));\r
1102                         *buf = 0;\r
1103                         MultiByteToWideChar(CP_ACP, 0, line, len+1, buf, len*4+1);\r
1104                         fileStatus.sFileName = buf;\r
1105 \r
1106                         if (*buf && (*m_pEnumCb)(&fileStatus,m_pUserData))\r
1107                                 return false;\r
1108                 }\r
1109 \r
1110                 return true;\r
1111         }\r
1112 };\r
1113 \r
1114 BOOL CGit::EnumFiles(const TCHAR *pszProjectPath, const TCHAR *pszSubPath, unsigned int nFlags, WGENUMFILECB *pEnumCb, void *pUserData)\r
1115 {\r
1116         if(!pszProjectPath || *pszProjectPath=='\0')\r
1117                 return FALSE;\r
1118 \r
1119         CGitCall_EnumFiles W_GitCall(pszProjectPath,pszSubPath,nFlags,pEnumCb,pUserData);\r
1120         CString cmd;\r
1121 \r
1122 /*      char W_szToDir[MAX_PATH];\r
1123         strncpy(W_szToDir,pszProjectPath,sizeof(W_szToDir)-1);\r
1124         if(W_szToDir[strlen(W_szToDir)-1]!='\\')\r
1125                 strncat(W_szToDir,"\\",sizeof(W_szToDir)-1);\r
1126 \r
1127         SetCurrentDirectoryA(W_szToDir);\r
1128         GetCurrentDirectoryA(sizeof(W_szToDir)-1,W_szToDir);\r
1129 */\r
1130         SetCurrentDir(pszProjectPath);\r
1131 \r
1132         CString sMode;\r
1133         if (nFlags)\r
1134         {\r
1135                 if (nFlags & WGEFF_NoRecurse) sMode += _T("r");\r
1136                 if (nFlags & WGEFF_FullPath) sMode += _T("f");\r
1137                 if (nFlags & WGEFF_DirStatusDelta) sMode += _T("d");\r
1138                 if (nFlags & WGEFF_DirStatusAll) sMode += _T("D");\r
1139                 if (nFlags & WGEFF_EmptyAsNormal) sMode += _T("e");\r
1140                 if (nFlags & WGEFF_SingleFile) sMode += _T("s");\r
1141         }\r
1142         else\r
1143         {\r
1144                 sMode = _T("-");\r
1145         }\r
1146 \r
1147         // NOTE: there seems to be some issue with msys based app receiving backslash on commandline, at least\r
1148         // if followed by " like for example 'igit "C:\"', the commandline igit receives is 'igit.exe C:" status' with\r
1149         // the 'C:" status' part as a single arg, Maybe it uses unix style processing. In order to avoid this just\r
1150         // use forward slashes for supplied project and sub paths\r
1151 \r
1152         CString sProjectPath = pszProjectPath;\r
1153         sProjectPath.Replace(_T('\\'), _T('/'));\r
1154 \r
1155         if (pszSubPath)\r
1156         {\r
1157                 CString sSubPath = pszSubPath;\r
1158                 sSubPath.Replace(_T('\\'), _T('/'));\r
1159 \r
1160                 cmd.Format(_T("igit.exe \"%s\" status %s \"%s\""), sProjectPath, sMode, sSubPath);\r
1161         }\r
1162         else\r
1163         {\r
1164                 cmd.Format(_T("igit.exe \"%s\" status %s"), sProjectPath, sMode);\r
1165         }\r
1166 \r
1167         //OutputDebugStringA("---");OutputDebugStringW(cmd);OutputDebugStringA("\r\n");\r
1168 \r
1169         W_GitCall.SetCmd(cmd);\r
1170         // NOTE: should igit get added as a part of msysgit then use below line instead of the above one\r
1171         //W_GitCall.SetCmd(CGit::ms_LastMsysGitDir + cmd);\r
1172 \r
1173         if ( Run(&W_GitCall) )\r
1174                 return FALSE;\r
1175 \r
1176         return TRUE;\r
1177 }\r
1178 \r
1179 BOOL CGit::CheckCleanWorkTree()\r
1180 {\r
1181         CString out;\r
1182         CString cmd;\r
1183         cmd=_T("git.exe rev-parse --verify HEAD");\r
1184 \r
1185         if(g_Git.Run(cmd,&out,CP_UTF8))\r
1186                 return FALSE;\r
1187 \r
1188         cmd=_T("git.exe update-index --ignore-submodules --refresh");\r
1189         if(g_Git.Run(cmd,&out,CP_UTF8))\r
1190                 return FALSE;\r
1191 \r
1192         cmd=_T("git.exe diff-files --quiet --ignore-submodules");\r
1193         if(g_Git.Run(cmd,&out,CP_UTF8))\r
1194                 return FALSE;\r
1195 \r
1196         cmd=_T("git diff-index --cached --quiet HEAD --ignore-submodules");\r
1197         if(g_Git.Run(cmd,&out,CP_UTF8))\r
1198                 return FALSE;\r
1199 \r
1200         return TRUE;\r
1201 }\r
1202 int CGit::Revert(CTGitPathList &list,bool keep)\r
1203 {\r
1204         int ret;\r
1205         for(int i=0;i<list.GetCount();i++)\r
1206         {       \r
1207                 ret = Revert((CTGitPath&)list[i],keep);\r
1208                 if(ret)\r
1209                         return ret;\r
1210         }\r
1211         return 0;\r
1212 }\r
1213 int CGit::Revert(CTGitPath &path,bool keep)\r
1214 {\r
1215         CString cmd, out;\r
1216         if(path.m_Action & CTGitPath::LOGACTIONS_ADDED)\r
1217         {       //To init git repository, there are not HEAD, so we can use git reset command\r
1218                 cmd.Format(_T("git.exe rm --cached -- \"%s\""),path.GetGitPathString());\r
1219 \r
1220                 if(g_Git.Run(cmd,&out,CP_ACP))\r
1221                         return -1;\r
1222         }\r
1223         else if(path.m_Action & CTGitPath::LOGACTIONS_REPLACED )\r
1224         {\r
1225                 cmd.Format(_T("git.exe mv -- \"%s\" \"%s\""),path.GetGitPathString(),path.GetGitOldPathString());\r
1226                 if(g_Git.Run(cmd,&out,CP_ACP))\r
1227                         return -1;\r
1228                 \r
1229                 cmd.Format(_T("git.exe checkout HEAD -f -- \"%s\""),path.GetGitOldPathString());\r
1230                 if(g_Git.Run(cmd,&out,CP_ACP))\r
1231                         return -1;\r
1232         }\r
1233         else\r
1234         {\r
1235                 cmd.Format(_T("git.exe checkout HEAD -f -- \"%s\""),path.GetGitPathString());\r
1236                 if(g_Git.Run(cmd,&out,CP_ACP))\r
1237                         return -1;\r
1238         }\r
1239         return 0;\r
1240 }\r
1241 \r
1242 int CGit::ListConflictFile(CTGitPathList &list,CTGitPath *path)\r
1243 {\r
1244         BYTE_VECTOR vector;\r
1245 \r
1246         CString cmd;\r
1247         if(path)\r
1248                 cmd.Format(_T("git.exe ls-files -u -t -z -- \"%s\""),path->GetGitPathString());\r
1249         else\r
1250                 cmd=_T("git.exe ls-files -u -t -z");\r
1251 \r
1252         if(g_Git.Run(cmd,&vector))\r
1253         {\r
1254                 return -1;\r
1255         }\r
1256 \r
1257         list.ParserFromLsFile(vector);\r
1258 \r
1259         return 0;\r
1260 }\r
1261 \r
1262 bool CGit::IsFastForward(CString &from, CString &to)\r
1263 {\r
1264         CString base,hash;\r
1265         CString cmd;\r
1266         cmd.Format(_T("git.exe merge-base %s %s"), to,from);\r
1267 \r
1268         if(g_Git.Run(cmd,&base,CP_ACP))\r
1269         {\r
1270                 //CMessageBox::Show(NULL,base,_T("TortoiseGit"),MB_OK|MB_ICONERROR);\r
1271                 return false;\r
1272         }\r
1273         base=base.Left(40);\r
1274 \r
1275         hash=g_Git.GetHash(from);\r
1276 \r
1277         hash=hash.Left(40);\r
1278         \r
1279         return hash == base;\r
1280 }\r
1281 \r
1282 unsigned int CGit::Hash2int(CString &hash)\r
1283 {\r
1284         int ret=0;\r
1285         for(int i=0;i<8;i++)\r
1286         {\r
1287                 ret =ret <<4;\r
1288                 if(hash[i]>=_T('a'))\r
1289                         ret |= (hash[i]-_T('a')+10)&0xFF;\r
1290                 else if(hash[i]>=_T('A'))\r
1291                         ret |= (hash[i]-_T('A')+10)&0xFF;\r
1292                 else\r
1293                         ret |= (hash[i]-_T('0'))&0xFF;          \r
1294                 \r
1295         }\r
1296         return ret;\r
1297 }