1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2016 - 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 // SettingGitRemote.cpp : implementation file
23 #include "TortoiseProc.h"
24 #include "SettingGitRemote.h"
26 #include "GitAdminDir.h"
27 #include "MessageBox.h"
30 #include "HistoryCombo.h"
32 // CSettingGitRemote dialog
34 IMPLEMENT_DYNAMIC(CSettingGitRemote
, ISettingsPropPage
)
36 CSettingGitRemote::CSettingGitRemote()
37 : ISettingsPropPage(CSettingGitRemote::IDD
)
41 , m_bPushDefault(FALSE
)
46 CSettingGitRemote::~CSettingGitRemote()
50 void CSettingGitRemote::DoDataExchange(CDataExchange
* pDX
)
52 CPropertyPage::DoDataExchange(pDX
);
53 DDX_Control(pDX
, IDC_LIST_REMOTE
, m_ctrlRemoteList
);
54 DDX_Text(pDX
, IDC_EDIT_REMOTE
, m_strRemote
);
55 DDX_Text(pDX
, IDC_EDIT_URL
, m_strUrl
);
56 DDX_Text(pDX
, IDC_EDIT_PUSHURL
, m_strPushUrl
);
57 DDX_Text(pDX
, IDC_EDIT_PUTTY_KEY
, m_strPuttyKeyfile
);
58 DDX_Control(pDX
, IDC_COMBO_TAGOPT
, m_ctrlTagOpt
);
59 DDX_Check(pDX
, IDC_CHECK_PRUNE
, m_bPrune
);
60 DDX_Check(pDX
, IDC_CHECK_PRUNEALL
, m_bPruneAll
);
61 DDX_Check(pDX
, IDC_CHECK_PUSHDEFAULT
, m_bPushDefault
);
64 BEGIN_MESSAGE_MAP(CSettingGitRemote
, CPropertyPage
)
66 ON_BN_CLICKED(IDC_BUTTON_BROWSE
, &CSettingGitRemote::OnBnClickedButtonBrowse
)
67 ON_BN_CLICKED(IDC_BUTTON_ADD
, &CSettingGitRemote::OnBnClickedButtonAdd
)
68 ON_LBN_SELCHANGE(IDC_LIST_REMOTE
, &CSettingGitRemote::OnLbnSelchangeListRemote
)
69 ON_EN_CHANGE(IDC_EDIT_REMOTE
, &CSettingGitRemote::OnEnChangeEditRemote
)
70 ON_EN_CHANGE(IDC_EDIT_URL
, &CSettingGitRemote::OnEnChangeEditUrl
)
71 ON_EN_CHANGE(IDC_EDIT_PUSHURL
, &CSettingGitRemote::OnEnChangeEditPushUrl
)
72 ON_EN_CHANGE(IDC_EDIT_PUTTY_KEY
, &CSettingGitRemote::OnEnChangeEditPuttyKey
)
73 ON_CBN_SELCHANGE(IDC_COMBO_TAGOPT
, &CSettingGitRemote::OnCbnSelchangeComboTagOpt
)
74 ON_BN_CLICKED(IDC_CHECK_PRUNE
, &CSettingGitRemote::OnBnClickedCheckprune
)
75 ON_BN_CLICKED(IDC_CHECK_PRUNEALL
, &CSettingGitRemote::OnBnClickedCheckpruneall
)
76 ON_BN_CLICKED(IDC_CHECK_PUSHDEFAULT
, &CSettingGitRemote::OnBnClickedCheckpushdefault
)
77 ON_BN_CLICKED(IDC_BUTTON_REMOVE
, &CSettingGitRemote::OnBnClickedButtonRemove
)
78 ON_BN_CLICKED(IDC_BUTTON_RENAME_REMOTE
, &CSettingGitRemote::OnBnClickedButtonRenameRemote
)
81 static void ShowEditBalloon(HWND hDlg
, UINT nIdControl
, UINT nIdText
, UINT nIdTitle
, int nIcon
= TTI_WARNING
)
83 CString
text(MAKEINTRESOURCE(nIdText
));
84 CString
title(MAKEINTRESOURCE(nIdTitle
));
86 bt
.cbStruct
= sizeof(bt
);
90 SendDlgItemMessage(hDlg
, nIdControl
, EM_SHOWBALLOONTIP
, 0, (LPARAM
)&bt
);
93 #define TIMER_PREFILL 1
95 BOOL
CSettingGitRemote::OnInitDialog()
97 ISettingsPropPage::OnInitDialog();
99 AdjustControlSize(IDC_CHECK_PRUNE
);
100 AdjustControlSize(IDC_CHECK_PRUNEALL
);
101 AdjustControlSize(IDC_CHECK_PUSHDEFAULT
);
103 STRING_VECTOR remotes
;
104 g_Git
.GetRemoteList(remotes
);
105 m_ctrlRemoteList
.ResetContent();
106 for (size_t i
= 0; i
< remotes
.size(); i
++)
107 m_ctrlRemoteList
.AddString(remotes
[i
]);
109 m_ctrlTagOpt
.AddString(CString(MAKEINTRESOURCE(IDS_FETCH_REACHABLE
)));
110 m_ctrlTagOpt
.AddString(CString(MAKEINTRESOURCE(IDS_NONE
)));
111 m_ctrlTagOpt
.AddString(CString(MAKEINTRESOURCE(IDS_ALL
)));
112 m_ctrlTagOpt
.SetCurSel(0);
114 CString pruneAll
= g_Git
.GetConfigValue(_T("fetch.prune"));
115 m_bPruneAll
= pruneAll
== _T("true") ? TRUE
: FALSE
;
117 //this->GetDlgItem(IDC_EDIT_REMOTE)->EnableWindow(FALSE);
118 this->UpdateData(FALSE
);
120 SetTimer(TIMER_PREFILL
, 1000, nullptr);
123 // CSettingGitRemote message handlers
125 void CSettingGitRemote::OnTimer(UINT_PTR nIDEvent
)
127 if (nIDEvent
== TIMER_PREFILL
)
129 if (m_strRemote
.IsEmpty() && m_ctrlRemoteList
.GetCount() == 0)
131 ShowEditBalloon(m_hWnd
, IDC_EDIT_URL
, IDS_B_T_PREFILL_ORIGIN
, IDS_HINT
, TTI_INFO
);
134 KillTimer(TIMER_PREFILL
);
138 void CSettingGitRemote::OnBnClickedButtonBrowse()
140 CFileDialog
dlg(TRUE
, nullptr, nullptr, OFN_HIDEREADONLY
| OFN_OVERWRITEPROMPT
, CString(MAKEINTRESOURCE(IDS_PUTTYKEYFILEFILTER
)));
143 INT_PTR ret
= dlg
.DoModal();
144 SetCurrentDirectory(g_Git
.m_CurrentDir
);
147 this->m_strPuttyKeyfile
= dlg
.GetPathName();
148 this->UpdateData(FALSE
);
149 OnEnChangeEditPuttyKey();
153 void CSettingGitRemote::OnBnClickedButtonAdd()
157 if(m_strRemote
.IsEmpty())
159 CMessageBox::Show(nullptr, IDS_PROC_GITCONFIG_REMOTEEMPTY
, IDS_APPNAME
, MB_OK
| MB_ICONERROR
);
162 if(m_strUrl
.IsEmpty())
164 CMessageBox::Show(nullptr, IDS_PROC_GITCONFIG_URLEMPTY
, IDS_APPNAME
, MB_OK
| MB_ICONERROR
);
168 m_ChangedMask
= REMOTE_NAME
| REMOTE_URL
| REMOTE_PUTTYKEY
| REMOTE_TAGOPT
| REMOTE_PRUNE
| REMOTE_PRUNEALL
| REMOTE_PUSHDEFAULT
| REMOTE_PUSHURL
;
169 if(IsRemoteExist(m_strRemote
))
172 msg
.Format(IDS_PROC_GITCONFIG_OVERWRITEREMOTE
, (LPCTSTR
)m_strRemote
);
173 if (CMessageBox::Show(nullptr, msg
, _T("TortoiseGit"), MB_YESNO
| MB_ICONQUESTION
| MB_DEFBUTTON2
) == IDYES
)
174 m_ChangedMask
&= ~REMOTE_NAME
;
182 BOOL
CSettingGitRemote::IsRemoteExist(CString
&remote
)
185 for(int i
=0;i
<m_ctrlRemoteList
.GetCount();i
++)
187 m_ctrlRemoteList
.GetText(i
,str
);
194 struct CheckRefspecStruct
200 static int CheckRemoteCollideWithRefspec(const git_config_entry
*entry
, void * payload
)
202 auto crs
= (CheckRefspecStruct
*)payload
;
204 if (entry
->name
== "remote." + crs
->remote
+ ".fetch")
206 CStringA value
= CStringA(entry
->value
);
207 CStringA match
= ":refs/remotes/" + crs
->remote
;
208 int pos
= value
.Find(match
);
211 if ((pos
+ match
.GetLength() == value
.GetLength()) || (value
[pos
+ match
.GetLength()] == '/'))
219 bool CSettingGitRemote::IsRemoteCollideWithRefspec(CString remote
)
221 CheckRefspecStruct crs
= { CUnicodeUtils::GetUTF8(remote
), false };
222 CAutoConfig
config(true);
223 git_config_add_file_ondisk(config
, CGit::GetGitPathStringA(g_Git
.GetGitLocalConfig()), GIT_CONFIG_LEVEL_LOCAL
, FALSE
);
224 for (const auto pattern
: { "remote\\..*\\.fetch", "svn-remote\\..*\\.fetch", "svn-remote\\..*\\.branches", "svn-remote\\..*\\.tags" })
226 git_config_foreach_match(config
, pattern
, CheckRemoteCollideWithRefspec
, &crs
);
233 void CSettingGitRemote::OnLbnSelchangeListRemote()
239 if (CMessageBox::Show(GetSafeHwnd(), IDS_PROC_GITCONFIG_SAVEREMOTE
, IDS_APPNAME
, 1, IDI_QUESTION
, IDS_SAVEBUTTON
, IDS_DISCARDBUTTON
) == 1)
246 index
= this->m_ctrlRemoteList
.GetCurSel();
250 m_strPushUrl
.Empty();
252 m_strPuttyKeyfile
.Empty();
253 this->UpdateData(FALSE
);
257 m_ctrlRemoteList
.GetText(index
,remote
);
258 this->m_strRemote
=remote
;
260 cmd
.Format(_T("remote.%s.url"), (LPCTSTR
)remote
);
262 m_strUrl
= g_Git
.GetConfigValue(cmd
);
264 cmd
.Format(_T("remote.%s.pushurl"), (LPCTSTR
)remote
);
265 m_strPushUrl
.Empty();
266 m_strPushUrl
= g_Git
.GetConfigValue(cmd
);
268 cmd
.Format(_T("remote.%s.puttykeyfile"), (LPCTSTR
)remote
);
270 this->m_strPuttyKeyfile
= g_Git
.GetConfigValue(cmd
);
275 cmd
.Format(_T("remote.%s.tagopt"), (LPCTSTR
)remote
);
276 CString tagopt
= g_Git
.GetConfigValue(cmd
);
278 if (tagopt
== "--no-tags")
280 else if (tagopt
== "--tags")
282 m_ctrlTagOpt
.SetCurSel(index
);
284 CString pushDefault
= g_Git
.GetConfigValue(_T("remote.pushdefault"));
285 m_bPushDefault
= pushDefault
== remote
? TRUE
: FALSE
;
286 cmd
.Format(_T("remote.%s.prune"), (LPCTSTR
)remote
);
287 CString prune
= g_Git
.GetConfigValue(cmd
);
288 m_bPrune
= prune
== _T("true") ? TRUE
: prune
== _T("false") ? FALSE
: 2;
289 CString pruneAll
= g_Git
.GetConfigValue(_T("fetch.prune"));
290 m_bPruneAll
= pruneAll
== _T("true") ? TRUE
: FALSE
;
292 GetDlgItem(IDC_BUTTON_ADD
)->EnableWindow(TRUE
);
293 GetDlgItem(IDC_BUTTON_REMOVE
)->EnableWindow(TRUE
);
294 GetDlgItem(IDC_BUTTON_RENAME_REMOTE
)->EnableWindow(TRUE
);
295 this->UpdateData(FALSE
);
298 void CSettingGitRemote::OnEnChangeEditRemote()
300 m_ChangedMask
|=REMOTE_NAME
;
304 if (IsRemoteCollideWithRefspec(m_strRemote
))
305 ShowEditBalloon(m_hWnd
, IDC_EDIT_REMOTE
, IDS_B_T_REMOTE_NAME_COLLIDE
, IDS_HINT
, TTI_WARNING
);
306 if( (!this->m_strRemote
.IsEmpty())&&(!this->m_strUrl
.IsEmpty()) )
310 void CSettingGitRemote::OnEnChangeEditUrl()
312 m_ChangedMask
|=REMOTE_URL
;
316 if (m_strRemote
.IsEmpty() && !m_strUrl
.IsEmpty() && m_ctrlRemoteList
.GetCount() == 0)
318 GetDlgItem(IDC_EDIT_REMOTE
)->SetWindowText(_T("origin"));
319 OnEnChangeEditRemote();
322 if( (!this->m_strRemote
.IsEmpty())&&(!this->m_strUrl
.IsEmpty()) )
326 void CSettingGitRemote::OnEnChangeEditPushUrl()
328 m_ChangedMask
|= REMOTE_PUSHURL
;
332 if (!this->m_strRemote
.IsEmpty())
336 void CSettingGitRemote::OnEnChangeEditPuttyKey()
338 m_ChangedMask
|=REMOTE_PUTTYKEY
;
341 if (!this->m_strUrl
.IsEmpty())
345 void CSettingGitRemote::OnCbnSelchangeComboTagOpt()
347 m_ChangedMask
|= REMOTE_TAGOPT
;
353 void CSettingGitRemote::OnBnClickedCheckprune()
355 m_ChangedMask
|= REMOTE_PRUNE
;
361 void CSettingGitRemote::OnBnClickedCheckpruneall()
363 m_ChangedMask
|= REMOTE_PRUNEALL
;
368 void CSettingGitRemote::OnBnClickedCheckpushdefault()
370 m_ChangedMask
|= REMOTE_PUSHDEFAULT
;
375 BOOL
CSettingGitRemote::Save(CString key
,CString value
)
379 cmd
.Format(_T("remote.%s.%s"), (LPCTSTR
)m_strRemote
, (LPCTSTR
)key
);
382 // don't check result code. it fails if the entry not exist
383 g_Git
.UnsetConfigValue(cmd
, CONFIG_LOCAL
);
384 if (!g_Git
.GetConfigValue(cmd
).IsEmpty())
387 msg
.Format(IDS_PROC_SAVECONFIGFAILED
, (LPCTSTR
)cmd
, (LPCTSTR
)value
);
388 CMessageBox::Show(nullptr, msg
, _T("TortoiseGit"), MB_OK
| MB_ICONERROR
);
394 if (g_Git
.SetConfigValue(cmd
, value
, CONFIG_LOCAL
))
397 msg
.Format(IDS_PROC_SAVECONFIGFAILED
, (LPCTSTR
)cmd
, (LPCTSTR
)value
);
398 CMessageBox::Show(nullptr, msg
, _T("TortoiseGit"), MB_OK
| MB_ICONERROR
);
404 BOOL
CSettingGitRemote::SaveGeneral(CString key
, CString value
)
408 // don't check result code. it fails if the entry not exist
409 g_Git
.UnsetConfigValue(key
, CONFIG_LOCAL
);
410 if (!g_Git
.GetConfigValue(key
).IsEmpty())
413 msg
.Format(IDS_PROC_SAVECONFIGFAILED
, (LPCTSTR
)key
, (LPCTSTR
)value
);
414 CMessageBox::Show(nullptr, msg
, _T("TortoiseGit"), MB_OK
| MB_ICONERROR
);
420 if (g_Git
.SetConfigValue(key
, value
, CONFIG_LOCAL
))
423 msg
.Format(IDS_PROC_SAVECONFIGFAILED
, (LPCTSTR
)key
, (LPCTSTR
)value
);
424 CMessageBox::Show(nullptr, msg
, _T("TortoiseGit"), MB_OK
| MB_ICONERROR
);
431 BOOL
CSettingGitRemote::OnApply()
436 if (m_ChangedMask
& REMOTE_PUSHDEFAULT
)
438 if (!m_strRemote
.Trim().IsEmpty() && m_bPushDefault
)
440 if (!SaveGeneral(_T("remote.pushdefault"), m_strRemote
.Trim()))
445 if (!SaveGeneral(_T("remote.pushdefault"), _T("")))
449 m_ChangedMask
&= ~REMOTE_PUSHDEFAULT
;
452 if (m_ChangedMask
& REMOTE_PRUNEALL
)
454 if (!SaveGeneral(_T("fetch.prune"), m_bPruneAll
== TRUE
? _T("true") : _T("")))
456 m_ChangedMask
&= ~REMOTE_PRUNEALL
;
459 if (m_ChangedMask
&& m_strRemote
.Trim().IsEmpty())
461 CMessageBox::Show(nullptr, IDS_PROC_GITCONFIG_REMOTEEMPTY
, IDS_APPNAME
, MB_OK
| MB_ICONERROR
);
465 if(m_ChangedMask
& REMOTE_NAME
)
468 if(m_strRemote
.IsEmpty())
470 CMessageBox::Show(nullptr, IDS_PROC_GITCONFIG_REMOTEEMPTY
, IDS_APPNAME
, MB_OK
| MB_ICONERROR
);
473 if(m_strUrl
.IsEmpty())
475 CMessageBox::Show(nullptr, IDS_PROC_GITCONFIG_URLEMPTY
, IDS_APPNAME
, MB_OK
| MB_ICONERROR
);
479 if (m_ctrlRemoteList
.GetCount() > 0)
481 // tagopt not --no-tags
482 if (m_ctrlTagOpt
.GetCurSel() != 1)
484 if (CMessageBox::ShowCheck(GetSafeHwnd(), IDS_PROC_GITCONFIG_ASKTAGOPT
, IDS_APPNAME
, MB_YESNO
| MB_ICONQUESTION
, _T("TagOptNoTagsWarning"), IDS_MSGBOX_DONOTSHOWAGAIN
) == IDYES
)
486 m_ctrlTagOpt
.SetCurSel(1);
487 m_ChangedMask
|= REMOTE_TAGOPT
;
492 m_strUrl
.Replace(L
'\\', L
'/');
494 cmd
.Format(_T("git.exe remote add \"%s\" \"%s\""), (LPCTSTR
)m_strRemote
, (LPCTSTR
)m_strUrl
);
495 if (g_Git
.Run(cmd
, &out
, CP_UTF8
))
497 CMessageBox::Show(nullptr, out
, _T("TorotiseGit"), MB_OK
| MB_ICONERROR
);
500 m_ChangedMask
&= ~REMOTE_URL
;
502 m_ctrlRemoteList
.SetCurSel(m_ctrlRemoteList
.AddString(m_strRemote
));
503 GetDlgItem(IDC_BUTTON_ADD
)->EnableWindow(TRUE
);
504 GetDlgItem(IDC_BUTTON_RENAME_REMOTE
)->EnableWindow(TRUE
);
505 if (!m_bNoFetch
&& CMessageBox::Show(nullptr, IDS_SETTINGS_FETCH_ADDEDREMOTE
, IDS_APPNAME
, MB_ICONQUESTION
| MB_YESNO
) == IDYES
)
506 CCommonAppUtils::RunTortoiseGitProc(_T("/command:fetch /path:\"") + g_Git
.m_CurrentDir
+ _T("\" /remote:\"") + m_strRemote
+ _T("\""));
508 if(m_ChangedMask
& REMOTE_URL
)
510 m_strUrl
.Replace(L
'\\', L
'/');
511 if (!Save(_T("url"),this->m_strUrl
))
515 if(m_ChangedMask
& REMOTE_PUTTYKEY
)
517 if (!Save(_T("puttykeyfile"),this->m_strPuttyKeyfile
))
521 if (m_ChangedMask
& REMOTE_TAGOPT
)
524 int index
= m_ctrlTagOpt
.GetCurSel();
526 tagopt
= "--no-tags";
529 if (!Save(_T("tagopt"), tagopt
))
533 if (m_ChangedMask
& REMOTE_PRUNE
)
535 if (!Save(_T("prune"), m_bPrune
== TRUE
? _T("true") : m_bPrune
== FALSE
? _T("false") : _T("")))
539 if (m_ChangedMask
& REMOTE_PUSHURL
)
541 m_strPushUrl
.Replace(L
'\\', L
'/');
542 if (!Save(_T("pushurl"), m_strPushUrl
))
549 return ISettingsPropPage::OnApply();
552 void CleanupSyncRemotes(const CString
& remote
)
554 CString workingDir
= g_Git
.m_CurrentDir
;
555 workingDir
.Replace(_T(':'), _T('_'));
556 CHistoryCombo::RemoveEntryFromHistory(_T("Software\\TortoiseGit\\History\\SyncURL\\") + workingDir
, _T("url"), remote
);
559 void CSettingGitRemote::OnBnClickedButtonRemove()
562 index
=m_ctrlRemoteList
.GetCurSel();
566 m_ctrlRemoteList
.GetText(index
,str
);
568 msg
.Format(IDS_WARN_REMOVE
, (LPCTSTR
)str
);
569 if (CMessageBox::Show(nullptr, msg
, _T("TortoiseGit"), MB_YESNO
| MB_ICONQUESTION
) == IDYES
)
572 cmd
.Format(_T("git.exe remote rm %s"), (LPCTSTR
)str
);
573 if (g_Git
.Run(cmd
, &out
, CP_UTF8
))
575 CMessageBox::Show(nullptr, out
,_T("TortoiseGit"), MB_OK
| MB_ICONERROR
);
579 CleanupSyncRemotes(str
);
580 m_ctrlRemoteList
.DeleteString(index
);
581 OnLbnSelchangeListRemote();
586 void CSettingGitRemote::OnBnClickedButtonRenameRemote()
588 int sel
= m_ctrlRemoteList
.GetCurSel();
591 CString oldRemote
, newRemote
;
592 m_ctrlRemoteList
.GetText(sel
, oldRemote
);
593 GetDlgItem(IDC_EDIT_REMOTE
)->GetWindowText(newRemote
);
595 cmd
.Format(_T("git.exe remote rename %s %s"), (LPCTSTR
)oldRemote
, (LPCTSTR
)newRemote
);
596 if (g_Git
.Run(cmd
, &out
, CP_UTF8
))
598 CMessageBox::Show(nullptr, out
, _T("TortoiseGit"), MB_OK
| MB_ICONERROR
);
602 CleanupSyncRemotes(oldRemote
);
603 m_ctrlRemoteList
.DeleteString(sel
);
604 m_ctrlRemoteList
.SetCurSel(m_ctrlRemoteList
.AddString(newRemote
));
605 m_ChangedMask
&= ~REMOTE_NAME
;
607 this->SetModified(FALSE
);