1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2014 - 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.
23 #include "GitStatus.h"
24 #include "GitAdminDir.h"
39 CString m_MessageFilter
;
47 CGitCall(CString cmd
):m_Cmd(cmd
){}
49 CString
GetCmd()const{return m_Cmd
;}
50 void SetCmd(CString cmd
){m_Cmd
=cmd
;}
52 //This function is called when command output data is available.
53 //When this function returns 'true' the git command should be aborted.
54 //This behavior is not implemented yet.
55 virtual bool OnOutputData(const BYTE
* data
, size_t size
)=0;
56 virtual bool OnOutputErrData(const BYTE
* data
, size_t size
)=0;
57 virtual void OnEnd(){}
65 class CEnvironment
:public std::vector
<TCHAR
>
68 void CopyProcessEnvironment();
69 CString
GetEnv(const TCHAR
*name
);
70 void SetEnv(const TCHAR
* name
, const TCHAR
* value
);
78 bool m_IsGitDllInited
;
80 GIT_DIFF m_GitSimpleListDiff
;
82 CComCriticalSection m_critGitDllSec
;
85 DWORD m_IsUseLibGit2_mask
;
87 CEnvironment m_Environment
;
89 static BOOL
GitPathFileExists(const CString
&path
)
91 if(path
[0] == _T('\\') && path
[1] == _T('\\'))
92 //it is netshare \\server\sharefoldername
93 // \\server\.git will create smb error log.
95 int length
= path
.GetLength();
100 int start
= path
.Find(_T('\\'),2);
104 start
= path
.Find(_T('\\'),start
+1);
108 return PathFileExists(path
);
112 return PathFileExists(path
);
114 void CheckAndInitDll()
116 if(!m_IsGitDllInited
)
119 m_IsGitDllInited
=true;
123 GIT_DIFF
GetGitDiff()
129 git_open_diff(&m_GitDiff
,"-C -M -r");
134 GIT_DIFF
GetGitSimpleListDiff()
136 if(m_GitSimpleListDiff
)
137 return m_GitSimpleListDiff
;
140 git_open_diff(&m_GitSimpleListDiff
,"-r -r");
141 return m_GitSimpleListDiff
;
145 BOOL
CheckMsysGitDir(BOOL bFallback
= TRUE
);
148 CString
GetHomeDirectory();
149 CString
GetGitLocalConfig();
150 CString
GetGitGlobalConfig();
151 CString
GetGitGlobalXDGConfigPath();
152 CString
GetGitGlobalXDGConfig();
153 CString
GetGitSystemConfig();
154 static CStringA
GetGitPathStringA(const CString
&path
);
155 static CString ms_LastMsysGitDir
; // the last msysgitdir added to the path, blank if none
156 static int ms_LastMsysGitVersion
;
157 static int m_LogEncode
;
158 static bool IsBranchNameValid(const CString
& branchname
);
159 bool IsBranchTagNameUnique(const CString
& name
);
160 bool BranchTagExists(const CString
& name
, bool isBranch
= true);
161 unsigned int Hash2int(const CGitHash
&hash
);
163 PROCESS_INFORMATION m_CurrentGitPi
;
168 int Run(CString cmd
, CString
* output
, int code
);
169 int Run(CString cmd
, CString
* output
, CString
* outputErr
, int code
);
170 int Run(CString cmd
, BYTE_VECTOR
*byte_array
, BYTE_VECTOR
*byte_arrayErr
= NULL
);
171 int Run(CGitCall
* pcall
);
174 static DWORD WINAPI
AsyncReadStdErrThread(LPVOID lpParam
);
175 typedef struct AsyncReadStdErrThreadArguments
179 } ASYNCREADSTDERRTHREADARGS
, *PASYNCREADSTDERRTHREADARGS
;
180 CString
GetUnifiedDiffCmd(const CTGitPath
& path
, const git_revnum_t
& rev1
, const git_revnum_t
& rev2
, bool bMerge
, bool bCombine
, int diffContext
);
183 int RunAsync(CString cmd
, PROCESS_INFORMATION
*pi
, HANDLE
* hRead
, HANDLE
*hErrReadOut
, CString
*StdioFile
= NULL
);
184 int RunLogFile(CString cmd
, const CString
&filename
, CString
*stdErr
);
186 int GetDiffPath(CTGitPathList
*PathList
, CGitHash
*hash1
, CGitHash
*hash2
, char *arg
=NULL
);
188 int GetGitEncode(TCHAR
* configkey
);
190 bool IsFastForward(const CString
&from
, const CString
&to
, CGitHash
* commonAncestor
= NULL
);
191 CString
GetConfigValue(const CString
& name
, int encoding
= CP_UTF8
, BOOL RemoveCR
= TRUE
);
192 bool GetConfigValueBool(const CString
& name
);
194 int SetConfigValue(const CString
& key
, const CString
& value
, CONFIG_TYPE type
= CONFIG_LOCAL
, int encoding
= CP_UTF8
);
195 int UnsetConfigValue(const CString
& key
, CONFIG_TYPE type
= CONFIG_LOCAL
, int encoding
= CP_UTF8
);
197 CString
GetUserName(void);
198 CString
GetUserEmail(void);
199 CString
GetCurrentBranch(bool fallback
= false);
200 CString
GetSymbolicRef(const wchar_t* symbolicRefName
= L
"HEAD", bool bStripRefsHeads
= true);
201 // read current branch name from HEAD file, returns 0 on success, -1 on failure, 1 detached (branch name "HEAD" returned)
202 static int GetCurrentBranchFromFile(const CString
&sProjectRoot
, CString
&sBranchOut
, bool fallback
= false);
203 BOOL
CheckCleanWorkTree();
204 int Revert(const CString
& commit
, const CTGitPathList
&list
, bool keep
=true);
205 int Revert(const CString
& commit
, const CTGitPath
&path
);
206 CString
GetGitLastErr(const CString
& msg
);
207 CString
GetGitLastErr(const CString
& msg
, int cmd
);
208 static CString
GetLibGit2LastErr();
209 static CString
GetLibGit2LastErr(const CString
& msg
);
210 bool SetCurrentDir(CString path
, bool submodule
= false)
212 bool b
= m_GitDir
.HasAdminDir(path
, submodule
? false : !!PathIsDirectory(path
), &m_CurrentDir
);
213 if (!b
&& g_GitAdminDir
.IsBareRepo(path
))
218 if(m_CurrentDir
.GetLength() == 2 && m_CurrentDir
[1] == _T(':')) //C: D:
220 m_CurrentDir
+= _T('\\');
224 CString m_CurrentDir
;
228 LOG_ORDER_CHRONOLOGIALREVERSED
,
237 BRANCH_FETCH_HEAD
= 0x4,
238 BRANCH_LOCAL_F
= BRANCH_LOCAL
| BRANCH_FETCH_HEAD
,
239 BRANCH_ALL
= BRANCH_LOCAL
| BRANCH_REMOTE
,
240 BRANCH_ALL_F
= BRANCH_ALL
| BRANCH_FETCH_HEAD
,
246 LOG_INFO_FILESTATE
=0x2,
248 LOG_INFO_FULLHISTORY
=0x8,
249 LOG_INFO_BOUNDARY
=0x10,
250 LOG_INFO_ALL_BRANCH
=0x20,
251 LOG_INFO_ONLY_HASH
=0x40,
252 LOG_INFO_DETECT_RENAME
=0x80,
253 LOG_INFO_DETECT_COPYRENAME
=0x100,
254 LOG_INFO_FIRST_PARENT
= 0x200,
255 LOG_INFO_NO_MERGE
= 0x400,
256 LOG_INFO_FOLLOW
= 0x800,
257 LOG_INFO_SHOW_MERGEDFILE
=0x1000,
258 LOG_INFO_FULL_DIFF
= 0x2000,
259 LOG_INFO_SIMPILFY_BY_DECORATION
= 0x4000,
260 LOG_INFO_LOCAL_BRANCHES
= 0x8000,
276 int GetRemoteList(STRING_VECTOR
&list
);
277 int GetBranchList(STRING_VECTOR
&list
, int *Current
,BRANCH_TYPE type
=BRANCH_LOCAL
);
278 int GetTagList(STRING_VECTOR
&list
);
279 int GetRemoteTags(const CString
& remote
, STRING_VECTOR
&list
);
280 int GetMapHashToFriendName(MAP_HASH_NAME
&map
);
282 CString
DerefFetchHead();
285 // When branchName == FETCH_HEAD, derefrence it.
286 // A selected branch name got from GetBranchList(), with flag BRANCH_FETCH_HEAD enabled,
287 // should go through this function before it is used.
288 CString
FixBranchName_Mod(CString
& branchName
);
289 CString
FixBranchName(const CString
& branchName
);
291 CString
GetLogCmd(const CString
&range
, const CTGitPath
*path
= NULL
, int count
=-1, int InfoMask
= LOG_INFO_FULL_DIFF
|LOG_INFO_STAT
|LOG_INFO_FILESTATE
|LOG_INFO_BOUNDARY
|LOG_INFO_DETECT_COPYRENAME
|LOG_INFO_SHOW_MERGEDFILE
, bool paramonly
=false, CFilterData
* filter
=NULL
);
293 int GetHash(CGitHash
&hash
, const CString
& friendname
);
295 int BuildOutputFormat(CString
&format
,bool IsFull
=TRUE
);
296 //int GetShortLog(const CString &log,CTGitPath * path=NULL, int count =-1);
297 static void StringAppend(CString
*str
, const BYTE
*p
, int code
= CP_UTF8
, int length
= -1);
299 BOOL
CanParseRev(CString ref
);
301 int ListConflictFile(CTGitPathList
&list
, const CTGitPath
*path
= nullptr);
302 int GetRefList(STRING_VECTOR
&list
);
304 int RefreshGitIndex();
305 int GetOneFile(const CString
&Refname
, const CTGitPath
&path
, const CString
&outputfile
);
307 //Example: master -> refs/heads/master
308 CString
GetFullRefName(const CString
& shortRefName
);
309 //Removes 'refs/heads/' or just 'refs'. Example: refs/heads/master -> master
310 static CString
StripRefName(CString refName
);
312 int GetCommitDiffList(const CString
&rev1
,const CString
&rev2
,CTGitPathList
&outpathlist
);
313 int GetInitAddList(CTGitPathList
&outpathlist
);
315 __int64
filetime_to_time_t(const FILETIME
*ft
)
317 long long winTime
= ((long long)ft
->dwHighDateTime
<< 32) + ft
->dwLowDateTime
;
318 winTime
-= 116444736000000000LL; /* Windows to Unix Epoch conversion */
319 winTime
/= 10000000; /* Nano to seconds resolution */
320 return (time_t)winTime
;
323 int GetFileModifyTime(LPCTSTR filename
, __int64
*time
, bool * isDir
=NULL
)
325 WIN32_FILE_ATTRIBUTE_DATA fdata
;
326 if (GetFileAttributesEx(filename
, GetFileExInfoStandard
, &fdata
))
329 *time
= filetime_to_time_t(&fdata
.ftLastWriteTime
);
332 *isDir
= !!( fdata
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
);
339 int GetShortHASHLength();
341 static BOOL
GetShortName(CString ref
, CString
&shortname
, CString prefix
)
343 //TRACE(_T("%s %s\r\n"),ref,prefix);
344 if (ref
.Left(prefix
.GetLength()) == prefix
)
346 shortname
= ref
.Right(ref
.GetLength() - prefix
.GetLength());
347 if (shortname
.Right(3) == _T("^{}"))
348 shortname
=shortname
.Left(shortname
.GetLength() - 3);
354 static CString
GetShortName(const CString
& ref
, REF_TYPE
*type
);
360 GIT_CMD_COMMIT_UPDATE_INDEX
,
364 bool UsingLibGit2(int cmd
);
366 int GetUnifiedDiff(const CTGitPath
& path
, const git_revnum_t
& rev1
, const git_revnum_t
& rev2
, CString patchfile
, bool bMerge
, bool bCombine
, int diffContext
);
367 int GetUnifiedDiff(const CTGitPath
& path
, const git_revnum_t
& rev1
, const git_revnum_t
& rev2
, CStringA
* buffer
, bool bMerge
, bool bCombine
, int diffContext
);
369 extern void GetTempPath(CString
&path
);
370 extern CString
GetTempFile();
371 extern DWORD
GetTortoiseGitTempPath(DWORD nBufferLength
, LPTSTR lpBuffer
);