1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-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.
22 #include "BrowseRefsDlg.h"
23 #include "MessageBox.h"
25 static UINT WM_GUIUPDATES
= RegisterWindowMessage(_T("TORTOISEGIT_CHOOSEVERSION_GUIUPDATES"));
30 CString m_initialRefName
;
34 CWinThread
* m_pLoadingThread
;
35 static UINT
LoadingThreadEntry(LPVOID pVoid
)
37 return ((CChooseVersion
*)pVoid
)->LoadingThread();
39 volatile LONG m_bLoadingThreadRunning
;
42 CHistoryCombo m_ChooseVersioinBranch
;
43 CHistoryCombo m_ChooseVersioinTags
;
44 CHistoryCombo m_ChooseVersioinVersion
;
45 CButton m_RadioBranch
;
47 CString m_pendingRefName
;
50 //Notification when version changed. Can be implemented in derived classes.
51 virtual void OnVersionChanged(){}
53 afx_msg
void OnBnClickedChooseRadio()
55 this->m_ChooseVersioinTags
.EnableWindow(FALSE
);
56 this->m_ChooseVersioinBranch
.EnableWindow(FALSE
);
57 this->m_ChooseVersioinVersion
.EnableWindow(FALSE
);
58 m_pWin
->GetDlgItem(IDC_BUTTON_BROWSE_REF
)->EnableWindow(FALSE
);
59 m_pWin
->GetDlgItem(IDC_BUTTON_SHOW
)->EnableWindow(FALSE
);
61 int radio
=m_pWin
->GetCheckedRadioButton(IDC_RADIO_HEAD
,IDC_RADIO_VERSION
);
66 case IDC_RADIO_BRANCH
:
67 this->m_ChooseVersioinBranch
.EnableWindow(TRUE
);
68 m_pWin
->GetDlgItem(IDC_BUTTON_BROWSE_REF
)->EnableWindow(TRUE
);
72 this->m_ChooseVersioinTags
.EnableWindow(TRUE
);
74 case IDC_RADIO_VERSION
:
75 this->m_ChooseVersioinVersion
.EnableWindow(TRUE
);
76 m_pWin
->GetDlgItem(IDC_BUTTON_SHOW
)->EnableWindow(TRUE
);
79 // enable version browse button if Version is selected
80 m_pWin
->GetDlgItem(IDC_BUTTON_SHOW
)->EnableWindow(radio
== IDC_RADIO_VERSION
);
84 void OnBnClickedChooseVersion()
86 // use the git log to allow selection of a version
88 // tell the dialog to use mode for selecting revisions
90 // only one revision must be selected however
91 dlg
.SingleSelection(true);
92 if ( dlg
.DoModal() == IDOK
)
94 // get selected hash if any
95 CString selectedHash
= dlg
.GetSelectedHash();
96 // load into window, do this even if empty so that it is clear that nothing has been selected
97 m_ChooseVersioinVersion
.SetWindowText( selectedHash
);
102 void UpdateRevsionName()
104 int radio
=m_pWin
->GetCheckedRadioButton(IDC_RADIO_HEAD
,IDC_RADIO_VERSION
);
108 this->m_VersionName
=_T("HEAD");
110 case IDC_RADIO_BRANCH
:
111 this->m_VersionName
=m_ChooseVersioinBranch
.GetString();
112 if (!m_VersionName
.IsEmpty() && !g_Git
.IsBranchTagNameUnique(this->m_VersionName
))
113 this->m_VersionName
= L
"refs/heads/" + this->m_VersionName
;
116 this->m_VersionName
= m_ChooseVersioinTags
.GetString();
117 if (!m_VersionName
.IsEmpty() && !g_Git
.IsBranchTagNameUnique(this->m_VersionName
))
118 this->m_VersionName
= L
"refs/tags/" + m_ChooseVersioinTags
.GetString();
120 case IDC_RADIO_VERSION
:
121 this->m_VersionName
=m_ChooseVersioinVersion
.GetString();
125 void SetDefaultChoose(int id
)
127 m_pWin
->CheckRadioButton(IDC_RADIO_HEAD
,IDC_RADIO_VERSION
,id
);
128 OnBnClickedChooseRadio();
131 void OnBnClickedButtonBrowseRef()
135 CString resultRef
= CBrowseRefsDlg::PickRef(false, m_VersionName
, gPickRef_All
);
136 if(resultRef
.IsEmpty())
138 m_pendingRefName
= m_VersionName
;
139 m_bNotFullName
= true;
140 InitChooseVersion(false, true);
143 m_pendingRefName
= resultRef
;
144 m_bNotFullName
= false;
145 InitChooseVersion(false, true);
148 void SelectRef(CString refName
, bool bRefNameIsPossiblyNotFullName
= true)
150 if(bRefNameIsPossiblyNotFullName
)
152 //Make sure refName is a full ref name first
153 CString fullRefName
= g_Git
.GetFullRefName(refName
);
154 if(!fullRefName
.IsEmpty())
155 refName
= fullRefName
;
158 if(wcsncmp(refName
,L
"refs/",5)==0)
159 refName
= refName
.Mid(5);
160 if(wcsncmp(refName
,L
"heads/",6)==0)
162 refName
= refName
.Mid(6);
163 SetDefaultChoose(IDC_RADIO_BRANCH
);
164 m_ChooseVersioinBranch
.SetCurSel(
165 m_ChooseVersioinBranch
.FindStringExact(-1, refName
));
167 else if(wcsncmp(refName
,L
"remotes/",8)==0)
169 SetDefaultChoose(IDC_RADIO_BRANCH
);
170 m_ChooseVersioinBranch
.SetCurSel(
171 m_ChooseVersioinBranch
.FindStringExact(-1, refName
));
173 else if(wcsncmp(refName
,L
"tags/",5)==0)
175 refName
= refName
.Mid(5);
176 refName
.Replace(_T("^{}"), _T(""));
177 SetDefaultChoose(IDC_RADIO_TAGS
);
178 m_ChooseVersioinTags
.SetCurSel(
179 m_ChooseVersioinTags
.FindStringExact(-1, refName
));
183 SetDefaultChoose(IDC_RADIO_VERSION
);
184 m_ChooseVersioinVersion
.AddString(refName
);
194 g_Git
.GetBranchList(list
,¤t
,CGit::BRANCH_ALL_F
);
195 m_ChooseVersioinBranch
.SetList(list
);
196 m_ChooseVersioinBranch
.SetCurSel(current
);
199 g_Git
.GetTagList(list
);
200 m_ChooseVersioinTags
.SetList(list
);
201 m_ChooseVersioinTags
.SetCurSel(0);
203 m_pWin
->SendMessage(WM_GUIUPDATES
);
205 InterlockedExchange(&m_bLoadingThreadRunning
, FALSE
);
210 m_RadioBranch
.EnableWindow(TRUE
);
211 m_RadioTag
.EnableWindow(TRUE
);
213 if (m_pendingRefName
.IsEmpty())
216 SelectRef(m_pendingRefName
, m_bNotFullName
);
218 if (m_bIsFirstTimeToSetFocus
&& m_pWin
->GetDlgItem(IDC_COMBOBOXEX_BRANCH
)->IsWindowEnabled())
219 m_pWin
->GetDlgItem(IDC_COMBOBOXEX_BRANCH
)->SetFocus();
220 m_bIsFirstTimeToSetFocus
= false;
221 m_pWin
->GetDlgItem(IDOK
)->EnableWindow(TRUE
);
223 void InitChooseVersion(bool setFocusToBranchComboBox
= false, bool bReInit
= false)
225 m_ChooseVersioinBranch
.SetMaxHistoryItems(0x7FFFFFFF);
226 m_ChooseVersioinTags
.SetMaxHistoryItems(0x7FFFFFFF);
229 m_RadioBranch
.EnableWindow(FALSE
);
230 m_RadioTag
.EnableWindow(FALSE
);
232 m_bIsFirstTimeToSetFocus
= setFocusToBranchComboBox
;
235 m_pendingRefName
= m_initialRefName
;
236 m_bNotFullName
= true;
239 m_pWin
->GetDlgItem(IDOK
)->EnableWindow(FALSE
);
241 InterlockedExchange(&m_bLoadingThreadRunning
, TRUE
);
243 if ( (m_pLoadingThread
=AfxBeginThread(LoadingThreadEntry
, this)) ==NULL
)
245 InterlockedExchange(&m_bLoadingThreadRunning
, FALSE
);
246 CMessageBox::Show(NULL
, IDS_ERR_THREADSTARTFAILED
, IDS_APPNAME
, MB_OK
| MB_ICONERROR
);
249 void WaitForFinishLoading()
251 if(m_bLoadingThreadRunning
&& m_pLoadingThread
)
253 DWORD ret
=::WaitForSingleObject(m_pLoadingThread
->m_hThread
,20000);
254 if(ret
== WAIT_TIMEOUT
)
255 ::TerminateThread(m_pLoadingThread
,0);
259 CString m_VersionName
;
261 bool m_bIsFirstTimeToSetFocus
;
262 CChooseVersion(CWnd
*win
)
264 , m_bIsFirstTimeToSetFocus(false)
265 , m_pLoadingThread(nullptr)
266 , m_bLoadingThreadRunning(FALSE
)
267 , m_bNotFullName(true)
274 #define CHOOSE_VERSION_DDX \
275 DDX_Control(pDX, IDC_COMBOBOXEX_BRANCH, m_ChooseVersioinBranch); \
276 DDX_Control(pDX, IDC_COMBOBOXEX_TAGS, m_ChooseVersioinTags); \
277 DDX_Control(pDX, IDC_COMBOBOXEX_VERSION, m_ChooseVersioinVersion); \
278 DDX_Control(pDX, IDC_RADIO_BRANCH, m_RadioBranch);\
279 DDX_Control(pDX, IDC_RADIO_TAGS, m_RadioTag);
281 #define CHOOSE_VERSION_EVENT\
282 ON_REGISTERED_MESSAGE(WM_GUIUPDATES, OnUpdateGUIHost)\
283 ON_BN_CLICKED(IDC_RADIO_HEAD, OnBnClickedChooseRadioHost)\
284 ON_BN_CLICKED(IDC_RADIO_BRANCH, OnBnClickedChooseRadioHost)\
285 ON_BN_CLICKED(IDC_RADIO_TAGS, OnBnClickedChooseRadioHost)\
286 ON_BN_CLICKED(IDC_BUTTON_SHOW, OnBnClickedShow)\
287 ON_BN_CLICKED(IDC_RADIO_VERSION, OnBnClickedChooseRadioHost)\
288 ON_BN_CLICKED(IDC_BUTTON_BROWSE_REF, OnBnClickedButtonBrowseRefHost)
290 #define CHOOSE_VERSION_ADDANCHOR \
292 AddAnchor(IDC_COMBOBOXEX_BRANCH, TOP_LEFT, TOP_RIGHT); \
293 AddAnchor(IDC_COMBOBOXEX_TAGS, TOP_LEFT, TOP_RIGHT); \
294 AddAnchor(IDC_COMBOBOXEX_VERSION, TOP_LEFT, TOP_RIGHT); \
295 AddAnchor(IDC_GROUP_BASEON, TOP_LEFT, TOP_RIGHT); \
296 AddAnchor(IDC_BUTTON_SHOW,TOP_RIGHT); \
297 AddAnchor(IDC_BUTTON_BROWSE_REF,TOP_RIGHT); \
300 #define CHOOSE_EVENT_RADIO() \
301 LRESULT OnUpdateGUIHost(WPARAM, LPARAM) { UpdateGUI(); return 0; } \
302 afx_msg void OnBnClickedChooseRadioHost(){OnBnClickedChooseRadio();}\
303 afx_msg void OnBnClickedShow(){OnBnClickedChooseVersion();}\
304 afx_msg void OnBnClickedButtonBrowseRefHost(){OnBnClickedButtonBrowseRef();}