Drop unused code
[TortoiseGit.git] / src / Git / Git.h
blobe07ade47cdc1a1000b3a2743e4c8eddc1769ee94
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.
20 #pragma once
21 #include "GitType.h"
22 #include "GitRev.h"
23 #include "GitStatus.h"
24 #include "GitAdminDir.h"
25 #include "gitdll.h"
26 #include <functional>
28 #define REG_MSYSGIT_PATH _T("Software\\TortoiseGit\\MSysGit")
29 #define REG_SYSTEM_GITCONFIGPATH _T("Software\\TortoiseGit\\SystemConfig")
30 #define REG_MSYSGIT_EXTRA_PATH _T("Software\\TortoiseGit\\MSysGitExtra")
32 #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)
34 struct git_repository;
36 class CFilterData
38 public:
40 enum
42 SHOW_NO_LIMIT, // NOTE: no limitation does not mean "without all limitations", it's just without the following limitations. That say, the log still could be limited by auther, committer, etc.
43 SHOW_LAST_SEL_DATE,
44 SHOW_LAST_N_COMMITS,
45 SHOW_LAST_N_YEARS,
46 SHOW_LAST_N_MONTHS,
47 SHOW_LAST_N_WEEKS,
50 CFilterData()
52 m_From=m_To=-1;
53 m_IsRegex=1;
54 m_NumberOfLogsScale = SHOW_NO_LIMIT;
55 m_NumberOfLogs = 1;
58 DWORD m_NumberOfLogsScale;
59 DWORD m_NumberOfLogs;
60 __time64_t m_From;
61 __time64_t m_To;
62 CString m_Author;
63 CString m_Committer;
64 CString m_MessageFilter;
65 BOOL m_IsRegex;
68 class CGitCall
70 public:
71 CGitCall(){}
72 CGitCall(CString cmd):m_Cmd(cmd){}
74 CString GetCmd()const{return m_Cmd;}
75 void SetCmd(CString cmd){m_Cmd=cmd;}
77 //This function is called when command output data is available.
78 //When this function returns 'true' the git command should be aborted.
79 //This behavior is not implemented yet.
80 virtual bool OnOutputData(const BYTE* data, size_t size)=0;
81 virtual bool OnOutputErrData(const BYTE* data, size_t size)=0;
82 virtual void OnEnd(){}
84 private:
85 CString m_Cmd;
88 typedef std::function<void (const CStringA&)> GitReceiverFunc;
90 class CTGitPath;
91 class CEnvironment : protected std::vector<TCHAR>
93 public:
94 void CopyProcessEnvironment();
95 CString GetEnv(const TCHAR *name);
96 void SetEnv(const TCHAR* name, const TCHAR* value);
97 void AddToPath(CString value);
98 void clear();
99 bool empty();
100 operator LPTSTR();
102 class CGit
104 private:
105 CString gitLastErr;
106 protected:
107 GIT_DIFF m_GitDiff;
108 GIT_DIFF m_GitSimpleListDiff;
109 #ifdef GTEST_INCLUDE_GTEST_GTEST_H_
110 public:
111 #endif
112 bool m_IsGitDllInited;
113 public:
114 CComCriticalSection m_critGitDllSec;
115 bool m_IsUseGitDLL;
116 bool m_IsUseLibGit2;
117 DWORD m_IsUseLibGit2_mask;
119 CEnvironment m_Environment;
121 static BOOL GitPathFileExists(const CString &path)
123 if(path[0] == _T('\\') && path[1] == _T('\\'))
124 //it is netshare \\server\sharefoldername
125 // \\server\.git will create smb error log.
127 int length = path.GetLength();
129 if(length<2)
130 return false;
132 int start = path.Find(_T('\\'),2);
133 if(start<0)
134 return false;
136 start = path.Find(_T('\\'),start+1);
137 if(start<0)
138 return false;
140 return PathFileExists(path);
143 else
144 return PathFileExists(path);
146 void CheckAndInitDll()
148 if(!m_IsGitDllInited)
150 git_init();
151 m_IsGitDllInited=true;
155 GIT_DIFF GetGitDiff()
157 if(m_GitDiff)
158 return m_GitDiff;
159 else
161 git_open_diff(&m_GitDiff,"-C -M -r");
162 return m_GitDiff;
166 GIT_DIFF GetGitSimpleListDiff()
168 if(m_GitSimpleListDiff)
169 return m_GitSimpleListDiff;
170 else
172 git_open_diff(&m_GitSimpleListDiff,"-r -r");
173 return m_GitSimpleListDiff;
177 BOOL CheckMsysGitDir(BOOL bFallback = TRUE);
178 BOOL FindAndSetGitExePath(BOOL bFallback);
179 BOOL m_bInitialized;
181 typedef enum
183 GIT_CMD_CLONE,
184 GIT_CMD_FETCH,
185 GIT_CMD_COMMIT_UPDATE_INDEX,
186 GIT_CMD_DIFF,
187 GIT_CMD_RESET,
188 GIT_CMD_REVERT,
189 GIT_CMD_MERGE_BASE,
190 GIT_CMD_DELETETAGBRANCH,
191 GIT_CMD_GETONEFILE,
192 GIT_CMD_ADD,
193 GIT_CMD_PUSH,
194 GIT_CMD_CHECK_CLEAN_WT,
195 GIT_CMD_CHECKCONFLICTS,
196 GIT_CMD_GET_COMMIT,
197 GIT_CMD_LOGLISTDIFF,
198 } LIBGIT2_CMD;
199 bool UsingLibGit2(LIBGIT2_CMD cmd) const;
201 * callback type should be git_cred_acquire_cb
203 static void SetGit2CredentialCallback(void* callback);
204 static void SetGit2CertificateCheckCertificate(void* callback);
206 CString GetHomeDirectory() const;
207 CString GetGitLocalConfig() const;
208 CString GetGitGlobalConfig() const;
209 CString GetGitGlobalXDGConfigPath() const;
210 CString GetGitGlobalXDGConfig() const;
211 CString GetGitSystemConfig() const;
212 git_repository * GetGitRepository() const;
213 static CStringA GetGitPathStringA(const CString &path);
214 static CString ms_LastMsysGitDir; // the last msysgitdir added to the path, blank if none
215 static CString ms_MsysGitRootDir;
216 static int ms_LastMsysGitVersion;
217 static bool ms_bCygwinGit;
218 static bool ms_bMsys2Git;
219 static int m_LogEncode;
220 static bool IsBranchNameValid(const CString& branchname);
221 bool IsBranchTagNameUnique(const CString& name);
223 * Checks if a branch or tag with the given name exists
224 *isBranch is true -> branch, tag otherwise
226 bool BranchTagExists(const CString& name, bool isBranch = true);
227 unsigned int Hash2int(const CGitHash &hash);
229 PROCESS_INFORMATION m_CurrentGitPi;
231 CGit(void);
232 ~CGit(void);
234 int Run(CString cmd, CString* output, int code);
235 int Run(CString cmd, CString* output, CString* outputErr, int code);
236 int Run(CString cmd, BYTE_VECTOR *byte_array, BYTE_VECTOR *byte_arrayErr = NULL);
237 int Run(CGitCall* pcall);
238 int Run(CString cmd, const GitReceiverFunc& recv);
240 private:
241 static DWORD WINAPI AsyncReadStdErrThread(LPVOID lpParam);
242 typedef struct AsyncReadStdErrThreadArguments
244 HANDLE fileHandle;
245 CGitCall* pcall;
246 } ASYNCREADSTDERRTHREADARGS, *PASYNCREADSTDERRTHREADARGS;
247 CString GetUnifiedDiffCmd(const CTGitPath& path, const git_revnum_t& rev1, const git_revnum_t& rev2, bool bMerge, bool bCombine, int diffContext);
249 public:
250 int RunAsync(CString cmd, PROCESS_INFORMATION *pi, HANDLE* hRead, HANDLE *hErrReadOut, CString *StdioFile = NULL);
251 int RunLogFile(CString cmd, const CString &filename, CString *stdErr);
253 int GetDiffPath(CTGitPathList *PathList, CGitHash *hash1, CGitHash *hash2, char *arg=NULL);
255 int GetGitEncode(TCHAR* configkey);
257 bool IsFastForward(const CString &from, const CString &to, CGitHash * commonAncestor = NULL);
258 CString GetConfigValue(const CString& name);
259 bool GetConfigValueBool(const CString& name);
260 int GetConfigValueInt32(const CString& name, int def = 0);
262 int SetConfigValue(const CString& key, const CString& value, CONFIG_TYPE type = CONFIG_LOCAL);
263 int UnsetConfigValue(const CString& key, CONFIG_TYPE type = CONFIG_LOCAL);
265 CString GetUserName(void);
266 CString GetUserEmail(void);
267 CString GetCurrentBranch(bool fallback = false);
268 void GetRemoteTrackedBranch(const CString& localBranch, CString& remote, CString& branch);
269 void GetRemoteTrackedBranchForHEAD(CString& remote, CString& branch);
270 // read current branch name from HEAD file, returns 0 on success, -1 on failure, 1 detached (branch name "HEAD" returned)
271 static int GetCurrentBranchFromFile(const CString &sProjectRoot, CString &sBranchOut, bool fallback = false);
273 Use this method only when the HEAD is exist.
275 BOOL CheckCleanWorkTree(bool stagedOk = false);
276 int Revert(const CString& commit, const CTGitPathList &list, CString& err);
277 int Revert(const CString& commit, const CTGitPath &path, CString& err);
278 int DeleteRef(const CString& reference);
280 Use this method only if m_IsUseLibGit2 is used for fallbacks.
281 If you directly use libgit2 methods, use GetLibGit2LastErr instead.
283 CString GetGitLastErr(const CString& msg);
284 CString GetGitLastErr(const CString& msg, LIBGIT2_CMD cmd);
285 static CString GetLibGit2LastErr();
286 static CString GetLibGit2LastErr(const CString& msg);
287 bool SetCurrentDir(CString path, bool submodule = false)
289 bool b = GitAdminDir::HasAdminDir(path, submodule ? false : !!PathIsDirectory(path), &m_CurrentDir);
290 if (!b && GitAdminDir::IsBareRepo(path))
292 m_CurrentDir = path;
293 b = true;
295 if(m_CurrentDir.GetLength() == 2 && m_CurrentDir[1] == _T(':')) //C: D:
297 m_CurrentDir += _T('\\');
299 return b;
301 CString m_CurrentDir;
303 enum
305 LOG_ORDER_CHRONOLOGIALREVERSED,
306 LOG_ORDER_TOPOORDER,
307 LOG_ORDER_DATEORDER,
310 typedef enum
312 BRANCH_LOCAL = 0x1,
313 BRANCH_REMOTE = 0x2,
314 BRANCH_FETCH_HEAD = 0x4,
315 BRANCH_LOCAL_F = BRANCH_LOCAL | BRANCH_FETCH_HEAD,
316 BRANCH_ALL = BRANCH_LOCAL | BRANCH_REMOTE,
317 BRANCH_ALL_F = BRANCH_ALL | BRANCH_FETCH_HEAD,
318 }BRANCH_TYPE;
320 typedef enum
322 LOG_INFO_STAT=0x1,
323 LOG_INFO_FILESTATE=0x2,
324 LOG_INFO_PATCH=0x4,
325 LOG_INFO_FULLHISTORY=0x8,
326 LOG_INFO_BOUNDARY=0x10,
327 LOG_INFO_ALL_BRANCH=0x20,
328 LOG_INFO_ONLY_HASH=0x40,
329 LOG_INFO_DETECT_RENAME=0x80,
330 LOG_INFO_DETECT_COPYRENAME=0x100,
331 LOG_INFO_FIRST_PARENT = 0x200,
332 LOG_INFO_NO_MERGE = 0x400,
333 LOG_INFO_FOLLOW = 0x800,
334 LOG_INFO_SHOW_MERGEDFILE=0x1000,
335 LOG_INFO_FULL_DIFF = 0x2000,
336 LOG_INFO_SIMPILFY_BY_DECORATION = 0x4000,
337 LOG_INFO_LOCAL_BRANCHES = 0x8000,
338 }LOG_INFO_MASK;
340 typedef enum
342 LOCAL_BRANCH,
343 REMOTE_BRANCH,
344 TAG,
345 STASH,
346 BISECT_GOOD,
347 BISECT_BAD,
348 NOTES,
349 UNKNOWN,
351 }REF_TYPE;
353 int GetRemoteList(STRING_VECTOR &list);
354 int GetBranchList(STRING_VECTOR &list, int *Current,BRANCH_TYPE type=BRANCH_LOCAL);
355 int GetTagList(STRING_VECTOR &list);
356 int GetRemoteTags(const CString& remote, STRING_VECTOR& list);
357 int DeleteRemoteRefs(const CString& remote, const STRING_VECTOR& list);
358 int GetBranchDescriptions(MAP_STRING_STRING& map);
359 int GetMapHashToFriendName(MAP_HASH_NAME &map);
360 static int GetMapHashToFriendName(git_repository* repo, MAP_HASH_NAME &map);
362 CString DerefFetchHead();
364 // FixBranchName():
365 // When branchName == FETCH_HEAD, derefrence it.
366 // A selected branch name got from GetBranchList(), with flag BRANCH_FETCH_HEAD enabled,
367 // should go through this function before it is used.
368 CString FixBranchName_Mod(CString& branchName);
369 CString FixBranchName(const CString& branchName);
371 CString GetLogCmd(const CString& range, const CTGitPath* path = nullptr, int InfoMask = LOG_INFO_FULL_DIFF | LOG_INFO_STAT | LOG_INFO_FILESTATE | LOG_INFO_BOUNDARY | LOG_INFO_DETECT_COPYRENAME | LOG_INFO_SHOW_MERGEDFILE, CFilterData* filter = nullptr);
373 int GetHash(CGitHash &hash, const CString& friendname);
374 static int GetHash(git_repository * repo, CGitHash &hash, const CString& friendname, bool skipFastCheck = false);
376 int BuildOutputFormat(CString &format,bool IsFull=TRUE);
377 static void StringAppend(CString *str, const BYTE *p, int code = CP_UTF8, int length = -1);
379 BOOL CanParseRev(CString ref);
381 Checks if HEAD points to an unborn branch
382 This method assumes, that we already know that we are in a working tree.
384 BOOL IsInitRepos();
385 /** Returns 0 if no conflict, if a conflict was found and -1 in case of a failure */
386 int HasWorkingTreeConflicts();
387 /** Returns 0 if no conflict, if a conflict was found and -1 in case of a failure */
388 int HasWorkingTreeConflicts(git_repository* repo);
389 int GetRefList(STRING_VECTOR &list);
391 int RefreshGitIndex();
392 int GetOneFile(const CString &Refname, const CTGitPath &path, const CString &outputfile);
394 //Example: master -> refs/heads/master
395 CString GetFullRefName(const CString& shortRefName);
396 //Removes 'refs/heads/' or just 'refs'. Example: refs/heads/master -> master
397 static CString StripRefName(CString refName);
399 int GetCommitDiffList(const CString &rev1, const CString &rev2, CTGitPathList &outpathlist, bool ignoreSpaceAtEol = false, bool ignoreSpaceChange = false, bool ignoreAllSpace = false, bool ignoreBlankLines = false);
400 int GetInitAddList(CTGitPathList &outpathlist);
401 int GetWorkingTreeChanges(CTGitPathList& result, bool amend = false, CTGitPathList* filterlist = nullptr);
403 static __int64 filetime_to_time_t(const FILETIME *ft)
405 long long winTime = ((long long)ft->dwHighDateTime << 32) + ft->dwLowDateTime;
406 winTime -= 116444736000000000LL; /* Windows to Unix Epoch conversion */
407 winTime /= 10000000; /* Nano to seconds resolution */
408 return (time_t)winTime;
411 static int GetFileModifyTime(LPCTSTR filename, __int64* time, bool* isDir = nullptr, __int64* size = nullptr)
413 WIN32_FILE_ATTRIBUTE_DATA fdata;
414 if (GetFileAttributesEx(filename, GetFileExInfoStandard, &fdata))
416 if(time)
417 *time = filetime_to_time_t(&fdata.ftLastWriteTime);
419 if (size)
420 *size = ((__int64)fdata.nFileSizeHigh << 32) + fdata.nFileSizeLow;
422 if(isDir)
423 *isDir = !!( fdata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
425 return 0;
427 return -1;
430 int GetShortHASHLength() const;
432 static BOOL GetShortName(const CString &ref, CString &shortname, CString prefix)
434 //TRACE(_T("%s %s\r\n"),ref,prefix);
435 if (ref.Left(prefix.GetLength()) == prefix)
437 shortname = ref.Right(ref.GetLength() - prefix.GetLength());
438 if (shortname.Right(3) == _T("^{}"))
439 shortname=shortname.Left(shortname.GetLength() - 3);
440 return TRUE;
442 return FALSE;
445 static CString GetShortName(const CString& ref, REF_TYPE *type);
447 static bool LoadTextFile(const CString &filename, CString &msg);
449 int GetUnifiedDiff(const CTGitPath& path, const git_revnum_t& rev1, const git_revnum_t& rev2, CString patchfile, bool bMerge, bool bCombine, int diffContext);
450 int GetUnifiedDiff(const CTGitPath& path, const git_revnum_t& rev1, const git_revnum_t& rev2, CStringA * buffer, bool bMerge, bool bCombine, int diffContext);
452 int GitRevert(int parent, const CGitHash &hash);
454 CString CombinePath(const CString &path) const
456 if (path.IsEmpty())
457 return m_CurrentDir;
458 if (m_CurrentDir.IsEmpty())
459 return path;
460 return m_CurrentDir + (m_CurrentDir.Right(1) == _T("\\") ? _T("") : _T("\\")) + path;
463 CString CombinePath(const CTGitPath &path) const
465 return CombinePath(path.GetWinPath());
468 CString CombinePath(const CTGitPath *path) const
470 ATLASSERT(path);
471 return CombinePath(path->GetWinPath());
474 extern void GetTempPath(CString &path);
475 extern CString GetTempFile();
476 extern DWORD GetTortoiseGitTempPath(DWORD nBufferLength, LPTSTR lpBuffer);
478 extern CGit g_Git;