Attach a refs/notes/byre note to each squash merge and push it

This commit is contained in:
Ian Gulliver
2026-10-15 19:44:53 -07:00
parent c5e3a680d8
commit 6741177f53
8 changed files with 275 additions and 11 deletions
+75
View File
@@ -0,0 +1,75 @@
package git
import (
"fmt"
"os"
"path/filepath"
"slices"
"strings"
)
const NotesRef = "refs/notes/byre"
const notesFetch = NotesRef + "*:" + NotesRef + "*"
func ConfigureNotesFetch(repo string) error {
key := "remote." + Remote + ".fetch"
out, stderr, code, err := runCode(repo, nil, "config", "--get-all", key)
if err != nil {
return err
}
if code != 0 && code != 1 {
return fmt.Errorf("git config --get-all %s: exit status %d: %s", key, code, stderr)
}
if slices.Contains(strings.Split(out, "\n"), notesFetch) {
return nil
}
_, err = run(repo, "config", "--add", key, notesFetch)
return err
}
func NoteCommit(repo, commit, note string, env []string) (string, error) {
blob, err := runInput(repo, nil, note, "hash-object", "-w", "--stdin")
if err != nil {
return "", err
}
parent, stderr, code, err := runCode(repo, nil, "rev-parse", "--verify", "--quiet", NotesRef)
if err != nil {
return "", err
}
if code != 0 && code != 1 {
return "", fmt.Errorf("git rev-parse %s: exit status %d: %s", NotesRef, code, stderr)
}
dir, err := os.MkdirTemp("", "byre-notes-*")
if err != nil {
return "", err
}
defer os.RemoveAll(dir)
index := []string{"GIT_INDEX_FILE=" + filepath.Join(dir, "index")}
commitArgs := []string{"commit-tree", "-m", "Notes added by byre"}
if code == 0 {
_, err = runEnv(repo, index, "read-tree", parent)
if err != nil {
return "", err
}
commitArgs = append(commitArgs, "-p", parent)
}
_, err = runEnv(repo, index, "update-index", "--add", "--cacheinfo", "100644,"+blob+","+commit)
if err != nil {
return "", err
}
tree, err := runEnv(repo, index, "write-tree")
if err != nil {
return "", err
}
return runEnv(repo, env, append(commitArgs, tree)...)
}
func SetNotes(repo, notes string) error {
_, err := run(repo, "update-ref", NotesRef, notes)
return err
}