1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2015 - 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 #define DEFAULT_USE_LIBGIT2_MASK (1 << CGit::GIT_CMD_MERGE_BASE) | (1 << CGit::GIT_CMD_DELETETAGBRANCH) | (1 << CGit::GIT_CMD_GETONEFILE) | (1 << CGit::GIT_CMD_ADD) | (1 << CGit::GIT_CMD_CHECKCONFLICTS) | (1 << CGit::GIT_CMD_GET_COMMIT)
33 struct git_repository
;
47 CString m_MessageFilter
;
55 CGitCall(CString cmd
):m_Cmd(cmd
){}
57 CString
GetCmd()const{return m_Cmd
;}
58 void SetCmd(CString cmd
){m_Cmd
=cmd
;}
60 //This function is called when command output data is available.
61 //When this function returns 'true' the git command should be aborted.
62 //This behavior is not implemented yet.
63 virtual bool OnOutputData(const BYTE
* data
, size_t size
)=0;
64 virtual bool OnOutputErrData(const BYTE
* data
, size_t size
)=0;
65 virtual void OnEnd(){}
71 typedef std::function
<void (const CStringA
&)> GitReceiverFunc
;
74 class CEnvironment
: protected std::vector
<TCHAR
>
77 void CopyProcessEnvironment();
78 CString
GetEnv(const TCHAR
*name
);
79 void SetEnv(const TCHAR
* name
, const TCHAR
* value
);
90 GIT_DIFF m_GitSimpleListDiff
;
91 #ifdef GTEST_INCLUDE_GTEST_GTEST_H_
94 bool m_IsGitDllInited
;
96 CComCriticalSection m_critGitDllSec
;
99 DWORD m_IsUseLibGit2_mask
;
101 CEnvironment m_Environment
;
103 static BOOL
GitPathFileExists(const CString
&path
)
105 if(path
[0] == _T('\\') && path
[1] == _T('\\'))
106 //it is netshare \\server\sharefoldername
107 // \\server\.git will create smb error log.
109 int length
= path
.GetLength();
114 int start
= path
.Find(_T('\\'),2);
118 start
= path
.Find(_T('\\'),start
+1);
122 return PathFileExists(path
);
126 return PathFileExists(path
);
128 void CheckAndInitDll()
130 if(!m_IsGitDllInited
)
133 m_IsGitDllInited
=true;
137 GIT_DIFF
GetGitDiff()
143 git_open_diff(&m_GitDiff
,"-C -M -r");
148 GIT_DIFF
GetGitSimpleListDiff()
150 if(m_GitSimpleListDiff
)
151 return m_GitSimpleListDiff
;
154 git_open_diff(&m_GitSimpleListDiff
,"-r -r");
155 return m_GitSimpleListDiff
;
159 BOOL
CheckMsysGitDir(BOOL bFallback
= TRUE
);
160 BOOL
FindAndSetGitExePath(BOOL bFallback
);
167 GIT_CMD_COMMIT_UPDATE_INDEX
,
172 GIT_CMD_DELETETAGBRANCH
,
176 GIT_CMD_CHECK_CLEAN_WT
,
177 GIT_CMD_CHECKCONFLICTS
,
181 bool UsingLibGit2(LIBGIT2_CMD cmd
) const;
183 * callback type should be git_cred_acquire_cb
185 static void SetGit2CredentialCallback(void* callback
);
186 static void SetGit2CertificateCheckCertificate(void* callback
);
188 CString
GetHomeDirectory() const;
189 CString
GetGitLocalConfig() const;
190 CString
GetGitGlobalConfig() const;
191 CString
GetGitGlobalXDGConfigPath() const;
192 CString
GetGitGlobalXDGConfig() const;
193 CString
GetGitSystemConfig() const;
194 git_repository
* GetGitRepository() const;
195 static CStringA
GetGitPathStringA(const CString
&path
);
196 static CString ms_LastMsysGitDir
; // the last msysgitdir added to the path, blank if none
197 static int ms_LastMsysGitVersion
;
198 static bool ms_bCygwinGit
;
199 static int m_LogEncode
;
200 static bool IsBranchNameValid(const CString
& branchname
);
201 bool IsBranchTagNameUnique(const CString
& name
);
203 * Checks if a branch or tag with the given name exists
204 *isBranch is true -> branch, tag otherwise
206 bool BranchTagExists(const CString
& name
, bool isBranch
= true);
207 unsigned int Hash2int(const CGitHash
&hash
);
209 PROCESS_INFORMATION m_CurrentGitPi
;
214 int Run(CString cmd
, CString
* output
, int code
);
215 int Run(CString cmd
, CString
* output
, CString
* outputErr
, int code
);
216 int Run(CString cmd
, BYTE_VECTOR
*byte_array
, BYTE_VECTOR
*byte_arrayErr
= NULL
);
217 int Run(CGitCall
* pcall
);
218 int Run(CString cmd
, const GitReceiverFunc
& recv
);
221 static DWORD WINAPI
AsyncReadStdErrThread(LPVOID lpParam
);
222 typedef struct AsyncReadStdErrThreadArguments
226 } ASYNCREADSTDERRTHREADARGS
, *PASYNCREADSTDERRTHREADARGS
;
227 CString
GetUnifiedDiffCmd(const CTGitPath
& path
, const git_revnum_t
& rev1
, const git_revnum_t
& rev2
, bool bMerge
, bool bCombine
, int diffContext
);
230 int RunAsync(CString cmd
, PROCESS_INFORMATION
*pi
, HANDLE
* hRead
, HANDLE
*hErrReadOut
, CString
*StdioFile
= NULL
);
231 int RunLogFile(CString cmd
, const CString
&filename
, CString
*stdErr
);
233 int GetDiffPath(CTGitPathList
*PathList
, CGitHash
*hash1
, CGitHash
*hash2
, char *arg
=NULL
);
235 int GetGitEncode(TCHAR
* configkey
);
237 bool IsFastForward(const CString
&from
, const CString
&to
, CGitHash
* commonAncestor
= NULL
);
238 CString
GetConfigValue(const CString
& name
);
239 bool GetConfigValueBool(const CString
& name
);
240 int GetConfigValueInt32(const CString
& name
, int def
= 0);
242 int SetConfigValue(const CString
& key
, const CString
& value
, CONFIG_TYPE type
= CONFIG_LOCAL
);
243 int UnsetConfigValue(const CString
& key
, CONFIG_TYPE type
= CONFIG_LOCAL
);
245 CString
GetUserName(void);
246 CString
GetUserEmail(void);
247 CString
GetCurrentBranch(bool fallback
= false);
248 void GetRemoteTrackedBranch(const CString
& localBranch
, CString
& remote
, CString
& branch
);
249 void GetRemoteTrackedBranchForHEAD(CString
& remote
, CString
& branch
);
250 // read current branch name from HEAD file, returns 0 on success, -1 on failure, 1 detached (branch name "HEAD" returned)
251 static int GetCurrentBranchFromFile(const CString
&sProjectRoot
, CString
&sBranchOut
, bool fallback
= false);
253 Use this method only when the HEAD is exist.
255 BOOL
CheckCleanWorkTree(bool stagedOk
= false);
256 int Revert(const CString
& commit
, const CTGitPathList
&list
, CString
& err
);
257 int Revert(const CString
& commit
, const CTGitPath
&path
, CString
& err
);
258 int DeleteRef(const CString
& reference
);
260 Use this method only if m_IsUseLibGit2 is used for fallbacks.
261 If you directly use libgit2 methods, use GetLibGit2LastErr instead.
263 CString
GetGitLastErr(const CString
& msg
);
264 CString
GetGitLastErr(const CString
& msg
, LIBGIT2_CMD cmd
);
265 static CString
GetLibGit2LastErr();
266 static CString
GetLibGit2LastErr(const CString
& msg
);
267 bool SetCurrentDir(CString path
, bool submodule
= false)
269 bool b
= GitAdminDir::HasAdminDir(path
, submodule
? false : !!PathIsDirectory(path
), &m_CurrentDir
);
270 if (!b
&& GitAdminDir::IsBareRepo(path
))
275 if(m_CurrentDir
.GetLength() == 2 && m_CurrentDir
[1] == _T(':')) //C: D:
277 m_CurrentDir
+= _T('\\');
281 CString m_CurrentDir
;
285 LOG_ORDER_CHRONOLOGIALREVERSED
,
294 BRANCH_FETCH_HEAD
= 0x4,
295 BRANCH_LOCAL_F
= BRANCH_LOCAL
| BRANCH_FETCH_HEAD
,
296 BRANCH_ALL
= BRANCH_LOCAL
| BRANCH_REMOTE
,
297 BRANCH_ALL_F
= BRANCH_ALL
| BRANCH_FETCH_HEAD
,
303 LOG_INFO_FILESTATE
=0x2,
305 LOG_INFO_FULLHISTORY
=0x8,
306 LOG_INFO_BOUNDARY
=0x10,
307 LOG_INFO_ALL_BRANCH
=0x20,
308 LOG_INFO_ONLY_HASH
=0x40,
309 LOG_INFO_DETECT_RENAME
=0x80,
310 LOG_INFO_DETECT_COPYRENAME
=0x100,
311 LOG_INFO_FIRST_PARENT
= 0x200,
312 LOG_INFO_NO_MERGE
= 0x400,
313 LOG_INFO_FOLLOW
= 0x800,
314 LOG_INFO_SHOW_MERGEDFILE
=0x1000,
315 LOG_INFO_FULL_DIFF
= 0x2000,
316 LOG_INFO_SIMPILFY_BY_DECORATION
= 0x4000,
317 LOG_INFO_LOCAL_BRANCHES
= 0x8000,
333 int GetRemoteList(STRING_VECTOR
&list
);
334 int GetBranchList(STRING_VECTOR
&list
, int *Current
,BRANCH_TYPE type
=BRANCH_LOCAL
);
335 int GetTagList(STRING_VECTOR
&list
);
336 int GetRemoteTags(const CString
& remote
, STRING_VECTOR
& list
);
337 int DeleteRemoteRefs(const CString
& remote
, const STRING_VECTOR
& list
);
338 int GetBranchDescriptions(MAP_STRING_STRING
& map
);
339 int GetMapHashToFriendName(MAP_HASH_NAME
&map
);
340 static int GetMapHashToFriendName(git_repository
* repo
, MAP_HASH_NAME
&map
);
342 CString
DerefFetchHead();
345 // When branchName == FETCH_HEAD, derefrence it.
346 // A selected branch name got from GetBranchList(), with flag BRANCH_FETCH_HEAD enabled,
347 // should go through this function before it is used.
348 CString
FixBranchName_Mod(CString
& branchName
);
349 CString
FixBranchName(const CString
& branchName
);
351 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
);
353 int GetHash(CGitHash
&hash
, const CString
& friendname
);
354 static int GetHash(git_repository
* repo
, CGitHash
&hash
, const CString
& friendname
, bool skipFastCheck
= false);
356 int BuildOutputFormat(CString
&format
,bool IsFull
=TRUE
);
357 static void StringAppend(CString
*str
, const BYTE
*p
, int code
= CP_UTF8
, int length
= -1);
359 BOOL
CanParseRev(CString ref
);
361 Checks if HEAD points to an unborn branch
362 This method assumes, that we already know that we are in a working tree.
365 /** Returns 0 if no conflict, if a conflict was found and -1 in case of a failure */
366 int HasWorkingTreeConflicts();
367 /** Returns 0 if no conflict, if a conflict was found and -1 in case of a failure */
368 int HasWorkingTreeConflicts(git_repository
* repo
);
369 int GetRefList(STRING_VECTOR
&list
);
371 int RefreshGitIndex();
372 int GetOneFile(const CString
&Refname
, const CTGitPath
&path
, const CString
&outputfile
);
374 //Example: master -> refs/heads/master
375 CString
GetFullRefName(const CString
& shortRefName
);
376 //Removes 'refs/heads/' or just 'refs'. Example: refs/heads/master -> master
377 static CString
StripRefName(CString refName
);
379 int GetCommitDiffList(const CString
&rev1
, const CString
&rev2
, CTGitPathList
&outpathlist
, bool ignoreSpaceAtEol
= false, bool ignoreSpaceChange
= false, bool ignoreAllSpace
= false, bool ignoreBlankLines
= false);
380 int GetInitAddList(CTGitPathList
&outpathlist
);
381 int GetWorkingTreeChanges(CTGitPathList
& result
, bool amend
= false, CTGitPathList
* filterlist
= nullptr);
383 __int64
filetime_to_time_t(const FILETIME
*ft
)
385 long long winTime
= ((long long)ft
->dwHighDateTime
<< 32) + ft
->dwLowDateTime
;
386 winTime
-= 116444736000000000LL; /* Windows to Unix Epoch conversion */
387 winTime
/= 10000000; /* Nano to seconds resolution */
388 return (time_t)winTime
;
391 int GetFileModifyTime(LPCTSTR filename
, __int64
* time
, bool* isDir
= nullptr, __int64
* size
= nullptr)
393 WIN32_FILE_ATTRIBUTE_DATA fdata
;
394 if (GetFileAttributesEx(filename
, GetFileExInfoStandard
, &fdata
))
397 *time
= filetime_to_time_t(&fdata
.ftLastWriteTime
);
400 *size
= ((__int64
)fdata
.nFileSizeHigh
<< 32) + fdata
.nFileSizeLow
;
403 *isDir
= !!( fdata
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
);
410 int GetShortHASHLength() const;
412 static BOOL
GetShortName(const CString
&ref
, CString
&shortname
, CString prefix
)
414 //TRACE(_T("%s %s\r\n"),ref,prefix);
415 if (ref
.Left(prefix
.GetLength()) == prefix
)
417 shortname
= ref
.Right(ref
.GetLength() - prefix
.GetLength());
418 if (shortname
.Right(3) == _T("^{}"))
419 shortname
=shortname
.Left(shortname
.GetLength() - 3);
425 static CString
GetShortName(const CString
& ref
, REF_TYPE
*type
);
427 static bool LoadTextFile(const CString
&filename
, CString
&msg
);
429 int GetUnifiedDiff(const CTGitPath
& path
, const git_revnum_t
& rev1
, const git_revnum_t
& rev2
, CString patchfile
, bool bMerge
, bool bCombine
, int diffContext
);
430 int GetUnifiedDiff(const CTGitPath
& path
, const git_revnum_t
& rev1
, const git_revnum_t
& rev2
, CStringA
* buffer
, bool bMerge
, bool bCombine
, int diffContext
);
432 int GitRevert(int parent
, const CGitHash
&hash
);
434 CString
CombinePath(const CString
&path
) const
438 if (m_CurrentDir
.IsEmpty())
440 return m_CurrentDir
+ (m_CurrentDir
.Right(1) == _T("\\") ? _T("") : _T("\\")) + path
;
443 CString
CombinePath(const CTGitPath
&path
) const
445 return CombinePath(path
.GetWinPath());
448 CString
CombinePath(const CTGitPath
*path
) const
451 return CombinePath(path
->GetWinPath());
454 extern void GetTempPath(CString
&path
);
455 extern CString
GetTempFile();
456 extern DWORD
GetTortoiseGitTempPath(DWORD nBufferLength
, LPTSTR lpBuffer
);