#include "registry.h"\r
#include "GitConfig.h"\r
\r
+\r
+static LPTSTR nextpath(LPCTSTR src, LPTSTR dst, UINT maxlen)\r
+{\r
+ LPCTSTR orgsrc;\r
+\r
+ while (*src == _T(';'))\r
+ src++;\r
+\r
+ orgsrc = src;\r
+\r
+ if (!--maxlen)\r
+ goto nullterm;\r
+\r
+ while (*src && *src != _T(';'))\r
+ {\r
+ if (*src != _T('"'))\r
+ {\r
+ *dst++ = *src++;\r
+ if (!--maxlen)\r
+ {\r
+ orgsrc = src;\r
+ goto nullterm;\r
+ }\r
+ }\r
+ else\r
+ {\r
+ src++;\r
+ while (*src && *src != _T('"'))\r
+ {\r
+ *dst++ = *src++;\r
+ if (!--maxlen)\r
+ {\r
+ orgsrc = src;\r
+ goto nullterm;\r
+ }\r
+ }\r
+\r
+ if (*src)\r
+ src++;\r
+ }\r
+ }\r
+\r
+ while (*src == _T(';'))\r
+ src++;\r
+\r
+nullterm:\r
+\r
+ *dst = 0;\r
+\r
+ return (orgsrc != src) ? (LPTSTR)src : NULL;\r
+}\r
+\r
+static inline BOOL FileExists(LPCTSTR lpszFileName)\r
+{\r
+ struct _stat st;\r
+ return _tstat(lpszFileName, &st) == 0;\r
+}\r
+\r
+static BOOL FindGitPath()\r
+{\r
+ size_t size;\r
+ _tgetenv_s(&size, NULL, 0, _T("PATH"));\r
+\r
+ if (!size)\r
+ {\r
+ return FALSE;\r
+ }\r
+\r
+ TCHAR *env = (TCHAR*)alloca(size * sizeof(TCHAR));\r
+ _tgetenv_s(&size, env, size, _T("PATH"));\r
+\r
+ TCHAR buf[_MAX_PATH];\r
+\r
+ // search in all paths defined in PATH\r
+ while ((env = nextpath(env, buf, _MAX_PATH-1)) && *buf)\r
+ {\r
+ TCHAR *pfin = buf + _tcslen(buf)-1;\r
+\r
+ // ensure trailing slash\r
+ if (*pfin != _T('/') && *pfin != _T('\\'))\r
+ _tcscpy(++pfin, _T("\\"));\r
+\r
+ const int len = _tcslen(buf);\r
+\r
+ if ((len + 7) < _MAX_PATH)\r
+ _tcscpy(pfin+1, _T("git.exe"));\r
+ else\r
+ break;\r
+\r
+ if ( FileExists(buf) )\r
+ {\r
+ // dir found\r
+ return TRUE;\r
+ }\r
+ }\r
+\r
+ return FALSE;\r
+}\r
+\r
+\r
#define MAX_DIRBUFFER 1000\r
CString CGit::ms_LastMsysGitDir;\r
CGit g_Git;\r
+BOOL g_IsWingitDllload = TRUE;\r
+\r
+LPBYTE wgGetRevisionID_safe(const char *pszProjectPath, const char *pszName)\r
+{\r
+ if(g_IsWingitDllload)\r
+ return wgGetRevisionID(pszProjectPath,pszName);\r
+ else\r
+ return NULL;\r
+}\r
+\r
+BOOL wgEnumFiles_safe(const char *pszProjectPath, const char *pszSubPath, unsigned int nFlags, WGENUMFILECB *pEnumCb, void *pUserData)\r
+{\r
+ if(g_IsWingitDllload)\r
+ return wgEnumFiles(pszProjectPath,pszSubPath,nFlags,pEnumCb,pUserData);\r
+ else\r
+ return FALSE;\r
+}\r
+\r
+BOOL CGit::IsVista()\r
+{\r
+\r
+ if( CRegStdWORD(_T("Software\\TortoiseGit\\CacheType") ) == 0)\r
+ {\r
+ g_IsWingitDllload=FALSE;\r
+ return TRUE;\r
+ }\r
+\r
+ OSVERSIONINFO osvi;\r
+ BOOL bIsWindowsXPorLater;\r
+\r
+ ZeroMemory(&osvi, sizeof(OSVERSIONINFO));\r
+ osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);\r
+\r
+ GetVersionEx(&osvi);\r
+ \r
+ if(osvi.dwMajorVersion >= 6)\r
+ return TRUE;\r
+ else\r
+ return FALSE;\r
+}\r
+\r
+static void InitWinGitDll()\r
+{\r
+ __try\r
+ {\r
+\r
+ if( CGit::IsVista () )\r
+ {\r
+ g_IsWingitDllload=FALSE;\r
+ return;\r
+ }\r
+ if ( !wgInit() )\r
+ {\r
+ // TODO\r
+ }\r
+ }\r
+ __except(1)\r
+ {\r
+ g_IsWingitDllload=FALSE;\r
+ return;\r
+ }\r
+\r
+}\r
CGit::CGit(void)\r
{\r
GetCurrentDirectory(MAX_DIRBUFFER,m_CurrentDir.GetBuffer(MAX_DIRBUFFER));\r
+ m_CurrentDir.ReleaseBuffer();\r
+ // make sure git/bin is in PATH before wingit.dll gets (delay) loaded by wgInit()\r
+ if ( !CheckMsysGitDir() )\r
+ {\r
+ // TODO\r
+ }\r
+ InitWinGitDll();\r
}\r
\r
CGit::~CGit(void)\r
\r
git_revnum_t CGit::GetHash(CString &friendname)\r
{\r
+ // NOTE: could replace this with wgGetRevisionID call\r
+\r
CString cmd;\r
CString out;\r
cmd.Format(_T("git.exe rev-parse %s" ),friendname);\r
CString one;\r
while( pos>=0 )\r
{\r
- i++;\r
one=output.Tokenize(_T("\n"),pos);\r
list.push_back(one.Right(one.GetLength()-2));\r
if(one[0] == _T('*'))\r
if(current)\r
*current=i;\r
+ i++;\r
}\r
}\r
return ret;\r
\r
BOOL CGit::CheckMsysGitDir()\r
{\r
- CRegString msysdir=CRegString(REG_MSYSGIT_PATH,_T(""),FALSE,HKEY_LOCAL_MACHINE);\r
- CString str=msysdir;\r
- if(str.IsEmpty())\r
- {\r
- CRegString msysinstalldir=CRegString(REG_MSYSGIT_INSTALL,_T(""),FALSE,HKEY_LOCAL_MACHINE);\r
- str=msysinstalldir;\r
- // check it has a trailing blank\r
- if (str.Right(1) != _T("\\"))\r
- {\r
- str += _T("\\");\r
- }\r
- str+=_T("bin");\r
- msysdir=str;\r
- msysdir.write();\r
+ static BOOL bInitialized = FALSE;\r
\r
+ if (bInitialized)\r
+ {\r
+ return TRUE;\r
}\r
- //CGit::m_MsysGitPath=str;\r
\r
TCHAR *oldpath,*home;\r
size_t size;\r
\r
- _tdupenv_s(&home,&size,_T("HOME")); \r
- \r
- if(home == NULL)\r
- { \r
+ // set HOME if not set already\r
+ _tgetenv_s(&size, NULL, 0, _T("HOME"));\r
+ if (!size)\r
+ {\r
_tdupenv_s(&home,&size,_T("USERPROFILE")); \r
_tputenv_s(_T("HOME"),home);\r
+ free(home);\r
}\r
- free(home);\r
- \r
- //set path\r
- _tdupenv_s(&oldpath,&size,_T("PATH")); \r
-\r
- CString path;\r
- CString unterminated_path = str; // path to msysgit without semicolon\r
- CString oldpath_s = oldpath;\r
- path.Format(_T("%s;"),str);\r
- // check msysgit not already in path\r
- if ( oldpath_s.Find( path ) < 0 && oldpath_s.Right( unterminated_path.GetLength() ) != unterminated_path )\r
- {\r
- // not already there, see if we have to take out one we added last time\r
- if ( ms_LastMsysGitDir != _T("") )\r
- {\r
- // we have added one so take it out\r
- int index = oldpath_s.Find( ms_LastMsysGitDir );\r
- if ( index >= 0 )\r
- {\r
- oldpath_s = oldpath_s.Left( index ) + \r
- oldpath_s.Right( oldpath_s.GetLength() - (index+ms_LastMsysGitDir.GetLength()) );\r
- }\r
- }\r
- // save the new msysdir path that we are about to add\r
- ms_LastMsysGitDir = path;\r
- // add the new one on the front of the existing path\r
- path+=oldpath_s;\r
- _tputenv_s(_T("PATH"),path);\r
- }\r
- free(oldpath);\r
\r
//setup ssh client\r
CRegString sshclient=CRegString(_T("Software\\TortoiseGit\\SSH"));\r
CString ssh=sshclient;\r
-// ssh.Format(_T("\"%s\""),(CString)sshclient);\r
\r
if(!ssh.IsEmpty())\r
{\r
_tputenv_s(_T("GIT_SSH"),ssh);\r
+ }else\r
+ {\r
+ _tputenv_s(_T("GIT_SSH"),_T(""));\r
}\r
\r
- CString cmd,out;\r
- cmd=_T("git.exe --version");\r
- if(g_Git.Run(cmd,&out,CP_UTF8))\r
+ // search PATH if git/bin directory is alredy present\r
+ if ( FindGitPath() )\r
+ {\r
+ bInitialized = TRUE;\r
+ return TRUE;\r
+ }\r
+\r
+ // add git/bin path to PATH\r
+\r
+ CRegString msysdir=CRegString(REG_MSYSGIT_PATH,_T(""),FALSE,HKEY_LOCAL_MACHINE);\r
+ CString str=msysdir;\r
+ if(str.IsEmpty())\r
+ {\r
+ CRegString msysinstalldir=CRegString(REG_MSYSGIT_INSTALL,_T(""),FALSE,HKEY_LOCAL_MACHINE);\r
+ str=msysinstalldir;\r
+ if ( !str.IsEmpty() )\r
+ {\r
+ str += (str[str.GetLength()-1] != '\\') ? "\\bin" : "bin";\r
+ msysdir=str;\r
+ msysdir.write();\r
+ }\r
+ else\r
+ {\r
+ return false;\r
+ }\r
+ }\r
+ //CGit::m_MsysGitPath=str;\r
+\r
+ //set path\r
+\r
+ _tdupenv_s(&oldpath,&size,_T("PATH")); \r
+\r
+ CString path;\r
+ path.Format(_T("%s;%s"),oldpath,str);\r
+\r
+ _tputenv_s(_T("PATH"),path);\r
+\r
+ free(oldpath);\r
+\r
+\r
+ if( !FindGitPath() )\r
{\r
return false;\r
}\r
else\r
+ {\r
+ bInitialized = TRUE;\r
return true;\r
-\r
-}
\ No newline at end of file
+ }\r
+}\r