KiCad PCB EDA Suite
Loading...
Searching...
No Matches
dialog_git_switch.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see AUTHORS.TXT for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 3
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20
21#include "dialog_git_switch.h"
22
24#include <trace_helpers.h>
25
26#include <wx/button.h>
27#include <wx/checkbox.h>
28#include <wx/listctrl.h>
29#include <wx/log.h>
30#include <wx/event.h>
31#include <wx/sizer.h>
32#include <wx/timer.h>
33#include <wx/stattext.h>
34#include <wx/textctrl.h>
35
36#include <git2.h>
37
38
39// Strip the "* " / " " prefix used to mark the current branch in the list.
40static wxString stripBranchMarker( const wxString& aName )
41{
42 if( aName.StartsWith( wxS( "* " ) ) || aName.StartsWith( wxS( " " ) ) )
43 return aName.Mid( 2 );
44
45 return aName;
46}
47
48
49DIALOG_GIT_SWITCH::DIALOG_GIT_SWITCH( wxWindow* aParent, git_repository* aRepository ) :
50 DIALOG_SHIM( aParent, wxID_ANY, _( "Git Branch Switch" ), wxDefaultPosition, wxDefaultSize,
51 wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER ),
52 m_timer( this ), m_repository( aRepository )
53{
54 wxBoxSizer* sizer = new wxBoxSizer( wxVERTICAL );
55
56 // Add explanation text
57 wxStaticText* explanationText =
58 new wxStaticText( this, wxID_ANY, _( "Select or enter a branch name:" ) );
59 sizer->Add( explanationText, 0, wxALL, 10 );
60
61 // Add branch list with three columns
62 m_branchList = new wxListView( this, wxID_ANY, wxDefaultPosition, wxDefaultSize,
63 wxLC_REPORT | wxLC_SINGLE_SEL );
64 m_branchList->InsertColumn( 0, _( "Branch" ) );
65 m_branchList->InsertColumn( 1, _( "Last Commit" ) );
66 m_branchList->InsertColumn( 2, _( "Last Updated" ) );
67 sizer->Add( m_branchList, 1, wxALL | wxEXPAND, 10 );
68
69 // Add branch name text box
70 m_branchNameText = new wxTextCtrl( this, wxID_ANY, wxEmptyString, wxDefaultPosition,
71 wxDefaultSize, wxTE_PROCESS_ENTER );
72 sizer->Add( m_branchNameText, 0, wxALL | wxEXPAND, 10 );
73
74 // Add buttons
75 wxStdDialogButtonSizer* buttonSizer = new wxStdDialogButtonSizer();
76 m_switchButton = new wxButton( this, wxID_OK, _( "Switch" ) );
77 buttonSizer->AddButton( m_switchButton );
78 m_switchButton->Disable();
79 wxButton* cancelButton = new wxButton( this, wxID_CANCEL, _( "Cancel" ) );
80 buttonSizer->AddButton( cancelButton );
81 buttonSizer->Realize();
82 sizer->Add( buttonSizer, 0, wxALIGN_RIGHT | wxALL, 10 );
83
84 // Bind events
85 Bind( wxEVT_LIST_ITEM_SELECTED, &DIALOG_GIT_SWITCH::OnBranchListSelection, this, m_branchList->GetId() );
86 Bind( wxEVT_LIST_ITEM_ACTIVATED, &DIALOG_GIT_SWITCH::OnBranchListDClick, this, m_branchList->GetId() );
87 Bind( wxEVT_BUTTON, &DIALOG_GIT_SWITCH::OnSwitchButton, this, m_switchButton->GetId() );
88 Bind( wxEVT_BUTTON, &DIALOG_GIT_SWITCH::OnCancelButton, this, cancelButton->GetId() );
89 Bind( wxEVT_TEXT, &DIALOG_GIT_SWITCH::OnTextChanged, this, m_branchNameText->GetId() );
90 Bind( wxEVT_TIMER, &DIALOG_GIT_SWITCH::OnTimer, this, m_timer.GetId() );
91
92 // Populate branch list
94
95 // Set sizer for the dialog
96 SetSizerAndFit( sizer );
97
99
100 m_existingBranch = false;
101}
102
104{
105 StopTimer();
106 Unbind( wxEVT_TIMER, &DIALOG_GIT_SWITCH::OnTimer, this, m_timer.GetId() );
107}
108
110{
111 m_branchList->DeleteAllItems();
112
113 // Get the branches
114 GetBranches();
115
116 long currentIndex = -1;
117
118 // Populate the list
119 for( auto& [ name, data ] : m_branches )
120 {
121 wxDateTime lastUpdated( data.lastUpdated );
122 wxString lastUpdatedString = lastUpdated.Format();
123 wxString displayName = ( name == m_currentBranch ? wxS( "* " ) : wxS( " " ) ) + name;
124
125 long itemIndex = m_branchList->InsertItem( m_branchList->GetItemCount(), displayName );
126 m_branchList->SetItem( itemIndex, 1, data.commitString );
127 m_branchList->SetItem( itemIndex, 2, lastUpdatedString );
128
129 if( name == m_currentBranch )
130 currentIndex = itemIndex;
131 }
132
133 m_branchList->SetColumnWidth( 0, wxLIST_AUTOSIZE );
134 m_branchList->SetColumnWidth( 1, wxLIST_AUTOSIZE );
135 m_branchList->SetColumnWidth( 2, wxLIST_AUTOSIZE );
136
137 if( currentIndex >= 0 )
138 {
139 m_branchList->Select( currentIndex );
140 m_branchList->EnsureVisible( currentIndex );
142 }
143}
144
145
146void DIALOG_GIT_SWITCH::OnBranchListDClick( wxListEvent& aEvent )
147{
148 int selection = aEvent.GetIndex();
149
150 if( selection != wxNOT_FOUND )
151 {
152 wxString branchName = stripBranchMarker( m_branchList->GetItemText( selection ) );
153 m_branchNameText->SetValue( branchName );
154
155 if( branchName != m_currentBranch )
156 EndModal( wxID_OK );
157 }
158}
159
160
162{
163 int selection = aEvent.GetIndex();
164
165 if( selection != wxNOT_FOUND )
166 {
167 wxString branchName = stripBranchMarker( m_branchList->GetItemText( selection ) );
168 m_branchNameText->SetValue( branchName );
169 m_switchButton->SetLabel( _( "Switch" ) );
170 m_switchButton->Enable( branchName != m_currentBranch );
171 }
172 else
173 {
174 // Deselect all elements in the list
175 for( int ii = 0; ii < m_branchList->GetItemCount(); ++ii )
176 m_branchList->SetItemState( ii, 0, 0 );
177 }
178}
179
180void DIALOG_GIT_SWITCH::OnSwitchButton(wxCommandEvent& aEvent)
181{
182 wxString branchName = m_branchNameText->GetValue();
183
184 // Check if the branch name exists
185 bool branchExists = m_branches.count(branchName);
186
187 if (branchExists)
188 {
189 EndModal(wxID_OK); // Return Switch code
190 }
191 else
192 {
193 EndModal(wxID_ADD); // Return Add code
194 }
195}
196
197
198void DIALOG_GIT_SWITCH::OnCancelButton(wxCommandEvent& aEvent)
199{
200 EndModal(wxID_CANCEL); // Return Cancel code
201}
202
203
205{
206 return m_branchNameText->GetValue();
207}
208
209
211{
212 m_timer.Start( 500, true );
213}
214
215
217{
218 m_timer.Stop();
219}
220
221
222void DIALOG_GIT_SWITCH::OnTimer( wxTimerEvent& aEvt )
223{
224 wxString branchName = m_branchNameText->GetValue();
225
226 if( branchName == m_lastEnteredText )
227 return;
228
229 m_lastEnteredText = branchName;
230
231 // Check if the branch name exists
232 bool branchExists = m_branches.count( branchName );
233
234 if( branchExists )
235 {
236 m_switchButton->SetLabel( _( "Switch" ) );
237 m_switchButton->Enable( branchName != m_currentBranch );
238 }
239 else
240 {
241 m_switchButton->SetLabel( _( "Add" ) );
242 m_switchButton->Enable();
243 }
244}
245
246
247void DIALOG_GIT_SWITCH::OnTextChanged( wxCommandEvent& aEvt )
248{
249 StartTimer();
250}
251
252
254{
255 // Clear the branch list
256 m_branches.clear();
257
258 git_branch_iterator* branchIterator = nullptr;
259 git_branch_t branchType;
260
261 // Get Current Branch
262 git_reference* currentBranchReference = nullptr;
263 git_repository_head( &currentBranchReference, m_repository );
264
265 if( !currentBranchReference )
266 {
267 wxLogTrace( traceGit, "Failed to get current branch" );
268 return;
269 }
270
271 KIGIT::GitReferencePtr currentBranch( currentBranchReference );
272 m_currentBranch = git_reference_shorthand( currentBranchReference );
273
274 // Initialize branch iterator
275 git_branch_iterator_new( &branchIterator, m_repository, GIT_BRANCH_ALL );
276 KIGIT::GitBranchIteratorPtr branchIteratorPtr( branchIterator );
277
278 // Iterate over local branches
279 git_reference* branchReference = nullptr;
280 while( git_branch_next( &branchReference, &branchType, branchIterator ) == 0 )
281 {
282 KIGIT::GitReferencePtr branchReferencePtr( branchReference );
283
284 // Get the branch OID
285 const git_oid* branchOid = git_reference_target( branchReference );
286
287 // Skip this branch if it doesn't have an OID
288 if( !branchOid )
289 continue;
290
291 git_commit* commit = nullptr;
292
293 if( git_commit_lookup( &commit, m_repository, branchOid ) )
294 {
295 // Skip this branch if it doesn't have a commit
296 continue;
297 }
298
299 KIGIT::GitCommitPtr commitPtr( commit );
300
301 // Retrieve commit details
302 BranchData branchData;
303 branchData.commitString = git_commit_message( commit );
304 branchData.lastUpdated = static_cast<time_t>( git_commit_time( commit ) );
305 branchData.isRemote = branchType == GIT_BRANCH_REMOTE;
306
307 m_branches[git_reference_shorthand( branchReference )] = branchData;
308 }
309}
const char * name
DIALOG_GIT_SWITCH(wxWindow *aParent, git_repository *aRepository)
wxListView * m_branchList
git_repository * m_repository
wxString GetBranchName() const
void OnTextChanged(wxCommandEvent &event)
std::map< wxString, BranchData > m_branches
void OnCancelButton(wxCommandEvent &event)
wxTextCtrl * m_branchNameText
void OnBranchListSelection(wxListEvent &event)
void OnBranchListDClick(wxListEvent &event)
void OnTimer(wxTimerEvent &event)
void OnSwitchButton(wxCommandEvent &event)
void finishDialogSettings()
In all dialogs, we must call the same functions to fix minimal dlg size, the default position and per...
DIALOG_SHIM(wxWindow *aParent, wxWindowID id, const wxString &title, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxDefaultSize, long style=wxDEFAULT_FRAME_STYLE|wxRESIZE_BORDER, const wxString &name=wxDialogNameStr)
static wxString stripBranchMarker(const wxString &aName)
#define _(s)
const wxChar *const traceGit
Flag to enable Git debugging output.
std::unique_ptr< git_commit, decltype([](git_commit *aCommit) { git_commit_free(aCommit); })> GitCommitPtr
A unique pointer for git_commit objects with automatic cleanup.
std::unique_ptr< git_reference, decltype([](git_reference *aRef) { git_reference_free(aRef); })> GitReferencePtr
A unique pointer for git_reference objects with automatic cleanup.
std::unique_ptr< git_branch_iterator, decltype([](git_branch_iterator *aIter) { git_branch_iterator_free(aIter); })> GitBranchIteratorPtr
A unique pointer for git_branch_iterator objects with automatic cleanup.
wxString commitString
wxLogTrace helper definitions.