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"
28 #define REG_MSYSGIT_PATH _T("Software\\TortoiseGit\\MSysGit")
29 #define REG_MSYSGIT_EXTRA_PATH _T("Software\\TortoiseGit\\MSysGitExtra")
31 struct git_repository
;
45 CString m_MessageFilter
;
53 CGitCall(CString cmd
):m_Cmd(cmd
){}
55 CString
GetCmd()const{return m_Cmd
;}
56 void SetCmd(CString cmd
){m_Cmd
=cmd
;}
58 //This function is called when command output data is available.
59 //When this function returns 'true' the git command should be aborted.
60 //This behavior is not implemented yet.
61 virtual bool OnOutputData(const BYTE
* data
, size_t size
)=0;
62 virtual bool OnOutputErrData(const BYTE
* data
, size_t size
)=0;
63 virtual void OnEnd(){}
69 typedef std::function
<void (const CStringA
&)> GitReceiverFunc
;
72 class CEnvironment
:public std::vector
<TCHAR
>
75 void CopyProcessEnvironment();
76 CString
GetEnv(const TCHAR
*name
);
77 void SetEnv(const TCHAR
* name
, const TCHAR
* value
);
85 bool m_IsGitDllInited
;
87 GIT_DIFF m_GitSimpleListDiff
;
89 CComCriticalSection m_critGitDllSec
;
92 DWORD m_IsUseLibGit2_mask
;
94 CEnvironment m_Environment
;
96 static BOOL
GitPathFileExists(const CString
&path
)
98 if(path
[0] == _T('\\') && path
[1] == _T('\\'))
99 //it is netshare \\server\sharefoldername
100 // \\server\.git will create smb error log.
102 int length
= path
.GetLength();
107 int start
= path
.Find(_T('\\'),2);
111 start
= path
.Find(_T('\\'),start
+1);
115 return PathFileExists(path
);
119 return PathFileExists(path
);
121 void CheckAndInitDll()
123 if(!m_IsGitDllInited
)
126 m_IsGitDllInited
=true;
130 GIT_DIFF
GetGitDiff()
136 git_open_diff(&m_GitDiff
,"-C -M -r");
141 GIT_DIFF
GetGitSimpleListDiff()
143 if(m_GitSimpleListDiff
)
144 return m_GitSimpleListDiff
;
147 git_open_diff(&m_GitSimpleListDiff
,"-r -r");
148 return m_GitSimpleListDiff
;
152 BOOL
CheckMsysGitDir(BOOL bFallback
= TRUE
);
159 GIT_CMD_COMMIT_UPDATE_INDEX
,
164 GIT_CMD_DELETETAGBRANCH
,
167 bool UsingLibGit2(LIBGIT2_CMD cmd
) const;
169 CString
GetHomeDirectory() const;
170 CString
GetGitLocalConfig() const;
171 CString
GetGitGlobalConfig() const;
172 CString
GetGitGlobalXDGConfigPath() const;
173 CString
GetGitGlobalXDGConfig() const;
174 CString
GetGitSystemConfig() const;
175 git_repository
* GetGitRepository() const;
176 static CStringA
GetGitPathStringA(const CString
&path
);
177 static CString ms_LastMsysGitDir
; // the last msysgitdir added to the path, blank if none
178 static int ms_LastMsysGitVersion
;
179 static int m_LogEncode
;
180 static bool IsBranchNameValid(const CString
& branchname
);
181 bool IsBranchTagNameUnique(const CString
& name
);
182 bool BranchTagExists(const CString
& name
, bool isBranch
= true);
183 unsigned int Hash2int(const CGitHash
&hash
);
185 PROCESS_INFORMATION m_CurrentGitPi
;
190 int Run(CString cmd
, CString
* output
, int code
);
191 int Run(CString cmd
, CString
* output
, CString
* outputErr
, int code
);
192 int Run(CString cmd
, BYTE_VECTOR
*byte_array
, BYTE_VECTOR
*byte_arrayErr
= NULL
);
193 int Run(CGitCall
* pcall
);
194 int Run(CString cmd
, const GitReceiverFunc
& recv
);
197 static DWORD WINAPI
AsyncReadStdErrThread(LPVOID lpParam
);
198 typedef struct AsyncReadStdErrThreadArguments
202 } ASYNCREADSTDERRTHREADARGS
, *PASYNCREADSTDERRTHREADARGS
;
203 CString
GetUnifiedDiffCmd(const CTGitPath
& path
, const git_revnum_t
& rev1
, const git_revnum_t
& rev2
, bool bMerge
, bool bCombine
, int diffContext
);
206 int RunAsync(CString cmd
, PROCESS_INFORMATION
*pi
, HANDLE
* hRead
, HANDLE
*hErrReadOut
, CString
*StdioFile
= NULL
);
207 int RunLogFile(CString cmd
, const CString
&filename
, CString
*stdErr
);
209 int GetDiffPath(CTGitPathList
*PathList
, CGitHash
*hash1
, CGitHash
*hash2
, char *arg
=NULL
);
211 int GetGitEncode(TCHAR
* configkey
);
213 bool IsFastForward(const CString
&from
, const CString
&to
, CGitHash
* commonAncestor
= NULL
);
214 CString
GetConfigValue(const CString
& name
);
215 bool GetConfigValueBool(const CString
& name
);
217 int SetConfigValue(const CString
& key
, const CString
& value
, CONFIG_TYPE type
= CONFIG_LOCAL
);
218 int UnsetConfigValue(const CString
& key
, CONFIG_TYPE type
= CONFIG_LOCAL
);
220 CString
GetUserName(void);
221 CString
GetUserEmail(void);
222 CString
GetCurrentBranch(bool fallback
= false);
223 void GetRemoteTrackedBranch(const CString
& localBranch
, CString
& remote
, CString
& branch
);
224 void GetRemoteTrackedBranchForHEAD(CString
& remote
, CString
& branch
);
225 // read current branch name from HEAD file, returns 0 on success, -1 on failure, 1 detached (branch name "HEAD" returned)
226 static int GetCurrentBranchFromFile(const CString
&sProjectRoot
, CString
&sBranchOut
, bool fallback
= false);
227 BOOL
CheckCleanWorkTree();
228 int Revert(const CString
& commit
, const CTGitPathList
&list
, bool keep
=true);
229 int Revert(const CString
& commit
, const CTGitPath
&path
);
230 int DeleteRef(const CString
& reference
);
231 CString
GetGitLastErr(const CString
& msg
);
232 CString
GetGitLastErr(const CString
& msg
, LIBGIT2_CMD cmd
);
233 static CString
GetLibGit2LastErr();
234 static CString
GetLibGit2LastErr(const CString
& msg
);
235 bool SetCurrentDir(CString path
, bool submodule
= false)
237 bool b
= m_GitDir
.HasAdminDir(path
, submodule
? false : !!PathIsDirectory(path
), &m_CurrentDir
);
238 if (!b
&& g_GitAdminDir
.IsBareRepo(path
))
243 if(m_CurrentDir
.GetLength() == 2 && m_CurrentDir
[1] == _T(':')) //C: D:
245 m_CurrentDir
+= _T('\\');
249 CString m_CurrentDir
;
253 LOG_ORDER_CHRONOLOGIALREVERSED
,
262 BRANCH_FETCH_HEAD
= 0x4,
263 BRANCH_LOCAL_F
= BRANCH_LOCAL
| BRANCH_FETCH_HEAD
,
264 BRANCH_ALL
= BRANCH_LOCAL
| BRANCH_REMOTE
,
265 BRANCH_ALL_F
= BRANCH_ALL
| BRANCH_FETCH_HEAD
,
271 LOG_INFO_FILESTATE
=0x2,
273 LOG_INFO_FULLHISTORY
=0x8,
274 LOG_INFO_BOUNDARY
=0x10,
275 LOG_INFO_ALL_BRANCH
=0x20,
276 LOG_INFO_ONLY_HASH
=0x40,
277 LOG_INFO_DETECT_RENAME
=0x80,
278 LOG_INFO_DETECT_COPYRENAME
=0x100,
279 LOG_INFO_FIRST_PARENT
= 0x200,
280 LOG_INFO_NO_MERGE
= 0x400,
281 LOG_INFO_FOLLOW
= 0x800,
282 LOG_INFO_SHOW_MERGEDFILE
=0x1000,
283 LOG_INFO_FULL_DIFF
= 0x2000,
284 LOG_INFO_SIMPILFY_BY_DECORATION
= 0x4000,
285 LOG_INFO_LOCAL_BRANCHES
= 0x8000,
301 int GetRemoteList(STRING_VECTOR
&list
);
302 int GetBranchList(STRING_VECTOR
&list
, int *Current
,BRANCH_TYPE type
=BRANCH_LOCAL
);
303 int GetTagList(STRING_VECTOR
&list
);
304 int GetRemoteTags(const CString
& remote
, STRING_VECTOR
&list
);
305 int GetMapHashToFriendName(MAP_HASH_NAME
&map
);
306 static int GetMapHashToFriendName(git_repository
* repo
, MAP_HASH_NAME
&map
);
308 CString
DerefFetchHead();
311 // When branchName == FETCH_HEAD, derefrence it.
312 // A selected branch name got from GetBranchList(), with flag BRANCH_FETCH_HEAD enabled,
313 // should go through this function before it is used.
314 CString
FixBranchName_Mod(CString
& branchName
);
315 CString
FixBranchName(const CString
& branchName
);
317 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
);
319 int GetHash(CGitHash
&hash
, const CString
& friendname
);
320 static int GetHash(git_repository
* repo
, CGitHash
&hash
, const CString
& friendname
, bool skipFastCheck
= false);
322 int BuildOutputFormat(CString
&format
,bool IsFull
=TRUE
);
323 static void StringAppend(CString
*str
, const BYTE
*p
, int code
= CP_UTF8
, int length
= -1);
325 BOOL
CanParseRev(CString ref
);
327 int ListConflictFile(CTGitPathList
&list
, const CTGitPath
*path
= nullptr);
328 int GetRefList(STRING_VECTOR
&list
);
330 int RefreshGitIndex();
331 int GetOneFile(const CString
&Refname
, const CTGitPath
&path
, const CString
&outputfile
);
333 //Example: master -> refs/heads/master
334 CString
GetFullRefName(const CString
& shortRefName
);
335 //Removes 'refs/heads/' or just 'refs'. Example: refs/heads/master -> master
336 static CString
StripRefName(CString refName
);
338 int GetCommitDiffList(const CString
&rev1
, const CString
&rev2
, CTGitPathList
&outpathlist
, bool ignoreSpaceAtEol
= false, bool ignoreSpaceChange
= false, bool ignoreAllSpace
= false, bool ignoreBlankLines
= false);
339 int GetInitAddList(CTGitPathList
&outpathlist
);
341 __int64
filetime_to_time_t(const FILETIME
*ft
)
343 long long winTime
= ((long long)ft
->dwHighDateTime
<< 32) + ft
->dwLowDateTime
;
344 winTime
-= 116444736000000000LL; /* Windows to Unix Epoch conversion */
345 winTime
/= 10000000; /* Nano to seconds resolution */
346 return (time_t)winTime
;
349 int GetFileModifyTime(LPCTSTR filename
, __int64
*time
, bool * isDir
=NULL
)
351 WIN32_FILE_ATTRIBUTE_DATA fdata
;
352 if (GetFileAttributesEx(filename
, GetFileExInfoStandard
, &fdata
))
355 *time
= filetime_to_time_t(&fdata
.ftLastWriteTime
);
358 *isDir
= !!( fdata
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
);
365 int GetShortHASHLength() const;
367 static BOOL
GetShortName(CString ref
, CString
&shortname
, CString prefix
)
369 //TRACE(_T("%s %s\r\n"),ref,prefix);
370 if (ref
.Left(prefix
.GetLength()) == prefix
)
372 shortname
= ref
.Right(ref
.GetLength() - prefix
.GetLength());
373 if (shortname
.Right(3) == _T("^{}"))
374 shortname
=shortname
.Left(shortname
.GetLength() - 3);
380 static CString
GetShortName(const CString
& ref
, REF_TYPE
*type
);
382 static bool LoadTextFile(const CString
&filename
, CString
&msg
);
384 int GetUnifiedDiff(const CTGitPath
& path
, const git_revnum_t
& rev1
, const git_revnum_t
& rev2
, CString patchfile
, bool bMerge
, bool bCombine
, int diffContext
);
385 int GetUnifiedDiff(const CTGitPath
& path
, const git_revnum_t
& rev1
, const git_revnum_t
& rev2
, CStringA
* buffer
, bool bMerge
, bool bCombine
, int diffContext
);
387 int GitRevert(int parent
, const CGitHash
&hash
);
389 extern void GetTempPath(CString
&path
);
390 extern CString
GetTempFile();
391 extern DWORD
GetTortoiseGitTempPath(DWORD nBufferLength
, LPTSTR lpBuffer
);