Attach a refs/notes/byre note to each squash merge and push it
This commit is contained in:
+20
-3
@@ -3,6 +3,7 @@ package git
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
@@ -217,8 +218,8 @@ const Remote = "origin"
|
||||
|
||||
var ErrRemoteMoved = errors.New("the remote's branch has moved")
|
||||
|
||||
func Push(repo, commit, branch string) error {
|
||||
out, stderr, code, err := runCode(repo, nil, "push", "--porcelain", Remote, commit+":refs/heads/"+branch)
|
||||
func Push(repo, commit, branch, notes string) error {
|
||||
out, stderr, code, err := runCode(repo, nil, "push", "--atomic", "--porcelain", Remote, commit+":refs/heads/"+branch, notes+":"+NotesRef)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -239,7 +240,7 @@ func FastForward(repo, rev string) error {
|
||||
}
|
||||
|
||||
func FetchFastForward(repo, branch string) error {
|
||||
_, err := run(repo, "fetch", Remote, branch)
|
||||
_, err := run(repo, "fetch", Remote)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -303,10 +304,26 @@ func runEnv(dir string, env []string, args ...string) (string, error) {
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func runInput(dir string, env []string, input string, args ...string) (string, error) {
|
||||
out, stderr, code, err := runCodeInput(dir, env, strings.NewReader(input), args...)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if code != 0 {
|
||||
return "", fmt.Errorf("git %s: exit status %d: %s", strings.Join(args, " "), code, stderr)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func runCode(dir string, env []string, args ...string) (string, string, int, error) {
|
||||
return runCodeInput(dir, env, nil, args...)
|
||||
}
|
||||
|
||||
func runCodeInput(dir string, env []string, stdin io.Reader, args ...string) (string, string, int, error) {
|
||||
cmd := exec.Command("git", args...)
|
||||
cmd.Dir = dir
|
||||
cmd.Env = append(os.Environ(), env...)
|
||||
cmd.Stdin = stdin
|
||||
stderr := &strings.Builder{}
|
||||
cmd.Stderr = stderr
|
||||
out, err := cmd.Output()
|
||||
|
||||
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user