Fixed issue #818: SafeCRLF = warn should be available
[TortoiseGit.git] / src / TortoiseProc / Settings / SettingGitConfig.cpp
blob52066393a1b04bf569a3d728d1affe67b90c5211
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2011 - 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.
19 // SettingGitConfig.cpp : implementation file
22 #include "stdafx.h"
23 #include "TortoiseProc.h"
24 #include "SettingGitConfig.h"
25 #include "Git.h"
26 #include "Settings.h"
27 #include "GitAdminDir.h"
28 #include "MessageBox.h"
29 #include "ProjectProperties.h"
30 #include "AppUtils.h"
31 #include "PathUtils.h"
32 // CSettingGitConfig dialog
34 IMPLEMENT_DYNAMIC(CSettingGitConfig, ISettingsPropPage)
36 CSettingGitConfig::CSettingGitConfig()
37 : ISettingsPropPage(CSettingGitConfig::IDD)
38 , m_UserName(_T(""))
39 , m_UserEmail(_T(""))
40 , m_UserSigningKey(_T(""))
41 , m_bGlobal(FALSE)
42 , m_bAutoCrlf(FALSE)
43 , m_bWarnNoSignedOffBy(FALSE)
45 m_ChangeMask=0;
48 CSettingGitConfig::~CSettingGitConfig()
52 void CSettingGitConfig::DoDataExchange(CDataExchange* pDX)
54 CPropertyPage::DoDataExchange(pDX);
55 DDX_Text(pDX, IDC_GIT_USERNAME, m_UserName);
56 DDX_Text(pDX, IDC_GIT_USEREMAIL, m_UserEmail);
57 DDX_Text(pDX, IDC_GIT_USERESINGNINGKEY, m_UserSigningKey);
58 DDX_Check(pDX, IDC_CHECK_GLOBAL, m_bGlobal);
59 DDX_Check(pDX, IDC_CHECK_AUTOCRLF, m_bAutoCrlf);
60 DDX_Control(pDX, IDC_COMBO_SAFECRLF, m_cSafeCrLf);
61 DDX_Check(pDX, IDC_CHECK_WARN_NO_SIGNED_OFF_BY, m_bWarnNoSignedOffBy);
64 BEGIN_MESSAGE_MAP(CSettingGitConfig, CPropertyPage)
65 ON_BN_CLICKED(IDC_CHECK_GLOBAL, &CSettingGitConfig::OnBnClickedCheckGlobal)
66 ON_EN_CHANGE(IDC_GIT_USERNAME, &CSettingGitConfig::OnEnChangeGitUsername)
67 ON_EN_CHANGE(IDC_GIT_USEREMAIL, &CSettingGitConfig::OnEnChangeGitUseremail)
68 ON_EN_CHANGE(IDC_GIT_USERESINGNINGKEY, &CSettingGitConfig::OnEnChangeGitUserSigningKey)
69 ON_BN_CLICKED(IDC_CHECK_AUTOCRLF, &CSettingGitConfig::OnBnClickedCheckAutocrlf)
70 ON_CBN_SELCHANGE(IDC_COMBO_SAFECRLF, &CSettingGitConfig::OnCbnSelchangeSafeCrLf)
71 ON_BN_CLICKED(IDC_EDITGLOBALGITCONFIG, &CSettingGitConfig::OnBnClickedEditglobalgitconfig)
72 ON_BN_CLICKED(IDC_EDITLOCALGITCONFIG, &CSettingGitConfig::OnBnClickedEditlocalgitconfig)
73 ON_BN_CLICKED(IDC_CHECK_WARN_NO_SIGNED_OFF_BY, &CSettingGitConfig::OnBnClickedCheckWarnNoSignedOffBy)
74 END_MESSAGE_MAP()
76 BOOL CSettingGitConfig::OnInitDialog()
78 ISettingsPropPage::OnInitDialog();
80 m_cSafeCrLf.AddString(_T("false"));
81 m_cSafeCrLf.AddString(_T("true"));
82 m_cSafeCrLf.AddString(_T("warn"));
84 m_UserName = g_Git.GetUserName();
85 m_UserEmail = g_Git.GetUserEmail();
86 m_UserSigningKey = g_Git.GetConfigValue(_T("user.signingkey"));
88 ProjectProperties::GetBOOLProps(this->m_bAutoCrlf, _T("core.autocrlf"));
89 BOOL bSafeCrLf = FALSE;
90 ProjectProperties::GetBOOLProps(bSafeCrLf, _T("core.safecrlf"));
91 if (bSafeCrLf)
92 m_cSafeCrLf.SetCurSel(1);
93 else
95 CString sSafeCrLf;
96 ProjectProperties::GetStringProps(sSafeCrLf, _T("core.safecrlf"));
97 sSafeCrLf = sSafeCrLf.MakeLower().Trim();
98 if (sSafeCrLf == _T("warn"))
99 m_cSafeCrLf.SetCurSel(2);
100 else
101 m_cSafeCrLf.SetCurSel(0);
103 ProjectProperties::GetBOOLProps(this->m_bWarnNoSignedOffBy, _T("tgit.warnnosignedoffby"));
105 CString str = ((CSettings*)GetParent())->m_CmdPath.GetWinPath();
106 bool isBareRepo = g_GitAdminDir.IsBareRepo(str);
107 CString proj;
108 if (g_GitAdminDir.HasAdminDir(str, &proj) || isBareRepo)
110 this->SetWindowText(_T("Config - ") + proj);
111 this->GetDlgItem(IDC_CHECK_GLOBAL)->EnableWindow(TRUE);
112 this->GetDlgItem(IDC_EDITLOCALGITCONFIG)->EnableWindow(TRUE);
114 else
116 m_bGlobal = TRUE;
117 this->GetDlgItem(IDC_CHECK_GLOBAL)->EnableWindow(FALSE);
118 this->GetDlgItem(IDC_EDITLOCALGITCONFIG)->EnableWindow(FALSE);
121 if (isBareRepo)
122 this->GetDlgItem(IDC_EDITLOCALGITCONFIG)->SetWindowText(_T("Edit local git config"));
124 this->UpdateData(FALSE);
125 return TRUE;
127 // CSettingGitConfig message handlers
129 void CSettingGitConfig::OnBnClickedCheckGlobal()
131 SetModified();
134 void CSettingGitConfig::OnEnChangeGitUsername()
136 m_ChangeMask |= GIT_NAME;
137 SetModified();
140 void CSettingGitConfig::OnEnChangeGitUseremail()
142 m_ChangeMask |= GIT_EMAIL;
143 SetModified();
146 void CSettingGitConfig::OnEnChangeGitUserSigningKey()
148 m_ChangeMask |= GIT_SIGNINGKEY;
149 SetModified();
152 BOOL CSettingGitConfig::OnApply()
154 CString cmd, out;
155 CONFIG_TYPE type=CONFIG_LOCAL;
156 this->UpdateData(FALSE);
158 if(this->m_bGlobal)
159 type = CONFIG_GLOBAL;
161 if(m_ChangeMask&GIT_NAME)
162 if(g_Git.SetConfigValue(_T("user.name"), this->m_UserName,type, g_Git.GetGitEncode(L"i18n.commitencoding")))
164 CMessageBox::Show(NULL, _T("Fail to save user name"), _T("TortoiseGit"), MB_OK|MB_ICONERROR);
165 return FALSE;
168 if(m_ChangeMask&GIT_EMAIL)
169 if(g_Git.SetConfigValue(_T("user.email"), this->m_UserEmail,type, g_Git.GetGitEncode(L"i18n.commitencoding")))
171 CMessageBox::Show(NULL, _T("Fail to save user email"), _T("TortoiseGit"), MB_OK|MB_ICONERROR);
172 return FALSE;
175 if(m_ChangeMask&GIT_SIGNINGKEY)
176 if(g_Git.SetConfigValue(_T("user.signingkey"), this->m_UserSigningKey, type, g_Git.GetGitEncode(L"i18n.commitencoding")))
178 CMessageBox::Show(NULL,_T("Fail to save user signingkey"),_T("TortoiseGit"),MB_OK|MB_ICONERROR);
179 return FALSE;
182 if(m_ChangeMask&GIT_WARNNOSIGNEDOFFBY)
183 if(g_Git.SetConfigValue(_T("tgit.warnnosignedoffby"), this->m_bWarnNoSignedOffBy?_T("true"):_T("false"), type))
185 CMessageBox::Show(NULL, _T("Fail to save warnnosignedoffby"), _T("TortoiseGit"), MB_OK|MB_ICONERROR);
186 return FALSE;
189 if(m_ChangeMask&GIT_CRLF)
190 if(g_Git.SetConfigValue(_T("core.autocrlf"), this->m_bAutoCrlf?_T("true"):_T("false"), type))
192 CMessageBox::Show(NULL, _T("Fail to save autocrlf"), _T("TortoiseGit"), MB_OK|MB_ICONERROR);
193 return FALSE;
196 if(m_ChangeMask&GIT_SAFECRLF)
198 CString safecrlf;
199 this->m_cSafeCrLf.GetWindowText(safecrlf);
200 if(g_Git.SetConfigValue(_T("core.safecrlf"), safecrlf, type))
202 CMessageBox::Show(NULL, _T("Fail to save safecrlf"), _T("TortoiseGit"), MB_OK|MB_ICONERROR);
203 return FALSE;
207 m_ChangeMask = 0;
208 SetModified(FALSE);
209 return ISettingsPropPage::OnApply();
211 void CSettingGitConfig::OnBnClickedCheckWarnNoSignedOffBy()
213 m_ChangeMask |= GIT_WARNNOSIGNEDOFFBY;
214 SetModified();
216 void CSettingGitConfig::OnBnClickedCheckAutocrlf()
218 m_ChangeMask |= GIT_CRLF;
219 SetModified();
222 void CSettingGitConfig::OnCbnSelchangeSafeCrLf()
224 m_ChangeMask |= GIT_SAFECRLF;
225 SetModified();
228 void CSettingGitConfig::OnBnClickedEditglobalgitconfig()
230 char charBuf[MAX_PATH];
231 TCHAR buf[MAX_PATH];
232 strcpy_s(charBuf, MAX_PATH, get_windows_home_directory());
233 _tcscpy_s(buf, MAX_PATH, CA2CT(charBuf));
234 _tcscat_s(buf, MAX_PATH, _T("\\.gitconfig"));
235 // use alternative editor because of LineEndings
236 CAppUtils::LaunchAlternativeEditor(buf);
239 void CSettingGitConfig::OnBnClickedEditlocalgitconfig()
241 CString path = g_Git.m_CurrentDir;
242 if (!g_GitAdminDir.IsBareRepo(g_Git.m_CurrentDir))
243 path += _T("\\.git");
244 path += _T("\\config");
245 // use alternative editor because of LineEndings
246 CAppUtils::LaunchAlternativeEditor(path);