| 1 | // Package forgetest is a local stand-in for the hub's merge API, for |
| 2 | // tests: it merges a task branch into a bare repository's default branch |
| 3 | // the way the hub's repository instance does (the branch must be at the |
| 4 | // expected commit; a conflict is refused; a merge commit with two |
| 5 | // parents; the branch is deleted). |
| 6 | package forgetest |
| 7 | |
| 8 | import ( |
| 9 | "context" |
| 10 | "errors" |
| 11 | "net/http" |
| 12 | "os/exec" |
| 13 | "strings" |
| 14 | |
| 15 | "amadan.net/oficina/equipo/internal/runner/forge" |
| 16 | ) |
| 17 | |
| 18 | // Hub is the hub's behaviour where it differs by release. |
| 19 | type Hub struct { |
| 20 | // CreatesUnbornDefault: the first merge into a repository whose |
| 21 | // default branch has no commits creates it at the branch's tip (the |
| 22 | // hub from amadan#139); without it the merge is refused unborn_default. |
| 23 | CreatesUnbornDefault bool |
| 24 | } |
| 25 | |
| 26 | // Merge merges branch into the default branch of the bare repository at |
| 27 | // dir, as the hub would answer req. |
| 28 | func Merge(ctx context.Context, git, dir, branch string, req forge.MergeRequest) (forge.MergeResult, error) { |
| 29 | return Hub{}.Merge(ctx, git, dir, branch, req) |
| 30 | } |
| 31 | |
| 32 | // Merge is the hub's merge API on a local bare repository. |
| 33 | func (h Hub) Merge(ctx context.Context, git, dir, branch string, req forge.MergeRequest) (forge.MergeResult, error) { |
| 34 | run := func(env []string, args ...string) (string, error) { |
| 35 | cmd := exec.CommandContext(ctx, git, append([]string{"--git-dir=" + dir}, args...)...) |
| 36 | cmd.Env = append([]string{"PATH=/usr/bin:/bin", "HOME=/nonexistent", "GIT_CONFIG_NOSYSTEM=1", "GIT_CONFIG_GLOBAL=/dev/null"}, env...) |
| 37 | out, err := cmd.Output() |
| 38 | return strings.TrimSpace(string(out)), err |
| 39 | } |
| 40 | def, err := run(nil, "symbolic-ref", "--short", "HEAD") |
| 41 | if err != nil { |
| 42 | return forge.MergeResult{}, err |
| 43 | } |
| 44 | if strings.HasPrefix(def, "equipo/") { |
| 45 | // A rebuild pointed HEAD at a task branch; the hub's stored head, |
| 46 | // the default it was created with, is main. The hub before the |
| 47 | // update read HEAD itself. |
| 48 | if !h.CreatesUnbornDefault && def == branch { |
| 49 | return forge.MergeResult{Status: http.StatusBadRequest, Reason: "is_default"}, nil |
| 50 | } |
| 51 | def = "main" |
| 52 | } |
| 53 | head, err := run(nil, "rev-parse", "--verify", "--quiet", "refs/heads/"+branch) |
| 54 | if err != nil { |
| 55 | return forge.MergeResult{Status: http.StatusNotFound, Reason: "no_such_branch"}, nil |
| 56 | } |
| 57 | if head != req.ExpectSHA { |
| 58 | return forge.MergeResult{Status: http.StatusConflict, Reason: "moved"}, nil |
| 59 | } |
| 60 | tip, err := run(nil, "rev-parse", "--verify", "refs/heads/"+def) |
| 61 | if err != nil { |
| 62 | // An unborn default: refused before the update, created at the |
| 63 | // branch's tip after it (create-only: someone else's is "moved"). |
| 64 | if !h.CreatesUnbornDefault { |
| 65 | return forge.MergeResult{Status: http.StatusConflict, Reason: "unborn_default"}, nil |
| 66 | } |
| 67 | if _, err := run(nil, "update-ref", "refs/heads/"+def, head, ""); err != nil { |
| 68 | return forge.MergeResult{Status: http.StatusConflict, Reason: "moved"}, nil |
| 69 | } |
| 70 | // As the hub since 53d8125: HEAD names the new default, and the |
| 71 | // source branch is kept (branch_closed false). |
| 72 | if _, err := run(nil, "symbolic-ref", "HEAD", "refs/heads/"+def); err != nil { |
| 73 | return forge.MergeResult{}, err |
| 74 | } |
| 75 | return forge.MergeResult{Status: http.StatusOK, Merged: true, SHA: head, Into: def, BranchClosed: false}, nil |
| 76 | } |
| 77 | if _, err := run(nil, "merge-base", "--is-ancestor", head, tip); err == nil { |
| 78 | return forge.MergeResult{Status: http.StatusConflict, Reason: "up_to_date"}, nil |
| 79 | } |
| 80 | out, err := run(nil, "merge-tree", "--write-tree", tip, head) |
| 81 | var exit *exec.ExitError |
| 82 | if errors.As(err, &exit) && exit.ExitCode() == 1 { |
| 83 | return forge.MergeResult{Status: http.StatusConflict, Reason: "conflicts"}, nil |
| 84 | } else if err != nil { |
| 85 | return forge.MergeResult{}, err |
| 86 | } |
| 87 | tree := strings.SplitN(out, "\n", 2)[0] |
| 88 | who := []string{"GIT_AUTHOR_NAME=hub", "GIT_AUTHOR_EMAIL=hub@example.com", "GIT_COMMITTER_NAME=hub", "GIT_COMMITTER_EMAIL=hub@example.com"} |
| 89 | msg := "Merge " + branch + " into " + def |
| 90 | if req.Message != "" { |
| 91 | msg += "\n\n" + req.Message |
| 92 | } |
| 93 | args := []string{"commit-tree", tree, "-p", tip} |
| 94 | if req.Strategy != "squash" { |
| 95 | args = append(args, "-p", head) |
| 96 | } |
| 97 | sha, err := run(who, append(args, "-m", msg)...) |
| 98 | if err != nil { |
| 99 | return forge.MergeResult{}, err |
| 100 | } |
| 101 | if _, err := run(nil, "update-ref", "refs/heads/"+def, sha, tip); err != nil { |
| 102 | return forge.MergeResult{Status: http.StatusConflict, Reason: "moved"}, nil |
| 103 | } |
| 104 | closed := false |
| 105 | if !req.KeepBranch { |
| 106 | _, err := run(nil, "update-ref", "-d", "refs/heads/"+branch, head) |
| 107 | closed = err == nil |
| 108 | } |
| 109 | return forge.MergeResult{Status: http.StatusOK, Merged: true, SHA: sha, Into: def, BranchClosed: closed}, nil |
| 110 | } |
| 111 | |