Disable saving of effective settings
[TortoiseGit.git] / src / TortoiseProc / Settings / GitSettings.h
blob038ba1229f4e4c676641eb8e7d55e120a90c454e
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 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 "../TortoiseProc.h"
22 #include "Git.h"
23 #include "Tooltip.h"
24 #include "../../Utils/UnicodeUtils.h"
26 class CSettings;
28 class CGitSettings
30 public:
31 CGitSettings()
32 : m_iConfigSource(0)
33 , m_bGlobal(false)
34 , m_bIsBareRepo(false)
35 , m_bHonorProjectConfig(false)
39 protected:
40 CComboBox m_cSaveTo;
41 int m_iConfigSource;
42 bool m_bGlobal;
43 bool m_bIsBareRepo;
44 bool m_bHonorProjectConfig;
46 void InitGitSettings(ISettingsPropPage *page, bool honorProjectConfig, CToolTips * tooltips)
48 m_bHonorProjectConfig = honorProjectConfig;
50 if (tooltips)
52 tooltips->AddTool(IDC_RADIO_SETTINGS_LOCAL, IDS_CONFIG_LOCAL_TT);
53 if (page->GetDlgItem(IDC_RADIO_SETTINGS_PROJECT) != nullptr)
54 tooltips->AddTool(IDC_RADIO_SETTINGS_PROJECT, IDS_CONFIG_PROJECT_TT);
55 tooltips->AddTool(IDC_RADIO_SETTINGS_GLOBAL, IDS_CONFIG_GLOBAL_TT);
58 CString str = g_Git.m_CurrentDir;
59 m_bIsBareRepo = g_GitAdminDir.IsBareRepo(str);
60 CString proj;
61 if (g_GitAdminDir.HasAdminDir(str, &proj) || m_bIsBareRepo)
63 CString title;
64 page->GetWindowText(title);
65 page->SetWindowText(title + _T(" - ") + proj);
66 page->GetDlgItem(IDC_RADIO_SETTINGS_LOCAL)->EnableWindow(TRUE);
68 m_cSaveTo.AddString(CString(MAKEINTRESOURCE(IDS_CONFIG_LOCAL)));
70 if (page->GetDlgItem(IDC_RADIO_SETTINGS_PROJECT) != nullptr)
72 page->GetDlgItem(IDC_RADIO_SETTINGS_PROJECT)->EnableWindow(honorProjectConfig);
73 if (honorProjectConfig && !m_bIsBareRepo)
74 m_cSaveTo.AddString(CString(MAKEINTRESOURCE(IDS_CONFIG_PROJECT)));
77 else
79 m_bGlobal = true;
80 page->GetDlgItem(IDC_RADIO_SETTINGS_LOCAL)->EnableWindow(FALSE);
81 if (page->GetDlgItem(IDC_RADIO_SETTINGS_PROJECT) != nullptr)
82 page->GetDlgItem(IDC_RADIO_SETTINGS_PROJECT)->EnableWindow(FALSE);
84 m_cSaveTo.AddString(CString(MAKEINTRESOURCE(IDS_CONFIG_GLOBAL)));
85 m_cSaveTo.SetCurSel(0);
87 page->CheckRadioButton(IDC_RADIO_SETTINGS_EFFECTIVE, IDC_RADIO_SETTINGS_SYSTEM, IDC_RADIO_SETTINGS_EFFECTIVE + m_iConfigSource);
89 LoadData();
92 bool Save(git_config * config, const CString &key, const CString &value, const bool askEmptyDelete = false, const CString def = _T(""))
94 CStringA keyA = CUnicodeUtils::GetUTF8(key);
95 int err = 0;
96 if (value.IsEmpty() || value == def)
98 const git_config_entry * entry = nullptr;
99 if (git_config_get_entry(&entry, config, keyA) == GIT_ENOTFOUND)
100 return true;
101 if (askEmptyDelete && CMessageBox::Show(nullptr, IDS_ASK_DELETE_EMPTY, IDS_APPNAME, 2, IDI_QUESTION, IDS_DELETEBUTTON, IDS_NO) == 2)
102 return true;
103 err = git_config_delete_entry(config, keyA);
105 else
107 CStringA valueA = CUnicodeUtils::GetMulti(value, CP_UTF8);
108 err = git_config_set_string(config, keyA, valueA);
110 if (err)
112 CString msg;
113 msg.Format(IDS_PROC_SAVECONFIGFAILED, key, value);
114 CMessageBox::Show(nullptr, g_Git.GetLibGit2LastErr(msg), _T("TortoiseGit"), MB_OK | MB_ICONERROR);
115 return false;
117 return true;
120 void LoadData()
122 git_config * config;
123 git_config_new(&config);
124 if (!m_bGlobal && (m_iConfigSource == 0 || m_iConfigSource == 1))
126 if (git_config_add_file_ondisk(config, CUnicodeUtils::GetUTF8(g_Git.GetGitLocalConfig()), GIT_CONFIG_LEVEL_APP, FALSE)) // this needs to have the highest priority in order to override .tgitconfig settings
127 MessageBox(nullptr, g_Git.GetLibGit2LastErr(), _T("TortoiseGit"), MB_ICONEXCLAMATION);
129 if ((m_iConfigSource == 0 && m_bHonorProjectConfig) || m_iConfigSource == 2)
131 if (!m_bIsBareRepo)
133 if (git_config_add_file_ondisk(config, CUnicodeUtils::GetUTF8(g_Git.m_CurrentDir) + "\\.tgitconfig", GIT_CONFIG_LEVEL_LOCAL, FALSE)) // this needs to have the second highest priority
134 MessageBox(nullptr, g_Git.GetLibGit2LastErr(), _T("TortoiseGit"), MB_ICONEXCLAMATION);
136 else
138 CString tmpFile = GetTempFile();
139 CTGitPath path(_T(".tgitconfig"));
140 if (g_Git.GetOneFile(_T("HEAD"), path, tmpFile) == 0)
142 if (git_config_add_file_ondisk(config, CUnicodeUtils::GetUTF8(tmpFile), GIT_CONFIG_LEVEL_LOCAL, FALSE)) // this needs to have the second highest priority
143 MessageBox(nullptr, g_Git.GetLibGit2LastErr(), _T("TortoiseGit"), MB_ICONEXCLAMATION);
147 if (m_iConfigSource == 0 || m_iConfigSource == 3)
149 if (git_config_add_file_ondisk(config, CUnicodeUtils::GetUTF8(g_Git.GetGitGlobalConfig()), GIT_CONFIG_LEVEL_GLOBAL, FALSE))
150 MessageBox(nullptr, g_Git.GetLibGit2LastErr(), _T("TortoiseGit"), MB_ICONEXCLAMATION);
151 else
153 if (git_config_add_file_ondisk(config, CUnicodeUtils::GetUTF8(g_Git.GetGitGlobalXDGConfig()), GIT_CONFIG_LEVEL_XDG, FALSE))
154 MessageBox(nullptr, g_Git.GetLibGit2LastErr(), _T("TortoiseGit"), MB_ICONEXCLAMATION);
157 if (m_iConfigSource == 0 || m_iConfigSource == 4)
159 if (git_config_add_file_ondisk(config, CUnicodeUtils::GetUTF8(g_Git.GetGitSystemConfig()), GIT_CONFIG_LEVEL_SYSTEM, FALSE))
160 MessageBox(nullptr, g_Git.GetLibGit2LastErr(), _T("TortoiseGit"), MB_ICONEXCLAMATION);
163 LoadDataImpl(config);
165 git_config_free(config);
167 EnDisableControls();
170 bool WarnUserSafeToDifferentDestination(int storeTo)
172 if ((storeTo == IDS_CONFIG_GLOBAL && m_iConfigSource != 3) || (storeTo == IDS_CONFIG_PROJECT && m_iConfigSource != 2) || (storeTo == IDS_CONFIG_LOCAL && m_iConfigSource != 1))
174 CString dest;
175 dest.LoadString(storeTo);
176 CString msg;
177 msg.Format(IDS_WARNUSERSAFEDIFFERENT, dest);
178 if (CMessageBox::Show(nullptr, msg, _T("TortoiseGit"), 2, IDI_QUESTION, CString(MAKEINTRESOURCE(IDS_SAVEBUTTON)), CString(MAKEINTRESOURCE(IDS_ABORTBUTTON))) == 2)
179 return false;
181 return true;
184 BOOL SafeData()
186 git_config * config;
187 git_config_new(&config);
189 int err = 0;
190 if (m_bGlobal || (m_cSaveTo.GetCurSel() == 1 && (!m_bHonorProjectConfig || m_bIsBareRepo)) || m_cSaveTo.GetCurSel() == 2)
192 if (!WarnUserSafeToDifferentDestination(IDS_CONFIG_GLOBAL))
193 return FALSE;
194 if (PathIsDirectory(g_Git.GetGitGlobalXDGConfigPath()))
195 err = git_config_add_file_ondisk(config, CUnicodeUtils::GetUTF8(g_Git.GetGitGlobalXDGConfig()), GIT_CONFIG_LEVEL_XDG, FALSE);
196 else
197 err = git_config_add_file_ondisk(config, CUnicodeUtils::GetUTF8(g_Git.GetGitGlobalConfig()), GIT_CONFIG_LEVEL_GLOBAL, FALSE);
199 else if (m_cSaveTo.GetCurSel() == 1 && !m_bIsBareRepo && m_bHonorProjectConfig)
201 if (!WarnUserSafeToDifferentDestination(IDS_CONFIG_PROJECT))
202 return FALSE;
203 err = git_config_add_file_ondisk(config, CUnicodeUtils::GetUTF8(g_Git.m_CurrentDir) + "\\.tgitconfig", GIT_CONFIG_LEVEL_APP, FALSE);
205 else
207 if (!WarnUserSafeToDifferentDestination(IDS_CONFIG_PROJECT))
208 return FALSE;
209 err = git_config_add_file_ondisk(config, CUnicodeUtils::GetMulti(g_Git.GetGitLocalConfig(), CP_UTF8), GIT_CONFIG_LEVEL_LOCAL, FALSE);
211 if (err)
213 MessageBox(nullptr, g_Git.GetLibGit2LastErr(), _T("TortoiseGit"), MB_ICONEXCLAMATION);
214 return FALSE;
217 int ret = SafeDataImpl(config);
219 git_config_free(config);
221 return ret;
224 virtual void LoadDataImpl(git_config * config) = 0;
225 virtual BOOL SafeDataImpl(git_config * config) = 0;
226 virtual void EnDisableControls() = 0;
228 static void AddTrueFalseToComboBox(CComboBox &combobox)
230 combobox.AddString(_T(""));
231 combobox.AddString(_T("true"));
232 combobox.AddString(_T("false"));
235 static void GetBoolConfigValueComboBox(git_config * config, const CString &key, CComboBox &combobox)
237 CStringA keyA = CUnicodeUtils::GetUTF8(key);
238 BOOL out = 0;
239 if (git_config_get_bool(&out, config, keyA) == GIT_ENOTFOUND)
240 combobox.SetCurSel(0);
241 else if (out)
242 combobox.SetCurSel(1);
243 else
244 combobox.SetCurSel(2);
247 static int GetConfigValue(git_config * config, const CString &key, CString &value)
249 CStringA keyA = CUnicodeUtils::GetUTF8(key);
250 const char *out = nullptr;
251 int retval = git_config_get_string(&out, config, keyA);
252 value = CUnicodeUtils::GetUnicode((CStringA)out);
253 return retval;
257 #define GITSETTINGS_DDX \
258 DDX_Control(pDX, IDC_COMBO_SETTINGS_SAFETO, m_cSaveTo);
260 #define GITSETTINGS_RADIO_EVENT \
261 ON_CBN_SELCHANGE(IDC_COMBO_SETTINGS_SAFETO, &OnChange) \
262 ON_BN_CLICKED(IDC_RADIO_SETTINGS_EFFECTIVE, &OnBnClickedChangedConfigSource) \
263 ON_BN_CLICKED(IDC_RADIO_SETTINGS_LOCAL, &OnBnClickedChangedConfigSource) \
264 ON_BN_CLICKED(IDC_RADIO_SETTINGS_PROJECT, &OnBnClickedChangedConfigSource) \
265 ON_BN_CLICKED(IDC_RADIO_SETTINGS_GLOBAL, &OnBnClickedChangedConfigSource) \
266 ON_BN_CLICKED(IDC_RADIO_SETTINGS_SYSTEM, &OnBnClickedChangedConfigSource)
268 #define GITSETTINGS_RADIO_EVENT_HANDLE \
269 afx_msg void OnBnClickedChangedConfigSource() \
271 m_iConfigSource = GetCheckedRadioButton(IDC_RADIO_SETTINGS_EFFECTIVE, IDC_RADIO_SETTINGS_SYSTEM) - IDC_RADIO_SETTINGS_EFFECTIVE; \
272 LoadData(); \