1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2011 - 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.
25 #include "GitConfig.h"
27 #include "UnicodeUtils.h"
30 int CGit::m_LogEncode
=CP_UTF8
;
31 typedef CComCritSecLock
<CComCriticalSection
> CAutoLocker
;
33 static LPTSTR
nextpath(LPCTSTR src
, LPTSTR dst
, UINT maxlen
)
37 while (*src
== _T(';'))
45 while (*src
&& *src
!= _T(';'))
59 while (*src
&& *src
!= _T('"'))
74 while (*src
== _T(';'))
81 return (orgsrc
!= src
) ? (LPTSTR
)src
: NULL
;
84 static inline BOOL
FileExists(LPCTSTR lpszFileName
)
87 return _tstat(lpszFileName
, &st
) == 0;
90 static BOOL
FindGitPath()
93 _tgetenv_s(&size
, NULL
, 0, _T("PATH"));
100 TCHAR
*env
= (TCHAR
*)alloca(size
* sizeof(TCHAR
));
101 _tgetenv_s(&size
, env
, size
, _T("PATH"));
103 TCHAR buf
[_MAX_PATH
];
105 // search in all paths defined in PATH
106 while ((env
= nextpath(env
, buf
, _MAX_PATH
-1)) && *buf
)
108 TCHAR
*pfin
= buf
+ _tcslen(buf
)-1;
110 // ensure trailing slash
111 if (*pfin
!= _T('/') && *pfin
!= _T('\\'))
112 _tcscpy(++pfin
, _T("\\"));
114 const int len
= _tcslen(buf
);
116 if ((len
+ 7) < _MAX_PATH
)
117 _tcscpy(pfin
+1, _T("git.exe"));
121 if ( FileExists(buf
) )
125 CGit::ms_LastMsysGitDir
= buf
;
134 #define MAX_DIRBUFFER 1000
135 #define CALL_OUTPUT_READ_CHUNK_SIZE 1024
137 CString
CGit::ms_LastMsysGitDir
;
143 GetCurrentDirectory(MAX_DIRBUFFER
,m_CurrentDir
.GetBuffer(MAX_DIRBUFFER
));
144 m_CurrentDir
.ReleaseBuffer();
145 m_IsGitDllInited
= false;
147 m_GitSimpleListDiff
=0;
148 m_IsUseGitDLL
= !!CRegDWORD(_T("Software\\TortoiseGit\\UsingGitDLL"),1);
149 this->m_bInitialized
=false;
151 m_critGitDllSec
.Init();
158 git_close_diff(m_GitDiff
);
161 if(this->m_GitSimpleListDiff
)
163 git_close_diff(m_GitSimpleListDiff
);
164 m_GitSimpleListDiff
=0;
168 static char g_Buffer
[4096];
170 int CGit::RunAsync(CString cmd
,PROCESS_INFORMATION
*piOut
,HANDLE
*hReadOut
,CString
*StdioFile
)
172 SECURITY_ATTRIBUTES sa
;
173 HANDLE hRead
, hWrite
;
174 HANDLE hStdioFile
= NULL
;
176 sa
.nLength
= sizeof(SECURITY_ATTRIBUTES
);
177 sa
.lpSecurityDescriptor
=NULL
;
178 sa
.bInheritHandle
=TRUE
;
179 if(!CreatePipe(&hRead
,&hWrite
,&sa
,0))
181 return GIT_ERROR_OPEN_PIP
;
186 hStdioFile
=CreateFile(*StdioFile
,GENERIC_WRITE
,FILE_SHARE_READ
| FILE_SHARE_WRITE
,
187 &sa
,CREATE_ALWAYS
,FILE_ATTRIBUTE_NORMAL
,NULL
);
191 PROCESS_INFORMATION pi
;
192 si
.cb
=sizeof(STARTUPINFO
);
197 si
.hStdOutput
=hStdioFile
;
199 si
.hStdOutput
=hWrite
;
201 si
.wShowWindow
=SW_HIDE
;
202 si
.dwFlags
=STARTF_USESTDHANDLES
|STARTF_USESHOWWINDOW
;
204 LPTSTR pEnv
= m_Environment
.size()? &m_Environment
[0]: NULL
;
205 DWORD dwFlags
= pEnv
? CREATE_UNICODE_ENVIRONMENT
: 0;
207 //DETACHED_PROCESS make ssh recognize that it has no console to launch askpass to input password.
208 dwFlags
|= DETACHED_PROCESS
| CREATE_NEW_PROCESS_GROUP
;
210 memset(&this->m_CurrentGitPi
,0,sizeof(PROCESS_INFORMATION
));
211 memset(&pi
, 0, sizeof(PROCESS_INFORMATION
));
213 if(cmd
.Find(_T("git")) == 0)
215 int firstSpace
= cmd
.Find(_T(" "));
217 cmd
= _T('"')+CGit::ms_LastMsysGitDir
+_T("\\")+ cmd
.Left(firstSpace
) + _T('"')+ cmd
.Mid(firstSpace
);
219 cmd
=_T('"')+CGit::ms_LastMsysGitDir
+_T("\\")+cmd
+_T('"');
222 if(!CreateProcess(NULL
,(LPWSTR
)cmd
.GetString(), NULL
,NULL
,TRUE
,dwFlags
,pEnv
,(LPWSTR
)m_CurrentDir
.GetString(),&si
,&pi
))
225 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER
| FORMAT_MESSAGE_FROM_SYSTEM
,
226 NULL
,GetLastError(), MAKELANGID(LANG_NEUTRAL
, SUBLANG_DEFAULT
),
229 return GIT_ERROR_CREATE_PROCESS
;
243 //Must use sperate function to convert ANSI str to union code string
244 //Becuase A2W use stack as internal convert buffer.
245 void CGit::StringAppend(CString
*str
,BYTE
*p
,int code
,int length
)
248 //str->Append(A2W_CP((LPCSTR)p,code));
256 len
= strlen((const char*)p
);
261 //buf = new WCHAR[len*4 + 1];
262 buf
= str
->GetBuffer(len
*4+1+str
->GetLength())+str
->GetLength();
263 SecureZeroMemory(buf
, (len
*4 + 1)*sizeof(WCHAR
));
264 MultiByteToWideChar(code
, 0, (LPCSTR
)p
, len
, buf
, len
*4);
265 str
->ReleaseBuffer();
269 BOOL
CGit::IsInitRepos()
273 if(g_Git
.Run(_T("git.exe rev-parse --revs-only HEAD"),&cmdout
,CP_UTF8
))
275 // CMessageBox::Show(NULL,cmdout,_T("TortoiseGit"),MB_OK);
283 int CGit::Run(CGitCall
* pcall
)
285 PROCESS_INFORMATION pi
;
287 if(RunAsync(pcall
->GetCmd(),&pi
,&hRead
))
288 return GIT_ERROR_CREATE_PROCESS
;
291 BYTE data
[CALL_OUTPUT_READ_CHUNK_SIZE
];
293 while(ReadFile(hRead
,data
,CALL_OUTPUT_READ_CHUNK_SIZE
,&readnumber
,NULL
))
295 // TODO: when OnOutputData() returns 'true', abort git-command. Send CTRL-C signal?
296 if(!bAborted
)//For now, flush output when command aborted.
297 if(pcall
->OnOutputData(data
,readnumber
))
303 CloseHandle(pi
.hThread
);
305 WaitForSingleObject(pi
.hProcess
, INFINITE
);
308 if(!GetExitCodeProcess(pi
.hProcess
,&exitcode
))
310 return GIT_ERROR_GET_EXIT_CODE
;
313 CloseHandle(pi
.hProcess
);
318 class CGitCall_ByteVector
: public CGitCall
321 CGitCall_ByteVector(CString cmd
,BYTE_VECTOR
* pvector
):CGitCall(cmd
),m_pvector(pvector
){}
322 virtual bool OnOutputData(const BYTE
* data
, size_t size
)
324 size_t oldsize
=m_pvector
->size();
325 m_pvector
->resize(m_pvector
->size()+size
);
326 memcpy(&*(m_pvector
->begin()+oldsize
),data
,size
);
329 BYTE_VECTOR
* m_pvector
;
332 int CGit::Run(CString cmd
,BYTE_VECTOR
*vector
)
334 CGitCall_ByteVector
call(cmd
,vector
);
337 int CGit::Run(CString cmd
, CString
* output
,int code
)
341 ret
=Run(cmd
,&vector
);
345 StringAppend(output
,&(vector
[0]),code
);
349 CString
CGit::GetUserName(void)
351 return GetConfigValue(L
"user.name", this->GetGitEncode(L
"i18n.commitencoding"));
353 CString
CGit::GetUserEmail(void)
355 return GetConfigValue(L
"user.email");
358 CString
CGit::GetConfigValue(CString name
,int encoding
, CString
*GitPath
, BOOL RemoveCR
)
362 if(this->m_IsUseGitDLL
)
364 CString
*git_path
=NULL
;
377 key
= CUnicodeUtils::GetMulti(name
, encoding
);
380 p
=CUnicodeUtils::GetMulti(*GitPath
,CP_ACP
);
382 if(git_get_config(key
.GetBuffer(), value
.GetBufferSetLength(4096), 4096, p
.GetBuffer()))
386 g_Git
.StringAppend(&configValue
,(BYTE
*)value
.GetBuffer(),encoding
);
388 return configValue
.Tokenize(_T("\n"),start
);
395 cmd
.Format(L
"git.exe config %s", name
);
396 Run(cmd
,&configValue
,encoding
);
398 return configValue
.Tokenize(_T("\n"),start
);
403 int CGit::SetConfigValue(CString key
, CString value
, CONFIG_TYPE type
, int encoding
, CString
*GitPath
)
405 if(this->m_IsUseGitDLL
)
414 CStringA keya
, valuea
;
415 keya
= CUnicodeUtils::GetMulti(key
, CP_UTF8
);
416 valuea
= CUnicodeUtils::GetMulti(value
, encoding
);
419 p
=CUnicodeUtils::GetMulti(*GitPath
,CP_ACP
);
421 return get_set_config(keya
.GetBuffer(), valuea
.GetBuffer(), type
, p
.GetBuffer());
430 option
= _T("--global");
433 option
= _T("--system");
438 cmd
.Format(_T("git.exe config %s %s \"%s\""), option
, key
, value
);
440 if(Run(cmd
,&out
,encoding
))
449 CString
CGit::GetCurrentBranch(void)
452 //Run(_T("git.exe branch"),&branch);
454 if(this->GetCurrentBranchFromFile(this->m_CurrentDir
,output
))
456 return _T("(no branch)");
462 CString
CGit::GetSymbolicRef(const wchar_t* symbolicRefName
, bool bStripRefsHeads
)
465 if(this->m_IsUseGitDLL
)
467 unsigned char sha1
[20];
470 const char *refs_heads_master
= git_resolve_ref(CUnicodeUtils::GetUTF8(CString(symbolicRefName
)), sha1
, 0, &flag
);
471 if(refs_heads_master
&& (flag
&REF_ISSYMREF
))
473 g_Git
.StringAppend(&refName
,(BYTE
*)refs_heads_master
);
475 refName
= StripRefName(refName
);
481 cmd
.Format(L
"git symbolic-ref %s", symbolicRefName
);
482 if(Run(cmd
, &refName
, CP_UTF8
) != 0)
483 return CString();//Error
485 refName
= refName
.Tokenize(L
"\n", iStart
);
487 refName
= StripRefName(refName
);
492 CString
CGit::GetFullRefName(CString shortRefName
)
496 cmd
.Format(L
"git rev-parse --symbolic-full-name %s", shortRefName
);
497 if(Run(cmd
, &refName
, CP_UTF8
) != 0)
498 return CString();//Error
500 return refName
.Tokenize(L
"\n", iStart
);
503 CString
CGit::StripRefName(CString refName
)
505 if(wcsncmp(refName
, L
"refs/heads/", 11) == 0)
506 refName
= refName
.Mid(11);
507 else if(wcsncmp(refName
, L
"refs/", 5) == 0)
508 refName
= refName
.Mid(5);
510 return refName
.Tokenize(_T("\n"),start
);
513 int CGit::GetCurrentBranchFromFile(const CString
&sProjectRoot
, CString
&sBranchOut
)
515 // read current branch name like git-gui does, by parsing the .git/HEAD file directly
517 if ( sProjectRoot
.IsEmpty() )
520 CString sHeadFile
= sProjectRoot
+ _T("\\") + g_GitAdminDir
.GetAdminDirName() + _T("\\HEAD");
523 _tfopen_s(&pFile
, sHeadFile
.GetString(), _T("r"));
531 fgets(s
, sizeof(s
), pFile
);
535 const char *pfx
= "ref: refs/heads/";
536 const int len
= 16;//strlen(pfx)
538 if ( !strncmp(s
, pfx
, len
) )
540 //# We're on a branch. It might not exist. But
541 //# HEAD looks good enough to be a branch.
542 sBranchOut
= s
+ len
;
543 sBranchOut
.TrimRight(_T(" \r\n\t"));
545 if ( sBranchOut
.IsEmpty() )
550 //# Assume this is a detached head.
559 int CGit::BuildOutputFormat(CString
&format
,bool IsFull
)
562 log
.Format(_T("#<%c>%%x00"),LOG_REV_ITEM_BEGIN
);
566 log
.Format(_T("#<%c>%%an%%x00"),LOG_REV_AUTHOR_NAME
);
568 log
.Format(_T("#<%c>%%ae%%x00"),LOG_REV_AUTHOR_EMAIL
);
570 log
.Format(_T("#<%c>%%ai%%x00"),LOG_REV_AUTHOR_DATE
);
572 log
.Format(_T("#<%c>%%cn%%x00"),LOG_REV_COMMIT_NAME
);
574 log
.Format(_T("#<%c>%%ce%%x00"),LOG_REV_COMMIT_EMAIL
);
576 log
.Format(_T("#<%c>%%ci%%x00"),LOG_REV_COMMIT_DATE
);
578 log
.Format(_T("#<%c>%%b%%x00"),LOG_REV_COMMIT_BODY
);
582 log
.Format(_T("#<%c>%%m%%H%%x00"),LOG_REV_COMMIT_HASH
);
584 log
.Format(_T("#<%c>%%P%%x00"),LOG_REV_COMMIT_PARENT
);
586 log
.Format(_T("#<%c>%%s%%x00"),LOG_REV_COMMIT_SUBJECT
);
591 log
.Format(_T("#<%c>%%x00"),LOG_REV_COMMIT_FILE
);
597 int CGit::GetLog(BYTE_VECTOR
& logOut
, const CString
&hash
, CTGitPath
*path
,int count
,int mask
,CString
*from
,CString
*to
)
599 CGitCall_ByteVector
gitCall(CString(),&logOut
);
600 return GetLog(&gitCall
,hash
,path
,count
,mask
,from
,to
);
603 CString
CGit::GetLogCmd( const CString
&hash
, CTGitPath
*path
, int count
, int mask
,CString
*from
,CString
*to
,bool paramonly
,
613 file
.Format(_T(" -- \"%s\""),path
->GetGitPathString());
616 num
.Format(_T("-n%d"),count
);
620 if(mask
& LOG_INFO_STAT
)
621 param
+= _T(" --numstat ");
622 if(mask
& LOG_INFO_FILESTATE
)
623 param
+= _T(" --raw ");
625 if(mask
& LOG_INFO_FULLHISTORY
)
626 param
+= _T(" --full-history ");
628 if(mask
& LOG_INFO_BOUNDARY
)
629 param
+= _T(" --left-right --boundary ");
631 if(mask
& CGit::LOG_INFO_ALL_BRANCH
)
632 param
+= _T(" --all ");
634 if(mask
& CGit::LOG_INFO_DETECT_COPYRENAME
)
637 if(mask
& CGit::LOG_INFO_DETECT_RENAME
)
640 if(mask
& CGit::LOG_INFO_FIRST_PARENT
)
641 param
+= _T(" --first-parent ");
643 if(mask
& CGit::LOG_INFO_NO_MERGE
)
644 param
+= _T(" --no-merges ");
646 if(mask
& CGit::LOG_INFO_FOLLOW
)
647 param
+= _T(" --follow ");
649 if(mask
& CGit::LOG_INFO_SHOW_MERGEDFILE
)
652 if(mask
& CGit::LOG_INFO_FULL_DIFF
)
653 param
+= _T(" --full-diff ");
655 if(from
!= NULL
&& to
!= NULL
)
658 range
.Format(_T(" %s..%s "),*from
,*to
);
665 if( Filter
&& (Filter
->m_From
!= -1))
667 st1
.Format(_T(" --max-age=%I64u "), Filter
->m_From
);
671 if( Filter
&& (Filter
->m_To
!= -1))
673 st2
.Format(_T(" --min-age=%I64u "), Filter
->m_To
);
678 if( Filter
&& (!Filter
->m_Author
.IsEmpty()))
680 st1
.Format(_T(" --author=\"%s\"" ),Filter
->m_Author
);
685 if( Filter
&& (!Filter
->m_Committer
.IsEmpty()))
687 st1
.Format(_T(" --committer=\"%s\"" ),Filter
->m_Author
);
692 if( Filter
&& (!Filter
->m_MessageFilter
.IsEmpty()))
694 st1
.Format(_T(" --grep=\"%s\"" ),Filter
->m_MessageFilter
);
701 if(!Filter
->m_IsRegex
)
702 param
+=_T(" --fixed-strings ");
704 param
+= _T(" --regexp-ignore-case --extended-regexp ");
707 if(paramonly
) //tgit.dll.Git.cpp:setup_revisions() only looks at args[1] and greater. To account for this, pass a dummy parameter in the 0th place
708 cmd
.Format(_T("--ignore-this-parameter %s -z %s --parents "), num
, param
);
712 BuildOutputFormat(log
,!(mask
&CGit::LOG_INFO_ONLY_HASH
));
713 cmd
.Format(_T("git.exe log %s -z %s --parents --pretty=format:\"%s\""),
721 //int CGit::GetLog(CGitCall* pgitCall, const CString &hash, CTGitPath *path ,int count,int mask)
722 int CGit::GetLog(CGitCall
* pgitCall
, const CString
&hash
, CTGitPath
*path
, int count
, int mask
,CString
*from
,CString
*to
)
724 pgitCall
->SetCmd( GetLogCmd(hash
,path
,count
,mask
,from
,to
) );
726 return Run(pgitCall
);
727 // return Run(cmd,&logOut);
731 void GetTempPath(CString
&path
)
733 TCHAR lpPathBuffer
[BUFSIZE
];
735 DWORD dwBufSize
=BUFSIZE
;
736 dwRetVal
= GetTempPath(dwBufSize
, // length of the buffer
737 lpPathBuffer
); // buffer for path
738 if (dwRetVal
> dwBufSize
|| (dwRetVal
== 0))
742 path
.Format(_T("%s"),lpPathBuffer
);
744 CString
GetTempFile()
746 TCHAR lpPathBuffer
[BUFSIZE
];
748 DWORD dwBufSize
=BUFSIZE
;
749 TCHAR szTempName
[BUFSIZE
];
752 dwRetVal
= GetTempPath(dwBufSize
, // length of the buffer
753 lpPathBuffer
); // buffer for path
754 if (dwRetVal
> dwBufSize
|| (dwRetVal
== 0))
758 // Create a temporary file.
759 uRetVal
= GetTempFileName(lpPathBuffer
, // directory for tmp files
760 TEXT("Patch"), // temp file name prefix
761 0, // create unique name
762 szTempName
); // buffer for name
770 return CString(szTempName
);
774 int CGit::RunLogFile(CString cmd
,const CString
&filename
)
777 PROCESS_INFORMATION pi
;
778 si
.cb
=sizeof(STARTUPINFO
);
781 SECURITY_ATTRIBUTES psa
={sizeof(psa
),NULL
,TRUE
};;
782 psa
.bInheritHandle
=TRUE
;
784 HANDLE houtfile
=CreateFile(filename
,GENERIC_WRITE
,FILE_SHARE_READ
| FILE_SHARE_WRITE
,
785 &psa
,CREATE_ALWAYS
,FILE_ATTRIBUTE_NORMAL
,NULL
);
787 si
.wShowWindow
= SW_HIDE
;
788 si
.dwFlags
= STARTF_USESTDHANDLES
|STARTF_USESHOWWINDOW
;
789 si
.hStdOutput
= houtfile
;
791 LPTSTR pEnv
= m_Environment
.size()? &m_Environment
[0]: NULL
;
792 DWORD dwFlags
= pEnv
? CREATE_UNICODE_ENVIRONMENT
: 0;
794 if(cmd
.Find(_T("git")) == 0)
795 cmd
=CGit::ms_LastMsysGitDir
+_T("\\")+cmd
;
797 if(!CreateProcess(NULL
,(LPWSTR
)cmd
.GetString(), NULL
,NULL
,TRUE
,dwFlags
,pEnv
,(LPWSTR
)m_CurrentDir
.GetString(),&si
,&pi
))
800 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER
|FORMAT_MESSAGE_FROM_SYSTEM
,
801 NULL
,GetLastError(),MAKELANGID(LANG_NEUTRAL
, SUBLANG_DEFAULT
),
804 return GIT_ERROR_CREATE_PROCESS
;
807 WaitForSingleObject(pi
.hProcess
,INFINITE
);
809 CloseHandle(pi
.hThread
);
810 CloseHandle(pi
.hProcess
);
811 CloseHandle(houtfile
);
816 CGitHash
CGit::GetHash(TCHAR
* friendname
)
818 if(this->m_IsUseGitDLL
)
820 this->CheckAndInitDll();
824 ref
= CUnicodeUtils::GetMulti(friendname
,CP_ACP
);
827 git_get_sha1(ref
, hash
.m_hash
);
838 cmd
.Format(_T("git.exe rev-parse %s" ),friendname
);
839 Run(cmd
,&out
,CP_UTF8
);
840 // int pos=out.ReverseFind(_T('\n'));
841 out
.FindOneOf(_T("\r\n"));
842 return CGitHash(out
);
846 int CGit::GetInitAddList(CTGitPathList
&outputlist
)
851 cmd
=_T("git.exe ls-files -s -t -z");
853 if(g_Git
.Run(cmd
,&cmdout
))
856 outputlist
.ParserFromLsFile(cmdout
);
857 for(int i
=0;i
<outputlist
.GetCount();i
++)
858 ((int)outputlist
[i
].m_Action
) = CTGitPath::LOGACTIONS_ADDED
;
862 int CGit::GetCommitDiffList(const CString
&rev1
,const CString
&rev2
,CTGitPathList
&outputlist
)
866 if(rev1
== GIT_REV_ZERO
|| rev2
== GIT_REV_ZERO
)
869 if(rev1
== GIT_REV_ZERO
)
870 cmd
.Format(_T("git.exe diff -r --raw -C -M --numstat -z %s"),rev2
);
872 cmd
.Format(_T("git.exe diff -r -R --raw -C -M --numstat -z %s"),rev1
);
875 cmd
.Format(_T("git.exe diff-tree -r --raw -C -M --numstat -z %s %s"),rev2
,rev1
);
879 if(g_Git
.Run(cmd
,&out
))
882 outputlist
.ParserFromLog(out
);
887 int addto_list_each_ref_fn(const char *refname
, const unsigned char * /*sha1*/, int /*flags*/, void *cb_data
)
889 STRING_VECTOR
*list
= (STRING_VECTOR
*)cb_data
;
891 g_Git
.StringAppend(&str
,(BYTE
*)refname
,CP_ACP
);
892 list
->push_back(str
);
896 int CGit::GetTagList(STRING_VECTOR
&list
)
900 if(this->m_IsUseGitDLL
)
902 return git_for_each_ref_in("refs/tags/",addto_list_each_ref_fn
, &list
);
907 cmd
=_T("git.exe tag -l");
909 ret
=g_Git
.Run(cmd
,&output
,CP_UTF8
);
917 one
=output
.Tokenize(_T("\n"),pos
);
925 int CGit::GetBranchList(STRING_VECTOR
&list
,int *current
,BRANCH_TYPE type
)
929 cmd
=_T("git.exe branch --no-color");
931 if(type
==(BRANCH_LOCAL
|BRANCH_REMOTE
))
933 else if(type
==BRANCH_REMOTE
)
937 ret
=g_Git
.Run(cmd
,&output
,CP_UTF8
);
944 one
=output
.Tokenize(_T("\n"),pos
);
945 one
.Trim(L
" \r\n\t");
946 if(one
.Find(L
" -> ") >= 0 || one
.IsEmpty())
947 continue; // skip something like: refs/origin/HEAD -> refs/origin/master
948 if(one
[0] == _T('*'))
961 int CGit::GetRemoteList(STRING_VECTOR
&list
)
965 cmd
=_T("git.exe remote");
966 ret
=g_Git
.Run(cmd
,&output
,CP_UTF8
);
973 one
=output
.Tokenize(_T("\n"),pos
);
980 int CGit::GetRefList(STRING_VECTOR
&list
)
983 if(this->m_IsUseGitDLL
)
985 return git_for_each_ref_in("",addto_list_each_ref_fn
, &list
);
990 cmd
=_T("git.exe show-ref -d");
991 ret
=g_Git
.Run(cmd
,&output
,CP_UTF8
);
998 one
=output
.Tokenize(_T("\n"),pos
);
999 int start
=one
.Find(_T(" "),0);
1003 name
=one
.Right(one
.GetLength()-start
-1);
1004 list
.push_back(name
);
1012 int addto_map_each_ref_fn(const char *refname
, const unsigned char *sha1
, int /*flags*/, void *cb_data
)
1014 MAP_HASH_NAME
*map
= (MAP_HASH_NAME
*)cb_data
;
1016 g_Git
.StringAppend(&str
,(BYTE
*)refname
,CP_ACP
);
1017 CGitHash
hash((char*)sha1
);
1019 (*map
)[hash
].push_back(str
);
1021 if(strncmp(refname
, "refs/tags", 9) == 0)
1024 if(!git_deref_tag(sha1
, refhash
))
1026 (*map
)[(char*)refhash
].push_back(str
+_T("^{}"));
1032 int CGit::GetMapHashToFriendName(MAP_HASH_NAME
&map
)
1035 if(this->m_IsUseGitDLL
)
1037 return git_for_each_ref_in("",addto_map_each_ref_fn
, &map
);
1042 cmd
=_T("git.exe show-ref -d");
1043 ret
=g_Git
.Run(cmd
,&output
,CP_UTF8
);
1050 one
=output
.Tokenize(_T("\n"),pos
);
1051 int start
=one
.Find(_T(" "),0);
1055 name
=one
.Right(one
.GetLength()-start
-1);
1058 hash
=one
.Left(start
);
1060 map
[CGitHash(hash
)].push_back(name
);
1068 BOOL
CGit::CheckMsysGitDir()
1075 this->m_Environment
.clear();
1076 m_Environment
.CopyProcessEnvironment();
1078 TCHAR
*oldpath
,*home
;
1079 size_t homesize
,size
,httpsize
;
1081 // set HOME if not set already
1082 _tgetenv_s(&homesize
, NULL
, 0, _T("HOME"));
1085 if ( (!_tdupenv_s(&home
,&size
,_T("USERPROFILE"))) && (home
!= NULL
) )
1087 m_Environment
.SetEnv(_T("HOME"),home
);
1092 ATLTRACE("CGit::CheckMsysGitDir Unable to SetEnv HOME\n");
1098 _tgetenv_s(&httpsize
, NULL
, 0, _T("http_proxy"));
1101 CString regServeraddress_copy
= CRegString(_T("Software\\TortoiseGit\\Servers\\global\\http-proxy-host"), _T(""));
1102 CString regServerport_copy
= CRegString(_T("Software\\TortoiseGit\\Servers\\global\\http-proxy-port"), _T(""));
1103 CString regUsername_copy
= CRegString(_T("Software\\TortoiseGit\\Servers\\global\\http-proxy-username"), _T(""));
1104 CString regPassword_copy
= CRegString(_T("Software\\TortoiseGit\\Servers\\global\\http-proxy-password"), _T(""));
1105 CString regTimeout_copy
= CRegString(_T("Software\\TortoiseGit\\Servers\\global\\http-proxy-timeout"), _T(""));
1106 CString regExceptions_copy
= CRegString(_T("Software\\TortoiseGit\\Servers\\global\\http-proxy-exceptions"), _T(""));
1109 if(!regServeraddress_copy
.IsEmpty())
1111 if(regServeraddress_copy
.Left(4) != _T("http"))
1112 http_proxy
=_T("http://");
1114 if(!regUsername_copy
.IsEmpty())
1116 http_proxy
+= regUsername_copy
;
1117 http_proxy
+= _T(":")+regPassword_copy
;
1118 http_proxy
+= _T("@");
1120 http_proxy
+=regServeraddress_copy
;
1121 if(!regServerport_copy
.IsEmpty())
1123 http_proxy
+=_T(":")+regServerport_copy
;
1125 m_Environment
.SetEnv(_T("http_proxy"),(LPTSTR
)http_proxy
.GetBuffer());
1129 CString sshclient
=CRegString(_T("Software\\TortoiseGit\\SSH"));
1131 if(!sshclient
.IsEmpty())
1133 m_Environment
.SetEnv(_T("GIT_SSH"),sshclient
.GetBuffer());
1136 CString ssh
=sshclient
;
1137 ssh
.Replace(_T("/"),_T("\\"));
1138 ssh
.Replace(_T("\\"),_T("\\\\"));
1139 ssh
=CString(_T("\""))+ssh
+_T('\"');
1140 m_Environment
.SetEnv(_T("SVN_SSH"),ssh
.GetBuffer());
1144 TCHAR sPlink
[MAX_PATH
];
1145 GetModuleFileName(NULL
, sPlink
, _countof(sPlink
));
1146 LPTSTR ptr
= _tcsrchr(sPlink
, _T('\\'));
1148 _tcscpy(ptr
+ 1, _T("TortoisePlink.exe"));
1149 m_Environment
.SetEnv(_T("GIT_SSH"), sPlink
);
1153 ssh
.Replace(_T("/"),_T("\\"));
1154 ssh
.Replace(_T("\\"),_T("\\\\"));
1155 ssh
=CString(_T("\""))+ssh
+_T('\"');
1156 m_Environment
.SetEnv(_T("SVN_SSH"),ssh
.GetBuffer());
1161 TCHAR sAskPass
[MAX_PATH
];
1162 GetModuleFileName(NULL
, sAskPass
, _countof(sAskPass
));
1163 LPTSTR ptr
= _tcsrchr(sAskPass
, _T('\\'));
1166 _tcscpy(ptr
+ 1, _T("SshAskPass.exe"));
1167 m_Environment
.SetEnv(_T("DISPLAY"),_T(":9999"));
1168 m_Environment
.SetEnv(_T("SSH_ASKPASS"),sAskPass
);
1169 m_Environment
.SetEnv(_T("GIT_ASKPASS"),sAskPass
);
1173 // add git/bin path to PATH
1175 CRegString msysdir
=CRegString(REG_MSYSGIT_PATH
,_T(""),FALSE
);
1179 CRegString msysinstalldir
=CRegString(REG_MSYSGIT_INSTALL
,_T(""),FALSE
,HKEY_LOCAL_MACHINE
);
1181 if ( !str
.IsEmpty() )
1183 str
+= (str
[str
.GetLength()-1] != '\\') ? "\\bin" : "bin";
1185 CGit::ms_LastMsysGitDir
= str
;
1190 // search PATH if git/bin directory is already present
1191 if ( FindGitPath() )
1193 m_bInitialized
= TRUE
;
1201 CGit::ms_LastMsysGitDir
= str
;
1204 // check for git.exe existance (maybe it was deinstalled in the meantime)
1205 if (!FileExists(CGit::ms_LastMsysGitDir
+ _T("\\git.exe")))
1209 _tdupenv_s(&oldpath
,&size
,_T("PATH"));
1212 path
.Format(_T("%s;%s"),oldpath
,str
+ _T(";")+ (CString
)CRegString(REG_MSYSGIT_EXTRA_PATH
,_T(""),FALSE
));
1214 m_Environment
.SetEnv(_T("PATH"),path
.GetBuffer());
1216 CString str1
= m_Environment
.GetEnv(_T("PATH"));
1218 CString sOldPath
= oldpath
;
1221 m_bInitialized
= TRUE
;
1225 class CGitCall_EnumFiles
: public CGitCall
1228 CGitCall_EnumFiles(const TCHAR
*pszProjectPath
, const TCHAR
*pszSubPath
, unsigned int nFlags
, WGENUMFILECB
*pEnumCb
, void *pUserData
)
1229 : m_pszProjectPath(pszProjectPath
),
1230 m_pszSubPath(pszSubPath
),
1233 m_pUserData(pUserData
)
1237 typedef std::map
<CStringA
,char> TStrCharMap
;
1239 const TCHAR
* m_pszProjectPath
;
1240 const TCHAR
* m_pszSubPath
;
1241 unsigned int m_nFlags
;
1242 WGENUMFILECB
* m_pEnumCb
;
1245 BYTE_VECTOR m_DataCollector
;
1247 virtual bool OnOutputData(const BYTE
* data
, size_t size
)
1249 m_DataCollector
.append(data
,size
);
1252 // lines from igit.exe are 0 terminated
1253 int found
=m_DataCollector
.findData((const BYTE
*)"",1);
1256 OnSingleLine( (LPCSTR
)&*m_DataCollector
.begin() );
1257 m_DataCollector
.erase(m_DataCollector
.begin(), m_DataCollector
.begin()+found
+1);
1259 return false;//Should never reach this
1261 virtual void OnEnd()
1265 BYTE
HexChar(char ch
)
1267 if (ch
>= '0' && ch
<= '9')
1268 return (UINT
)(ch
- '0');
1269 else if (ch
>= 'A' && ch
<= 'F')
1270 return (UINT
)(ch
- 'A') + 10;
1271 else if (ch
>= 'a' && ch
<= 'f')
1272 return (UINT
)(ch
- 'a') + 10;
1277 bool OnSingleLine(LPCSTR line
)
1281 wgFile_s fileStatus
;
1285 fileStatus
.nFlags
= 0;
1287 fileStatus
.nFlags
|= WGFF_Directory
;
1288 else if (*line
!= 'F')
1295 fileStatus
.nStatus
= WGFS_Unknown
;
1298 case 'N': fileStatus
.nStatus
= WGFS_Normal
; break;
1299 case 'M': fileStatus
.nStatus
= WGFS_Modified
; break;
1300 case 'S': fileStatus
.nStatus
= WGFS_Staged
; break;
1301 case 'A': fileStatus
.nStatus
= WGFS_Added
; break;
1302 case 'C': fileStatus
.nStatus
= WGFS_Conflicted
; break;
1303 case 'D': fileStatus
.nStatus
= WGFS_Deleted
; break;
1304 case 'I': fileStatus
.nStatus
= WGFS_Ignored
; break;
1305 case 'U': fileStatus
.nStatus
= WGFS_Unversioned
; break;
1306 case 'E': fileStatus
.nStatus
= WGFS_Empty
; break;
1307 case '?': fileStatus
.nStatus
= WGFS_Unknown
; break;
1317 fileStatus
.sha1
= NULL
;
1318 if ( !(fileStatus
.nFlags
& WGFF_Directory
) )
1320 for (int i
=0; i
<20; i
++)
1322 sha1
[i
] = (HexChar(line
[0])<<4)&0xF0;
1323 sha1
[i
] |= HexChar(line
[1])&0xF;
1332 int len
= strlen(line
);
1333 if (len
&& len
< 2048)
1335 WCHAR
*buf
= (WCHAR
*)alloca((len
*4+2)*sizeof(WCHAR
));
1337 MultiByteToWideChar(CP_ACP
, 0, line
, len
+1, buf
, len
*4+1);
1338 fileStatus
.sFileName
= buf
;
1340 if (*buf
&& (*m_pEnumCb
)(&fileStatus
,m_pUserData
))
1348 BOOL
CGit::EnumFiles(const TCHAR
*pszProjectPath
, const TCHAR
*pszSubPath
, unsigned int nFlags
, WGENUMFILECB
*pEnumCb
, void *pUserData
)
1350 if(!pszProjectPath
|| *pszProjectPath
=='\0')
1353 CGitCall_EnumFiles
W_GitCall(pszProjectPath
,pszSubPath
,nFlags
,pEnumCb
,pUserData
);
1356 /* char W_szToDir[MAX_PATH];
1357 strncpy(W_szToDir,pszProjectPath,sizeof(W_szToDir)-1);
1358 if(W_szToDir[strlen(W_szToDir)-1]!='\\')
1359 strncat(W_szToDir,"\\",sizeof(W_szToDir)-1);
1361 SetCurrentDirectoryA(W_szToDir);
1362 GetCurrentDirectoryA(sizeof(W_szToDir)-1,W_szToDir);
1364 SetCurrentDir(pszProjectPath
);
1369 if (nFlags
& WGEFF_NoRecurse
) sMode
+= _T("r");
1370 if (nFlags
& WGEFF_FullPath
) sMode
+= _T("f");
1371 if (nFlags
& WGEFF_DirStatusDelta
) sMode
+= _T("d");
1372 if (nFlags
& WGEFF_DirStatusAll
) sMode
+= _T("D");
1373 if (nFlags
& WGEFF_EmptyAsNormal
) sMode
+= _T("e");
1374 if (nFlags
& WGEFF_SingleFile
) sMode
+= _T("s");
1381 // NOTE: there seems to be some issue with msys based app receiving backslash on commandline, at least
1382 // if followed by " like for example 'igit "C:\"', the commandline igit receives is 'igit.exe C:" status' with
1383 // the 'C:" status' part as a single arg, Maybe it uses unix style processing. In order to avoid this just
1384 // use forward slashes for supplied project and sub paths
1386 CString sProjectPath
= pszProjectPath
;
1387 sProjectPath
.Replace(_T('\\'), _T('/'));
1391 CString sSubPath
= pszSubPath
;
1392 sSubPath
.Replace(_T('\\'), _T('/'));
1394 cmd
.Format(_T("tgit.exe statusex \"%s\" status %s \"%s\""), sProjectPath
, sMode
, sSubPath
);
1398 cmd
.Format(_T("tgit.exe statusex \"%s\" status %s"), sProjectPath
, sMode
);
1401 //OutputDebugStringA("---");OutputDebugStringW(cmd);OutputDebugStringA("\r\n");
1403 W_GitCall
.SetCmd(cmd
);
1404 // NOTE: should igit get added as a part of msysgit then use below line instead of the above one
1405 //W_GitCall.SetCmd(CGit::ms_LastMsysGitDir + cmd);
1407 if ( Run(&W_GitCall
) )
1413 BOOL
CGit::CheckCleanWorkTree()
1417 cmd
=_T("git.exe rev-parse --verify HEAD");
1419 if(g_Git
.Run(cmd
,&out
,CP_UTF8
))
1422 cmd
=_T("git.exe update-index --ignore-submodules --refresh");
1423 if(g_Git
.Run(cmd
,&out
,CP_UTF8
))
1426 cmd
=_T("git.exe diff-files --quiet --ignore-submodules");
1427 if(g_Git
.Run(cmd
,&out
,CP_UTF8
))
1430 cmd
=_T("git diff-index --cached --quiet HEAD --ignore-submodules");
1431 if(g_Git
.Run(cmd
,&out
,CP_UTF8
))
1436 int CGit::Revert(CTGitPathList
&list
,bool keep
)
1439 for(int i
=0;i
<list
.GetCount();i
++)
1441 ret
= Revert((CTGitPath
&)list
[i
],keep
);
1447 int CGit::Revert(CTGitPath
&path
,bool /*keep*/)
1451 if(path
.m_Action
& CTGitPath::LOGACTIONS_REPLACED
)
1453 cmd
.Format(_T("git.exe mv -- \"%s\" \"%s\""),path
.GetGitPathString(),path
.GetGitOldPathString());
1454 if(g_Git
.Run(cmd
,&out
,CP_ACP
))
1457 cmd
.Format(_T("git.exe checkout HEAD -f -- \"%s\""),path
.GetGitOldPathString());
1458 if(g_Git
.Run(cmd
,&out
,CP_ACP
))
1461 }else if(path
.m_Action
& CTGitPath::LOGACTIONS_ADDED
)
1462 { //To init git repository, there are not HEAD, so we can use git reset command
1463 cmd
.Format(_T("git.exe rm --cached -- \"%s\""),path
.GetGitPathString());
1465 if(g_Git
.Run(cmd
,&out
,CP_ACP
))
1470 cmd
.Format(_T("git.exe checkout HEAD -f -- \"%s\""),path
.GetGitPathString());
1471 if(g_Git
.Run(cmd
,&out
,CP_ACP
))
1477 int CGit::ListConflictFile(CTGitPathList
&list
,CTGitPath
*path
)
1483 cmd
.Format(_T("git.exe ls-files -u -t -z -- \"%s\""),path
->GetGitPathString());
1485 cmd
=_T("git.exe ls-files -u -t -z");
1487 if(g_Git
.Run(cmd
,&vector
))
1492 list
.ParserFromLsFile(vector
);
1497 bool CGit::IsFastForward(const CString
&from
, const CString
&to
)
1500 CGitHash basehash
,hash
;
1502 cmd
.Format(_T("git.exe merge-base %s %s"), to
,from
);
1504 if(g_Git
.Run(cmd
,&base
,CP_ACP
))
1506 //CMessageBox::Show(NULL,base,_T("TortoiseGit"),MB_OK|MB_ICONERROR);
1511 hash
=g_Git
.GetHash(from
);
1513 return hash
== basehash
;
1516 unsigned int CGit::Hash2int(CGitHash
&hash
)
1519 for(int i
=0;i
<4;i
++)
1522 ret
|= hash
.m_hash
[i
];
1527 int CGit::RefreshGitIndex()
1529 if(g_Git
.m_IsUseGitDLL
)
1533 return git_run_cmd("update-index","update-index -q --refresh");
1543 cmd
=_T("git.exe update-index --refresh");
1544 return Run(cmd
,&output
,CP_ACP
);
1548 int CGit::GetOneFile(CString Refname
, CTGitPath
&path
, const CString
&outputfile
)
1550 if(g_Git
.m_IsUseGitDLL
)
1554 g_Git
.CheckAndInitDll();
1555 CStringA ref
, patha
, outa
;
1556 ref
= CUnicodeUtils::GetMulti(Refname
,CP_ACP
);
1557 patha
= CUnicodeUtils::GetMulti(path
.GetGitPathString(), CP_ACP
);
1558 outa
= CUnicodeUtils::GetMulti(outputfile
,CP_ACP
);
1559 ::DeleteFile(outputfile
);
1560 return git_checkout_file((const char*)ref
.GetBuffer(),(const char*)patha
.GetBuffer(),(const char*)outa
.GetBuffer());
1569 cmd
.Format(_T("git.exe cat-file -p %s:\"%s\""), Refname
, path
.GetGitPathString());
1570 return g_Git
.RunLogFile(cmd
,outputfile
);
1573 void CEnvironment::CopyProcessEnvironment()
1575 TCHAR
*p
= GetEnvironmentStrings();
1576 while(*p
!=0 || *(p
+1) !=0)
1577 this->push_back(*p
++);
1579 push_back(_T('\0'));
1580 push_back(_T('\0'));
1583 CString
CEnvironment::GetEnv(TCHAR
*name
)
1586 for(int i
=0;i
<size();i
++)
1590 CString sname
= str
.Tokenize(_T("="),start
);
1591 if(sname
.CompareNoCase(name
) == 0)
1593 return &(*this)[i
+start
+1];
1600 void CEnvironment::SetEnv(TCHAR
*name
, TCHAR
* value
)
1603 for( i
=0;i
<size();i
++)
1605 CString str
= &(*this)[i
];
1607 CString sname
= str
.Tokenize(_T("="),start
);
1608 if(sname
.CompareNoCase(name
) == 0)
1617 i
-= 1; // roll back terminate \0\0
1618 this->push_back(_T('\0'));
1621 CEnvironment::iterator it
;
1625 while(*it
&& i
<size())
1634 this->insert(it
,*name
++);
1639 this->insert(it
, _T('='));
1645 this->insert(it
,*value
++);
1652 int CGit::GetGitEncode(TCHAR
* configkey
)
1654 CString str
=GetConfigValue(configkey
);
1659 return CUnicodeUtils::GetCPCode(str
);
1663 int CGit::GetDiffPath(CTGitPathList
*PathList
, CGitHash
*hash1
, CGitHash
*hash2
, char *arg
)
1669 CAutoLocker
lock(g_Git
.m_critGitDllSec
);
1672 diff
= GetGitDiff();
1674 git_open_diff(&diff
, arg
);
1683 isStat
= !!strstr(arg
, "stat");
1688 ret
= git_root_diff(diff
, hash1
->m_hash
, &file
, &count
,isStat
);
1690 ret
= git_diff(diff
,hash2
->m_hash
,hash1
->m_hash
,&file
,&count
,isStat
);
1699 for(int j
=0;j
<count
;j
++)
1708 int mode
=0,IsBin
=0,inc
=0,dec
=0;
1709 git_get_diff_file(diff
,file
,j
,&newname
,&oldname
,
1710 &mode
,&IsBin
,&inc
,&dec
);
1712 StringAppend(&strnewname
,(BYTE
*)newname
,CP_ACP
);
1713 StringAppend(&stroldname
,(BYTE
*)oldname
,CP_ACP
);
1715 path
.SetFromGit(strnewname
,&stroldname
);
1716 path
.ParserAction((BYTE
)mode
);
1720 path
.m_StatAdd
=_T("-");
1721 path
.m_StatDel
=_T("-");
1724 path
.m_StatAdd
.Format(_T("%d"),inc
);
1725 path
.m_StatDel
.Format(_T("%d"),dec
);
1727 PathList
->AddPath(path
);
1729 git_diff_flush(diff
);
1732 git_close_diff(diff
);