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.
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
;
48 //Notification when version changed. Can be implemented in derived classes.
49 virtual void OnVersionChanged(){}
51 afx_msg
void OnBnClickedChooseRadio()
53 this->m_ChooseVersioinTags
.EnableWindow(FALSE
);
54 this->m_ChooseVersioinBranch
.EnableWindow(FALSE
);
55 this->m_ChooseVersioinVersion
.EnableWindow(FALSE
);
56 m_pWin
->GetDlgItem(IDC_BUTTON_BROWSE_REF
)->EnableWindow(FALSE
);
57 m_pWin
->GetDlgItem(IDC_BUTTON_SHOW
)->EnableWindow(FALSE
);
59 int radio
=m_pWin
->GetCheckedRadioButton(IDC_RADIO_HEAD
,IDC_RADIO_VERSION
);
64 case IDC_RADIO_BRANCH
:
65 this->m_ChooseVersioinBranch
.EnableWindow(TRUE
);
66 m_pWin
->GetDlgItem(IDC_BUTTON_BROWSE_REF
)->EnableWindow(TRUE
);
70 this->m_ChooseVersioinTags
.EnableWindow(TRUE
);
72 case IDC_RADIO_VERSION
:
73 this->m_ChooseVersioinVersion
.EnableWindow(TRUE
);
74 m_pWin
->GetDlgItem(IDC_BUTTON_SHOW
)->EnableWindow(TRUE
);
77 // enable version browse button if Version is selected
78 m_pWin
->GetDlgItem(IDC_BUTTON_SHOW
)->EnableWindow(radio
== IDC_RADIO_VERSION
);
82 void OnBnClickedChooseVersion()
84 // use the git log to allow selection of a version
86 // tell the dialog to use mode for selecting revisions
88 // only one revision must be selected however
89 dlg
.SingleSelection(true);
90 if ( dlg
.DoModal() == IDOK
)
92 // get selected hash if any
93 CString selectedHash
= dlg
.GetSelectedHash();
94 // load into window, do this even if empty so that it is clear that nothing has been selected
95 m_ChooseVersioinVersion
.SetWindowText( selectedHash
);
100 void UpdateRevsionName()
102 int radio
=m_pWin
->GetCheckedRadioButton(IDC_RADIO_HEAD
,IDC_RADIO_VERSION
);
106 this->m_VersionName
=_T("HEAD");
108 case IDC_RADIO_BRANCH
:
109 this->m_VersionName
=m_ChooseVersioinBranch
.GetString();
110 if (!g_Git
.IsBranchTagNameUnique(this->m_VersionName
))
111 this->m_VersionName
= L
"refs/heads/" + this->m_VersionName
;
114 this->m_VersionName
= m_ChooseVersioinTags
.GetString();
115 if (!g_Git
.IsBranchTagNameUnique(this->m_VersionName
))
116 this->m_VersionName
= L
"refs/tags/" + m_ChooseVersioinTags
.GetString();
118 case IDC_RADIO_VERSION
:
119 this->m_VersionName
=m_ChooseVersioinVersion
.GetString();
123 void SetDefaultChoose(int id
)
125 m_pWin
->CheckRadioButton(IDC_RADIO_HEAD
,IDC_RADIO_VERSION
,id
);
126 OnBnClickedChooseRadio();
129 void OnBnClickedButtonBrowseRef()
133 CString resultRef
= CBrowseRefsDlg::PickRef(false, m_VersionName
, gPickRef_All
);
134 if(resultRef
.IsEmpty())
136 SelectRef(resultRef
, false);
139 void SelectRef(CString refName
, bool bRefNameIsPossiblyNotFullName
= true)
141 if(bRefNameIsPossiblyNotFullName
)
143 //Make sure refName is a full ref name first
144 CString fullRefName
= g_Git
.GetFullRefName(refName
);
145 if(!fullRefName
.IsEmpty())
146 refName
= fullRefName
;
149 if(wcsncmp(refName
,L
"refs/",5)==0)
150 refName
= refName
.Mid(5);
151 if(wcsncmp(refName
,L
"heads/",6)==0)
153 refName
= refName
.Mid(6);
154 SetDefaultChoose(IDC_RADIO_BRANCH
);
155 m_ChooseVersioinBranch
.SetCurSel(
156 m_ChooseVersioinBranch
.FindStringExact(-1, refName
));
158 else if(wcsncmp(refName
,L
"remotes/",8)==0)
160 SetDefaultChoose(IDC_RADIO_BRANCH
);
161 m_ChooseVersioinBranch
.SetCurSel(
162 m_ChooseVersioinBranch
.FindStringExact(-1, refName
));
164 else if(wcsncmp(refName
,L
"tags/",5)==0)
166 refName
= refName
.Mid(5);
167 refName
.Replace(_T("^{}"), _T(""));
168 SetDefaultChoose(IDC_RADIO_TAGS
);
169 m_ChooseVersioinTags
.SetCurSel(
170 m_ChooseVersioinTags
.FindStringExact(-1, refName
));
174 SetDefaultChoose(IDC_RADIO_VERSION
);
175 m_ChooseVersioinVersion
.AddString(refName
);
185 g_Git
.GetBranchList(list
,¤t
,CGit::BRANCH_ALL_F
);
186 m_ChooseVersioinBranch
.AddString(list
, false);
187 m_ChooseVersioinBranch
.SetCurSel(current
);
190 g_Git
.GetTagList(list
);
191 m_ChooseVersioinTags
.AddString(list
, false);
192 m_ChooseVersioinTags
.SetCurSel(0);
194 m_pWin
->SendMessage(WM_GUIUPDATES
);
196 InterlockedExchange(&m_bLoadingThreadRunning
, FALSE
);
201 m_RadioBranch
.EnableWindow(TRUE
);
202 m_RadioTag
.EnableWindow(TRUE
);
204 if (m_initialRefName
.IsEmpty())
207 SelectRef(m_initialRefName
);
211 m_ChooseVersioinBranch
.SetMaxHistoryItems(0x7FFFFFFF);
212 m_ChooseVersioinTags
.SetMaxHistoryItems(0x7FFFFFFF);
215 m_RadioBranch
.EnableWindow(FALSE
);
216 m_RadioTag
.EnableWindow(FALSE
);
218 InterlockedExchange(&m_bLoadingThreadRunning
, TRUE
);
220 if ( (m_pLoadingThread
=AfxBeginThread(LoadingThreadEntry
, this)) ==NULL
)
222 InterlockedExchange(&m_bLoadingThreadRunning
, FALSE
);
223 CMessageBox::Show(NULL
, IDS_ERR_THREADSTARTFAILED
, IDS_APPNAME
, MB_OK
| MB_ICONERROR
);
226 void WaitForFinishLoading()
228 if(m_bLoadingThreadRunning
&& m_pLoadingThread
)
230 DWORD ret
=::WaitForSingleObject(m_pLoadingThread
->m_hThread
,20000);
231 if(ret
== WAIT_TIMEOUT
)
232 ::TerminateThread(m_pLoadingThread
,0);
236 CString m_VersionName
;
238 CChooseVersion(CWnd
*win
)
245 #define CHOOSE_VERSION_DDX \
246 DDX_Control(pDX, IDC_COMBOBOXEX_BRANCH, m_ChooseVersioinBranch); \
247 DDX_Control(pDX, IDC_COMBOBOXEX_TAGS, m_ChooseVersioinTags); \
248 DDX_Control(pDX, IDC_COMBOBOXEX_VERSION, m_ChooseVersioinVersion); \
249 DDX_Control(pDX, IDC_RADIO_BRANCH, m_RadioBranch);\
250 DDX_Control(pDX, IDC_RADIO_TAGS, m_RadioTag);
252 #define CHOOSE_VERSION_EVENT\
253 ON_REGISTERED_MESSAGE(WM_GUIUPDATES, OnUpdateGUIHost)\
254 ON_BN_CLICKED(IDC_RADIO_HEAD, OnBnClickedChooseRadioHost)\
255 ON_BN_CLICKED(IDC_RADIO_BRANCH, OnBnClickedChooseRadioHost)\
256 ON_BN_CLICKED(IDC_RADIO_TAGS, OnBnClickedChooseRadioHost)\
257 ON_BN_CLICKED(IDC_BUTTON_SHOW, OnBnClickedShow)\
258 ON_BN_CLICKED(IDC_RADIO_VERSION, OnBnClickedChooseRadioHost)\
259 ON_BN_CLICKED(IDC_BUTTON_BROWSE_REF, OnBnClickedButtonBrowseRefHost)
261 #define CHOOSE_VERSION_ADDANCHOR \
263 AddAnchor(IDC_COMBOBOXEX_BRANCH, TOP_LEFT, TOP_RIGHT); \
264 AddAnchor(IDC_COMBOBOXEX_TAGS, TOP_LEFT, TOP_RIGHT); \
265 AddAnchor(IDC_COMBOBOXEX_VERSION, TOP_LEFT, TOP_RIGHT); \
266 AddAnchor(IDC_GROUP_BASEON, TOP_LEFT, TOP_RIGHT); \
267 AddAnchor(IDC_BUTTON_SHOW,TOP_RIGHT); \
268 AddAnchor(IDC_BUTTON_BROWSE_REF,TOP_RIGHT); \
271 #define CHOOSE_EVENT_RADIO() \
272 LRESULT OnUpdateGUIHost(WPARAM, LPARAM) { UpdateGUI(); return 0; } \
273 afx_msg void OnBnClickedChooseRadioHost(){OnBnClickedChooseRadio();}\
274 afx_msg void OnBnClickedShow(){OnBnClickedChooseVersion();}\
275 afx_msg void OnBnClickedButtonBrowseRefHost(){OnBnClickedButtonBrowseRef();}