SyncDlg: Make check if local branch fast-fowards to remote branch work again
[TortoiseGit.git] / src / TortoiseProc / BranchCombox.h
blob509e6bf27d172187dea6949a6f30fc86e63bcdd7
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2009-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 #pragma once
20 #include "afxwin.h"
21 #include "LogDlg.h"
22 #include "BrowseRefsDlg.h"
23 #include "HistoryCombo.h"
24 #include "LoglistUtils.h"
25 #include "UnicodeUtils.h"
26 #include "Tooltip.h"
28 class CBranchCombox
30 public:
31 CBranchCombox()
33 m_LocalBranchFilter = gPickRef_Head;
34 m_RemoteBranchFilter = gPickRef_Remote;
35 m_DialogName=_T("sync");
36 m_pTooltip=NULL;
38 protected:
39 CHistoryCombo m_ctrlLocalBranch;
40 CHistoryCombo m_ctrlRemoteBranch;
41 int m_LocalBranchFilter;
42 int m_RemoteBranchFilter;
44 CToolTips *m_pTooltip;
46 CString m_DialogName;
48 CString m_RegKeyRemoteBranch;
50 void CbnSelchangeLocalBranch()
52 //Select pull-remote from current branch
53 CString configName;
54 CString currentBranch=this->m_ctrlLocalBranch.GetString();
55 configName.Format(L"branch.%s.remote", currentBranch);
56 CString pullRemote = g_Git.GetConfigValue(configName);
58 this->SetRemote(pullRemote);
60 //Select pull-branch from current branch
61 configName.Format(L"branch.%s.merge", currentBranch);
62 CString pullBranch = CGit::StripRefName(g_Git.GetConfigValue(configName));
64 CString defaultUpstream;
65 defaultUpstream.Format(L"remotes/%s/%s", pullRemote, pullBranch);
66 int found = m_ctrlRemoteBranch.FindStringExact(0, defaultUpstream);
67 if(found >= 0)
69 m_ctrlRemoteBranch.SetCurSel(found);
71 else if(!pullBranch.IsEmpty())
73 int index=m_ctrlRemoteBranch.FindStringExact(0,pullBranch);
74 if( index<0 )
75 m_ctrlRemoteBranch.AddString(pullBranch);
76 else
77 m_ctrlRemoteBranch.SetCurSel(index);
79 else if(!this->m_RegKeyRemoteBranch.IsEmpty())
81 m_ctrlRemoteBranch.SetCurSel(0);
83 else
85 m_ctrlRemoteBranch.SetCurSel(-1);
88 this->AddBranchToolTips(&this->m_ctrlLocalBranch,this->m_pTooltip);
90 LocalBranchChange();
92 void CbnSelchangeRemoteBranch()
94 if(this->m_RegKeyRemoteBranch.IsEmpty())
95 this->AddBranchToolTips(&this->m_ctrlRemoteBranch,this->m_pTooltip);
97 RemoteBranchChange();
99 void BnClickedButtonBrowseLocalBranch()
101 if(CBrowseRefsDlg::PickRefForCombo(&m_ctrlLocalBranch, m_LocalBranchFilter))
102 CbnSelchangeLocalBranch();
104 void BnClickedButtonBrowseRemoteBranch()
106 if(!this->m_RegKeyRemoteBranch.IsEmpty())
108 CString remoteBranchName;
109 CString remoteName;
110 this->m_ctrlRemoteBranch.GetWindowText(remoteBranchName);
111 //remoteName = m_Remote.GetString();
112 //remoteBranchName = remoteName + '/' + remoteBranchName;
113 remoteBranchName = CBrowseRefsDlg::PickRef(false, remoteBranchName, gPickRef_Remote);
114 if(remoteBranchName.IsEmpty())
115 return; //Canceled
117 remoteBranchName = remoteBranchName.Mid(13);//Strip 'refs/remotes/'
118 int slashPlace = remoteBranchName.Find('/');
119 remoteName = remoteBranchName.Left(slashPlace);
120 remoteBranchName = remoteBranchName.Mid(slashPlace + 1); //Strip remote name (for example 'origin/')
122 //Select remote
123 //int remoteSel = m_Remote.FindStringExact(0,remoteName);
124 //if(remoteSel >= 0)
125 // m_Remote.SetCurSel(remoteSel);
126 this->SetRemote(remoteName);
128 //Select branch
129 m_ctrlRemoteBranch.AddString(remoteBranchName);
130 CbnSelchangeRemoteBranch();
133 else
135 if(CBrowseRefsDlg::PickRefForCombo(&m_ctrlRemoteBranch, m_RemoteBranchFilter))
136 CbnSelchangeRemoteBranch();
140 virtual void LocalBranchChange(){};
141 virtual void RemoteBranchChange(){};
142 virtual void SetRemote(CString remote){};
144 void AddBranchToolTips(CHistoryCombo *pBranch, CToolTips *tip)
146 if(pBranch&&tip)
148 CString text=pBranch->GetString();
149 CString tooltip;
150 GitRev rev;
154 if (rev.GetCommit(text))
156 ASSERT(FALSE);
157 return;
160 catch (char* msg)
162 CString err(msg);
163 MessageBox(NULL, _T("Could not get commit \"") + text + _T("\".\nlibgit reports:\n") + err, _T("TortoiseGit"), MB_ICONERROR);
164 return;
167 tooltip.Format(_T("%s: %s\n%s: %s <%s>\n%s: %s\n%s:\n%s\n%s"),
168 CString(MAKEINTRESOURCE(IDS_LOG_REVISION)),
169 rev.m_CommitHash.ToString(),
170 CString(MAKEINTRESOURCE(IDS_LOG_AUTHOR)),
171 rev.GetAuthorName(),
172 rev.GetAuthorEmail(),
173 CString(MAKEINTRESOURCE(IDS_LOG_DATE)),
174 CLoglistUtils::FormatDateAndTime(rev.GetAuthorDate(), DATE_LONGDATE),
175 CString(MAKEINTRESOURCE(IDS_LOG_MESSAGE)),
176 rev.GetSubject(),
177 rev.GetBody());
179 pBranch->DisableTooltip();
181 tip->AddTool(pBranch->GetComboBoxCtrl(),tooltip);
185 void LoadBranchInfo()
187 m_ctrlLocalBranch.SetMaxHistoryItems(0x0FFFFFFF);
188 m_ctrlRemoteBranch.SetMaxHistoryItems(0x0FFFFFFF);
190 STRING_VECTOR list;
191 list.clear();
192 int current=0;
193 m_ctrlLocalBranch.Reset();
194 m_ctrlRemoteBranch.Reset();
196 g_Git.GetBranchList(list,&current,CGit::BRANCH_LOCAL_F);
198 m_ctrlLocalBranch.AddString(list);
200 CString currentBranch = g_Git.GetSymbolicRef();
202 if(this->m_RegKeyRemoteBranch.IsEmpty())
204 list.clear();
205 g_Git.GetBranchList(list, NULL, CGit::BRANCH_REMOTE);
207 m_ctrlRemoteBranch.AddString(list);
210 else
212 m_ctrlRemoteBranch.LoadHistory(m_RegKeyRemoteBranch,_T("sync"));
215 if(!this->m_strLocalBranch.IsEmpty())
217 m_ctrlLocalBranch.AddString(m_strLocalBranch);
218 currentBranch = m_strLocalBranch;
220 else
222 m_ctrlLocalBranch.SetCurSel(current);
225 if(!m_strRemoteBranch.IsEmpty())
227 m_ctrlRemoteBranch.AddString(m_strRemoteBranch);
228 m_ctrlRemoteBranch.SetCurSel(m_ctrlRemoteBranch.GetCount()-1);
230 else
232 CbnSelchangeLocalBranch();
235 this->LocalBranchChange();
236 this->RemoteBranchChange();
239 public:
240 CString m_strLocalBranch;
241 CString m_strRemoteBranch;
243 void SaveHistory()
245 if(!this->m_RegKeyRemoteBranch.IsEmpty())
247 m_ctrlRemoteBranch.AddString(m_strRemoteBranch);
248 m_ctrlRemoteBranch.SaveHistory();
253 #define BRANCH_COMBOX_DDX \
254 DDX_Control(pDX, IDC_COMBOBOXEX_LOCAL_BRANCH, m_ctrlLocalBranch); \
255 DDX_Control(pDX, IDC_COMBOBOXEX_REMOTE_BRANCH, m_ctrlRemoteBranch); \
257 #define BRANCH_COMBOX_EVENT \
258 ON_CBN_SELCHANGE(IDC_COMBOBOXEX_LOCAL_BRANCH, OnCbnSelchangeLocalBranch) \
259 ON_CBN_SELCHANGE(IDC_COMBOBOXEX_REMOTE_BRANCH, OnCbnSelchangeRemoteBranch) \
260 ON_BN_CLICKED(IDC_BUTTON_LOCAL_BRANCH, OnBnClickedButtonBrowseLocalBranch) \
261 ON_BN_CLICKED(IDC_BUTTON_REMOTE_BRANCH, OnBnClickedButtonBrowseRemoteBranch) \
263 #define BRANCH_COMBOX_ADD_ANCHOR() \
264 AddAnchor(IDC_COMBOBOXEX_LOCAL_BRANCH,TOP_LEFT);\
265 AddAnchor(IDC_COMBOBOXEX_REMOTE_BRANCH,TOP_RIGHT);\
266 AddAnchor(IDC_BUTTON_LOCAL_BRANCH,TOP_LEFT);\
267 AddAnchor(IDC_BUTTON_REMOTE_BRANCH,TOP_RIGHT);\
268 AddAnchor(IDC_STATIC_REMOTE_BRANCH,TOP_RIGHT);
270 #define BRANCH_COMBOX_EVENT_HANDLE() \
271 afx_msg void OnCbnSelchangeLocalBranch(){CbnSelchangeLocalBranch();} \
272 afx_msg void OnCbnSelchangeRemoteBranch(){CbnSelchangeRemoteBranch();}\
273 afx_msg void OnBnClickedButtonBrowseLocalBranch(){BnClickedButtonBrowseLocalBranch();}\
274 afx_msg void OnBnClickedButtonBrowseRemoteBranch(){BnClickedButtonBrowseRemoteBranch();}