Initialize buffers
[TortoiseGit.git] / src / TortoiseProc / Settings / SettingGitCredential.cpp
blob22eabc381160b3954a64ab1be7dd7650aa75bf60
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2013-2014 - 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 // SettingGitCredential.cpp : implementation file
22 #include "stdafx.h"
23 #include "TortoiseProc.h"
24 #include "SettingGitCredential.h"
25 #include "Settings.h"
26 #include "GitAdminDir.h"
27 #include "MessageBox.h"
28 #include "AppUtils.h"
29 #include "Git.h"
30 #include "PathUtils.h"
32 namespace SimpleCredentialType
34 static int Advanced;
35 static int None;
36 static int LocalWincred;
37 static int LocalWinstore;
38 static int GlobalWincred;
39 static int GlobalWinstore;
40 static int SystemWincred;
42 static void Init()
44 Advanced = -1;
45 None = -1;
46 LocalWincred = -1;
47 LocalWinstore = -1;
48 GlobalWincred = -1;
49 GlobalWinstore = -1;
50 SystemWincred = -1;
54 namespace ConfigType
56 static int Local;
57 static int Global;
58 static int System;
60 static void Init()
62 Local = -1;
63 Global = -1;
64 System = -1;
68 // CSettingGitCredential dialog
70 IMPLEMENT_DYNAMIC(CSettingGitCredential, ISettingsPropPage)
72 CSettingGitCredential::CSettingGitCredential(CString cmdPath)
73 : ISettingsPropPage(CSettingGitCredential::IDD)
74 , m_strUrl(_T(""))
75 , m_strHelper(_T(""))
76 , m_strUsername(_T(""))
77 , m_bUseHttpPath(FALSE)
78 , m_cmdPath(cmdPath)
81 m_ChangedMask = 0;
84 CSettingGitCredential::~CSettingGitCredential()
88 void CSettingGitCredential::DoDataExchange(CDataExchange* pDX)
90 CPropertyPage::DoDataExchange(pDX);
91 DDX_Control(pDX, IDC_COMBO_SIMPLECREDENTIAL, m_ctrlSimpleCredential);
92 DDX_Control(pDX, IDC_LIST_REMOTE, m_ctrlUrlList);
93 DDX_Text(pDX, IDC_EDIT_URL, m_strUrl);
94 DDX_Text(pDX, IDC_COMBO_HELPER, m_strHelper);
95 DDX_Text(pDX, IDC_EDIT_USERNAME, m_strUsername);
96 DDX_Check(pDX, IDC_CHECK_USEHTTPPATH, m_bUseHttpPath);
97 DDX_Control(pDX, IDC_COMBO_CONFIGTYPE, m_ctrlConfigType);
101 BEGIN_MESSAGE_MAP(CSettingGitCredential, CPropertyPage)
102 ON_CBN_SELCHANGE(IDC_COMBO_SIMPLECREDENTIAL, &CSettingGitCredential::OnCbnSelchangeComboSimplecredential)
103 ON_BN_CLICKED(IDC_BUTTON_ADD, &CSettingGitCredential::OnBnClickedButtonAdd)
104 ON_LBN_SELCHANGE(IDC_LIST_REMOTE, &CSettingGitCredential::OnLbnSelchangeListUrl)
105 ON_CBN_SELCHANGE(IDC_COMBO_CONFIGTYPE, &CSettingGitCredential::OnCbnSelchangeComboConfigType)
106 ON_EN_CHANGE(IDC_EDIT_URL, &CSettingGitCredential::OnEnChangeEditUrl)
107 ON_CBN_EDITCHANGE(IDC_COMBO_HELPER, &CSettingGitCredential::OnEnChangeEditHelper)
108 ON_CBN_SELCHANGE(IDC_COMBO_HELPER, &CSettingGitCredential::OnEnChangeEditHelper)
109 ON_EN_CHANGE(IDC_EDIT_USERNAME, &CSettingGitCredential::OnEnChangeEditUsername)
110 ON_BN_CLICKED(IDC_CHECK_USEHTTPPATH, &CSettingGitCredential::OnBnClickedCheckUsehttppath)
111 ON_BN_CLICKED(IDC_BUTTON_REMOVE, &CSettingGitCredential::OnBnClickedButtonRemove)
112 END_MESSAGE_MAP()
114 static bool RunUAC()
116 CString sCmd;
117 sCmd.Format(_T("/command:settings /page:gitcredential /path:\"%s\""), g_Git.m_CurrentDir);
118 return CAppUtils::RunTortoiseGitProc(sCmd, true);
121 static CString GetWinstorePath()
123 TCHAR winstorebuf[MAX_PATH] = {0};
124 ExpandEnvironmentStrings(_T("%AppData%\\GitCredStore\\git-credential-winstore.exe"), winstorebuf, MAX_PATH);
125 CString winstore;
126 winstore.Format(_T("!'%s'"), winstorebuf);
127 return winstore;
130 static bool WincredExists()
132 CString path = g_Git.ms_LastMsysGitDir;
133 if (g_Git.ms_LastMsysGitDir.Right(1) != _T("\\"))
134 path.AppendChar(_T('\\'));
135 path.Append(_T("..\\libexec\\git-core\\git-credential-wincred.exe"));
136 return !!PathFileExists(path);
139 static bool WinstoreExists()
141 return !!PathFileExists(GetWinstorePath());
144 static CStringA RegexEscape(CStringA str)
146 CStringA result;
147 for (int i = 0; i < str.GetLength(); ++i)
149 char c = str[i];
150 switch (c)
152 case '\\': case '*': case '+': case '?': case '|':
153 case '{': case '[': case '(': case ')': case '^':
154 case '$': case '.': case '#': case ' ':
155 result.AppendChar('\\');
156 result.AppendChar(c);
157 break;
158 case '\t': result.Append("\\t"); break;
159 case '\n': result.Append("\\n"); break;
160 case '\r': result.Append("\\r"); break;
161 case '\f': result.Append("\\f"); break;
162 default: result.AppendChar(c); break;
166 return result;
169 BOOL CSettingGitCredential::OnInitDialog()
171 ISettingsPropPage::OnInitDialog();
173 CString proj;
174 bool hasLocal = g_GitAdminDir.HasAdminDir(m_cmdPath, &proj);
175 if (hasLocal)
177 CString title;
178 this->GetWindowText(title);
179 this->SetWindowText(title + _T(" - ") + proj);
182 m_ctrlUrlList.ResetContent();
184 ConfigType::Init();
185 AddConfigType(ConfigType::Local, CString(MAKEINTRESOURCE(IDS_SETTINGS_LOCAL)), hasLocal);
186 AddConfigType(ConfigType::Global, CString(MAKEINTRESOURCE(IDS_SETTINGS_GLOBAL)));
187 AddConfigType(ConfigType::System, CString(MAKEINTRESOURCE(IDS_SETTINGS_SYSTEM)));
188 m_ctrlConfigType.SetCurSel(0);
190 if (WincredExists())
191 ((CComboBox*) GetDlgItem(IDC_COMBO_HELPER))->AddString(_T("wincred"));
192 if (WinstoreExists())
193 ((CComboBox*) GetDlgItem(IDC_COMBO_HELPER))->AddString(GetWinstorePath());
195 SimpleCredentialType::Init();
196 AddSimpleCredential(SimpleCredentialType::Advanced, CString(MAKEINTRESOURCE(IDS_ADVANCED)));
197 AddSimpleCredential(SimpleCredentialType::None, CString(MAKEINTRESOURCE(IDS_NONE)));
198 AddSimpleCredential(SimpleCredentialType::LocalWincred, CString(MAKEINTRESOURCE(IDS_LOCAL_WINCRED)), hasLocal && WincredExists());
199 AddSimpleCredential(SimpleCredentialType::LocalWinstore, CString(MAKEINTRESOURCE(IDS_LOCAL_WINSTORE)), hasLocal && WinstoreExists());
200 AddSimpleCredential(SimpleCredentialType::GlobalWincred, CString(MAKEINTRESOURCE(IDS_GLOBAL_WINCRED)), WincredExists());
201 AddSimpleCredential(SimpleCredentialType::GlobalWinstore, CString(MAKEINTRESOURCE(IDS_GLOBAL_WINSTORE)), WinstoreExists());
202 AddSimpleCredential(SimpleCredentialType::SystemWincred, CString(MAKEINTRESOURCE(IDS_SYSTEM_WINCRED)), WincredExists());
204 LoadList();
206 EnableAdvancedOptions();
208 UpdateData(FALSE);
209 return TRUE;
211 // CSettingGitCredential message handlers
213 void CSettingGitCredential::OnCbnSelchangeComboSimplecredential()
215 EnableAdvancedOptions();
216 SetModified();
219 void CSettingGitCredential::OnBnClickedButtonAdd()
221 UpdateData();
223 if (m_strHelper.Trim().IsEmpty())
225 CMessageBox::Show(NULL, IDS_GITCREDENTIAL_HELPEREMPTY, IDS_APPNAME, MB_OK | MB_ICONERROR);
226 return;
229 m_ChangedMask = CREDENTIAL_URL | CREDENTIAL_HELPER | CREDENTIAL_USERNAME | CREDENTIAL_USEHTTPPATH;
230 int sel = m_ctrlConfigType.GetCurSel();
231 CString prefix = sel == ConfigType::System ? _T("S") : sel == ConfigType::Global ? _T("G") : _T("L");
232 CString text;
233 if (!m_strUrl.IsEmpty())
234 text.Format(_T("%s:%s"), prefix, m_strUrl);
235 else
236 text = prefix;
237 if (IsUrlExist(text))
239 CString msg;
240 msg.Format(IDS_GITCREDENTIAL_OVERWRITEHELPER, m_strUrl);
241 if (CMessageBox::Show(NULL, msg, _T("TortoiseGit"), MB_YESNO | MB_ICONQUESTION | MB_DEFBUTTON2) == IDYES)
242 m_ChangedMask &= ~CREDENTIAL_URL;
243 else
244 return;
246 else
248 if (m_strUsername.IsEmpty())
249 m_ChangedMask &= ~CREDENTIAL_USERNAME;
250 if (!m_bUseHttpPath)
251 m_ChangedMask &= ~CREDENTIAL_USEHTTPPATH;
254 OnApply();
257 void CSettingGitCredential::EnableAdvancedOptions()
259 BOOL enable = m_ctrlSimpleCredential.GetCurSel() == SimpleCredentialType::Advanced ? TRUE : FALSE;
260 GetDlgItem(IDC_LIST_REMOTE)->EnableWindow(enable);
261 GetDlgItem(IDC_EDIT_URL)->EnableWindow(enable);
262 GetDlgItem(IDC_COMBO_HELPER)->EnableWindow(enable);
263 GetDlgItem(IDC_EDIT_USERNAME)->EnableWindow(enable);
264 GetDlgItem(IDC_COMBO_CONFIGTYPE)->EnableWindow(enable);
265 GetDlgItem(IDC_CHECK_USEHTTPPATH)->EnableWindow(enable);
266 GetDlgItem(IDC_BUTTON_ADD)->EnableWindow(enable);
267 GetDlgItem(IDC_BUTTON_REMOVE)->EnableWindow(enable);
270 BOOL CSettingGitCredential::IsUrlExist(CString &text)
272 CString str;
273 for(int i = 0; i < m_ctrlUrlList.GetCount();i++)
275 m_ctrlUrlList.GetText(i, str);
276 if (str == text)
278 return true;
281 return false;
284 void CSettingGitCredential::OnLbnSelchangeListUrl()
286 CWaitCursor wait;
288 if (m_ChangedMask)
290 if (CMessageBox::Show(NULL, IDS_GITCREDENTIAL_SAVEHELPER, IDS_APPNAME, 1, IDI_QUESTION, IDS_SAVEBUTTON, IDS_DISCARDBUTTON) == 1)
291 OnApply();
293 SetModified(FALSE);
295 CString cmd, output;
296 int index = m_ctrlUrlList.GetCurSel();
297 if (index < 0)
299 m_strHelper.Empty();
300 m_strUrl.Empty();
301 m_strHelper.Empty();
302 m_bUseHttpPath = FALSE;
303 UpdateData(FALSE);
304 return;
307 CString text;
308 m_ctrlUrlList.GetText(index, text);
309 int pos = text.Find(_T(':'));
310 CString prefix = pos >= 0 ? text.Left(pos) : text.Left(1);
311 m_ctrlConfigType.SetCurSel(prefix == _T("S") ? ConfigType::System : prefix == _T("X") ? ConfigType::Global : prefix == _T("G") ? ConfigType::Global : ConfigType::Local);
312 m_strUrl = pos >= 0 ? text.Mid(pos + 1) : _T("");
314 m_strHelper = Load(_T("helper"));
315 m_strUsername = Load(_T("username"));
316 m_bUseHttpPath = Load(_T("useHttpPath")) == _T("true") ? TRUE : FALSE;
318 m_ChangedMask = 0;
320 GetDlgItem(IDC_BUTTON_ADD)->EnableWindow(TRUE);
321 GetDlgItem(IDC_BUTTON_REMOVE)->EnableWindow(TRUE);
322 this->UpdateData(FALSE);
325 void CSettingGitCredential::OnCbnSelchangeComboConfigType()
327 SetModified();
330 void CSettingGitCredential::OnEnChangeEditUrl()
332 m_ChangedMask |= CREDENTIAL_URL;
333 SetModified();
336 void CSettingGitCredential::OnEnChangeEditHelper()
338 m_ChangedMask |= CREDENTIAL_HELPER;
340 UpdateData();
341 if (!m_strHelper.IsEmpty())
342 SetModified();
343 else
344 SetModified(0);
347 void CSettingGitCredential::OnEnChangeEditUsername()
349 m_ChangedMask |= CREDENTIAL_USERNAME;
350 SetModified();
353 void CSettingGitCredential::OnBnClickedCheckUsehttppath()
355 m_ChangedMask |= CREDENTIAL_USEHTTPPATH;
356 SetModified();
359 static int GetCredentialDefaultUrlCallback(const git_config_entry *entry, void *payload)
361 CString display = entry->level == 1 ? _T("S") : entry->level == 2 ? _T("X") : entry->level == 3 ? _T("G") : _T("L");
362 ((STRING_VECTOR *)payload)->push_back(display);
363 return 0;
366 static int GetCredentialUrlCallback(const git_config_entry *entry, void *payload)
368 CString name = CUnicodeUtils::GetUnicode(entry->name);
369 int pos1 = name.Find(_T('.'));
370 int pos2 = name.ReverseFind(_T('.'));
371 CString url = name.Mid(pos1 + 1, pos2 - pos1 - 1);
372 CString display;
373 display.Format(_T("%s:%s"), entry->level == 1 ? _T("S") : entry->level == 2 ? _T("X") : entry->level == 3 ? _T("G") : _T("L"), url);
374 ((STRING_VECTOR *)payload)->push_back(display);
375 return 0;
378 static int GetCredentialEntryCallback(const git_config_entry *entry, void *payload)
380 CString name = CUnicodeUtils::GetUnicode(entry->name);
381 ((STRING_VECTOR *)payload)->push_back(name);
382 return 0;
385 static int GetCredentialAnyEntryCallback(const git_config_entry *entry, void *payload)
387 CString name = CUnicodeUtils::GetUnicode(entry->name);
388 CString value = CUnicodeUtils::GetUnicode(entry->value);
389 CString display = entry->level == 1 ? _T("S") : entry->level == 2 ? _T("X") : entry->level == 3 ? _T("G") : _T("L");
390 CString text;
391 text.Format(_T("%s\n%s\n%s"), display, name, value);
392 ((STRING_VECTOR *)payload)->push_back(text);
393 return 0;
396 void CSettingGitCredential::AddConfigType(int &index, CString text, bool add)
398 if (add)
399 index = m_ctrlConfigType.AddString(text);
402 void CSettingGitCredential::AddSimpleCredential(int &index, CString text, bool add)
404 if (add)
405 index = m_ctrlSimpleCredential.AddString(text);
408 void CSettingGitCredential::LoadList()
410 git_config * config;
411 git_config_new(&config);
412 git_config_add_file_ondisk(config, CGit::GetGitPathStringA(g_Git.GetGitLocalConfig()), GIT_CONFIG_LEVEL_LOCAL, FALSE);
413 git_config_add_file_ondisk(config, CGit::GetGitPathStringA(g_Git.GetGitGlobalConfig()), GIT_CONFIG_LEVEL_GLOBAL, FALSE);
414 git_config_add_file_ondisk(config, CGit::GetGitPathStringA(g_Git.GetGitGlobalXDGConfig()), GIT_CONFIG_LEVEL_XDG, FALSE);
415 git_config_add_file_ondisk(config, CGit::GetGitPathStringA(g_Git.GetGitSystemConfig()), GIT_CONFIG_LEVEL_SYSTEM, FALSE);
417 STRING_VECTOR defaultList, urlList;
418 git_config_foreach_match(config, "credential\\.helper", GetCredentialDefaultUrlCallback, &defaultList);
419 git_config_foreach_match(config, "credential\\..*\\.helper", GetCredentialUrlCallback, &urlList);
420 STRING_VECTOR anyList;
421 git_config_foreach_match(config, "credential\\..*", GetCredentialAnyEntryCallback, &anyList);
422 git_config_free(config);
424 for (size_t i = 0; i < defaultList.size(); ++i)
425 m_ctrlUrlList.AddString(defaultList[i]);
426 for (size_t i = 0; i < urlList.size(); ++i)
427 m_ctrlUrlList.AddString(urlList[i]);
429 if (anyList.empty())
431 m_ctrlSimpleCredential.SetCurSel(SimpleCredentialType::None);
432 return;
434 if (anyList.size() > 1)
436 m_ctrlSimpleCredential.SetCurSel(SimpleCredentialType::Advanced);
437 return;
440 int pos = 0;
441 CString prefix = anyList[0].Tokenize(_T("\n"), pos);
442 CString key = anyList[0].Tokenize(_T("\n"), pos);
443 CString value = anyList[0].Tokenize(_T("\n"), pos);
444 if (key != _T("credential.helper"))
446 m_ctrlSimpleCredential.SetCurSel(SimpleCredentialType::Advanced);
447 return;
450 CString winstore = GetWinstorePath();
451 if (prefix == _T("L"))
453 if (value == _T("wincred"))
455 m_ctrlSimpleCredential.SetCurSel(SimpleCredentialType::LocalWincred);
456 return;
458 else if (value == winstore)
460 m_ctrlSimpleCredential.SetCurSel(SimpleCredentialType::LocalWinstore);
461 return;
465 if (prefix == _T("G") || prefix == _T("X"))
467 if (value == _T("wincred"))
469 m_ctrlSimpleCredential.SetCurSel(SimpleCredentialType::GlobalWincred);
470 return;
472 else if (value == winstore)
474 m_ctrlSimpleCredential.SetCurSel(SimpleCredentialType::GlobalWinstore);
475 return;
479 if (prefix == _T("S"))
481 if (value == _T("wincred"))
483 m_ctrlSimpleCredential.SetCurSel(SimpleCredentialType::SystemWincred);
484 return;
488 m_ctrlSimpleCredential.SetCurSel(SimpleCredentialType::Advanced);
491 CString CSettingGitCredential::Load(CString key)
493 CString cmd;
495 if (m_strUrl.IsEmpty())
496 cmd.Format(_T("credential.%s"), key);
497 else
498 cmd.Format(_T("credential.%s.%s"), m_strUrl, key);
500 git_config * config;
501 git_config_new(&config);
502 int sel = m_ctrlConfigType.GetCurSel();
503 if (sel == ConfigType::Local)
505 git_config_add_file_ondisk(config, CGit::GetGitPathStringA(g_Git.GetGitLocalConfig()), GIT_CONFIG_LEVEL_LOCAL, FALSE);
507 else if (sel == ConfigType::Global)
509 git_config_add_file_ondisk(config, CGit::GetGitPathStringA(g_Git.GetGitGlobalConfig()), GIT_CONFIG_LEVEL_GLOBAL, FALSE);
510 git_config_add_file_ondisk(config, CGit::GetGitPathStringA(g_Git.GetGitGlobalXDGConfig()), GIT_CONFIG_LEVEL_XDG, FALSE);
512 else if (sel == ConfigType::System)
514 git_config_add_file_ondisk(config, CGit::GetGitPathStringA(g_Git.GetGitSystemConfig()), GIT_CONFIG_LEVEL_SYSTEM, FALSE);
517 CStringA cmdA = CUnicodeUtils::GetUTF8(cmd);
518 CStringA valueA;
519 const git_config_entry *entry;
520 if (!git_config_get_entry(&entry, config, cmdA))
521 valueA = CStringA(entry->value);
522 cmdA.ReleaseBuffer();
523 git_config_free(config);
525 return CUnicodeUtils::GetUnicode(valueA);
528 void CSettingGitCredential::Save(CString key, CString value)
530 CString cmd, out;
532 if (m_strUrl.IsEmpty())
533 cmd.Format(_T("credential.%s"), key);
534 else
535 cmd.Format(_T("credential.%s.%s"), m_strUrl, key);
537 int sel = m_ctrlConfigType.GetCurSel();
538 CONFIG_TYPE configLevel = sel == ConfigType::System ? CONFIG_SYSTEM : sel == ConfigType::Global ? CONFIG_GLOBAL : CONFIG_LOCAL;
540 bool old = g_Git.m_IsUseGitDLL;
541 // workaround gitdll bug
542 // TODO: switch to libgit2
543 g_Git.m_IsUseGitDLL = false;
544 if (g_Git.SetConfigValue(cmd, value, configLevel, CP_UTF8))
546 CString msg;
547 msg.Format(IDS_PROC_SAVECONFIGFAILED, cmd, value);
548 CMessageBox::Show(NULL, msg, _T("TortoiseGit"), MB_OK | MB_ICONERROR);
550 if (value.IsEmpty())
552 if (g_Git.UnsetConfigValue(cmd, configLevel, CP_UTF8))
554 CString msg;
555 msg.Format(IDS_PROC_SAVECONFIGFAILED, cmd, value);
556 CMessageBox::Show(NULL, msg, _T("TortoiseGit"), MB_OK | MB_ICONERROR);
559 g_Git.m_IsUseGitDLL = old;
562 static int DeleteOtherKeys(int type)
564 CString match;
565 if (type == SimpleCredentialType::LocalWincred)
566 match = _T("L\ncredential.helper\nwincred");
567 else if (type == SimpleCredentialType::LocalWinstore)
568 match = _T("L\ncredential.helper\n") + GetWinstorePath();
569 else if (type == SimpleCredentialType::GlobalWincred)
570 match = _T("G\ncredential.helper\nwincred");
571 else if (type == SimpleCredentialType::GlobalWinstore)
572 match = _T("G\ncredential.helper\n") + GetWinstorePath();
573 else if (type == SimpleCredentialType::SystemWincred)
574 match = _T("S\ncredential.helper\nwincred");
576 git_config * config;
577 git_config_new(&config);
578 git_config_add_file_ondisk(config, CGit::GetGitPathStringA(g_Git.GetGitLocalConfig()), GIT_CONFIG_LEVEL_LOCAL, FALSE);
579 git_config_add_file_ondisk(config, CGit::GetGitPathStringA(g_Git.GetGitGlobalConfig()), GIT_CONFIG_LEVEL_GLOBAL, FALSE);
580 git_config_add_file_ondisk(config, CGit::GetGitPathStringA(g_Git.GetGitGlobalXDGConfig()), GIT_CONFIG_LEVEL_XDG, FALSE);
581 git_config_add_file_ondisk(config, CGit::GetGitPathStringA(g_Git.GetGitSystemConfig()), GIT_CONFIG_LEVEL_SYSTEM, FALSE);
583 STRING_VECTOR list;
584 git_config_foreach_match(config, "credential\\..*", GetCredentialAnyEntryCallback, &list);
585 for (size_t i = 0; i < list.size(); ++i)
587 int pos = 0;
588 CString prefix = list[i].Tokenize(_T("\n"), pos);
589 if (prefix == _T("S") && !CAppUtils::IsAdminLogin())
591 RunUAC();
592 return -1;
596 int result = 0;
597 // workaround gitdll bug
598 // TODO: switch to libgit2
599 bool old = g_Git.m_IsUseGitDLL;
600 g_Git.m_IsUseGitDLL = false;
601 for (size_t i = 0; i < list.size(); ++i)
603 if (list[i] != match)
605 int pos = 0;
606 CString prefix = list[i].Tokenize(_T("\n"), pos);
607 CString key = list[i].Tokenize(_T("\n"), pos);
608 CONFIG_TYPE configLevel = prefix == _T("S") ? CONFIG_SYSTEM : prefix == _T("G") || prefix == _T("X") ? CONFIG_GLOBAL : CONFIG_LOCAL;
609 if (g_Git.UnsetConfigValue(key, configLevel, CP_UTF8))
611 CString msg;
612 msg.Format(IDS_PROC_SAVECONFIGFAILED, key, _T(""));
613 CMessageBox::Show(NULL, msg, _T("TortoiseGit"), MB_OK | MB_ICONERROR);
614 result = 1;
615 break;
619 g_Git.m_IsUseGitDLL = old;
621 return result;
624 bool SaveSimpleCredential(int type)
626 CONFIG_TYPE configLevel;
627 CString value;
628 if (type == SimpleCredentialType::LocalWincred)
630 configLevel = CONFIG_LOCAL;
631 value = _T("wincred");
633 else if (type == SimpleCredentialType::LocalWinstore)
635 configLevel = CONFIG_LOCAL;
636 value = GetWinstorePath();
638 else if (type == SimpleCredentialType::GlobalWincred)
640 configLevel = CONFIG_GLOBAL;
641 value = _T("wincred");
643 else if (type == SimpleCredentialType::GlobalWinstore)
645 configLevel = CONFIG_GLOBAL;
646 value = GetWinstorePath();
648 else if (type == SimpleCredentialType::SystemWincred)
650 configLevel = CONFIG_SYSTEM;
651 value = _T("wincred");
653 else
655 return true;
658 // workaround gitdll bug
659 // TODO: switch to libgit2
660 bool old = g_Git.m_IsUseGitDLL;
661 g_Git.m_IsUseGitDLL = false;
662 if (g_Git.SetConfigValue(_T("credential.helper"), value, configLevel, CP_UTF8))
664 CString msg;
665 msg.Format(IDS_PROC_SAVECONFIGFAILED, _T("credential.helper"), value);
666 CMessageBox::Show(NULL, msg, _T("TortoiseGit"), MB_OK | MB_ICONERROR);
667 return false;
669 g_Git.m_IsUseGitDLL = old;
670 return true;
673 BOOL CSettingGitCredential::OnApply()
675 CWaitCursor wait;
676 UpdateData();
678 int type = m_ctrlSimpleCredential.GetCurSel();
679 if (type == SimpleCredentialType::SystemWincred && !CAppUtils::IsAdminLogin())
681 RunUAC();
682 EndDialog(0);
683 return FALSE;
685 if (type != SimpleCredentialType::Advanced)
687 if (!SaveSimpleCredential(type))
688 return FALSE;
690 int ret = DeleteOtherKeys(type);
691 if (ret < 0)
692 EndDialog(0);
693 if (ret)
694 return FALSE;
695 SetModified(FALSE);
696 return ISettingsPropPage::OnApply();
699 int sel = m_ctrlConfigType.GetCurSel();
700 if (sel == ConfigType::System && !CAppUtils::IsAdminLogin())
702 RunUAC();
703 EndDialog(0);
704 return FALSE;
707 if (m_ChangedMask & CREDENTIAL_URL)
709 //Add Helper
710 if (m_strHelper.IsEmpty())
712 CMessageBox::Show(NULL, IDS_GITCREDENTIAL_HELPEREMPTY, IDS_APPNAME, MB_OK | MB_ICONERROR);
713 return FALSE;
715 m_strUrl.Replace(L'\\', L'/');
716 m_strHelper.Replace(L'\\', L'/');
718 Save(_T("helper"), m_strHelper);
719 m_ChangedMask &= ~CREDENTIAL_HELPER;
721 int sel = m_ctrlConfigType.GetCurSel();
722 CString prefix = sel == ConfigType::System ? _T("S") : sel == ConfigType::Global ? _T("G") : _T("L");
723 CString text;
724 if (!m_strUrl.IsEmpty())
725 text.Format(_T("%s:%s"), prefix, m_strUrl);
726 else
727 text = prefix;
728 int urlIndex = m_ctrlUrlList.AddString(text);
729 m_ctrlUrlList.SetCurSel(urlIndex);
730 GetDlgItem(IDC_BUTTON_ADD)->EnableWindow(TRUE);
733 if (m_ChangedMask & CREDENTIAL_HELPER)
735 m_strHelper.Replace(L'\\', L'/');
736 Save(_T("helper"), m_strHelper);
739 if (m_ChangedMask & CREDENTIAL_USERNAME)
740 Save(_T("username"), m_strUsername);
742 if (m_ChangedMask & CREDENTIAL_USEHTTPPATH)
743 Save(_T("useHttpPath"), m_bUseHttpPath ? _T("true") : _T(""));
745 SetModified(FALSE);
747 m_ChangedMask = 0;
748 return ISettingsPropPage::OnApply();
751 void CSettingGitCredential::OnBnClickedButtonRemove()
753 int index = m_ctrlUrlList.GetCurSel();
754 if (index >= 0)
756 CString str;
757 m_ctrlUrlList.GetText(index, str);
758 CString msg;
759 msg.Format(IDS_GITCREDENTIAL_DELETEHELPER, str);
760 if (CMessageBox::Show(NULL, msg, _T("TortoiseGit"), MB_YESNO | MB_ICONQUESTION) == IDYES)
762 git_config * config;
763 git_config_new(&config);
764 int pos = str.Find(_T(':'));
765 CString prefix = pos >= 0 ? str.Left(pos) : str;
766 CString url = pos >= 0 ? str.Mid(pos + 1) : _T("");
767 CONFIG_TYPE configLevel = CONFIG_LOCAL;
768 switch (prefix[0])
770 case _T('L'):
772 configLevel = CONFIG_LOCAL;
773 git_config_add_file_ondisk(config, CGit::GetGitPathStringA(g_Git.GetGitLocalConfig()), GIT_CONFIG_LEVEL_LOCAL, FALSE);
774 break;
776 case _T('G'):
777 case _T('X'):
779 configLevel = CONFIG_GLOBAL;
780 git_config_add_file_ondisk(config, CGit::GetGitPathStringA(g_Git.GetGitGlobalConfig()), GIT_CONFIG_LEVEL_GLOBAL, FALSE);
781 git_config_add_file_ondisk(config, CGit::GetGitPathStringA(g_Git.GetGitGlobalXDGConfig()), GIT_CONFIG_LEVEL_XDG, FALSE);
782 break;
784 case _T('S'):
786 if (!CAppUtils::IsAdminLogin())
788 RunUAC();
789 git_config_free(config);
790 EndDialog(0);
791 return;
793 configLevel = CONFIG_SYSTEM;
794 git_config_add_file_ondisk(config, CGit::GetGitPathStringA(g_Git.GetGitSystemConfig()), GIT_CONFIG_LEVEL_SYSTEM, FALSE);
795 break;
799 STRING_VECTOR list;
800 CStringA urlA = CUnicodeUtils::GetUTF8(url);
801 CStringA pattern = urlA.IsEmpty() ? "^credential\\.[^.]+$" : ("credential\\." + RegexEscape(urlA) + "\\..*");
802 git_config_foreach_match(config, pattern, GetCredentialEntryCallback, &list);
803 git_config_free(config);
804 for (size_t i = 0; i < list.size(); ++i)
805 g_Git.UnsetConfigValue(list[i], configLevel, CP_UTF8);
806 m_ctrlUrlList.DeleteString(index);
807 OnLbnSelchangeListUrl();