Fixed issue #468: Non-ASCII characters in user info aren't stored correctly
[TortoiseGit.git] / src / Git / Git.h
blob9e0d107521fc4b38ed4e4940273b31b737ae0803
1 #pragma once
2 #include "GitType.h"
3 #include "GitRev.h"
4 #include "GitStatus.h"
5 #include "GitAdminDir.h"
6 #include "gitdll.h"
8 class CGitCall
10 public:
11 CGitCall(){}
12 CGitCall(CString cmd):m_Cmd(cmd){}
14 CString GetCmd()const{return m_Cmd;}
15 void SetCmd(CString cmd){m_Cmd=cmd;}
17 //This function is called when command output data is available.
18 //When this function returns 'true' the git command should be aborted.
19 //This behavior is not implemented yet.
20 virtual bool OnOutputData(const BYTE* data, size_t size)=0;
21 virtual void OnEnd(){}
23 private:
24 CString m_Cmd;
28 class CTGitPath;
29 class CEnvironment:public std::vector<TCHAR>
31 public:
32 void CopyProcessEnvironment();
33 CString GetEnv(TCHAR *name);
34 void SetEnv(TCHAR* name, TCHAR* value);
36 class CGit
38 private:
39 GitAdminDir m_GitDir;
40 protected:
41 bool m_IsGitDllInited;
42 GIT_DIFF m_GitDiff;
43 public:
44 CComCriticalSection m_critGitDllSec;
45 bool m_IsUseGitDLL;
47 CEnvironment m_Environment;
49 static BOOL GitPathFileExists(CString &path)
51 if(path[0] == _T('\\') && path[1] == _T('\\'))
52 //it is netshare \\server\sharefoldername
53 // \\server\.git will create smb error log.
55 int length = path.GetLength();
57 if(length<2)
58 return false;
60 int start = path.Find(_T('\\'),2);
61 if(start<0)
62 return false;
64 start = path.Find(_T('\\'),start+1);
65 if(start<0)
66 return false;
68 return PathFileExists(path);
71 else
72 return PathFileExists(path);
74 void CheckAndInitDll()
76 if(!m_IsGitDllInited)
78 git_init();
79 m_IsGitDllInited=true;
83 GIT_DIFF GetGitDiff()
85 if(m_GitDiff)
86 return m_GitDiff;
87 else
89 git_open_diff(&m_GitDiff,"-C -M -r");
90 return m_GitDiff;
94 BOOL CheckMsysGitDir();
95 BOOL m_bInitialized;
97 static CString ms_LastMsysGitDir; // the last msysgitdir added to the path, blank if none
98 static int m_LogEncode;
99 unsigned int Hash2int(CString &hash);
100 // static CString m_MsysGitPath;
102 PROCESS_INFORMATION m_CurrentGitPi;
104 CGit(void);
105 ~CGit(void);
107 int Run(CString cmd, CString* output,int code);
108 int Run(CString cmd, BYTE_VECTOR *byte_array);
109 int Run(CGitCall* pcall);
111 int RunAsync(CString cmd,PROCESS_INFORMATION *pi, HANDLE* hRead, CString *StdioFile=NULL);
112 int RunLogFile(CString cmd, CString &filename);
114 int GetGitEncode(TCHAR* configkey);
116 bool IsFastForward(CString &from, CString &to);
117 CString GetConfigValue(CString name, int encoding=CP_UTF8, CString *GitPath=NULL);
119 int SetConfigValue(CString key, CString value, CONFIG_TYPE type=CONFIG_LOCAL, int encoding=CP_UTF8, CString *GitPath=NULL);
121 CString GetUserName(void);
122 CString GetUserEmail(void);
123 CString GetCurrentBranch(void);
124 CString GetSymbolicRef(const wchar_t* symbolicRefName = L"HEAD", bool bStripRefsHeads = true);
125 // read current branch name from HEAD file, returns 0 on success, -1 on failure, 1 detached (branch name "HEAD" returned)
126 int GetCurrentBranchFromFile(const CString &sProjectRoot, CString &sBranchOut);
127 BOOL CheckCleanWorkTree();
128 int Revert(CTGitPath &path,bool keep=true);
129 int Revert(CTGitPathList &list,bool keep=true);
131 bool SetCurrentDir(CString path)
133 bool b = m_GitDir.HasAdminDir(path,&m_CurrentDir);
134 if(m_CurrentDir.GetLength() == 2 && m_CurrentDir[1] == _T(':')) //C: D:
136 m_CurrentDir+=_T('\\');
138 return b;
140 CString m_CurrentDir;
142 typedef enum
144 BRANCH_LOCAL=0x1,
145 BRANCH_REMOTE=0x2,
146 BRANCH_ALL=BRANCH_LOCAL|BRANCH_REMOTE,
147 }BRANCH_TYPE;
149 typedef enum
151 LOG_INFO_STAT=0x1,
152 LOG_INFO_FILESTATE=0x2,
153 LOG_INFO_PATCH=0x4,
154 LOG_INFO_FULLHISTORY=0x8,
155 LOG_INFO_BOUNDARY=0x10,
156 LOG_INFO_ALL_BRANCH=0x20,
157 LOG_INFO_ONLY_HASH=0x40,
158 LOG_INFO_DETECT_RENAME=0x80,
159 LOG_INFO_DETECT_COPYRENAME=0x100,
160 LOG_INFO_FIRST_PARENT = 0x200,
161 LOG_INFO_NO_MERGE = 0x400,
162 LOG_INFO_FOLLOW = 0x800,
163 LOG_INFO_SHOW_MERGEDFILE=0x1000,
164 LOG_INFO_FULL_DIFF = 0x2000,
165 }LOG_INFO_MASK;
167 int GetRemoteList(STRING_VECTOR &list);
168 int GetBranchList(STRING_VECTOR &list, int *Current,BRANCH_TYPE type=BRANCH_LOCAL);
169 int GetTagList(STRING_VECTOR &list);
170 int GetMapHashToFriendName(MAP_HASH_NAME &map);
172 //hash is empty means all. -1 means all
174 int GetLog(CGitCall* pgitCall, CString &hash, 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,
175 CString *from=NULL,CString *to=NULL);
176 int GetLog(BYTE_VECTOR& logOut,CString &hash, 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,
177 CString *from=NULL,CString *to=NULL);
179 CString GetLogCmd(CString &hash, 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,
180 CString *from=NULL,CString *to=NULL, bool paramonly=false);
182 BOOL EnumFiles(const TCHAR *pszProjectPath, const TCHAR *pszSubPath, unsigned int nFlags, WGENUMFILECB *pEnumCb, void *pUserData);
184 git_revnum_t GetHash(const CString &friendname);
186 int BuildOutputFormat(CString &format,bool IsFull=TRUE);
187 //int GetShortLog(CString &log,CTGitPath * path=NULL, int count =-1);
188 static void StringAppend(CString *str,BYTE *p,int code=CP_UTF8,int length=-1);
190 BOOL IsInitRepos();
191 int ListConflictFile(CTGitPathList &list,CTGitPath *path=NULL);
192 int GetRefList(STRING_VECTOR &list);
194 int RefreshGitIndex();
195 int GetOneFile(CString Refname, CTGitPath &path, CString &outputfile);
197 //Example: master -> refs/heads/master
198 CString GetFullRefName(CString shortRefName);
199 //Removes 'refs/heads/' or just 'refs'. Example: refs/heads/master -> master
200 static CString StripRefName(CString refName);
202 int GetCommitDiffList(CString &rev1,CString &rev2,CTGitPathList &outpathlist);
203 int GetInitAddList(CTGitPathList &outpathlist);
205 __int64 filetime_to_time_t(const FILETIME *ft)
207 long long winTime = ((long long)ft->dwHighDateTime << 32) + ft->dwLowDateTime;
208 winTime -= 116444736000000000LL; /* Windows to Unix Epoch conversion */
209 winTime /= 10000000; /* Nano to seconds resolution */
210 return (time_t)winTime;
213 int GetFileModifyTime(LPCTSTR filename, __int64 *time, bool * isDir=NULL)
215 WIN32_FILE_ATTRIBUTE_DATA fdata;
216 if (GetFileAttributesEx(filename, GetFileExInfoStandard, &fdata))
218 if(time)
219 *time = filetime_to_time_t(&fdata.ftLastWriteTime);
221 if(isDir)
222 *isDir = !!( fdata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
224 return 0;
226 return -1;
229 extern void GetTempPath(CString &path);
230 extern CString GetTempFile();
233 extern CGit g_Git;
235 #if 0
236 inline static BOOL wgEnumFiles(const TCHAR *pszProjectPath, const TCHAR *pszSubPath, unsigned int nFlags, WGENUMFILECB *pEnumCb, void *pUserData)
238 return g_Git.EnumFiles(pszProjectPath, pszSubPath, nFlags, pEnumCb, pUserData);
240 #endif