|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | + |
| 7 | + "github.com/spf13/cobra" |
| 8 | + gitpkg "github.com/steveyegge/gastown/internal/git" |
| 9 | + "github.com/steveyegge/gastown/internal/style" |
| 10 | +) |
| 11 | + |
| 12 | +var ( |
| 13 | + syncMTDryRun bool |
| 14 | + syncMTRemote string |
| 15 | + syncMTSource string |
| 16 | + syncMTTarget string |
| 17 | + syncMTEscalate bool |
| 18 | + syncMTSeverity string |
| 19 | +) |
| 20 | + |
| 21 | +// syncCmd is the parent for branch/data synchronization tooling. |
| 22 | +var syncCmd = &cobra.Command{ |
| 23 | + Use: "sync", |
| 24 | + GroupID: GroupWork, |
| 25 | + Short: "Synchronization tooling for branches and refs", |
| 26 | + Long: `Synchronization helpers that keep related git refs consistent. |
| 27 | +
|
| 28 | +Subcommands: |
| 29 | + gt sync merge-target Fast-forward the refinery merge-target onto main`, |
| 30 | +} |
| 31 | + |
| 32 | +var syncMergeTargetCmd = &cobra.Command{ |
| 33 | + Use: "merge-target", |
| 34 | + Short: "Fast-forward the refinery merge-target branch onto main", |
| 35 | + Args: cobra.NoArgs, |
| 36 | + RunE: runSyncMergeTarget, |
| 37 | + Long: `Keep the refinery's merge-target branch in sync with main. |
| 38 | +
|
| 39 | +The refinery rebases polecat MRs onto merge-target before merging. Polecats |
| 40 | +branch off main. If main and merge-target diverge — which happens whenever a |
| 41 | +commit lands on main without propagating to merge-target (e.g. a hotfix |
| 42 | +cherry-pick) — every MR rebase conflicts on the un-propagated content, and the |
| 43 | +queue silently stops draining. |
| 44 | +
|
| 45 | +This command resyncs merge-target to main when it is safe to do so: |
| 46 | +
|
| 47 | + - If merge-target is missing, there is nothing to sync (integration-branch |
| 48 | + refinery is not in use) — succeeds silently. |
| 49 | + - If merge-target already equals main, it is in sync — succeeds silently. |
| 50 | + - If merge-target is strictly behind main (every merge-target commit is also |
| 51 | + on main), it is fast-forwarded to main via a refspec push. The working tree |
| 52 | + is never touched. |
| 53 | + - If merge-target has commits that are NOT on main, that is real divergence |
| 54 | + requiring human attention. The command prints a diagnostic and exits |
| 55 | + non-zero (and, with --escalate, files a HIGH escalation). |
| 56 | +
|
| 57 | +Safe to run after any push to main, on a schedule, or as a manual operator tool. |
| 58 | +
|
| 59 | +EXAMPLES: |
| 60 | + gt sync merge-target # Fast-forward merge-target onto main |
| 61 | + gt sync merge-target --dry-run # Show what would change |
| 62 | + gt sync merge-target --escalate # File an escalation on real divergence |
| 63 | + gt sync merge-target --source master # Use a non-default source branch`, |
| 64 | +} |
| 65 | + |
| 66 | +func init() { |
| 67 | + syncMergeTargetCmd.Flags().BoolVarP(&syncMTDryRun, "dry-run", "n", false, "Show what would change without pushing") |
| 68 | + syncMergeTargetCmd.Flags().StringVar(&syncMTRemote, "remote", "origin", "Remote to read from and push to") |
| 69 | + syncMergeTargetCmd.Flags().StringVar(&syncMTSource, "source", "", "Source branch (default: remote's default branch, e.g. main)") |
| 70 | + syncMergeTargetCmd.Flags().StringVar(&syncMTTarget, "target", "merge-target", "Merge-target branch to fast-forward") |
| 71 | + syncMergeTargetCmd.Flags().BoolVar(&syncMTEscalate, "escalate", false, "File a HIGH escalation if real divergence is detected") |
| 72 | + syncMergeTargetCmd.Flags().StringVar(&syncMTSeverity, "severity", "high", "Escalation severity when --escalate is set (critical, high, medium, low)") |
| 73 | + |
| 74 | + syncCmd.AddCommand(syncMergeTargetCmd) |
| 75 | + rootCmd.AddCommand(syncCmd) |
| 76 | +} |
| 77 | + |
| 78 | +func runSyncMergeTarget(cmd *cobra.Command, args []string) error { |
| 79 | + g := gitpkg.NewGit(".") |
| 80 | + if !g.IsRepo() { |
| 81 | + return fmt.Errorf("not a git repository") |
| 82 | + } |
| 83 | + |
| 84 | + remote := syncMTRemote |
| 85 | + target := syncMTTarget |
| 86 | + source := syncMTSource |
| 87 | + if source == "" { |
| 88 | + source = g.RemoteDefaultBranch() |
| 89 | + } |
| 90 | + |
| 91 | + // Refresh remote state so ancestry checks reflect what's actually pushed. |
| 92 | + fmt.Printf("Fetching from %s...\n", style.Dim.Render(remote)) |
| 93 | + if err := g.Fetch(remote); err != nil { |
| 94 | + return fmt.Errorf("fetching %s: %w", remote, err) |
| 95 | + } |
| 96 | + |
| 97 | + sourceRef := remote + "/" + source |
| 98 | + targetRef := remote + "/" + target |
| 99 | + |
| 100 | + sourceSHA, err := g.Rev(sourceRef) |
| 101 | + if err != nil { |
| 102 | + return fmt.Errorf("source branch %s not found (is --source correct?): %w", sourceRef, err) |
| 103 | + } |
| 104 | + |
| 105 | + // merge-target may legitimately not exist (integration-branch refinery off). |
| 106 | + targetExists, err := g.RemoteBranchExists(remote, target) |
| 107 | + if err != nil { |
| 108 | + return fmt.Errorf("checking %s: %w", targetRef, err) |
| 109 | + } |
| 110 | + if !targetExists { |
| 111 | + fmt.Printf("%s %s has no %q branch — nothing to sync.\n", |
| 112 | + style.Bold.Render("✓"), remote, target) |
| 113 | + return nil |
| 114 | + } |
| 115 | + |
| 116 | + targetSHA, err := g.Rev(targetRef) |
| 117 | + if err != nil { |
| 118 | + return fmt.Errorf("resolving %s: %w", targetRef, err) |
| 119 | + } |
| 120 | + |
| 121 | + if sourceSHA == targetSHA { |
| 122 | + fmt.Printf("%s %s is in sync with %s.\n", |
| 123 | + style.Bold.Render("✓"), target, source) |
| 124 | + return nil |
| 125 | + } |
| 126 | + |
| 127 | + // Real divergence: any commit on merge-target that is not on main means a |
| 128 | + // fast-forward would discard work. That needs a human, not an auto-resync. |
| 129 | + targetIsAncestor, err := g.IsAncestor(targetRef, sourceRef) |
| 130 | + if err != nil { |
| 131 | + return fmt.Errorf("checking ancestry of %s: %w", targetRef, err) |
| 132 | + } |
| 133 | + if !targetIsAncestor { |
| 134 | + ahead, _ := g.CommitsAhead(sourceRef, targetRef) // commits on target not on source |
| 135 | + return reportMergeTargetDivergence(cmd, remote, source, target, ahead) |
| 136 | + } |
| 137 | + |
| 138 | + // Safe fast-forward: every merge-target commit is already on main. |
| 139 | + behind, err := g.CommitsAhead(targetRef, sourceRef) // commits on source not on target |
| 140 | + if err != nil { |
| 141 | + return fmt.Errorf("counting commits: %w", err) |
| 142 | + } |
| 143 | + |
| 144 | + if syncMTDryRun { |
| 145 | + fmt.Printf("%s Would fast-forward %s to %s (%d commit(s) behind).\n", |
| 146 | + style.Warning.Render("~"), target, source, behind) |
| 147 | + return nil |
| 148 | + } |
| 149 | + |
| 150 | + // Push main's tip onto merge-target. Refspec push updates the remote branch |
| 151 | + // without checking it out, so the working tree is never mutated (and the |
| 152 | + // town-root mutation guard is not triggered). Non-force: git rejects this if |
| 153 | + // it is not a fast-forward, which is a final safety net behind the ancestry |
| 154 | + // check above. |
| 155 | + refspec := fmt.Sprintf("%s:refs/heads/%s", sourceSHA, target) |
| 156 | + if err := g.PushRefspec(remote, refspec, false); err != nil { |
| 157 | + return fmt.Errorf("fast-forwarding %s to %s: %w", target, source, err) |
| 158 | + } |
| 159 | + |
| 160 | + fmt.Printf("%s Fast-forwarded %s to %s (%d commit(s)).\n", |
| 161 | + style.Bold.Render("✓"), target, source, behind) |
| 162 | + return nil |
| 163 | +} |
| 164 | + |
| 165 | +// reportMergeTargetDivergence prints an operator diagnostic for real divergence |
| 166 | +// and, when --escalate is set, files an escalation. It always returns a non-zero |
| 167 | +// error so callers (operators, schedulers) can detect the unhandled condition. |
| 168 | +func reportMergeTargetDivergence(cmd *cobra.Command, remote, source, target string, ahead int) error { |
| 169 | + var b strings.Builder |
| 170 | + fmt.Fprintf(&b, "%s %s has diverged from %s\n\n", |
| 171 | + style.Warning.Render("⚠"), target, source) |
| 172 | + fmt.Fprintf(&b, " %s has %d commit(s) that are NOT on %s.\n", target, ahead, source) |
| 173 | + fmt.Fprintf(&b, " A fast-forward would discard them, so this needs a human.\n\n") |
| 174 | + fmt.Fprintf(&b, " Inspect the divergent commits:\n") |
| 175 | + fmt.Fprintf(&b, " git log %s/%s ^%s/%s --oneline\n\n", remote, target, remote, source) |
| 176 | + fmt.Fprintf(&b, " If those commits are genuinely unwanted (e.g. salvage that already\n") |
| 177 | + fmt.Fprintf(&b, " landed on %s another way), resync after tagging a backup:\n", source) |
| 178 | + fmt.Fprintf(&b, " git tag %s-pre-resync %s/%s\n", target, remote, target) |
| 179 | + fmt.Fprintf(&b, " git push %s %s/%s:refs/heads/%s --force-with-lease\n", remote, remote, source, target) |
| 180 | + fmt.Print(b.String()) |
| 181 | + |
| 182 | + if syncMTEscalate { |
| 183 | + description := fmt.Sprintf("merge-target sync: %s diverged from %s (%d unmerged commit(s))", target, source, ahead) |
| 184 | + if err := escalateMergeTargetDivergence(cmd, description); err != nil { |
| 185 | + style.PrintWarning("failed to file escalation: %v", err) |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + return fmt.Errorf("%s has diverged from %s — manual intervention required", target, source) |
| 190 | +} |
| 191 | + |
| 192 | +// escalateMergeTargetDivergence reuses the standard escalation path (bead + |
| 193 | +// routed mail) by configuring the shared escalate flags and invoking runEscalate. |
| 194 | +// A stable fingerprint suppresses duplicate escalations across repeated runs. |
| 195 | +func escalateMergeTargetDivergence(cmd *cobra.Command, description string) error { |
| 196 | + severity := strings.ToLower(syncMTSeverity) |
| 197 | + if severity == "" { |
| 198 | + severity = "high" |
| 199 | + } |
| 200 | + |
| 201 | + // Configure the shared escalate command state for a single create. |
| 202 | + escalateSeverity = severity |
| 203 | + escalateReason = "Refinery rebases MRs onto merge-target; while it has diverged from the source branch, every MR rebase conflicts and the queue stops draining. Resync merge-target (see `gt sync merge-target`)." |
| 204 | + escalateSource = "gt sync merge-target" |
| 205 | + escalateFingerprint = "sync-merge-target-divergence" |
| 206 | + escalateRelatedBead = "" |
| 207 | + escalateDryRun = false |
| 208 | + escalateStdin = false |
| 209 | + escalateJSON = false |
| 210 | + |
| 211 | + return runEscalate(cmd, []string{description}) |
| 212 | +} |
0 commit comments