gunk-dev/the-valley

publish.go

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