26#include <wx/richmsgdlg.h>
27#include <wx/textdlg.h>
48 m_branchesList->AppendColumn(
_(
"Branch" ), wxLIST_FORMAT_LEFT, FromDIP( 220 ) );
49 m_branchesList->AppendColumn(
_(
"Status" ), wxLIST_FORMAT_LEFT, FromDIP( 70 ) );
50 m_branchesList->AppendColumn(
_(
"Commit" ), wxLIST_FORMAT_LEFT, FromDIP( 100 ) );
51 m_branchesList->AppendColumn(
_(
"Date" ), wxLIST_FORMAT_LEFT, FromDIP( 90 ) );
63 [
this]( wxCommandEvent& )
69 [
this]( wxListEvent& aEvent )
76 [
this]( wxListEvent& )
79 &&
m_branchesList->GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED ) == -1 )
86 m_branchesList->SetToolTip(
_(
"Double click a local branch to switch the project to it." ) );
88 [
this]( wxListEvent& aEvent )
90 int row = aEvent.GetIndex();
97 "press New Branch to start your own branch "
106 [
this]( wxCommandEvent& )
111 [
this]( wxCommandEvent& )
116 [
this]( wxCommandEvent& )
141 if( !
m_dialog->SessionStillCurrent() )
146 auto branches = std::make_shared<std::vector<KIGIT_BRANCH_INFO>>();
152 *branches = aCommon.GetBranches();
153 KIGIT::SortBranchesForDisplay( *branches );
157 if( !m_dialog->SessionStillCurrent() )
161 wxString search = m_branchSearch->GetValue().Lower();
162 wxString selected = KIGIT::SelectedText( m_branchesList, 0 );
163 long selectRow = wxNOT_FOUND;
165 m_detailQuery.Cancel();
166 m_fillingBranches = true;
167 m_branchesList->DeleteAllItems();
168 m_branchIsRemote.clear();
170 for( const KIGIT_BRANCH_INFO& branch : *branches )
172 if( !search.IsEmpty() && !branch.name.Lower().Contains( search ) )
175 long idx = m_branchesList->InsertItem( m_branchesList->GetItemCount(), branch.name );
177 if( branch.isCurrent )
178 m_branchesList->SetItem( idx, 1, _(
"current" ) );
179 else if( branch.isRemote )
180 m_branchesList->SetItem( idx, 1, _(
"remote" ) );
182 m_branchesList->SetItem( idx, 2, branch.tipHash );
183 m_branchesList->SetItem( idx, 3, KIGIT::RelativeDate( branch.when ) );
184 m_branchIsRemote.push_back( branch.isRemote );
187 if( branch.name == selected || ( selected.IsEmpty() && branch.isCurrent ) )
194 m_branchesList->SetColumnWidth( 3, wxLIST_AUTOSIZE );
195 m_branchesList->SetColumnWidth( 3, std::max( m_branchesList->GetColumnWidth( 3 ), FromDIP( 90 ) ) );
199 m_branchCount->SetLabel( wxString::Format( wxPLURAL(
"%d branch",
"%d branches", shown ), shown ) );
201 if( selectRow != wxNOT_FOUND )
203 m_branchesList->SetItemState( selectRow, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED );
226 if( aBranch.IsEmpty() || !
m_dialog->SessionStillCurrent() )
229 auto detail = std::make_shared<KIGIT_BRANCH_DETAIL>();
235 *detail = aCommon.GetBranchDetail( aBranch );
239 if( m_dialog->SessionStillCurrent() && KIGIT::SelectedText( m_branchesList, 0 ) == aBranch )
241 fillBranchDetail( *detail );
264 sync =
_(
"Not tracking another branch." );
266 sync = wxString::Format(
_(
"Could not compare with %s." ), aDetail.
upstream );
272 sync = wxString::Format(
_(
"Up to date with %s." ), aDetail.
upstream );
273 else if( counts.
behind == 0 )
274 sync = wxString::Format( wxPLURAL(
"%zu commit ahead of %s.",
"%zu commits ahead of %s.", counts.
ahead ),
276 else if( counts.
ahead == 0 )
277 sync = wxString::Format( wxPLURAL(
"%zu commit behind %s.",
"%zu commits behind %s.", counts.
behind ),
280 sync = wxString::Format( wxPLURAL(
"%zu commit ahead and %zu behind %s.",
281 "%zu commits ahead and %zu behind %s.", counts.
ahead ),
303 if( !
m_dialog->SessionStillCurrent() )
309 from.IsEmpty() ?
_(
"New branch name:" ) : wxString::Format(
_(
"New branch from '%s', name:" ), from );
315 if( !nameError.IsEmpty() )
318 name = wxGetTextFromUser( prompt,
_(
"New Branch" ),
name,
this );
320 if(
name.IsEmpty() || !
m_dialog->SessionStillCurrent() )
324 }
while( !nameError.IsEmpty() );
331 wxString::Format(
_(
"Could not create branch '%s'." ),
name ) );
337 if( !
m_dialog->SessionStillCurrent() )
349 DisplayInfoMessage(
this,
_(
"This branch lives on the remote, so it cannot be deleted from here." ) );
356 std::optional<size_t> unmerged;
357 bool checked =
false;
360 auto outcome = std::make_shared<OUTCOME>();
366 outcome->current = aCommon.GetCurrentBranchName();
367 outcome->unmerged = aCommon.GetUnmergedCommitCount( name );
368 outcome->checked = true;
370 [
this, outcome,
name]()
372 if( !m_dialog->SessionStillCurrent() || !outcome->checked )
375 if( name == outcome->current )
377 DisplayErrorMessage( this, _(
"You cannot delete the branch you are on." ) );
381 if( !outcome->unmerged )
383 DisplayErrorMessage( this, _(
"Could not compare this branch with the current version." ) );
389 if( *outcome->unmerged == 1 )
391 question = wxString::Format( _(
"Branch '%s' has one commit that is not in the current version."
392 "\n\nDelete it anyway?" ),
395 else if( *outcome->unmerged > 1 )
397 question = wxString::Format( _(
"Branch '%s' has %zu commits that are not in the current version."
398 "\n\nDelete it anyway?" ),
399 name, *outcome->unmerged );
403 question = wxString::Format( _(
"Delete branch '%s'?" ), name );
406 wxRichMessageDialog confirm(
this, question,
_(
"Delete Branch" ),
407 wxYES_NO | wxNO_DEFAULT | wxICON_WARNING | wxCENTER );
408 confirm.SetYesNoLabels(
_(
"&Delete" ),
_(
"Cancel" ) );
410 if( confirm.ShowModal() != wxID_YES || !
m_dialog->SessionStillCurrent() )
416 return aCommon.DeleteBranch( name );
418 wxString::Format(
_(
"Could not delete branch '%s'." ),
name ) );
431 if( !
m_dialog->SessionStillCurrent() )
441 bool hasRemote =
false;
442 bool detached =
false;
443 bool checked =
false;
446 std::shared_ptr<KIGIT_COMMON> common =
m_frame->Prj().GetGitCommon();
447 auto outcome = std::make_shared<OUTCOME>();
451 [
this, aRetriedAfterTrust]()
454 onForcePush( aRetriedAfterTrust );
471 outcome->pushUrl = aCommon.
GetPushUrl( outcome->remoteName );
472 outcome->upstream = outcome->remoteName + wxS(
"/" ) + outcome->branch;
473 outcome->checked =
true;
475 [
this, common, outcome, aRetriedAfterTrust]()
477 if( !
m_dialog->SessionStillCurrent() || !outcome->checked ||
m_push.IsBusy() )
480 if( outcome->detached )
483 _(
"This project is showing an old snapshot rather than a branch, so there "
484 "is no branch to push. Double click a branch to go back to it first." ) );
488 if( !outcome->hasRemote )
494 wxRichMessageDialog confirm(
496 wxString::Format(
_(
"Force push replaces '%s' at %s with your local '%s'.\n\n"
497 "Commits that exist only on the remote will be lost." ),
500 _(
"Force Push" ), wxYES_NO | wxNO_DEFAULT | wxICON_WARNING | wxCENTER );
501 confirm.SetYesNoLabels(
_(
"Force &Push" ),
_(
"Cancel" ) );
503 if( confirm.ShowModal() != wxID_YES || !
m_dialog->SessionStillCurrent() )
513 startForcePush( common, outcome->branch, outcome->headOid, outcome->remoteName, outcome->pushUrl,
514 aRetriedAfterTrust );
520 const wxString& aExpectedHeadOid,
const wxString& aRemoteName,
521 const wxString& aPushUrl,
bool aRetriedAfterTrust )
529 bool authFailure =
false;
530 bool cancelled =
false;
532 std::optional<KIGIT::HOST_KEY_FAILURE> hostKey;
535 auto outcome = std::make_shared<OUTCOME>();
543 bool started =
m_push.Start(
545 [aCommon, outcome, aExpectedBranch, aExpectedHeadOid, aRemoteName, aPushUrl, alive =
m_push.Alive(),
548 GIT_PUSH_HANDLER handler( aCommon.get() );
549 handler.SetProgressReporter( std::make_unique<KIGIT_STATUS_REPORTER>(
551 [dialog]( const wxString& aText )
553 dialog->ShowTransferProgress( aText );
555 handler.SetCancelToken( aToken );
556 handler.SetWaitForActionLock(
true );
557 handler.SetExpectedTarget( aExpectedBranch, aExpectedHeadOid );
558 handler.SetExpectedDestination( aRemoteName, aPushUrl );
559 outcome->result = handler.PerformPush(
true );
561 outcome->error = error.
summary;
562 outcome->details = error.
details;
563 outcome->authFailure = handler.WasAuthFailure();
564 outcome->hostKey = handler.GetHostKeyFailure();
568 outcome->cancelled = handler.IsThisOperationCancelled();
576 aCommon->NotifyRefreshListeners();
580 [
this, aCommon, outcome, aRetriedAfterTrust]()
582 if( !m_dialog->SessionStillCurrent() )
585 m_pushBusy.Release();
592 m_credentialPrompts = 0;
598 m_credentialPrompts = 0;
605 failure.
hostKey = outcome->hostKey;
606 failure.
error = outcome->error;
609 aRetriedAfterTrust, m_credentialPrompts ) )
624 wxString error = outcome->error;
626 if( error.IsEmpty() )
634 m_pushBusy.Release();
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...
The project's git session.
static bool DeferForSecret(const std::shared_ptr< KIGIT_COMMON > &aCommon, const std::weak_ptr< bool > &aOwner, std::function< void()> aRetry)
Read the remote's secret from the keyring on the UI thread without blocking it, so an unlock prompt c...
std::shared_ptr< std::atomic< bool > > CANCEL_TOKEN
One token per operation.
wxString GetCurrentBranchName() const
wxString GetRemoteNameOrDefault() const
Returns GetRemotename() when non-empty, otherwise "origin".
wxString GetPushUrl(const wxString &aRemoteName) const
The address a push to aRemoteName connects to, its push URL or else its URL. Empty without one.
bool CreateBranch(const wxString &aName, const wxString &aFrom=wxEmptyString)
wxString GetHeadCommitId() const
Full commit ID at HEAD, or empty for an unborn or missing HEAD.
bool HasPushAndPullRemote() const
bool IsHeadDetached() const
True when HEAD points at a commit rather than a branch, as after checking out a tag.
wxStaticText * m_branchAuthor
wxButton * m_newBranchButton
wxButton * m_deleteBranchButton
wxButton * m_forcePushButton
wxStaticText * m_branchSync
WX_SPLITTER_WINDOW * m_branchesSplitter
wxListCtrl * m_branchesList
wxTextCtrl * m_branchMessage
wxStaticText * m_branchCount
wxPanel * m_branchListPanel
PANEL_GIT_BRANCHES_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)
wxStaticText * m_branchCommit
wxSearchCtrl * m_branchSearch
wxStaticText * m_branchTitle
void StopTransfer()
Ask a force push still running to stop. It finishes on its own.
PANEL_GIT_BRANCHES(wxWindow *aParent, GIT_ADVANCED_DIALOG *aDialog, EDA_BASE_FRAME *aFrame)
void fillBranchDetail(const KIGIT_BRANCH_DETAIL &aDetail)
int m_branchSashPos
Where the user left the sash between the list and its detail pane.
KIGIT_QUERY m_listQuery
List and detail reads share the session lock without waiting on the UI thread.
void onForcePush(bool aRetriedAfterTrust=false)
Force push the current branch, after an explicit confirmation.
void showBranchDetail(const wxString &aBranch)
Fill the detail pane with aBranch, or hide it when the name is empty or gone.
int m_credentialPrompts
Keyring reads and credential prompts the current force push has shown, reset when it ends.
GIT_ADVANCED_DIALOG * m_dialog
KIGIT_QUERY m_detailQuery
void reloadBranches()
Refill the list after the search text changed.
void Populate()
Fill the list from the repository's branches. The repository is read on a worker.
GIT_TRANSFER_BUSY::HOLD m_pushBusy
The force push's hold on its repository while it runs.
std::vector< bool > m_branchIsRemote
Whether each listed row is a remote-tracking branch.
void startForcePush(std::shared_ptr< KIGIT_COMMON > aCommon, const wxString &aExpectedBranch, const wxString &aExpectedHeadOid, const wxString &aRemoteName, const wxString &aPushUrl, bool aRetriedAfterTrust)
KIGIT_TRANSFER m_push
The force push this page started.
~PANEL_GIT_BRANCHES() override
KIGIT_QUERY m_actionQuery
Reads needed before an action's confirmation, separate from list refreshes.
bool m_fillingBranches
True while the list is being refilled, when its selection events are its own.
void DisplayInfoMessage(wxWindow *aParent, const wxString &aMessage, const wxString &aExtraInfo)
Display an informational message box with aMessage.
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
This file is part of the common library.
Base window classes and related definitions.
@ Busy
Another git operation held the action lock, so nothing was pushed.
APIEXPORT wxString StripEmbeddedUserinfo(const wxString &aText)
Remove the user and password from every URL quoted in a message, since the remote's can carry a passw...
void ClearSyncWarnings(wxWindow *aWindow)
Clear the background fetch's warnings from the frame holding aWindow.
wxString BranchNameError(const wxString &aName)
Empty when aName is a branch name git would accept, otherwise why it would not, in the words to show ...
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.
void ShowBusyMessage(wxWindow *aParent)
Show a busy message for a user action. UI thread only.
TRANSFER_RETRY AnswerTransferFailure(wxWindow *aParent, const std::weak_ptr< bool > &aAlive, KIGIT_COMMON *aCommon, PROJECT *aProject, const TRANSFER_FAILURE &aFailure, bool aRetriedAfterTrust, int &aPrompts)
Answer a failed transfer the way every window does.
wxString SelectedText(wxListCtrl *aList, int aColumn)
The text of the selected row in a report list, or empty when nothing is selected.
@ RETRY_TRUSTED
Run it again, now that the server's key is trusted.
@ GONE
The window closed during a prompt, so the caller must not touch it.
@ CANCELLED
Stop and say so, the user cancelled the transfer or a prompt.
@ RETRY
Run the transfer again.
@ REPORT
Show the failure.
@ HOST_DECLINED
Stop without a message, the user chose not to trust the server.
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...
APIEXPORT wxString UnexplainedFailure(KIGIT_OPERATION aOperation, const wxString &aUrl)
The summary for an operation that failed without saying why, naming the server it was talking to.
void CallOnUi(std::function< void()> aFn)
Run aFn on the UI thread.
void MarkDestructive(wxButton *aButton)
Paint a button's text the diff's red, for one that throws something away.
Why a transfer to a remote failed.
std::optional< HOST_KEY_FAILURE > hostKey
The server's key is unknown or has changed.
bool authFailure
The remote refused every credential offered.
wxString error
Shown above the credential fields.
size_t behind
Commits only in the second reference.
size_t ahead
Commits only in the first reference.
std::optional< KIGIT_AHEAD_BEHIND > divergence
Comparison with upstream, if it could be read.
wxString upstream
The branch this one tracks, or empty.
A summary the user can act on, with the raw text that explains it kept apart for the details.
wxString result
Test unit parsing edge cases and error handling.