76 lines
1.8 KiB
Go
76 lines
1.8 KiB
Go
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
|
|
}
|