1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2018 - 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.
21 #include "StringUtils.h"
23 #include "MessageBox.h"
26 #define GIT_FOR_WINDOWS_URL L"https://git-for-windows.github.io/"
28 class CConfigureGitExe
32 : m_regCygwinHack(L
"Software\\TortoiseGit\\CygwinHack", FALSE
)
33 , m_regMsys2Hack(L
"Software\\TortoiseGit\\Msys2Hack", FALSE
)
36 static bool CheckGitVersion(HWND hwnd
)
38 if (CAppUtils::IsGitVersionNewerOrEqual(hwnd
, 2, 11))
42 tmp
.Format(IDS_PROC_OLDMSYSGIT
, L
"2.11+");
43 int ret
= CMessageBox::ShowCheck(hwnd
, tmp
, L
"TortoiseGit", 1, IDI_EXCLAMATION
, CString(MAKEINTRESOURCE(IDS_PROC_GOTOMSYSGITWEBSITE
)), CString(MAKEINTRESOURCE(IDS_ABORTBUTTON
)), CString(MAKEINTRESOURCE(IDS_IGNOREBUTTON
)), L
"OldMsysgitVersionWarning", CString(MAKEINTRESOURCE(IDS_PROC_NOTSHOWAGAINIGNORE
)));
47 CMessageBox::RemoveRegistryKey(L
"OldMsysgitVersionWarning"); // only store answer if it is "Ignore"
49 ShellExecute(hwnd
, L
"open", GIT_FOR_WINDOWS_URL
, nullptr, nullptr, SW_SHOW
);
55 CRegDWORD m_regCygwinHack
;
56 CRegDWORD m_regMsys2Hack
;
57 CRegString m_regMsysGitPath
;
58 CRegString m_regMsysGitExtranPath
;
60 static void PerformCommonGitPathCleanup(CString
& path
)
64 if (path
.Find(L
'%') >= 0)
66 int neededSize
= ExpandEnvironmentStrings(path
, nullptr, 0);
67 CString
origPath(path
);
68 ExpandEnvironmentStrings(origPath
, CStrBuf(path
, neededSize
, CStrBuf::SET_LENGTH
), neededSize
);
71 path
.Replace(L
'/', L
'\\');
72 path
.Replace(L
"\\\\", L
"\\");
74 if (CStringUtils::EndsWith(path
, L
"git.exe"))
75 path
.Truncate(path
.GetLength() - 7);
77 path
.TrimRight(L
'\\');
79 // prefer git.exe in cmd-directory for Git for Windows based on msys2
80 if (path
.GetLength() > 12 && (CStringUtils::EndsWith(path
, L
"\\mingw32\\bin") || CStringUtils::EndsWith(path
, L
"\\mingw64\\bin")) && PathFileExists(path
.Left(path
.GetLength() - 12) + L
"\\cmd\\git.exe"))
81 path
= path
.Left(path
.GetLength() - 12) + L
"\\cmd";
83 // prefer git.exe in bin-directory, see https://github.com/msysgit/msysgit/issues/103
84 if (path
.GetLength() > 5 && CStringUtils::EndsWith(path
, L
"\\cmd") && PathFileExists(path
.Left(path
.GetLength() - 4) + L
"\\bin\\git.exe"))
85 path
= path
.Left(path
.GetLength() - 4) + L
"\\bin";
88 static bool GuessExtraPath(CString gitpath
, CString
& pathaddition
)
90 if (gitpath
.IsEmpty())
93 PerformCommonGitPathCleanup(gitpath
);
95 if (!CStringUtils::EndsWith(gitpath
, L
"bin"))
98 gitpath
.Truncate(gitpath
.GetLength() - 3);
99 gitpath
+= L
"mingw\\bin";
100 if (!::PathFileExists(gitpath
))
103 if (pathaddition
.Find(gitpath
+ L
';') >= 0 || CStringUtils::EndsWith(pathaddition
, gitpath
))
106 pathaddition
= gitpath
+ pathaddition
;
110 static bool SelectFolder(HWND hwnd
, CString
& gitpath
, CString
& pathaddition
)
116 if (!CCommonAppUtils::FileOpenSave(dir
, nullptr, 0, IDS_GITEXEFILEFILTER
, true, hwnd
))
120 PerformCommonGitPathCleanup(gitpath
);
121 if (!PathFileExists(gitpath
+ L
"\\git.exe"))
122 CMessageBox::Show(hwnd
, IDS_PROC_NOMSYSGIT
, IDS_APPNAME
, MB_ICONEXCLAMATION
);
123 GuessExtraPath(gitpath
, pathaddition
);
127 bool CheckGitExe(HWND hwnd
, CString
& gitpath
, CString
& pathaddition
, int versionLabelId
, std::function
<void(UINT
)> callHelp
, bool* needWorkarounds
= nullptr)
129 SetWindowText(GetDlgItem(hwnd
, versionLabelId
), L
"");
131 PerformCommonGitPathCleanup(gitpath
);
133 GuessExtraPath(gitpath
, pathaddition
);
135 if (gitpath
.IsEmpty() || !PathFileExists(gitpath
+ L
"\\git.exe"))
137 CMessageBox::Show(hwnd
, IDS_PROC_NOMSYSGIT
, IDS_APPNAME
, MB_ICONERROR
);
141 CString oldpath
= m_regMsysGitPath
;
142 CString oldextranpath
= m_regMsysGitExtranPath
;
144 StoreSetting(hwnd
, gitpath
, m_regMsysGitPath
);
145 StoreSetting(hwnd
, pathaddition
, m_regMsysGitExtranPath
);
147 StoreSetting(hwnd
, oldpath
, m_regMsysGitPath
);
148 StoreSetting(hwnd
, oldextranpath
, m_regMsysGitExtranPath
);
151 CString checkhelpHint
;
152 checkhelpHint
.LoadString(IDS_SEEMANUALGITEXEPATH
);
154 g_Git
.m_bInitialized
= false;
156 if (g_Git
.CheckMsysGitDir(FALSE
))
159 int ret
= g_Git
.Run(L
"git.exe --version", &out
, CP_UTF8
);
160 SetWindowText(GetDlgItem(hwnd
, versionLabelId
), out
);
163 if (ret
== 0xC0000135)
165 if (CMessageBox::Show(hwnd
, IDS_ERR_GITDLLMISSING
, IDS_APPNAME
, 1, IDI_ERROR
, IDS_MSGBOX_OK
, IDS_MSGBOX_HELP
) == 2)
166 callHelp(IDD_SETTINGSMAIN
);
170 tmp
.Format(IDS_ERR_GITCALLFAILED
, ret
);
171 tmp
.AppendChar(L
'\n');
172 tmp
.AppendChar(L
'\n');
173 tmp
.Append(checkhelpHint
);
174 if (CMessageBox::Show(hwnd
, tmp
, L
"TortoiseGit", 1, IDI_ERROR
, CString(MAKEINTRESOURCE(IDS_MSGBOX_OK
)), CString(MAKEINTRESOURCE(IDS_MSGBOX_HELP
))) == 2)
175 callHelp(IDD_SETTINGSMAIN
);
178 else if (!CStringUtils::StartsWith(out
.Trim(), L
"git version "))
181 tmp
.Format(IDS_ERR_GITNOVALIDOUTPUT
, (LPCTSTR
)out
.Trim());
182 tmp
.AppendChar(L
'\n');
183 tmp
.AppendChar(L
'\n');
184 tmp
.Append(checkhelpHint
);
185 if (CMessageBox::Show(hwnd
, tmp
, L
"TortoiseGit", 1, IDI_ERROR
, CString(MAKEINTRESOURCE(IDS_MSGBOX_OK
)), CString(MAKEINTRESOURCE(IDS_MSGBOX_HELP
))) == 2)
186 callHelp(IDD_SETTINGSMAIN
);
189 else if (!(CGit::ms_bCygwinGit
|| CGit::ms_bMsys2Git
) && out
.Find(L
"msysgit") == -1 && out
.Find(L
"windows") == -1)
191 bool wasAlreadyWarned
= !needWorkarounds
|| *needWorkarounds
;
192 if (CMessageBox::Show(hwnd
, IDS_ERR_GITNEEDHACKS
, IDS_APPNAME
, 1, IDI_INFORMATION
, IDS_MSGBOX_OK
, IDS_MSGBOX_HELP
) == 2)
193 callHelp(IDD_SETTINGSMAIN
);
195 *needWorkarounds
= true;
196 if (!wasAlreadyWarned
)
199 else if ((CGit::ms_bCygwinGit
|| CGit::ms_bMsys2Git
) && out
.Find(L
"msysgit") > 0 && out
.Find(L
"windows") > 0)
201 if (CMessageBox::Show(hwnd
, IDS_ERR_GITUNNEEDEDHACKS
, IDS_APPNAME
, 1, IDI_INFORMATION
, IDS_MSGBOX_OK
, IDS_MSGBOX_HELP
) == 2)
202 callHelp(IDD_SETTINGSMAIN
);
204 *needWorkarounds
= true;
211 tmp
.LoadString(IDS_PROC_NOMSYSGIT
);
212 tmp
.AppendChar(L
'\n');
213 tmp
.AppendChar(L
'\n');
214 tmp
.Append(checkhelpHint
);
215 if (CMessageBox::Show(hwnd
, tmp
, L
"TortoiseGit", 1, IDI_ERROR
, CString(MAKEINTRESOURCE(IDS_MSGBOX_OK
)), CString(MAKEINTRESOURCE(IDS_MSGBOX_HELP
))) == 2)
216 callHelp(IDD_SETTINGSMAIN
);
220 CGit::ms_LastMsysGitVersion
= 0;
221 if (!CheckGitVersion(hwnd
))
226 template<class T
, class Reg
>
227 void StoreSetting(HWND hwnd
, const T
& value
, Reg
& registryKey
)
230 if (registryKey
.GetLastError() != ERROR_SUCCESS
)
231 CMessageBox::Show(hwnd
, registryKey
.getErrorString(), L
"TortoiseGit", MB_ICONERROR
);