gunk-dev/the-valley

publish.go

source8043b5e912d0main's published tip history
4.0 KiB
1package main
2
3import (
4 "fmt"
5 "strings"
6)
7
8// publish pushes the branch and the signer's attestation ref to remote in
9// one atomic push, or the ref alone when branchSpec is "". notes holds
10// this run's notes, one subtree per check.
11//
12// The remote takes an existing attestation ref only with added notes: an
13// update keeps every note the ref holds, byte for byte. So when the remote
14// already holds ref, the tree pushed is the one it holds with this run's
15// notes beside it, and the local ref is moved to that tree. A check the
16// held tree already has a note for keeps that note, and this run's note
17// for it is not published. The push leases ref at the value read, so a
18// writer that moved it meanwhile is never overwritten; the ref is then
19// read again and the push tried once more.
20func publish(root, remote, branchSpec, ref string, notes map[string]string) error {
21 at := pushURL(root, remote)
22 held, err := remoteRef(root, at, ref)
23 if err != nil {
24 return err
25 }
26 err = publishOnto(root, remote, at, branchSpec, ref, held, notes)
27 if err == nil {
28 return nil
29 }
30 now, rerr := remoteRef(root, at, ref)
31 if rerr != nil || now == held {
32 return err
33 }
34 fmt.Printf("moved %s on %s while this run published; read again\n", ref, remote)
35 return publishOnto(root, remote, at, branchSpec, ref, now, notes)
36}
37
38// pushURL is where a push to remote goes, so the ref is read from the
39// repository it is pushed to: a configured remote's push URL, or remote
40// itself when it is a URL or a path.
41func pushURL(root, remote string) string {
42 out, err := gitRemote(root, "remote", "get-url", "--push", remote)
43 if url := strings.TrimSpace(out); err == nil && url != "" {
44 return url
45 }
46 return remote
47}
48
49// publishOnto pushes the branch and ref, as held on the remote ("" for
50// none) with notes added to it. at is the remote's push URL, which held is
51// fetched from.
52func publishOnto(root, remote, at, branchSpec, ref, held string, notes map[string]string) error {
53 entries, added := []string{}, 0
54 if held != "" {
55 if _, err := gitRemote(root, "fetch", "--quiet", "--no-tags", "--no-write-fetch-head", at, held); err != nil {
56 return err
57 }
58 listing, err := git(root, "ls-tree", held)
59 if err != nil {
60 return err
61 }
62 entries = strings.Split(strings.TrimSuffix(listing, "\n"), "\n")
63 }
64 has := map[string]bool{}
65 for _, e := range entries {
66 if _, name, ok := strings.Cut(e, "\t"); ok {
67 has[name] = true
68 }
69 }
70 for _, check := range sortedKeys(notes) {
71 if has[check] {
72 fmt.Printf("held %s: %s already holds this signer's note for it; nothing added\n", check, remote)
73 continue
74 }
75 entries = append(entries, fmt.Sprintf("040000 tree %s\t%s", notes[check], check))
76 added++
77 }
78
79 args := []string{"--atomic"}
80 var specs []string
81 if branchSpec != "" {
82 specs = append(specs, branchSpec)
83 }
84 if added > 0 {
85 tree, err := gitInput(root, []byte(strings.Join(entries, "\n")+"\n"), "mktree")
86 if err != nil {
87 return err
88 }
89 tree = strings.TrimSpace(tree)
90 if held != "" {
91 if _, err := git(root, "update-ref", ref, tree); err != nil {
92 return err
93 }
94 fmt.Printf("added %d of %d notes to %s on %s -> %s\n", added, len(notes), ref, remote, tree)
95 }
96 // The tree is pushed by its id: another run may move the local
97 // ref before this push reads it.
98 args = append(args, "--force-with-lease="+ref+":"+held)
99 specs = append(specs, tree+":"+ref)
100 }
101 if len(specs) == 0 {
102 return nil // the remote holds every note this run made
103 }
104 args = append(append(args, remote), specs...)
105 out, err := gitRemote(root, append([]string{"push"}, args...)...)
106 if err != nil {
107 return err
108 }
109 fmt.Printf("pushed %s\n%s", strings.Join(args, " "), out)
110 return nil
111}
112
113// remoteRef reads ref's value on remote, or "" when the remote has none.
114func remoteRef(root, remote, ref string) (string, error) {
115 out, err := gitRemote(root, "ls-remote", "--refs", remote, ref)
116 if err != nil {
117 return "", err
118 }
119 for _, line := range strings.Split(out, "\n") {
120 if id, name, ok := strings.Cut(line, "\t"); ok && name == ref {
121 return id, nil
122 }
123 }
124 return "", nil
125}