| 1 | package cmd |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "log/slog" |
| 6 | "os" |
| 7 | "os/exec" |
| 8 | "strings" |
| 9 | |
| 10 | "the-valley/flow/internal/event" |
| 11 | "the-valley/flow/internal/git" |
| 12 | "the-valley/flow/internal/run" |
| 13 | ) |
| 14 | |
| 15 | // pushedHead is a head on origin that a run pushed, and the change its |
| 16 | // Change-Id trailer names ("" when it names none klaus can use). |
| 17 | type pushedHead struct { |
| 18 | branch, sha, changeID string |
| 19 | } |
| 20 | |
| 21 | // recordPushedChange makes the run's change and branch the ones it actually |
| 22 | // pushed. Launch records a change and branch for the run, but an agent may |
| 23 | // leave them, as one launched to continue a change does when it makes a new |
| 24 | // change instead: it switches to a branch of its own, and its commits carry |
| 25 | // their own Change-Id. |
| 26 | // |
| 27 | // The branches finalize looks at are the one the worktree ends on and |
| 28 | // launch's, other than defaultBranch. A branch's head on origin counts as |
| 29 | // pushed by the run when the local branch contains it, as it does after a |
| 30 | // push from the worktree, and the head the run started from (launchHead) |
| 31 | // doesn't. Its change is its Change-Id trailer. |
| 32 | // Of the heads the run pushed, the run's change is the one its final head |
| 33 | // belongs to: the pushed head whose change the final head's trailer names, |
| 34 | // else the head of the branch the worktree ends on. Finalize records that |
| 35 | // change, its branch and its head, and emits agent:change-recorded when they |
| 36 | // aren't launch's or when the run pushed heads of more than one change. |
| 37 | // Moving to another change drops the runs launch recorded as taken over |
| 38 | // (TookOver), which are the launch change's. When the run pushed nothing, |
| 39 | // or nothing that names another branch or change, launch's values stay. |
| 40 | func recordPushedChange(ctx context.Context, baseDir string, state *run.State, launchHead, defaultBranch string) { |
| 41 | dir, final := state.Worktree, state.HeadSHA |
| 42 | if dir == "" || final == "" { |
| 43 | return |
| 44 | } |
| 45 | if _, err := os.Stat(dir); err != nil { |
| 46 | return |
| 47 | } |
| 48 | var branches []string |
| 49 | if cur := currentBranch(ctx, dir); cur != "" && cur != state.Branch { |
| 50 | branches = append(branches, cur) |
| 51 | } |
| 52 | if state.Branch != "" { |
| 53 | branches = append(branches, state.Branch) |
| 54 | } |
| 55 | var heads []pushedHead |
| 56 | for _, b := range branches { |
| 57 | if b == defaultBranch { |
| 58 | continue |
| 59 | } |
| 60 | sha, err := git.RemoteBranchSHA(ctx, dir, b) |
| 61 | if err != nil || sha == "" { |
| 62 | continue |
| 63 | } |
| 64 | if !git.IsAncestor(ctx, dir, sha, "refs/heads/"+b) || launchHead != "" && git.IsAncestor(ctx, dir, sha, launchHead) { |
| 65 | continue |
| 66 | } |
| 67 | heads = append(heads, pushedHead{branch: b, sha: sha, changeID: changeIDOf(ctx, dir, sha)}) |
| 68 | } |
| 69 | if len(heads) == 0 { |
| 70 | return |
| 71 | } |
| 72 | chosen := heads[0] |
| 73 | if want := changeIDOf(ctx, dir, final); want != "" { |
| 74 | for _, h := range heads { |
| 75 | if h.changeID == want { |
| 76 | chosen = h |
| 77 | break |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | changes := make(map[string]bool) |
| 82 | for _, h := range heads { |
| 83 | if h.changeID != "" { |
| 84 | changes[h.changeID] = true |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | launchChange, launchBranch := state.ChangeID, state.Branch |
| 89 | if chosen.changeID != "" && chosen.changeID != launchChange { |
| 90 | state.ChangeID = chosen.changeID |
| 91 | state.TookOver = nil |
| 92 | if state.Unready != nil && state.Unready.ChangeID == "" { |
| 93 | state.Unready.ChangeID = launchChange |
| 94 | } |
| 95 | } |
| 96 | state.Branch = chosen.branch |
| 97 | branch, sha := chosen.branch, chosen.sha |
| 98 | state.PushedBranch, state.PushedSHA = &branch, &sha |
| 99 | state.PushedSubject = commitSubject(ctx, dir, sha) |
| 100 | |
| 101 | if state.ChangeID == launchChange && state.Branch == launchBranch && len(changes) < 2 { |
| 102 | return |
| 103 | } |
| 104 | data := map[string]interface{}{ |
| 105 | "id": state.ID, |
| 106 | "change_id": state.ChangeID, |
| 107 | "branch": branch, |
| 108 | "head": sha, |
| 109 | "launch_change_id": launchChange, |
| 110 | "launch_branch": launchBranch, |
| 111 | } |
| 112 | if len(changes) > 1 { |
| 113 | var pushed []interface{} |
| 114 | for _, h := range heads { |
| 115 | pushed = append(pushed, map[string]interface{}{"change_id": h.changeID, "branch": h.branch, "head": h.sha}) |
| 116 | } |
| 117 | data["pushed"] = pushed |
| 118 | } |
| 119 | slog.Info("the run pushed another change or branch than launch recorded", "id", state.ID, |
| 120 | "change_id", state.ChangeID, "branch", branch, "launch_change_id", launchChange, "launch_branch", launchBranch) |
| 121 | if baseDir != "" { |
| 122 | emitRunEvent(baseDir, state, event.AgentChangeRecorded, data) |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | // currentBranch is the branch checked out in dir, or "" for a detached HEAD. |
| 127 | func currentBranch(ctx context.Context, dir string) string { |
| 128 | out, err := exec.CommandContext(ctx, "git", "-C", dir, "symbolic-ref", "--quiet", "--short", "HEAD").Output() |
| 129 | if err != nil { |
| 130 | return "" |
| 131 | } |
| 132 | return strings.TrimSpace(string(out)) |
| 133 | } |
| 134 | |
| 135 | // changeIDOf is the change ID commit rev's Change-Id trailer names in dir, |
| 136 | // or "" when it names none in the form klaus gives change IDs. |
| 137 | func changeIDOf(ctx context.Context, dir, rev string) string { |
| 138 | id, err := git.CommitChangeID(ctx, dir, rev) |
| 139 | if err != nil || !run.ValidChangeID(id) { |
| 140 | return "" |
| 141 | } |
| 142 | return id |
| 143 | } |