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
23 #include "TortoiseProc.h"
24 #include "SettingGitCredential.h"
26 #include "GitAdminDir.h"
27 #include "MessageBox.h"
30 #include "PathUtils.h"
32 namespace SimpleCredentialType
36 static int LocalWincred
;
37 static int LocalWinstore
;
38 static int GlobalWincred
;
39 static int GlobalWinstore
;
40 static int SystemWincred
;
68 // CSettingGitCredential dialog
70 IMPLEMENT_DYNAMIC(CSettingGitCredential
, ISettingsPropPage
)
72 CSettingGitCredential::CSettingGitCredential(CString cmdPath
)
73 : ISettingsPropPage(CSettingGitCredential::IDD
)
76 , m_strUsername(_T(""))
77 , m_bUseHttpPath(FALSE
)
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
)
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
);
126 winstore
.Format(_T("!'%s'"), winstorebuf
);
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
)
147 for (int i
= 0; i
< str
.GetLength(); ++i
)
152 case '\\': case '*': case '+': case '?': case '|':
153 case '{': case '[': case '(': case ')': case '^':
154 case '$': case '.': case '#': case ' ':
155 result
.AppendChar('\\');
156 result
.AppendChar(c
);
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;
169 BOOL
CSettingGitCredential::OnInitDialog()
171 ISettingsPropPage::OnInitDialog();
174 bool hasLocal
= g_GitAdminDir
.HasAdminDir(m_cmdPath
, &proj
);
178 this->GetWindowText(title
);
179 this->SetWindowText(title
+ _T(" - ") + proj
);
182 m_ctrlUrlList
.ResetContent();
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);
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());
206 EnableAdvancedOptions();
211 // CSettingGitCredential message handlers
213 void CSettingGitCredential::OnCbnSelchangeComboSimplecredential()
215 EnableAdvancedOptions();
219 void CSettingGitCredential::OnBnClickedButtonAdd()
223 if (m_strHelper
.Trim().IsEmpty())
225 CMessageBox::Show(NULL
, IDS_GITCREDENTIAL_HELPEREMPTY
, IDS_APPNAME
, MB_OK
| MB_ICONERROR
);
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");
233 if (!m_strUrl
.IsEmpty())
234 text
.Format(_T("%s:%s"), prefix
, m_strUrl
);
237 if (IsUrlExist(text
))
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
;
248 if (m_strUsername
.IsEmpty())
249 m_ChangedMask
&= ~CREDENTIAL_USERNAME
;
251 m_ChangedMask
&= ~CREDENTIAL_USEHTTPPATH
;
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
)
273 for(int i
= 0; i
< m_ctrlUrlList
.GetCount();i
++)
275 m_ctrlUrlList
.GetText(i
, str
);
284 void CSettingGitCredential::OnLbnSelchangeListUrl()
290 if (CMessageBox::Show(NULL
, IDS_GITCREDENTIAL_SAVEHELPER
, IDS_APPNAME
, 1, IDI_QUESTION
, IDS_SAVEBUTTON
, IDS_DISCARDBUTTON
) == 1)
296 int index
= m_ctrlUrlList
.GetCurSel();
302 m_bUseHttpPath
= FALSE
;
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
;
320 GetDlgItem(IDC_BUTTON_ADD
)->EnableWindow(TRUE
);
321 GetDlgItem(IDC_BUTTON_REMOVE
)->EnableWindow(TRUE
);
322 this->UpdateData(FALSE
);
325 void CSettingGitCredential::OnCbnSelchangeComboConfigType()
330 void CSettingGitCredential::OnEnChangeEditUrl()
332 m_ChangedMask
|= CREDENTIAL_URL
;
336 void CSettingGitCredential::OnEnChangeEditHelper()
338 m_ChangedMask
|= CREDENTIAL_HELPER
;
341 if (!m_strHelper
.IsEmpty())
347 void CSettingGitCredential::OnEnChangeEditUsername()
349 m_ChangedMask
|= CREDENTIAL_USERNAME
;
353 void CSettingGitCredential::OnBnClickedCheckUsehttppath()
355 m_ChangedMask
|= CREDENTIAL_USEHTTPPATH
;
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
);
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);
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
);
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
);
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");
391 text
.Format(_T("%s\n%s\n%s"), display
, name
, value
);
392 ((STRING_VECTOR
*)payload
)->push_back(text
);
396 void CSettingGitCredential::AddConfigType(int &index
, CString text
, bool add
)
399 index
= m_ctrlConfigType
.AddString(text
);
402 void CSettingGitCredential::AddSimpleCredential(int &index
, CString text
, bool add
)
405 index
= m_ctrlSimpleCredential
.AddString(text
);
408 void CSettingGitCredential::LoadList()
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
]);
431 m_ctrlSimpleCredential
.SetCurSel(SimpleCredentialType::None
);
434 if (anyList
.size() > 1)
436 m_ctrlSimpleCredential
.SetCurSel(SimpleCredentialType::Advanced
);
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
);
450 CString winstore
= GetWinstorePath();
451 if (prefix
== _T("L"))
453 if (value
== _T("wincred"))
455 m_ctrlSimpleCredential
.SetCurSel(SimpleCredentialType::LocalWincred
);
458 else if (value
== winstore
)
460 m_ctrlSimpleCredential
.SetCurSel(SimpleCredentialType::LocalWinstore
);
465 if (prefix
== _T("G") || prefix
== _T("X"))
467 if (value
== _T("wincred"))
469 m_ctrlSimpleCredential
.SetCurSel(SimpleCredentialType::GlobalWincred
);
472 else if (value
== winstore
)
474 m_ctrlSimpleCredential
.SetCurSel(SimpleCredentialType::GlobalWinstore
);
479 if (prefix
== _T("S"))
481 if (value
== _T("wincred"))
483 m_ctrlSimpleCredential
.SetCurSel(SimpleCredentialType::SystemWincred
);
488 m_ctrlSimpleCredential
.SetCurSel(SimpleCredentialType::Advanced
);
491 CString
CSettingGitCredential::Load(CString key
)
495 if (m_strUrl
.IsEmpty())
496 cmd
.Format(_T("credential.%s"), key
);
498 cmd
.Format(_T("credential.%s.%s"), m_strUrl
, key
);
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
);
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
)
532 if (m_strUrl
.IsEmpty())
533 cmd
.Format(_T("credential.%s"), key
);
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
))
547 msg
.Format(IDS_PROC_SAVECONFIGFAILED
, cmd
, value
);
548 CMessageBox::Show(NULL
, msg
, _T("TortoiseGit"), MB_OK
| MB_ICONERROR
);
552 if (g_Git
.UnsetConfigValue(cmd
, configLevel
, CP_UTF8
))
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
)
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");
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
);
584 git_config_foreach_match(config
, "credential\\..*", GetCredentialAnyEntryCallback
, &list
);
585 for (size_t i
= 0; i
< list
.size(); ++i
)
588 CString prefix
= list
[i
].Tokenize(_T("\n"), pos
);
589 if (prefix
== _T("S") && !CAppUtils::IsAdminLogin())
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
)
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
))
612 msg
.Format(IDS_PROC_SAVECONFIGFAILED
, key
, _T(""));
613 CMessageBox::Show(NULL
, msg
, _T("TortoiseGit"), MB_OK
| MB_ICONERROR
);
619 g_Git
.m_IsUseGitDLL
= old
;
624 bool SaveSimpleCredential(int type
)
626 CONFIG_TYPE configLevel
;
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");
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
))
665 msg
.Format(IDS_PROC_SAVECONFIGFAILED
, _T("credential.helper"), value
);
666 CMessageBox::Show(NULL
, msg
, _T("TortoiseGit"), MB_OK
| MB_ICONERROR
);
669 g_Git
.m_IsUseGitDLL
= old
;
673 BOOL
CSettingGitCredential::OnApply()
678 int type
= m_ctrlSimpleCredential
.GetCurSel();
679 if (type
== SimpleCredentialType::SystemWincred
&& !CAppUtils::IsAdminLogin())
685 if (type
!= SimpleCredentialType::Advanced
)
687 if (!SaveSimpleCredential(type
))
690 int ret
= DeleteOtherKeys(type
);
696 return ISettingsPropPage::OnApply();
699 int sel
= m_ctrlConfigType
.GetCurSel();
700 if (sel
== ConfigType::System
&& !CAppUtils::IsAdminLogin())
707 if (m_ChangedMask
& CREDENTIAL_URL
)
710 if (m_strHelper
.IsEmpty())
712 CMessageBox::Show(NULL
, IDS_GITCREDENTIAL_HELPEREMPTY
, IDS_APPNAME
, MB_OK
| MB_ICONERROR
);
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");
724 if (!m_strUrl
.IsEmpty())
725 text
.Format(_T("%s:%s"), prefix
, m_strUrl
);
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(""));
748 return ISettingsPropPage::OnApply();
751 void CSettingGitCredential::OnBnClickedButtonRemove()
753 int index
= m_ctrlUrlList
.GetCurSel();
757 m_ctrlUrlList
.GetText(index
, str
);
759 msg
.Format(IDS_GITCREDENTIAL_DELETEHELPER
, str
);
760 if (CMessageBox::Show(NULL
, msg
, _T("TortoiseGit"), MB_YESNO
| MB_ICONQUESTION
) == IDYES
)
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
;
772 configLevel
= CONFIG_LOCAL
;
773 git_config_add_file_ondisk(config
, CGit::GetGitPathStringA(g_Git
.GetGitLocalConfig()), GIT_CONFIG_LEVEL_LOCAL
, FALSE
);
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
);
786 if (!CAppUtils::IsAdminLogin())
789 git_config_free(config
);
793 configLevel
= CONFIG_SYSTEM
;
794 git_config_add_file_ondisk(config
, CGit::GetGitPathStringA(g_Git
.GetGitSystemConfig()), GIT_CONFIG_LEVEL_SYSTEM
, FALSE
);
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();