7da64cf0bbfdba83ccb3c9a8111d6353934cd962
[TortoiseGit.git] / src / Git / Git.h
blob7da64cf0bbfdba83ccb3c9a8111d6353934cd962
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2013 - 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"
27 class CFilterData
29 public:
30 CFilterData()
32 m_From=m_To=-1;
33 m_IsRegex=1;
35 __time64_t m_From;
36 __time64_t m_To;
37 CString m_Author;
38 CString m_Committer;
39 CString m_MessageFilter;
40 BOOL m_IsRegex;
43 class CGitCall
45 public:
46 CGitCall(){}
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(){}
59 private:
60 CString m_Cmd;
64 class CTGitPath;
65 class CEnvironment:public std::vector<TCHAR>
67 public:
68 void CopyProcessEnvironment();
69 CString GetEnv(TCHAR *name);
70 void SetEnv(TCHAR* name, TCHAR* value);
72 class CGit
74 private:
75 GitAdminDir m_GitDir;
76 CString gitLastErr;
77 protected:
78 bool m_IsGitDllInited;
79 GIT_DIFF m_GitDiff;
80 GIT_DIFF m_GitSimpleListDiff;
81 public:
82 CComCriticalSection m_critGitDllSec;
83 bool m_IsUseGitDLL;
84 bool m_IsUseLibGit2;
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();
97 if(length<2)
98 return false;
100 int start = path.Find(_T('\\'),2);
101 if(start<0)
102 return false;
104 start = path.Find(_T('\\'),start+1);
105 if(start<0)
106 return false;
108 return PathFileExists(path);
111 else
112 return PathFileExists(path);
114 void CheckAndInitDll()
116 if(!m_IsGitDllInited)
118 git_init();
119 m_IsGitDllInited=true;
123 GIT_DIFF GetGitDiff()
125 if(m_GitDiff)
126 return m_GitDiff;
127 else
129 git_open_diff(&m_GitDiff,"-C -M -r");
130 return m_GitDiff;
134 GIT_DIFF GetGitSimpleListDiff()
136 if(m_GitSimpleListDiff)
137 return m_GitSimpleListDiff;
138 else
140 git_open_diff(&m_GitSimpleListDiff,"-r -r");
141 return m_GitSimpleListDiff;
145 BOOL CheckMsysGitDir();
146 BOOL m_bInitialized;
148 CString GetHomeDirectory();
149 CString GetGitLocalConfig();
150 CString GetGitGlobalConfig();
151 CString GetGitGlobalXDGConfigPath();
152 CString GetGitGlobalXDGConfig();
153 CString GetGitSystemConfig();
154 static CString ms_LastMsysGitDir; // the last msysgitdir added to the path, blank if none
155 static int m_LogEncode;
156 static bool IsBranchNameValid(CString branchname);
157 bool IsBranchTagNameUnique(const CString& name);
158 bool BranchTagExists(const CString& name, bool isBranch = true);
159 unsigned int Hash2int(CGitHash &hash);
160 // static CString m_MsysGitPath;
162 PROCESS_INFORMATION m_CurrentGitPi;
164 CGit(void);
165 ~CGit(void);
167 int Run(CString cmd, CString* output, int code);
168 int Run(CString cmd, CString* output, CString* outputErr, int code);
169 int Run(CString cmd, BYTE_VECTOR *byte_array, BYTE_VECTOR *byte_arrayErr = NULL);
170 int Run(CGitCall* pcall);
172 private:
173 static DWORD WINAPI AsyncReadStdErrThread(LPVOID lpParam);
174 typedef struct AsyncReadStdErrThreadArguments
176 HANDLE fileHandle;
177 CGitCall* pcall;
178 } ASYNCREADSTDERRTHREADARGS, *PASYNCREADSTDERRTHREADARGS;
180 public:
181 int RunAsync(CString cmd, PROCESS_INFORMATION *pi, HANDLE* hRead, HANDLE *hErrReadOut, CString *StdioFile = NULL);
182 int RunLogFile(CString cmd, const CString &filename);
184 int GetDiffPath(CTGitPathList *PathList, CGitHash *hash1, CGitHash *hash2, char *arg=NULL);
186 int GetGitEncode(TCHAR* configkey);
188 bool IsFastForward(const CString &from, const CString &to, CGitHash * commonAncestor = NULL);
189 CString GetConfigValue(CString name, int encoding=CP_UTF8, CString *GitPath=NULL,BOOL RemoveCR=TRUE);
191 int SetConfigValue(CString key, CString value, CONFIG_TYPE type=CONFIG_LOCAL, int encoding=CP_UTF8, CString *GitPath=NULL);
192 int UnsetConfigValue(CString key, CONFIG_TYPE type=CONFIG_LOCAL, int encoding=CP_UTF8, CString *GitPath=NULL);
194 CString GetUserName(void);
195 CString GetUserEmail(void);
196 CString GetCurrentBranch(void);
197 CString GetSymbolicRef(const wchar_t* symbolicRefName = L"HEAD", bool bStripRefsHeads = true);
198 // read current branch name from HEAD file, returns 0 on success, -1 on failure, 1 detached (branch name "HEAD" returned)
199 int GetCurrentBranchFromFile(const CString &sProjectRoot, CString &sBranchOut);
200 BOOL CheckCleanWorkTree();
201 int Revert(CString commit, CTGitPathList &list, bool keep=true);
202 int Revert(CString commit, CTGitPath &path);
203 CString GetGitLastErr(CString msg);
204 static CString GetLibGit2LastErr();
205 static CString GetLibGit2LastErr(CString msg);
206 bool SetCurrentDir(CString path, bool submodule = false)
208 bool b = m_GitDir.HasAdminDir(path, submodule ? false : !!PathIsDirectory(path), &m_CurrentDir);
209 if (!b && g_GitAdminDir.IsBareRepo(path))
211 m_CurrentDir = path;
212 b = true;
214 if(m_CurrentDir.GetLength() == 2 && m_CurrentDir[1] == _T(':')) //C: D:
216 m_CurrentDir += _T('\\');
218 return b;
220 CString m_CurrentDir;
222 enum
224 LOG_ORDER_CHRONOLOGIALREVERSED,
225 LOG_ORDER_TOPOORDER,
226 LOG_ORDER_DATEORDER,
229 typedef enum
231 BRANCH_LOCAL = 0x1,
232 BRANCH_REMOTE = 0x2,
233 BRANCH_FETCH_HEAD = 0x4,
234 BRANCH_LOCAL_F = BRANCH_LOCAL | BRANCH_FETCH_HEAD,
235 BRANCH_ALL = BRANCH_LOCAL | BRANCH_REMOTE,
236 BRANCH_ALL_F = BRANCH_ALL | BRANCH_FETCH_HEAD,
237 }BRANCH_TYPE;
239 typedef enum
241 LOG_INFO_STAT=0x1,
242 LOG_INFO_FILESTATE=0x2,
243 LOG_INFO_PATCH=0x4,
244 LOG_INFO_FULLHISTORY=0x8,
245 LOG_INFO_BOUNDARY=0x10,
246 LOG_INFO_ALL_BRANCH=0x20,
247 LOG_INFO_ONLY_HASH=0x40,
248 LOG_INFO_DETECT_RENAME=0x80,
249 LOG_INFO_DETECT_COPYRENAME=0x100,
250 LOG_INFO_FIRST_PARENT = 0x200,
251 LOG_INFO_NO_MERGE = 0x400,
252 LOG_INFO_FOLLOW = 0x800,
253 LOG_INFO_SHOW_MERGEDFILE=0x1000,
254 LOG_INFO_FULL_DIFF = 0x2000,
255 LOG_INFO_SIMPILFY_BY_DECORATION = 0x4000,
256 }LOG_INFO_MASK;
258 typedef enum
260 LOCAL_BRANCH,
261 REMOTE_BRANCH,
262 TAG,
263 STASH,
264 BISECT_GOOD,
265 BISECT_BAD,
266 NOTES,
267 UNKNOWN,
269 }REF_TYPE;
271 int GetRemoteList(STRING_VECTOR &list);
272 int GetBranchList(STRING_VECTOR &list, int *Current,BRANCH_TYPE type=BRANCH_LOCAL);
273 int GetTagList(STRING_VECTOR &list);
274 int GetRemoteTags(CString remote, STRING_VECTOR &list);
275 int GetMapHashToFriendName(MAP_HASH_NAME &map);
277 CString DerefFetchHead();
279 // FixBranchName():
280 // When branchName == FETCH_HEAD, derefrence it.
281 // A selected branch name got from GetBranchList(), with flag BRANCH_FETCH_HEAD enabled,
282 // should go through this function before it is used.
283 CString FixBranchName_Mod(CString& branchName);
284 CString FixBranchName(const CString& branchName);
286 CString GetLogCmd(CString &range, 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);
288 int GetHash(CGitHash &hash, TCHAR* friendname);
289 int GetHash(CGitHash &hash, CString ref) { return GetHash(hash, ref.GetBuffer()); }
291 int BuildOutputFormat(CString &format,bool IsFull=TRUE);
292 //int GetShortLog(const CString &log,CTGitPath * path=NULL, int count =-1);
293 static void StringAppend(CString *str,BYTE *p,int code=CP_UTF8,int length=-1);
295 BOOL CanParseRev(CString ref);
296 BOOL IsInitRepos();
297 int ListConflictFile(CTGitPathList &list,CTGitPath *path=NULL);
298 int GetRefList(STRING_VECTOR &list);
300 int RefreshGitIndex();
301 int GetOneFile(CString Refname, CTGitPath &path, const CString &outputfile);
303 //Example: master -> refs/heads/master
304 CString GetFullRefName(CString shortRefName);
305 //Removes 'refs/heads/' or just 'refs'. Example: refs/heads/master -> master
306 static CString StripRefName(CString refName);
308 int GetCommitDiffList(const CString &rev1,const CString &rev2,CTGitPathList &outpathlist);
309 int GetInitAddList(CTGitPathList &outpathlist);
311 __int64 filetime_to_time_t(const FILETIME *ft)
313 long long winTime = ((long long)ft->dwHighDateTime << 32) + ft->dwLowDateTime;
314 winTime -= 116444736000000000LL; /* Windows to Unix Epoch conversion */
315 winTime /= 10000000; /* Nano to seconds resolution */
316 return (time_t)winTime;
319 int GetFileModifyTime(LPCTSTR filename, __int64 *time, bool * isDir=NULL)
321 WIN32_FILE_ATTRIBUTE_DATA fdata;
322 if (GetFileAttributesEx(filename, GetFileExInfoStandard, &fdata))
324 if(time)
325 *time = filetime_to_time_t(&fdata.ftLastWriteTime);
327 if(isDir)
328 *isDir = !!( fdata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
330 return 0;
332 return -1;
335 int GetShortHASHLength();
337 static BOOL GetShortName(CString ref, CString &shortname, CString prefix)
339 //TRACE(_T("%s %s\r\n"),ref,prefix);
340 if (ref.Left(prefix.GetLength()) == prefix)
342 shortname = ref.Right(ref.GetLength() - prefix.GetLength());
343 if (shortname.Right(3) == _T("^{}"))
344 shortname=shortname.Left(shortname.GetLength() - 3);
345 return TRUE;
347 return FALSE;
350 static CString GetShortName(CString ref, REF_TYPE *type);
352 enum
354 GIT_CMD_CLONE,
355 GIT_CMD_FETCH,
356 GIT_CMD_COMMIT_UPDATE_INDEX,
358 bool UsingLibGit2(int cmd);
360 extern void GetTempPath(CString &path);
361 extern CString GetTempFile();
362 extern DWORD GetTortoiseGitTempPath(DWORD nBufferLength, LPTSTR lpBuffer);
364 extern CGit g_Git;