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.
21 #include "../TortoiseProc.h"
23 #include "../../Utils/UnicodeUtils.h"
33 , m_bIsBareRepo(false)
34 , m_bHonorProjectConfig(false)
43 bool m_bHonorProjectConfig
;
45 void InitGitSettings(ISettingsPropPage
*page
, bool honorProjectConfig
)
47 m_bHonorProjectConfig
= honorProjectConfig
;
49 CString str
= g_Git
.m_CurrentDir
;
50 m_bIsBareRepo
= g_GitAdminDir
.IsBareRepo(str
);
52 if (g_GitAdminDir
.HasAdminDir(str
, &proj
) || m_bIsBareRepo
)
55 page
->GetWindowText(title
);
56 page
->SetWindowText(title
+ _T(" - ") + proj
);
57 page
->GetDlgItem(IDC_RADIO_SETTINGS_LOCAL
)->EnableWindow(TRUE
);
59 m_cSaveTo
.AddString(CString(MAKEINTRESOURCE(IDS_CONFIG_LOCAL
)));
61 if (page
->GetDlgItem(IDC_RADIO_SETTINGS_PROJECT
) != nullptr)
63 page
->GetDlgItem(IDC_RADIO_SETTINGS_PROJECT
)->EnableWindow(honorProjectConfig
);
64 if (honorProjectConfig
&& !m_bIsBareRepo
)
65 m_cSaveTo
.AddString(CString(MAKEINTRESOURCE(IDS_CONFIG_PROJECT
)));
71 page
->GetDlgItem(IDC_RADIO_SETTINGS_LOCAL
)->EnableWindow(FALSE
);
72 if (page
->GetDlgItem(IDC_RADIO_SETTINGS_PROJECT
) != nullptr)
73 page
->GetDlgItem(IDC_RADIO_SETTINGS_PROJECT
)->EnableWindow(FALSE
);
75 m_cSaveTo
.AddString(CString(MAKEINTRESOURCE(IDS_CONFIG_GLOBAL
)));
76 m_cSaveTo
.SetCurSel(0);
78 page
->CheckRadioButton(IDC_RADIO_SETTINGS_EFFECTIVE
, IDC_RADIO_SETTINGS_SYSTEM
, IDC_RADIO_SETTINGS_EFFECTIVE
+ m_iConfigSource
);
83 bool Save(git_config
* config
, const CString
&key
, const CString
&value
)
85 CStringA keyA
= CUnicodeUtils::GetUTF8(key
);
89 const git_config_entry
* entry
= nullptr;
90 if (git_config_get_entry(&entry
, config
, keyA
) == GIT_ENOTFOUND
)
92 err
= git_config_delete_entry(config
, keyA
);
96 CStringA valueA
= CUnicodeUtils::GetMulti(value
, CP_UTF8
);
97 err
= git_config_set_string(config
, keyA
, valueA
);
102 msg
.Format(IDS_PROC_SAVECONFIGFAILED
, key
, value
);
103 CMessageBox::Show(nullptr, g_Git
.GetLibGit2LastErr(msg
), _T("TortoiseGit"), MB_OK
| MB_ICONERROR
);
112 git_config_new(&config
);
113 if (!m_bGlobal
&& (m_iConfigSource
== 0 || m_iConfigSource
== 1))
115 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
116 MessageBox(nullptr, g_Git
.GetLibGit2LastErr(), _T("TortoiseGit"), MB_ICONEXCLAMATION
);
118 if ((m_iConfigSource
== 0 && m_bHonorProjectConfig
) || m_iConfigSource
== 2)
122 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
123 MessageBox(nullptr, g_Git
.GetLibGit2LastErr(), _T("TortoiseGit"), MB_ICONEXCLAMATION
);
127 CString tmpFile
= GetTempFile();
128 CTGitPath
path(_T(".tgitconfig"));
129 if (g_Git
.GetOneFile(_T("HEAD"), path
, tmpFile
) == 0)
131 if (git_config_add_file_ondisk(config
, CUnicodeUtils::GetUTF8(tmpFile
), GIT_CONFIG_LEVEL_LOCAL
, FALSE
)) // this needs to have the second highest priority
132 MessageBox(nullptr, g_Git
.GetLibGit2LastErr(), _T("TortoiseGit"), MB_ICONEXCLAMATION
);
136 if (m_iConfigSource
== 0 || m_iConfigSource
== 3)
138 if (git_config_add_file_ondisk(config
, CUnicodeUtils::GetUTF8(g_Git
.GetGitGlobalConfig()), GIT_CONFIG_LEVEL_GLOBAL
, FALSE
))
139 MessageBox(nullptr, g_Git
.GetLibGit2LastErr(), _T("TortoiseGit"), MB_ICONEXCLAMATION
);
142 if (git_config_add_file_ondisk(config
, CUnicodeUtils::GetUTF8(g_Git
.GetGitGlobalXDGConfig()), GIT_CONFIG_LEVEL_XDG
, FALSE
))
143 MessageBox(nullptr, g_Git
.GetLibGit2LastErr(), _T("TortoiseGit"), MB_ICONEXCLAMATION
);
146 if (m_iConfigSource
== 0 || m_iConfigSource
== 4)
148 if (git_config_add_file_ondisk(config
, CUnicodeUtils::GetUTF8(g_Git
.GetGitSystemConfig()), GIT_CONFIG_LEVEL_SYSTEM
, FALSE
))
149 MessageBox(nullptr, g_Git
.GetLibGit2LastErr(), _T("TortoiseGit"), MB_ICONEXCLAMATION
);
152 LoadDataImpl(config
);
154 git_config_free(config
);
160 git_config_new(&config
);
163 if (m_bGlobal
|| (m_cSaveTo
.GetCurSel() == 1 && (!m_bHonorProjectConfig
|| m_bIsBareRepo
)) || m_cSaveTo
.GetCurSel() == 2)
165 if (PathIsDirectory(g_Git
.GetGitGlobalXDGConfigPath()))
166 err
= git_config_add_file_ondisk(config
, CUnicodeUtils::GetUTF8(g_Git
.GetGitGlobalXDGConfig()), GIT_CONFIG_LEVEL_XDG
, FALSE
);
168 err
= git_config_add_file_ondisk(config
, CUnicodeUtils::GetUTF8(g_Git
.GetGitGlobalConfig()), GIT_CONFIG_LEVEL_GLOBAL
, FALSE
);
170 else if (m_cSaveTo
.GetCurSel() == 1 && !m_bIsBareRepo
&& m_bHonorProjectConfig
)
172 err
= git_config_add_file_ondisk(config
, CUnicodeUtils::GetUTF8(g_Git
.m_CurrentDir
) + "\\.tgitconfig", GIT_CONFIG_LEVEL_APP
, FALSE
);
176 err
= git_config_add_file_ondisk(config
, CUnicodeUtils::GetMulti(g_Git
.GetGitLocalConfig(), CP_UTF8
), GIT_CONFIG_LEVEL_LOCAL
, FALSE
);
180 MessageBox(nullptr, g_Git
.GetLibGit2LastErr(), _T("TortoiseGit"), MB_ICONEXCLAMATION
);
184 int ret
= SafeDataImpl(config
);
186 git_config_free(config
);
191 virtual void LoadDataImpl(git_config
* config
) = 0;
192 virtual BOOL
SafeDataImpl(git_config
* config
) = 0;
194 static void AddTrueFalseToComboBox(CComboBox
&combobox
)
196 combobox
.AddString(_T(""));
197 combobox
.AddString(_T("true"));
198 combobox
.AddString(_T("false"));
201 static void GetBoolConfigValueComboBox(git_config
* config
, const CString
&key
, CComboBox
&combobox
)
203 CStringA keyA
= CUnicodeUtils::GetUTF8(key
);
205 if (git_config_get_bool(&out
, config
, keyA
) == GIT_ENOTFOUND
)
206 combobox
.SetCurSel(0);
208 combobox
.SetCurSel(1);
210 combobox
.SetCurSel(2);
213 static int GetConfigValue(git_config
* config
, const CString
&key
, CString
&value
)
215 CStringA keyA
= CUnicodeUtils::GetUTF8(key
);
216 const char *out
= nullptr;
217 int retval
= git_config_get_string(&out
, config
, keyA
);
218 value
= CUnicodeUtils::GetUnicode((CStringA
)out
);
223 #define GITSETTINGS_DDX \
224 DDX_Control(pDX, IDC_COMBO_SETTINGS_SAFETO, m_cSaveTo);
226 #define GITSETTINGS_RADIO_EVENT \
227 ON_CBN_SELCHANGE(IDC_COMBO_SETTINGS_SAFETO, &OnChange) \
228 ON_BN_CLICKED(IDC_RADIO_SETTINGS_EFFECTIVE, &OnBnClickedChangedConfigSource) \
229 ON_BN_CLICKED(IDC_RADIO_SETTINGS_LOCAL, &OnBnClickedChangedConfigSource) \
230 ON_BN_CLICKED(IDC_RADIO_SETTINGS_PROJECT, &OnBnClickedChangedConfigSource) \
231 ON_BN_CLICKED(IDC_RADIO_SETTINGS_GLOBAL, &OnBnClickedChangedConfigSource) \
232 ON_BN_CLICKED(IDC_RADIO_SETTINGS_SYSTEM, &OnBnClickedChangedConfigSource)
234 #define GITSETTINGS_RADIO_EVENT_HANDLE \
235 afx_msg void OnBnClickedChangedConfigSource() \
237 m_iConfigSource = GetCheckedRadioButton(IDC_RADIO_SETTINGS_EFFECTIVE, IDC_RADIO_SETTINGS_SYSTEM) - IDC_RADIO_SETTINGS_EFFECTIVE; \