1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2012 - 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"
31 // CSettingGitRemote dialog
33 IMPLEMENT_DYNAMIC(CSettingGitRemote
, ISettingsPropPage
)
35 CSettingGitRemote::CSettingGitRemote(CString cmdPath
)
36 : ISettingsPropPage(CSettingGitRemote::IDD
)
39 , m_strPuttyKeyfile(_T(""))
47 CSettingGitRemote::~CSettingGitRemote()
51 void CSettingGitRemote::DoDataExchange(CDataExchange
* pDX
)
53 CPropertyPage::DoDataExchange(pDX
);
54 DDX_Control(pDX
, IDC_LIST_REMOTE
, m_ctrlRemoteList
);
55 DDX_Text(pDX
, IDC_EDIT_REMOTE
, m_strRemote
);
56 DDX_Text(pDX
, IDC_EDIT_URL
, m_strUrl
);
57 DDX_Text(pDX
, IDC_EDIT_PUTTY_KEY
, m_strPuttyKeyfile
);
58 DDX_Control(pDX
, IDC_COMBO_TAGOPT
, m_ctrlTagOpt
);
62 BEGIN_MESSAGE_MAP(CSettingGitRemote
, CPropertyPage
)
63 ON_BN_CLICKED(IDC_BUTTON_BROWSE
, &CSettingGitRemote::OnBnClickedButtonBrowse
)
64 ON_BN_CLICKED(IDC_BUTTON_ADD
, &CSettingGitRemote::OnBnClickedButtonAdd
)
65 ON_LBN_SELCHANGE(IDC_LIST_REMOTE
, &CSettingGitRemote::OnLbnSelchangeListRemote
)
66 ON_EN_CHANGE(IDC_EDIT_REMOTE
, &CSettingGitRemote::OnEnChangeEditRemote
)
67 ON_EN_CHANGE(IDC_EDIT_URL
, &CSettingGitRemote::OnEnChangeEditUrl
)
68 ON_EN_CHANGE(IDC_EDIT_PUTTY_KEY
, &CSettingGitRemote::OnEnChangeEditPuttyKey
)
69 ON_CBN_SELCHANGE(IDC_COMBO_TAGOPT
, &CSettingGitRemote::OnCbnSelchangeComboTagOpt
)
70 ON_BN_CLICKED(IDC_BUTTON_REMOVE
, &CSettingGitRemote::OnBnClickedButtonRemove
)
73 BOOL
CSettingGitRemote::OnInitDialog()
75 ISettingsPropPage::OnInitDialog();
77 //CString str=((CSettings*)GetParent())->m_CmdPath.GetWinPath();
79 if( g_GitAdminDir
.HasAdminDir(m_cmdPath
,&proj
) )
82 this->GetWindowText(title
);
83 this->SetWindowText(title
+ _T(" - ") + proj
);
86 STRING_VECTOR remotes
;
87 g_Git
.GetRemoteList(remotes
);
88 m_ctrlRemoteList
.ResetContent();
89 for (size_t i
= 0; i
< remotes
.size(); i
++)
90 m_ctrlRemoteList
.AddString(remotes
[i
]);
92 m_ctrlTagOpt
.AddString(CString(MAKEINTRESOURCE(IDS_FETCH_REACHABLE
)));
93 m_ctrlTagOpt
.AddString(CString(MAKEINTRESOURCE(IDS_NONE
)));
94 m_ctrlTagOpt
.AddString(CString(MAKEINTRESOURCE(IDS_FETCH_TAGS_ONLY
)));
95 m_ctrlTagOpt
.SetCurSel(0);
97 //this->GetDlgItem(IDC_EDIT_REMOTE)->EnableWindow(FALSE);
98 this->UpdateData(FALSE
);
101 // CSettingGitRemote message handlers
103 void CSettingGitRemote::OnBnClickedButtonBrowse()
105 CFileDialog
dlg(TRUE
,NULL
,
107 OFN_HIDEREADONLY
| OFN_OVERWRITEPROMPT
,
108 CString(MAKEINTRESOURCE(IDS_PUTTYKEYFILEFILTER
)));
111 INT_PTR ret
= dlg
.DoModal();
112 SetCurrentDirectory(g_Git
.m_CurrentDir
);
115 this->m_strPuttyKeyfile
= dlg
.GetPathName();
116 this->UpdateData(FALSE
);
117 OnEnChangeEditPuttyKey();
121 void CSettingGitRemote::OnBnClickedButtonAdd()
125 if(m_strRemote
.IsEmpty())
127 CMessageBox::Show(NULL
, IDS_PROC_GITCONFIG_REMOTEEMPTY
, IDS_APPNAME
, MB_OK
| MB_ICONERROR
);
130 if(m_strUrl
.IsEmpty())
132 CMessageBox::Show(NULL
, IDS_PROC_GITCONFIG_URLEMPTY
, IDS_APPNAME
, MB_OK
| MB_ICONERROR
);
136 m_ChangedMask
= REMOTE_NAME
| REMOTE_URL
| REMOTE_PUTTYKEY
| REMOTE_TAGOPT
;
137 if(IsRemoteExist(m_strRemote
))
140 msg
.Format(IDS_PROC_GITCONFIG_OVERWRITEREMOTE
, m_strRemote
);
141 if(CMessageBox::Show(NULL
, msg
, _T("TortoiseGit"), MB_YESNO
| MB_ICONQUESTION
| MB_DEFBUTTON2
) == IDYES
)
143 m_ChangedMask
&= ~REMOTE_NAME
;
152 BOOL
CSettingGitRemote::IsRemoteExist(CString
&remote
)
155 for(int i
=0;i
<m_ctrlRemoteList
.GetCount();i
++)
157 m_ctrlRemoteList
.GetText(i
,str
);
166 void CSettingGitRemote::OnLbnSelchangeListRemote()
172 if(CMessageBox::Show(NULL
, IDS_PROC_GITCONFIG_SAVEREMOTE
, IDS_APPNAME
, 1, IDI_QUESTION
, IDS_SAVEBUTTON
, IDS_DISCARDBUTTON
) == 1)
179 index
= this->m_ctrlRemoteList
.GetCurSel();
184 m_strPuttyKeyfile
.Empty();
185 this->UpdateData(FALSE
);
189 m_ctrlRemoteList
.GetText(index
,remote
);
190 this->m_strRemote
=remote
;
192 cmd
.Format(_T("remote.%s.url"),remote
);
194 m_strUrl
= g_Git
.GetConfigValue(cmd
, CP_UTF8
);
196 cmd
.Format(_T("remote.%s.puttykeyfile"),remote
);
198 this->m_strPuttyKeyfile
= g_Git
.GetConfigValue(cmd
, CP_UTF8
);
203 cmd
.Format(_T("remote.%s.tagopt"), remote
);
204 CString tagopt
= g_Git
.GetConfigValue(cmd
, CP_UTF8
);
206 if (tagopt
== "--no-tags")
208 else if (tagopt
== "--tags")
210 m_ctrlTagOpt
.SetCurSel(index
);
212 GetDlgItem(IDC_BUTTON_ADD
)->EnableWindow(TRUE
);
213 GetDlgItem(IDC_BUTTON_REMOVE
)->EnableWindow(TRUE
);
214 this->UpdateData(FALSE
);
218 void CSettingGitRemote::OnEnChangeEditRemote()
220 m_ChangedMask
|=REMOTE_NAME
;
223 if( (!this->m_strRemote
.IsEmpty())&&(!this->m_strUrl
.IsEmpty()) )
226 this->SetModified(0);
229 void CSettingGitRemote::OnEnChangeEditUrl()
231 m_ChangedMask
|=REMOTE_URL
;
234 if( (!this->m_strRemote
.IsEmpty())&&(!this->m_strUrl
.IsEmpty()) )
237 this->SetModified(0);
240 void CSettingGitRemote::OnEnChangeEditPuttyKey()
242 m_ChangedMask
|=REMOTE_PUTTYKEY
;
245 if( (!this->m_strRemote
.IsEmpty())&&(!this->m_strUrl
.IsEmpty()) )
248 this->SetModified(0);
251 void CSettingGitRemote::OnCbnSelchangeComboTagOpt()
253 m_ChangedMask
|= REMOTE_TAGOPT
;
256 if (this->m_ctrlTagOpt
.GetCurSel() > 0)
259 this->SetModified(0);
262 void CSettingGitRemote::Save(CString key
,CString value
)
266 cmd
.Format(_T("remote.%s.%s"),this->m_strRemote
,key
);
269 // don't check result code. it fails if the entry not exist
270 g_Git
.UnsetConfigValue(cmd
, CONFIG_LOCAL
, CP_UTF8
, &g_Git
.m_CurrentDir
);
271 if (!g_Git
.GetConfigValue(cmd
).IsEmpty())
274 msg
.Format(IDS_PROC_SAVECONFIGFAILED
, cmd
, value
);
275 CMessageBox::Show(NULL
, msg
, _T("TortoiseGit"), MB_OK
| MB_ICONERROR
);
280 if (g_Git
.SetConfigValue(cmd
, value
, CONFIG_LOCAL
, CP_UTF8
, &g_Git
.m_CurrentDir
))
283 msg
.Format(IDS_PROC_SAVECONFIGFAILED
, cmd
, value
);
284 CMessageBox::Show(NULL
, msg
, _T("TortoiseGit"), MB_OK
| MB_ICONERROR
);
287 BOOL
CSettingGitRemote::OnApply()
291 if(m_ChangedMask
& REMOTE_NAME
)
294 if(m_strRemote
.IsEmpty())
296 CMessageBox::Show(NULL
, IDS_PROC_GITCONFIG_REMOTEEMPTY
, IDS_APPNAME
, MB_OK
| MB_ICONERROR
);
299 if(m_strUrl
.IsEmpty())
301 CMessageBox::Show(NULL
, IDS_PROC_GITCONFIG_URLEMPTY
, IDS_APPNAME
, MB_OK
| MB_ICONERROR
);
304 m_strUrl
.Replace(L
'\\', L
'/');
306 cmd
.Format(_T("git.exe remote add \"%s\" \"%s\""),m_strRemote
,m_strUrl
);
307 if (g_Git
.Run(cmd
, &out
, CP_UTF8
))
309 CMessageBox::Show(NULL
,out
,_T("TorotiseGit"),MB_OK
|MB_ICONERROR
);
312 m_ChangedMask
&= ~REMOTE_URL
;
314 this->m_ctrlRemoteList
.AddString(m_strRemote
);
315 GetDlgItem(IDC_BUTTON_ADD
)->EnableWindow(TRUE
);
316 if (!m_bNoFetch
&& CMessageBox::Show(NULL
, IDS_SETTINGS_FETCH_ADDEDREMOTE
, IDS_APPNAME
, MB_ICONQUESTION
| MB_YESNO
) == IDYES
)
317 CCommonAppUtils::RunTortoiseGitProc(_T("/command:fetch /path:\"") + g_Git
.m_CurrentDir
+ _T("\" /remote:\"") + m_strRemote
+ _T("\""));
319 if(m_ChangedMask
& REMOTE_URL
)
321 m_strUrl
.Replace(L
'\\', L
'/');
322 Save(_T("url"),this->m_strUrl
);
325 if(m_ChangedMask
& REMOTE_PUTTYKEY
)
327 Save(_T("puttykeyfile"),this->m_strPuttyKeyfile
);
330 if (m_ChangedMask
& REMOTE_TAGOPT
)
333 int index
= m_ctrlTagOpt
.GetCurSel();
335 tagopt
= "--no-tags";
338 Save(_T("tagopt"), tagopt
);
344 return ISettingsPropPage::OnApply();
346 void CSettingGitRemote::OnBnClickedButtonRemove()
349 index
=m_ctrlRemoteList
.GetCurSel();
353 m_ctrlRemoteList
.GetText(index
,str
);
355 msg
.Format(IDS_PROC_GITCONFIG_DELETEREMOTE
, str
);
356 if(CMessageBox::Show(NULL
, msg
, _T("TortoiseGit"), MB_YESNO
| MB_ICONQUESTION
) == IDYES
)
359 cmd
.Format(_T("git.exe remote rm %s"),str
);
360 if (g_Git
.Run(cmd
, &out
, CP_UTF8
))
362 CMessageBox::Show(NULL
, out
,_T("TortoiseGit"),MB_OK
|MB_ICONERROR
);
366 m_ctrlRemoteList
.DeleteString(index
);
367 OnLbnSelchangeListRemote();