| 1 | # ABSTRACT: Sync karr board with remote | ||
| 2 | |||
| 3 | package App::karr::Cmd::Sync; | ||
| 4 | our $VERSION = '0.602'; | ||
| 5 | use Moo; | ||
| 6 | use MooX::Cmd; | ||
| 7 | use feature 'say'; | ||
| 8 | use MooX::Options ( | ||
| 9 | usage_string => 'USAGE: karr sync [--push] [--pull] [--prune] [--accept-foreign-board]', | ||
| 10 | ); | ||
| 11 | use App::karr::Role::BoardAccess; | ||
| 12 | |||
| 13 | with 'App::karr::Role::BoardAccess'; | ||
| 14 | |||
| 15 | |||
| 16 | option push => ( is => 'ro', default => 0, doc => 'Push refs to remote' ); | ||
| 17 | option pull => ( is => 'ro', default => 0, doc => 'Pull refs from remote' ); | ||
| 18 | option prune => ( | ||
| 19 | is => 'ro', | ||
| 20 | default => 0, | ||
| 21 | doc => 'Accept a pull that deletes every remaining board ref', | ||
| 22 | ); | ||
| 23 | option accept_foreign_board => ( | ||
| 24 | is => 'ro', | ||
| 25 | default => 0, | ||
| 26 | doc => 'Accept a pull whose remote presents a different board identity', | ||
| 27 | ); | ||
| 28 | |||
| 29 | sub execute { | ||
| 30 | my ( $self, $args, $data ) = @_; | ||
| 31 | |||
| 32 | my $git = $self->git; | ||
| 33 | |||
| 34 | unless ( $git->is_repo ) { | ||
| 35 | say "Not a git repository. Skipping sync."; | ||
| 36 | return; | ||
| 37 | } | ||
| 38 | |||
| 39 | my $email = $git->git_user_email; | ||
| 40 | my $name = $git->git_user_name; | ||
| 41 | unless ($email) { | ||
| 42 | say q(No git user.email configured. Run: git config --global user.email 'you@example.com'); | ||
| 43 | return; | ||
| 44 | } | ||
| 45 | |||
| 46 | say "User: $name <$email>"; | ||
| 47 | |||
| 48 | my $push_only = $self->push && !$self->pull; | ||
| 49 | my $pull_only = $self->pull && !$self->push; | ||
| 50 | |||
| 51 | # Both halves go through App::karr::Role::SyncLifecycle, which this command | ||
| 52 | # has composed all along (via App::karr::Role::BoardAccess) without using. | ||
| 53 | # It called $git->pull and $git->push exactly once each and died on a false | ||
| 54 | # return -- so the command every failed sync is pointed at ("Run 'karr sync' | ||
| 55 | # to retry") was the one command with no retry and no idea what contention | ||
| 56 | # is, while #181 had already taught the role and the insurance push to spend | ||
| 57 | # their three attempts on a push another push had merely beaten to a ref | ||
| 58 | # (#183). Nothing is duplicated here: the retry-only output, the "errors | ||
| 59 | # always on STDERR, --quiet silences only the banners" contract, and the | ||
| 60 | # refusal-versus-contention verdict all live in the role. | ||
| 61 | unless ($push_only) { | ||
| 62 | print STDERR "Pulling refs/karr/ from remote...\n" unless $self->quiet; | ||
| 63 | # --prune is the one place that may reconcile the board down to | ||
| 64 | # nothing, and --accept-foreign-board the one place that may adopt a | ||
| 65 | # remote whose board identity is not this board's; everywhere else | ||
| 66 | # App::karr::Git refuses and says so (#82, #95). The role forwards both | ||
| 67 | # to App::karr::Git::pull unchanged, on every attempt -- neither is ever | ||
| 68 | # implied, and a retry does not quietly widen what was allowed. | ||
| 69 | my @accept = ( | ||
| 70 | accept_wipe => $self->prune, | ||
| 71 | accept_foreign => $self->accept_foreign_board, | ||
| 72 | ); | ||
| 73 | # sync_before is sync_pull plus a SyncGuard, and a guard left armed is | ||
| 74 | # a push at process teardown -- exactly what --pull says not to do. So | ||
| 75 | # the pull-only form takes the pull without the insurance. | ||
| 76 | $pull_only ? $self->sync_pull(@accept) : $self->sync_before(@accept); | ||
| 77 | } | ||
| 78 | |||
| 79 | unless ($pull_only) { | ||
| 80 | print STDERR "Pushing refs/karr/ to remote...\n" unless $self->quiet; | ||
| 81 | $self->sync_after; | ||
| 82 | } | ||
| 83 | |||
| 84 | # The fleet namespace, second and in the same command (#190). | ||
| 85 | # | ||
| 86 | # In `karr sync` rather than in a command of its own: this is the command | ||
| 87 | # every failed sync, every runbook and every skill doc already points at, | ||
| 88 | # and a second one would be a second thing to remember whose failure mode | ||
| 89 | # is silent -- a chain that quietly plans against a board it has not seen, | ||
| 90 | # a run log no other machine can read. The ticket's counter-argument, that | ||
| 91 | # coordination state has a different lifetime from board state, is real and | ||
| 92 | # is spent elsewhere: the implicit sync every writing command makes | ||
| 93 | # (sync_before/sync_after) stays board-only, so nothing outside this | ||
| 94 | # explicitly-typed command pays for the fleet namespace. | ||
| 95 | # | ||
| 96 | # Second rather than first, and never on its own: the fleet namespace has | ||
| 97 | # no board-identity check and no wholesale-wipe refusal of its own | ||
| 98 | # (App::karr::Git/pull_foundation says why). It does not need them here, | ||
| 99 | # because the board's run first, in this same command, against this same | ||
| 100 | # remote -- a re-created origin, an edited remote URL or a swapped clone is | ||
| 101 | # refused up there and this half never runs. Putting it first, or offering | ||
| 102 | # a flag that runs it alone, would quietly remove that protection. | ||
| 103 | unless ($push_only) { | ||
| 104 | print STDERR "Pulling refs/karr-foundation/ from remote...\n" | ||
| 105 | unless $self->quiet; | ||
| 106 | $self->sync_pull_foundation; | ||
| 107 | } | ||
| 108 | |||
| 109 | unless ($pull_only) { | ||
| 110 | # Silent and free where there is nothing to publish: push_foundation | ||
| 111 | # returns without a round trip when this clone holds no fleet refs and | ||
| 112 | # has no unpublished deletion, which is every repository that is not | ||
| 113 | # the hub. | ||
| 114 | print STDERR "Pushing refs/karr-foundation/ to remote...\n" | ||
| 115 | unless $self->quiet; | ||
| 116 | $self->sync_push_foundation; | ||
| 117 | } | ||
| 118 | |||
| 119 | say "Done."; | ||
| 120 | } | ||
| 121 | |||
| 122 | 1; | ||
| 123 | |||
| 124 | __END__ |