From 25976ca598c736bfe631cb901031da7ec871d2f2 Mon Sep 17 00:00:00 2001 From: Sven Strickroth Date: Sat, 20 Jun 2015 18:05:19 +0200 Subject: [PATCH] Optimize PATH handling in CEnvironment Signed-off-by: Sven Strickroth --- src/Git/Git.cpp | 35 ++++++++++++++++++++--------------- src/Git/Git.h | 1 + test/UnitTests/GitTest.cpp | 31 +++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 15 deletions(-) diff --git a/src/Git/Git.cpp b/src/Git/Git.cpp index 1e339acae..55ee92a0d 100644 --- a/src/Git/Git.cpp +++ b/src/Git/Git.cpp @@ -2052,10 +2052,8 @@ BOOL CGit::CheckMsysGitDir(BOOL bFallback) m_Environment.CopyProcessEnvironment(); m_Environment.SetEnv(_T("GIT_DIR"), nullptr); // Remove %GIT_DIR% before executing git.exe - TCHAR *oldpath; - size_t homesize,size; - // set HOME if not set already + size_t homesize; _tgetenv_s(&homesize, NULL, 0, _T("HOME")); if (!homesize) m_Environment.SetEnv(_T("HOME"), GetHomeDirectory()); @@ -2116,18 +2114,8 @@ BOOL CGit::CheckMsysGitDir(BOOL bFallback) msysGitTemplateDir.ReleaseBuffer(); SetLibGit2TemplatePath(msysGitTemplateDir); - //set path - _tdupenv_s(&oldpath,&size,_T("PATH")); - - CString path; - path.Format(_T("%s;%s"), oldpath, CGit::ms_LastMsysGitDir + _T(";") + (CString)CRegString(REG_MSYSGIT_EXTRA_PATH, _T(""), FALSE)); - - m_Environment.SetEnv(_T("PATH"), path); - - CString str1 = m_Environment.GetEnv(_T("PATH")); - - CString sOldPath = oldpath; - free(oldpath); + m_Environment.AddToPath(CGit::ms_LastMsysGitDir); + m_Environment.AddToPath((CString)CRegString(REG_MSYSGIT_EXTRA_PATH, _T(""), FALSE)); #if !defined(TGITCACHE) && !defined(TORTOISESHELL) // register filter only once @@ -2595,6 +2583,23 @@ void CEnvironment::SetEnv(const TCHAR *name, const TCHAR* value) } +void CEnvironment::AddToPath(CString value) +{ + value.TrimRight(L"\\"); + if (value.IsEmpty()) + return; + + CString path = GetEnv(L"PATH").TrimRight(L";") + L";"; + + // do not double add paths to %PATH% + if (path.Find(value + L";") >= 0 || path.Find(value + L"\\;") >= 0) + return; + + path += value; + + SetEnv(L"PATH", path); +} + int CGit::GetGitEncode(TCHAR* configkey) { CString str=GetConfigValue(configkey); diff --git a/src/Git/Git.h b/src/Git/Git.h index 18b657c87..e3c76e240 100644 --- a/src/Git/Git.h +++ b/src/Git/Git.h @@ -77,6 +77,7 @@ public: void CopyProcessEnvironment(); CString GetEnv(const TCHAR *name); void SetEnv(const TCHAR* name, const TCHAR* value); + void AddToPath(CString value); void clear(); bool empty(); operator LPTSTR(); diff --git a/test/UnitTests/GitTest.cpp b/test/UnitTests/GitTest.cpp index df6a5e0da..ae652d63d 100644 --- a/test/UnitTests/GitTest.cpp +++ b/test/UnitTests/GitTest.cpp @@ -959,6 +959,37 @@ TEST(CGit, CEnvironment) EXPECT_STREQ(_T(""), env.GetEnv(L"key4")); env.CopyProcessEnvironment(); EXPECT_STREQ(windir, env.GetEnv(L"windir")); + + env.clear(); + CString path = L"c:\\windows;c:\\windows\\system32"; + env.SetEnv(L"PATH", path); + env.AddToPath(L""); + EXPECT_STREQ(path, env.GetEnv(L"PATH")); + env.AddToPath(L"c:\\windows"); + EXPECT_STREQ(path, env.GetEnv(L"PATH")); + env.AddToPath(L"c:\\windows\\"); + EXPECT_STREQ(path, env.GetEnv(L"PATH")); + env.AddToPath(L"c:\\windows\\system32"); + EXPECT_STREQ(path, env.GetEnv(L"PATH")); + env.AddToPath(L"c:\\windows\\system32\\"); + EXPECT_STREQ(path, env.GetEnv(L"PATH")); + path += L";c:\\windows\\system"; + env.AddToPath(L"c:\\windows\\system"); + EXPECT_STREQ(path, env.GetEnv(L"PATH")); + path += L";c:\\test"; + env.AddToPath(L"c:\\test\\"); + EXPECT_STREQ(path, env.GetEnv(L"PATH")); + env.AddToPath(L"c:\\test\\"); + EXPECT_STREQ(path, env.GetEnv(L"PATH")); + env.AddToPath(L"c:\\test"); + EXPECT_STREQ(path, env.GetEnv(L"PATH")); + path = L"c:\\windows;c:\\windows\\system32;"; + env.SetEnv(L"PATH", path); + env.AddToPath(L""); + EXPECT_STREQ(path, env.GetEnv(L"PATH")); + env.AddToPath(L"c:\\test"); + path += L"c:\\test"; + EXPECT_STREQ(path, env.GetEnv(L"PATH")); } static void GetOneFile(CGit& m_Git) -- 2.11.4.GIT