PushDlg: Allow users to store pushremote and pushbranch
[TortoiseGit.git] / src / TortoiseProc / PushDlg.cpp
blobe8ec271b6e4f7a6c583b5872ea9df0a02b4794b4
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.
20 // PushDlg.cpp : implementation file
23 #include "stdafx.h"
24 #include "TortoiseProc.h"
25 #include "PushDlg.h"
27 #include "Git.h"
28 #include "registry.h"
29 #include "AppUtils.h"
30 #include "LogDlg.h"
31 #include "BrowseRefsDlg.h"
32 #include "RefLogDlg.h"
33 #include "MessageBox.h"
35 // CPushDlg dialog
37 IMPLEMENT_DYNAMIC(CPushDlg, CHorizontalResizableStandAloneDialog)
39 CPushDlg::CPushDlg(CWnd* pParent /*=NULL*/)
40 : CHorizontalResizableStandAloneDialog(CPushDlg::IDD, pParent)
41 , m_bPushAllBranches(FALSE)
42 , m_bForce(FALSE)
43 , m_bPack(FALSE)
44 , m_bTags(FALSE)
45 , m_bAutoLoad(FALSE)
46 , m_bPushAllRemotes(FALSE)
48 m_bAutoLoad = CAppUtils::IsSSHPutty();
51 CPushDlg::~CPushDlg()
55 void CPushDlg::DoDataExchange(CDataExchange* pDX)
57 CHorizontalResizableStandAloneDialog::DoDataExchange(pDX);
58 DDX_Control(pDX, IDC_BRANCH_REMOTE, this->m_BranchRemote);
59 DDX_Control(pDX, IDC_BRANCH_SOURCE, this->m_BranchSource);
60 DDX_Control(pDX, IDC_REMOTE, this->m_Remote);
61 DDX_Control(pDX, IDC_URL, this->m_RemoteURL);
62 DDX_Control(pDX, IDC_BUTTON_BROWSE_SOURCE_BRANCH, m_BrowseLocalRef);
63 DDX_Check(pDX,IDC_FORCE,this->m_bForce);
64 DDX_Check(pDX,IDC_PUSHALL,this->m_bPushAllBranches);
65 DDX_Check(pDX,IDC_PACK,this->m_bPack);
66 DDX_Check(pDX,IDC_TAGS,this->m_bTags);
67 DDX_Check(pDX,IDC_PUTTYKEY_AUTOLOAD,this->m_bAutoLoad);
68 DDX_Check(pDX, IDC_PROC_PUSH_SET_PUSHREMOTE, m_bSetPushRemote);
69 DDX_Check(pDX, IDC_PROC_PUSH_SET_PUSHBRANCH, m_bSetPushBranch);
72 BEGIN_MESSAGE_MAP(CPushDlg, CHorizontalResizableStandAloneDialog)
73 ON_BN_CLICKED(IDC_RD_REMOTE, &CPushDlg::OnBnClickedRd)
74 ON_BN_CLICKED(IDC_RD_URL, &CPushDlg::OnBnClickedRd)
75 ON_CBN_SELCHANGE(IDC_BRANCH_SOURCE, &CPushDlg::OnCbnSelchangeBranchSource)
76 ON_CBN_SELCHANGE(IDC_REMOTE, &CPushDlg::EnDisablePushRemoteArchiveBranch)
77 ON_BN_CLICKED(IDOK, &CPushDlg::OnBnClickedOk)
78 ON_BN_CLICKED(IDC_REMOTE_MANAGE, &CPushDlg::OnBnClickedRemoteManage)
79 ON_BN_CLICKED(IDC_BUTTON_BROWSE_SOURCE_BRANCH, &CPushDlg::OnBnClickedButtonBrowseSourceBranch)
80 ON_BN_CLICKED(IDC_BUTTON_BROWSE_DEST_BRANCH, &CPushDlg::OnBnClickedButtonBrowseDestBranch)
81 ON_BN_CLICKED(IDC_PUSHALL, &CPushDlg::OnBnClickedPushall)
82 END_MESSAGE_MAP()
84 BOOL CPushDlg::OnInitDialog()
86 CHorizontalResizableStandAloneDialog::OnInitDialog();
87 CAppUtils::MarkWindowAsUnpinnable(m_hWnd);
89 AddAnchor(IDOK,BOTTOM_RIGHT);
90 AddAnchor(IDCANCEL,BOTTOM_RIGHT);
91 AddAnchor(IDC_BRANCH_GROUP, TOP_LEFT,TOP_RIGHT);
92 AddAnchor(IDC_STATIC_REMOTE, TOP_LEFT);
93 AddAnchor(IDC_STATIC_SOURCE, TOP_LEFT);
95 AddAnchor(IDC_PUSHALL, TOP_LEFT);
96 AddAnchor(IDC_BRANCH_REMOTE, TOP_LEFT, TOP_RIGHT);
97 AddAnchor(IDC_BUTTON_BROWSE_DEST_BRANCH, TOP_RIGHT);
98 AddAnchor(IDC_BRANCH_SOURCE, TOP_LEFT, TOP_RIGHT);
99 AddAnchor(IDC_BUTTON_BROWSE_SOURCE_BRANCH, TOP_RIGHT);
101 AddAnchor(IDC_URL_GROUP, TOP_LEFT,TOP_RIGHT);
102 AddAnchor(IDC_RD_REMOTE, TOP_LEFT);
103 AddAnchor(IDC_RD_URL, TOP_LEFT);
105 AddAnchor(IDC_REMOTE, TOP_LEFT, TOP_RIGHT);
107 AddAnchor(IDC_URL, TOP_LEFT,TOP_RIGHT);
109 AddAnchor(IDC_OPTION_GROUP, TOP_LEFT,TOP_RIGHT);
111 AddAnchor(IDC_FORCE, TOP_LEFT);
112 AddAnchor(IDC_PACK, TOP_LEFT);
113 AddAnchor(IDC_TAGS, TOP_LEFT);
114 AddAnchor(IDC_PUTTYKEY_AUTOLOAD,TOP_LEFT);
115 AddAnchor(IDC_PROC_PUSH_SET_PUSHBRANCH, TOP_LEFT);
116 AddAnchor(IDC_PROC_PUSH_SET_PUSHREMOTE, TOP_LEFT);
118 AddAnchor(IDC_REMOTE_MANAGE,TOP_RIGHT);
119 AddAnchor(IDHELP, BOTTOM_RIGHT);
121 AddOthersToAnchor();
123 AdjustControlSize(IDC_RD_REMOTE);
124 AdjustControlSize(IDC_RD_URL);
125 AdjustControlSize(IDC_PUSHALL);
126 AdjustControlSize(IDC_FORCE);
127 AdjustControlSize(IDC_PACK);
128 AdjustControlSize(IDC_TAGS);
129 AdjustControlSize(IDC_PUTTYKEY_AUTOLOAD);
130 AdjustControlSize(IDC_PROC_PUSH_SET_PUSHBRANCH);
131 AdjustControlSize(IDC_PROC_PUSH_SET_PUSHREMOTE);
133 CString sWindowTitle;
134 GetWindowText(sWindowTitle);
135 CAppUtils::SetWindowTitle(m_hWnd, g_Git.m_CurrentDir, sWindowTitle);
137 this->GetDlgItem(IDC_PUTTYKEY_AUTOLOAD)->EnableWindow(CAppUtils::IsSSHPutty());
139 EnableSaveRestore(_T("PushDlg"));
141 m_RemoteURL.SetURLHistory(TRUE);
143 CString WorkingDir=g_Git.m_CurrentDir;
144 WorkingDir.Replace(_T(':'),_T('_'));
145 m_RemoteURL.LoadHistory(CString(_T("Software\\TortoiseGit\\History\\PushURLS\\"))+WorkingDir, _T("url"));
146 CString clippath=CAppUtils::GetClipboardLink();
147 if(clippath.IsEmpty())
148 m_RemoteURL.SetCurSel(0);
149 else
150 m_RemoteURL.SetWindowText(clippath);
152 m_RemoteURL.EnableWindow(FALSE);
153 CheckRadioButton(IDC_RD_REMOTE,IDC_RD_URL,IDC_RD_REMOTE);
156 this->m_regAutoLoad = CRegDWORD(CString(_T("Software\\TortoiseGit\\History\\PushDlgAutoLoad\\"))+WorkingDir,
157 m_bAutoLoad);
158 m_bAutoLoad = this->m_regAutoLoad;
159 if(!CAppUtils::IsSSHPutty())
160 m_bAutoLoad = false;
162 m_BrowseLocalRef.m_bRightArrow = TRUE;
163 m_BrowseLocalRef.m_bDefaultClick = FALSE;
164 m_BrowseLocalRef.m_bMarkDefault = FALSE;
165 m_BrowseLocalRef.AddEntry(CString(MAKEINTRESOURCE(IDS_REFBROWSE)));
166 m_BrowseLocalRef.AddEntry(CString(MAKEINTRESOURCE(IDS_LOG)));
167 m_BrowseLocalRef.AddEntry(CString(MAKEINTRESOURCE(IDS_REFLOG)));
169 m_tooltips.Create(this);
170 m_tooltips.AddTool(IDC_PROC_PUSH_SET_PUSHBRANCH, IDS_PUSHDLG_PUSHBRANCH_TT);
171 m_tooltips.AddTool(IDC_PROC_PUSH_SET_PUSHREMOTE, IDS_PUSHDLG_PUSHREMOTE_TT);
173 Refresh();
175 this->UpdateData(false);
176 return TRUE;
179 void CPushDlg::Refresh()
181 CString WorkingDir=g_Git.m_CurrentDir;
182 WorkingDir.Replace(_T(':'),_T('_'));
184 CRegString remote(CString(_T("Software\\TortoiseGit\\History\\PushRemote\\")+WorkingDir));
185 m_RemoteReg = remote;
186 int sel = -1;
188 STRING_VECTOR list;
189 m_Remote.Reset();
191 list.push_back(CString(MAKEINTRESOURCE(IDS_PROC_PUSHFETCH_ALLREMOTES)));
192 if(!g_Git.GetRemoteList(list))
194 if (list.size() <= 2)
195 list.erase(list.begin());
197 for(unsigned int i=0;i<list.size();i++)
199 m_Remote.AddString(list[i]);
200 if(list[i] == remote)
201 sel = i;
204 // if the last selected remote was "- All -" and "- All -" is still in the list -> select it
205 if (list.size() > 1 && remote == CString(MAKEINTRESOURCE(IDS_PROC_PUSHFETCH_ALLREMOTES)))
206 sel = 0;
207 m_Remote.SetCurSel(sel);
209 int current=0;
210 list.clear();
211 m_BranchSource.Reset();
212 m_BranchSource.AddString(_T(" ")); // empty string does not work, for removal of remote branches/tags
213 m_BranchSource.SetMaxHistoryItems(0x7FFFFFFF);
214 if(!g_Git.GetBranchList(list,&current))
216 for(unsigned int i=0;i<list.size();i++)
217 m_BranchSource.AddString(list[i]);
218 current++; // shift for " "
220 if (wcsncmp(m_BranchSourceName, _T("refs/"), 5) == 0)
221 m_BranchSourceName = m_BranchSourceName.Mid(5);
222 if (wcsncmp(m_BranchSourceName, _T("heads/"), 6) == 0)
224 m_BranchSourceName = m_BranchSourceName.Mid(6);
225 m_BranchSource.SetCurSel(m_BranchSource.FindStringExact(-1, m_BranchSourceName));
227 else if (wcsncmp(m_BranchSourceName, _T("remotes/"), 8) == 0)
228 m_BranchSource.SetCurSel(m_BranchSource.FindStringExact(-1, m_BranchSourceName));
229 else
230 m_BranchSource.SetCurSel(current);
232 GetRemoteBranch(m_BranchSource.GetString());
234 this->GetDlgItem(IDOK)->EnableWindow(m_BranchSource.GetCount() != 0);
237 void CPushDlg::GetRemoteBranch(CString currentBranch)
239 CString WorkingDir=g_Git.m_CurrentDir;
240 WorkingDir.Replace(_T(':'), _T('_'));
242 if (currentBranch.IsEmpty())
244 EnDisablePushRemoteArchiveBranch();
245 return;
248 CString configName;
250 configName.Format(L"branch.%s.pushremote", currentBranch);
251 CString pushRemote = g_Git.GetConfigValue(configName);
252 if( pushRemote.IsEmpty() )
254 configName.Format(L"branch.%s.remote", currentBranch);
255 pushRemote = g_Git.GetConfigValue(configName);
258 CRegString remote(CString(_T("Software\\TortoiseGit\\History\\PushRemote\\")+WorkingDir));
260 if (!pushRemote.IsEmpty())
262 remote = pushRemote;
263 // if a pushRemote exists, select it
264 for (int i = 0; i < m_Remote.GetCount(); i++)
266 CString str;
267 int n = m_Remote.GetLBTextLen(i);
268 m_Remote.GetLBText(i, str.GetBuffer(n));
269 str.ReleaseBuffer();
270 if (str == pushRemote)
272 m_Remote.SetCurSel(i);
273 break;
277 // select no remote if no push-remote is specified AND push to all remotes is not selected
278 else if (!(m_Remote.GetCount() > 1 && m_Remote.GetCurSel() == 0))
280 m_Remote.SetCurSel(-1);
283 //Select pull-branch from current branch
284 configName.Format(L"branch.%s.pushbranch", currentBranch);
285 CString pushBranch = CGit::StripRefName(g_Git.GetConfigValue(configName));
286 if( pushBranch.IsEmpty() )
288 configName.Format(L"branch.%s.merge", currentBranch);
289 pushBranch = CGit::StripRefName(g_Git.GetConfigValue(configName));
292 m_BranchRemote.LoadHistory(CString(_T("Software\\TortoiseGit\\History\\RemoteBranch\\"))+WorkingDir, _T("branch"));
293 if( !pushBranch.IsEmpty() )
294 m_BranchRemote.AddString(pushBranch);
296 EnDisablePushRemoteArchiveBranch();
299 void CPushDlg::EnDisablePushRemoteArchiveBranch()
301 if ((m_Remote.GetCount() > 1 && m_Remote.GetCurSel() == 0) || m_bPushAllBranches || GetCheckedRadioButton(IDC_RD_REMOTE,IDC_RD_URL) == IDC_RD_URL || m_BranchSource.GetString().Trim().IsEmpty())
303 DialogEnableWindow(IDC_PROC_PUSH_SET_PUSHBRANCH, FALSE);
304 DialogEnableWindow(IDC_PROC_PUSH_SET_PUSHREMOTE, FALSE);
305 m_bSetPushRemote = FALSE;
306 m_bSetPushBranch = FALSE;
307 UpdateData(FALSE);
309 else
311 DialogEnableWindow(IDC_PROC_PUSH_SET_PUSHBRANCH, TRUE);
312 DialogEnableWindow(IDC_PROC_PUSH_SET_PUSHREMOTE, TRUE);
316 // CPushDlg message handlers
318 void CPushDlg::OnBnClickedRd()
320 if( GetCheckedRadioButton(IDC_RD_REMOTE,IDC_RD_URL) == IDC_RD_REMOTE)
322 m_Remote.EnableWindow(TRUE);
323 GetDlgItem(IDC_REMOTE_MANAGE)->EnableWindow(TRUE);
324 m_RemoteURL.EnableWindow(FALSE);
326 if( GetCheckedRadioButton(IDC_RD_REMOTE,IDC_RD_URL) == IDC_RD_URL)
328 m_Remote.EnableWindow(FALSE);
329 GetDlgItem(IDC_REMOTE_MANAGE)->EnableWindow(FALSE);
330 m_RemoteURL.EnableWindow(TRUE);
332 EnDisablePushRemoteArchiveBranch();
336 void CPushDlg::OnCbnSelchangeBranchSource()
338 GetRemoteBranch(m_BranchSource.GetString());
341 void CPushDlg::OnBnClickedOk()
343 CHorizontalResizableStandAloneDialog::UpdateData(TRUE);
345 if( GetCheckedRadioButton(IDC_RD_REMOTE,IDC_RD_URL) == IDC_RD_REMOTE)
347 m_URL=m_Remote.GetString();
348 if (m_URL.IsEmpty())
350 CMessageBox::Show(NULL, IDS_PROC_GITCONFIG_REMOTEEMPTY, IDS_APPNAME, MB_OK);
351 return;
353 m_bPushAllRemotes = (m_Remote.GetCurSel() == 0 && m_Remote.GetCount() > 1);
355 if( GetCheckedRadioButton(IDC_RD_REMOTE,IDC_RD_URL) == IDC_RD_URL)
357 m_URL=m_RemoteURL.GetString();
360 if (!m_bPushAllBranches)
362 this->m_BranchRemoteName=m_BranchRemote.GetString().Trim();
363 this->m_BranchSourceName=m_BranchSource.GetString().Trim();
365 if (m_BranchSourceName.IsEmpty() && m_BranchRemoteName.IsEmpty())
367 if (CMessageBox::Show(NULL, IDS_B_T_BOTHEMPTY, IDS_APPNAME, MB_ICONQUESTION | MB_YESNO) == IDNO)
368 return;
370 if (m_BranchSourceName.IsEmpty() && !m_BranchRemoteName.IsEmpty())
372 if (CMessageBox::Show(NULL, IDS_B_T_LOCALEMPTY, IDS_APPNAME, MB_ICONEXCLAMATION | MB_YESNO) == IDNO)
373 return;
375 else if (!m_BranchRemoteName.IsEmpty() && !g_Git.IsBranchNameValid(this->m_BranchRemoteName))
377 CMessageBox::Show(NULL, IDS_B_T_INVALID, IDS_APPNAME, MB_OK);
378 return;
380 else if (!m_BranchSourceName.IsEmpty() && !g_Git.IsBranchTagNameUnique(m_BranchSourceName))
382 CMessageBox::Show(NULL, IDS_B_T_NOT_UNIQUE, IDS_APPNAME, MB_OK | MB_ICONEXCLAMATION);
383 return;
385 else
387 // do not store branch names on removal
388 this->m_RemoteURL.SaveHistory();
389 this->m_BranchRemote.SaveHistory();
390 m_RemoteReg = m_Remote.GetString();
392 if (!m_BranchSourceName.IsEmpty())
394 CString configName;
395 if (m_bSetPushBranch)
397 configName.Format(L"branch.%s.pushbranch", m_BranchSourceName);
398 if (!m_BranchRemoteName.IsEmpty())
399 g_Git.SetConfigValue(configName, m_BranchRemoteName);
400 else
401 g_Git.UnsetConfigValue(configName);
403 if (m_bSetPushRemote)
405 configName.Format(L"branch.%s.pushremote", m_BranchSourceName);
406 if (!m_BranchRemoteName.IsEmpty())
407 g_Git.SetConfigValue(configName, m_URL);
408 else
409 g_Git.UnsetConfigValue(configName);
415 this->m_regAutoLoad = m_bAutoLoad ;
417 CHorizontalResizableStandAloneDialog::OnOK();
420 void CPushDlg::OnBnClickedRemoteManage()
422 CAppUtils::LaunchRemoteSetting();
423 Refresh();
426 void CPushDlg::OnBnClickedButtonBrowseSourceBranch()
428 switch (m_BrowseLocalRef.GetCurrentEntry())
430 case 0: /* Browse Refence*/
432 if(CBrowseRefsDlg::PickRefForCombo(&m_BranchSource, gPickRef_Head))
433 OnCbnSelchangeBranchSource();
435 break;
437 case 1: /* Log */
439 CLogDlg dlg;
440 dlg.SetSelect(true);
441 if(dlg.DoModal() == IDOK)
443 if (dlg.GetSelectedHash().IsEmpty())
444 return;
446 m_BranchSource.SetWindowText(dlg.GetSelectedHash());
447 OnCbnSelchangeBranchSource();
450 break;
452 case 2: /*RefLog*/
454 CRefLogDlg dlg;
455 if(dlg.DoModal() == IDOK)
457 m_BranchSource.SetWindowText(dlg.m_SelectedHash);
458 OnCbnSelchangeBranchSource();
461 break;
465 void CPushDlg::OnBnClickedButtonBrowseDestBranch()
467 CString remoteBranchName;
468 CString remoteName;
469 m_BranchRemote.GetWindowText(remoteBranchName);
470 remoteName = m_Remote.GetString();
471 remoteBranchName = remoteName + '/' + remoteBranchName;
472 remoteBranchName = CBrowseRefsDlg::PickRef(false, remoteBranchName, gPickRef_Remote);
473 if(remoteBranchName.IsEmpty())
474 return; //Canceled
475 remoteBranchName = remoteBranchName.Mid(13);//Strip 'refs/remotes/'
476 int slashPlace = remoteBranchName.Find('/');
477 remoteName = remoteBranchName.Left(slashPlace);
478 remoteBranchName = remoteBranchName.Mid(slashPlace + 1); //Strip remote name (for example 'origin/')
480 //Select remote
481 int remoteSel = m_Remote.FindStringExact(0,remoteName);
482 if(remoteSel >= 0)
483 m_Remote.SetCurSel(remoteSel);
485 //Select branch
486 m_BranchRemote.AddString(remoteBranchName);
489 BOOL CPushDlg::PreTranslateMessage(MSG* pMsg)
491 m_tooltips.RelayEvent(pMsg);
493 if (pMsg->message == WM_KEYDOWN)
495 switch (pMsg->wParam)
497 case VK_F5:
499 Refresh();
501 break;
505 return CHorizontalResizableStandAloneDialog::PreTranslateMessage(pMsg);
508 void CPushDlg::OnBnClickedPushall()
510 this->UpdateData();
511 this->GetDlgItem(IDC_BRANCH_REMOTE)->EnableWindow(!m_bPushAllBranches);
512 this->GetDlgItem(IDC_BRANCH_SOURCE)->EnableWindow(!m_bPushAllBranches);
513 this->GetDlgItem(IDC_BUTTON_BROWSE_SOURCE_BRANCH)->EnableWindow(!m_bPushAllBranches);
514 this->GetDlgItem(IDC_BUTTON_BROWSE_DEST_BRANCH)->EnableWindow(!m_bPushAllBranches);
515 this->GetDlgItem(IDC_TAGS)->EnableWindow(!m_bPushAllBranches);
516 if (m_bTags && m_bPushAllBranches)
518 m_bTags = FALSE;
519 UpdateData(FALSE);
521 EnDisablePushRemoteArchiveBranch();