KiCad PCB EDA Suite
Loading...
Searching...
No Matches
panel_git_stashes.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 modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
21
22#include <memory>
23
24#include <wx/textdlg.h>
25
26#include <confirm.h>
31#include <eda_base_frame.h>
34#include <kiplatform/ui.h>
35#include <project.h>
36
37
38namespace
39{
40
41// A stash index is only valid for the listing it came from. Another program adding or dropping
42// a stash shifts every index below it, so act on the stash itself.
43static bool findStashByIdUnlocked( KIGIT_COMMON& aCommon, const wxString& aId, size_t& aIndexOut )
44{
45 if( aId.IsEmpty() )
46 return false;
47
48 for( const KIGIT_STASH_INFO& s : aCommon.GetStashes() )
49 {
50 if( s.id == aId )
51 {
52 aIndexOut = s.index;
53 return true;
54 }
55 }
56
57 return false;
58}
59
60} // namespace
61
62
64 PANEL_GIT_STASHES_BASE( aParent ),
65 m_dialog( aDialog ),
66 m_frame( aFrame )
67{
68 m_stashesList->AppendColumn( _( "Description" ), wxLIST_FORMAT_LEFT, FromDIP( 360 ) );
69 m_stashesList->AppendColumn( _( "Date" ), wxLIST_FORMAT_LEFT, FromDIP( 100 ) );
70 m_stashFiles->AppendColumn( _( "File" ), wxLIST_FORMAT_LEFT, FromDIP( 300 ) );
71 m_stashFiles->SetToolTip( _( "Double click a file to see what the stash changes in it." ) );
72
74
76
77 // The detail pane appears once a stash is selected.
78 m_stashSashPos = FromDIP( 460 );
80
81 m_stashesList->Bind( wxEVT_LIST_ITEM_SELECTED,
82 [this]( wxListEvent& aEvent )
83 {
84 if( !m_fillingStashes )
85 showStashDetail( stashIdForRow( aEvent.GetIndex() ) );
86 } );
87
88 m_stashesList->Bind( wxEVT_LIST_ITEM_DESELECTED,
89 [this]( wxListEvent& )
90 {
92 && m_stashesList->GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED ) == -1 )
93 {
95 }
96 } );
97
98 // A stash is a commit, so comparing it with the commit it was taken from is what it changes.
99 m_stashFiles->Bind( wxEVT_LIST_ITEM_ACTIVATED,
100 [this]( wxListEvent& aEvent )
101 {
102 long sel = m_stashesList->GetNextItem( -1, wxLIST_NEXT_ALL,
103 wxLIST_STATE_SELECTED );
104 wxString id = stashIdForRow( sel );
105
106 if( !id.IsEmpty() )
107 {
109 m_stashFiles->GetItemText( aEvent.GetIndex() ) );
110 }
111 } );
112
113 m_applyStashButton->Bind( wxEVT_BUTTON,
114 [this]( wxCommandEvent& )
115 {
116 onStashApply();
117 } );
118 m_popStashButton->Bind( wxEVT_BUTTON,
119 [this]( wxCommandEvent& )
120 {
121 onStashPop();
122 } );
123 m_dropStashButton->Bind( wxEVT_BUTTON,
124 [this]( wxCommandEvent& )
125 {
126 onStashDrop();
127 } );
128 m_stashSaveButton->Bind( wxEVT_BUTTON,
129 [this]( wxCommandEvent& )
130 {
131 onStashSave();
132 } );
133}
134
135
137{
138 if( !m_dialog->SessionStillCurrent() )
139 return;
140
141 m_detailQuery.Cancel();
142
143 auto stashes = std::make_shared<std::vector<KIGIT_STASH_INFO>>();
144
145 m_listQuery.Run(
146 m_frame->Prj().GetGitCommon(),
147 [stashes]( KIGIT_COMMON& aCommon )
148 {
149 *stashes = aCommon.GetStashes();
150 },
151 [this, stashes]()
152 {
153 if( !m_dialog->SessionStillCurrent() )
154 return;
155
156 long selectedRow = m_stashesList->GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
157 wxString selected = stashIdForRow( selectedRow );
158 long selectRow = wxNOT_FOUND;
159
160 m_detailQuery.Cancel();
161 m_fillingStashes = true;
162 m_stashesList->DeleteAllItems();
163 m_stashIds.clear();
164
165 for( const KIGIT_STASH_INFO& s : *stashes )
166 {
167 long idx = m_stashesList->InsertItem( m_stashesList->GetItemCount(), s.message );
168 m_stashesList->SetItem( idx, 1, KIGIT::RelativeDate( s.when ) );
169 m_stashesList->SetItemData( idx, static_cast<long>( s.index ) );
170 m_stashIds.push_back( s.id );
171
172 // Keep the selection on the same stash, or start on the newest.
173 if( s.id == selected || ( selected.IsEmpty() && idx == 0 ) )
174 selectRow = idx;
175 }
176
177 int shown = m_stashesList->GetItemCount();
178 m_stashCount->SetLabel( wxString::Format( wxPLURAL( "%d stash", "%d stashes", shown ), shown ) );
179
180 if( selectRow != wxNOT_FOUND )
181 {
182 m_stashesList->SetItemState( selectRow, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED );
183 showStashDetail( stashIdForRow( selectRow ) );
184 }
185 else
186 {
188 }
189
190 m_fillingStashes = false;
191 } );
192}
193
194
195void PANEL_GIT_STASHES::showStashDetail( const wxString& aId )
196{
197 // The old file list must not be used with the newly selected stash while its read is pending.
199
200 if( aId.IsEmpty() || !m_dialog->SessionStillCurrent() )
201 return;
202
203 auto detail = std::make_shared<KIGIT_STASH_DETAIL>();
204
205 m_detailQuery.Run(
206 m_frame->Prj().GetGitCommon(),
207 [detail, aId]( KIGIT_COMMON& aCommon )
208 {
209 *detail = aCommon.GetStashDetail( aId );
210 },
211 [this, detail, aId]()
212 {
213 if( !m_dialog->SessionStillCurrent() )
214 return;
215
216 long selectedRow = m_stashesList->GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
217
218 if( stashIdForRow( selectedRow ) == aId )
219 fillStashDetail( *detail );
220 } );
221}
222
223
225{
226 if( !aDetail.found )
227 {
229 return;
230 }
231
232 m_stashWhen->SetLabel( wxString::Format( _( "Stashed: %s" ), KIGIT::RelativeDate( aDetail.when ) ) );
233 m_stashBranch->SetLabel( wxString::Format( _( "Branch: %s" ), aDetail.branch ) );
234 m_stashCommit->SetLabel( wxString::Format( _( "Commit: %s" ), aDetail.baseHash ) );
235 m_stashMessage->ChangeValue( aDetail.message );
236
237 m_stashFiles->DeleteAllItems();
238
239 for( const wxString& file : aDetail.files )
240 m_stashFiles->InsertItem( m_stashFiles->GetItemCount(), file );
241
242 if( !m_stashesSplitter->IsSplit() )
244
245 m_stashDetail->Layout();
246}
247
248
254
255
256wxString PANEL_GIT_STASHES::stashIdForRow( long aRow ) const
257{
258 if( aRow < 0 || static_cast<size_t>( aRow ) >= m_stashIds.size() )
259 return wxEmptyString;
260
261 return m_stashIds[aRow];
262}
263
264
265void PANEL_GIT_STASHES::restoreSelectedStash( bool aRemoveAfterApply )
266{
267 if( !m_dialog->SessionStillCurrent() )
268 return;
269
270 // Resolved before the dialogs below. Each runs a nested event loop in which a refresh can
271 // rebuild the list, and a row number would then stand for a different stash.
272 wxString id = stashIdForRow( m_stashesList->GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED ) );
273
274 if( id.IsEmpty() )
275 return;
276
277 // Applying needs a clean tree, so report unsaved editors before asking to rewrite files.
278 m_dialog->RefuseOverUncommittedWork(
279 [this, id, aRemoveAfterApply]( const GIT_WORKTREE_STATUS& aStatus )
280 {
281 wxString question = aRemoveAfterApply ? _( "Apply this stash and remove it from the list?" )
282 : _( "Apply this stash to the working tree?" );
283
284 if( !IsOK( this, question ) )
285 return;
286
287 if( !m_dialog->ConfirmWorkingTreeChange( aStatus ) )
288 return;
289
290 m_dialog->RunGitAction(
291 [id, aRemoveAfterApply]( KIGIT_COMMON& aCommon )
292 {
293 size_t index = 0;
294
295 if( !findStashByIdUnlocked( aCommon, id, index ) )
296 return false;
297
298 return aRemoveAfterApply ? aCommon.StashPop( index ) : aCommon.StashApply( index );
299 },
300 aRemoveAfterApply ? _( "Could not pop the stash. Another program may have changed the stash "
301 "list, so nothing was applied." )
302 : _( "Could not apply the stash. Another program may have changed the stash "
303 "list, so nothing was applied." ),
304 /* rewrites working tree */ true );
305 } );
306}
307
308
313
314
319
320
322{
323 if( !m_dialog->SessionStillCurrent() )
324 return;
325
326 // Resolved before the confirmation, which runs a nested event loop in which a refresh can
327 // rebuild the list and renumber the rows.
328 wxString id = stashIdForRow( m_stashesList->GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED ) );
329
330 if( id.IsEmpty() )
331 return;
332
333 if( !IsOK( this, _( "Drop this stash? This cannot be undone." ) ) )
334 return;
335
336 m_dialog->RunGitAction(
337 [id]( KIGIT_COMMON& aCommon )
338 {
339 size_t index = 0;
340
341 if( !findStashByIdUnlocked( aCommon, id, index ) )
342 return false;
343
344 return aCommon.StashDrop( index );
345 },
346 _( "Could not drop the stash. Another program may have changed the stash list, so nothing "
347 "was dropped." ) );
348}
349
350
352{
353 if( !m_dialog->SessionStillCurrent() )
354 return;
355
356 struct PREFLIGHT
357 {
358 wxString lfsMessage;
359 KIGIT::GIT_IDENTITY identity;
360 };
361
362 std::shared_ptr<KIGIT_COMMON> common = m_frame->Prj().GetGitCommon();
363 auto preflight = std::make_shared<PREFLIGHT>();
364
365 m_actionQuery.Run(
366 common,
367 [preflight]( KIGIT_COMMON& aCommon )
368 {
369 preflight->lfsMessage = KIGIT::PROJECT_GIT_UTILS::LfsRefusal( &aCommon );
370 preflight->identity = KIGIT::ReadGitIdentity( aCommon );
371 },
372 [this, common, preflight]()
373 {
374 if( !m_dialog->SessionStillCurrent() || !common->GetRepo() )
375 return;
376
377 if( !preflight->lfsMessage.IsEmpty() )
378 {
379 DisplayErrorMessage( this, preflight->lfsMessage );
380 return;
381 }
382
383 // Stashing writes a commit object, so it needs an author identity like a commit does.
384 if( !KIGIT::EnsureGitIdentity( this, m_frame, common, preflight->identity ) )
385 return;
386
387 // Unsaved editor changes are written to disk here, so they are part of what gets
388 // stashed instead of surviving in memory.
389 if( !m_dialog->ConfirmWorkingTreeChange() )
390 return;
391
393 } );
394}
395
396
398{
399 struct OUTCOME
400 {
401 GIT_WORKTREE_STATUS worktree;
402 bool hasNewFilesOnly = false;
403 };
404
405 wxString projectPath = m_frame->Prj().GetProjectPath();
406 bool dark = KIPLATFORM::UI::IsDarkTheme();
407 auto outcome = std::make_shared<OUTCOME>();
408
409 m_actionQuery.Run(
410 m_frame->Prj().GetGitCommon(),
411 [outcome, projectPath, dark]( KIGIT_COMMON& aCommon )
412 {
413 outcome->worktree = ReadGitWorktreeStatus( aCommon, projectPath );
414 outcome->hasNewFilesOnly = !outcome->worktree.hasTrackedChanges
415 && !BuildGitChangeEntries( aCommon, projectPath, dark ).empty();
416 },
417 [this, outcome]()
418 {
419 if( !m_dialog->SessionStillCurrent() )
420 return;
421
422 if( !ProjectSafeToRewrite( m_frame, this, outcome->worktree, /* allow tracked changes */ true ) )
423 return;
424
425 if( !outcome->worktree.hasTrackedChanges )
426 {
427 // A stash only includes files that have been committed before. When the only changes
428 // are brand new files, saying "no changes" would contradict what the panel shows.
429 if( outcome->hasNewFilesOnly )
430 {
431 DisplayInfoMessage( this, _( "The changed files have never been committed, and a stash only "
432 "includes files that have been. Commit them from the Version "
433 "Control panel first, or discard them there." ) );
434 }
435 else
436 {
437 DisplayInfoMessage( this, _( "There are no changes to stash." ) );
438 }
439
440 return;
441 }
442
443 wxTextEntryDialog dlg( this, _( "Optional description for this stash:" ), _( "Stash Changes" ) );
444
445 if( dlg.ShowModal() != wxID_OK || !m_dialog->SessionStillCurrent() )
446 return;
447
448 // Editing can resume while either the status read or the description dialog runs.
449 // Do not rewrite a document whose editor has acquired fresh unsaved changes.
450 if( !ProjectSafeToRewrite( m_frame, this, outcome->worktree, /* allow tracked changes */ true ) )
451 return;
452
453 wxString message = dlg.GetValue().Trim( true ).Trim( false );
454
455 m_dialog->RunGitAction(
456 [message]( KIGIT_COMMON& aCommon )
457 {
458 return aCommon.StashSave( message );
459 },
460 _( "Could not stash the changes." ), /* rewrites working tree */ true );
461 } );
462}
int index
The base frame for deriving all KiCad main window classes.
The advanced Git window: a non-modal dialog with a top bar naming the repository and branch and a sid...
static wxString LfsRefusal(KIGIT_COMMON *aCommon)
Return a refusal message for LFS files or an unknown project folder, otherwise empty.
The project's git session.
bool StashPop(size_t aIndex)
std::vector< KIGIT_STASH_INFO > GetStashes() const
bool StashApply(size_t aIndex)
bool StashDrop(size_t aIndex)
PANEL_GIT_STASHES_BASE(wxWindow *parent, wxWindowID id=wxID_ANY, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxSize(-1,-1), long style=wxTAB_TRAVERSAL, const wxString &name=wxEmptyString)
WX_SPLITTER_WINDOW * m_stashesSplitter
int m_stashSashPos
Where the user left the sash between the list and its detail pane.
void showStashDetail(const wxString &aId)
Fill the detail pane with the stash aId names, or hide it when the id is empty or gone.
void Populate()
Fill the list from the repository's stashes. The repository is read on a worker.
EDA_BASE_FRAME * m_frame
wxString stashIdForRow(long aRow) const
The stash shown on aRow, by its commit, or empty when the row is gone.
PANEL_GIT_STASHES(wxWindow *aParent, GIT_ADVANCED_DIALOG *aDialog, EDA_BASE_FRAME *aFrame)
KIGIT_QUERY m_listQuery
List and detail reads share the session lock without waiting on the UI thread.
KIGIT_QUERY m_detailQuery
void checkStashChanges()
After saving the editors, read the changes before asking for a stash description.
std::vector< wxString > m_stashIds
The stash commit behind each row, since a row's index is only valid for the listing it came from.
void restoreSelectedStash(bool aRemoveAfterApply)
bool m_fillingStashes
True while the list is being refilled, when its selection events are its own.
GIT_ADVANCED_DIALOG * m_dialog
KIGIT_QUERY m_actionQuery
Reads needed before an action's confirmation, separate from list refreshes.
void fillStashDetail(const KIGIT_STASH_DETAIL &aDetail)
bool IsOK(wxWindow *aParent, const wxString &aMessage)
Display a yes/no dialog with aMessage and returns the user response.
Definition confirm.cpp:274
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition confirm.cpp:217
This file is part of the common library.
#define _(s)
Base window classes and related definitions.
void ShowGitCommitDiff(EDA_BASE_FRAME *aFrame, const wxString &aCommitHash, wxWindow *aParentWindow, const wxString &aRelativePath)
Open the diff for what a commit changed, comparing it against its parent.
bool ProjectSafeToRewrite(EDA_BASE_FRAME *aFrame, wxWindow *aParent, const GIT_WORKTREE_STATUS &aStatus, bool aAllowTrackedChanges)
Combine the repository snapshot with live editor state and explain any refusal.
std::string message
GIT_IDENTITY ReadGitIdentity(KIGIT_COMMON &aCommon)
Read on a worker while holding the session's action lock.
bool EnsureGitIdentity(wxWindow *aParent, EDA_BASE_FRAME *aFrame, const std::shared_ptr< KIGIT_COMMON > &aCommon, const GIT_IDENTITY &aIdentity)
Open the same editor when the worker snapshot has no usable Git identity.
void UnsplitKeepingSash(wxSplitterWindow *aSplitter, wxWindow *aPane, int &aSashPos)
Close a splitter's second pane, keeping the sash where the user left it for next time.
wxString RelativeDate(const wxDateTime &aWhen)
"just now", "3 hours ago", or the ISO date once it is more than a month old.
void KeepDetailWidth(WX_SPLITTER_WINDOW *aSplitter)
Keep a split detail pane at the width its controls need, against both a narrow window and a dragged s...
void MarkDestructive(wxButton *aButton)
Paint a button's text the diff's red, for one that throws something away.
bool IsDarkTheme()
Determine if the desktop interface is currently using a dark theme or a light theme.
Definition wxgtk/ui.cpp:57
Repository state used before an operation that requires a clean working tree.
Repository values and whether Git can use them to record a commit.
wxString branch
The branch the stash was taken on, as its message records it.
std::vector< wxString > files
Paths the stash changes, new files included.
wxString message
What was stashed, without the "WIP on branch:" prefix.
wxString baseHash
Short hash of the commit the stash was taken on.