Distinguish rejected rehearsal pushes from uncertain writes
This commit is contained in:
@@ -41,7 +41,13 @@ func checkDiagnosticTag(ctx context.Context, repo, tag, object, commit string) e
|
||||
|
||||
// PushDiagnosticTag publishes a verified local diagnostic object without
|
||||
// moving a conflicting remote ref. Callers reconcile an error by observation.
|
||||
func (endpoint Endpoint) PushDiagnosticTag(ctx context.Context, repo, tag, object, commit string) error {
|
||||
func (endpoint Endpoint) PushDiagnosticTag(ctx context.Context, repo, tag, object, commit string) (resultErr error) {
|
||||
delegated := false
|
||||
defer func() {
|
||||
if resultErr != nil && !delegated {
|
||||
resultErr = &PushError{PushNotAttempted, resultErr}
|
||||
}
|
||||
}()
|
||||
if err := checkDiagnosticTag(ctx, repo, tag, object, commit); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -62,6 +68,7 @@ func (endpoint Endpoint) PushDiagnosticTag(ctx context.Context, repo, tag, objec
|
||||
if remote.State != RefAbsent {
|
||||
return fmt.Errorf("diagnostic tag already exists with different evidence: %w", ErrRemoteTagConflict)
|
||||
}
|
||||
delegated = true
|
||||
return endpoint.PushTag(ctx, repo, tag, object)
|
||||
}
|
||||
|
||||
|
||||
@@ -2,10 +2,58 @@ package git
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestDiagnosticPushRetainsNonDispatchEvidence(t *testing.T) {
|
||||
fixture := newRemoteFixture(t)
|
||||
endpoint, err := Resolve(t.Context(), fixture.work, "origin")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
tag := "releasekit-test/root/" + strings.Repeat("b", 32)
|
||||
object, err := CreateAnnotatedTag(t.Context(), fixture.work, tag, fixture.commit, []byte("diagnostic\n"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var failure *PushError
|
||||
err = endpoint.PushDiagnosticTag(t.Context(), fixture.work, tag, strings.Repeat("c", len(object)), fixture.commit)
|
||||
if !errors.As(err, &failure) || failure.Disposition != PushNotAttempted {
|
||||
t.Fatalf("pre-dispatch error=%v evidence=%+v", err, failure)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(fixture.bare, "hooks", "pre-receive"), []byte("#!/bin/sh\nexit 1\n"), 0700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
err = endpoint.PushDiagnosticTag(t.Context(), fixture.work, tag, object, fixture.commit)
|
||||
if !errors.As(err, &failure) || failure.Disposition != PushRejected {
|
||||
t.Fatalf("rejected error=%v evidence=%+v", err, failure)
|
||||
}
|
||||
observed, err := endpoint.QueryTag(t.Context(), fixture.work, tag)
|
||||
if err != nil || observed.State != RefAbsent {
|
||||
t.Fatalf("rejected ref=%+v error=%v", observed, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPushRejectionRequiresDefinitiveExactRefStatus(t *testing.T) {
|
||||
ref := "refs/tags/releasekit-test/root/" + strings.Repeat("a", 32)
|
||||
for _, test := range []struct {
|
||||
summary, destination string
|
||||
want bool
|
||||
}{
|
||||
{"[remote rejected] (pre-receive hook declined)", ref, true},
|
||||
{"[rejected] (already exists)", ref, true},
|
||||
{"[remote failure] (remote failed to report status)", ref, false},
|
||||
{"[remote rejected] (pre-receive hook declined)", "refs/tags/foreign", false},
|
||||
} {
|
||||
if got := rejectedPush("!\tobject:"+test.destination+"\t"+test.summary+"\n", ref); got != test.want {
|
||||
t.Fatalf("summary=%q destination=%q result=%t", test.summary, test.destination, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestDiagnosticTagDeletionRequiresRecordedObjectAndCommit(t *testing.T) {
|
||||
fixture := newRemoteFixture(t)
|
||||
ctx := t.Context()
|
||||
|
||||
@@ -291,6 +291,14 @@ func pushURLs(ctx context.Context, repositoryDir, remoteName string) ([]string,
|
||||
}
|
||||
|
||||
func run(ctx context.Context, dir string, config []string, args ...string) (string, error) {
|
||||
return runWithEnvironment(ctx, dir, config, nil, args...)
|
||||
}
|
||||
|
||||
func runWithEnvironment(ctx context.Context, dir string, config []string, environment map[string]string, args ...string) (string, error) {
|
||||
if environment == nil {
|
||||
environment = make(map[string]string)
|
||||
}
|
||||
environment["GIT_NO_REPLACE_OBJECTS"] = "1"
|
||||
commandArgs := make([]string, 0, len(config)*2+len(args))
|
||||
for _, value := range config {
|
||||
commandArgs = append(commandArgs, "-c", value)
|
||||
@@ -300,7 +308,7 @@ func run(ctx context.Context, dir string, config []string, args ...string) (stri
|
||||
err := process.Run(ctx, process.Invocation{
|
||||
Args: append([]string{"git"}, commandArgs...),
|
||||
Dir: dir,
|
||||
Env: map[string]string{"GIT_NO_REPLACE_OBJECTS": "1"},
|
||||
Env: environment,
|
||||
Stdout: &stdout,
|
||||
Stderr: &stderr,
|
||||
})
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@@ -203,17 +204,64 @@ func (endpoint Endpoint) QueryTag(ctx context.Context, repositoryDir, tag string
|
||||
return finalizeRef(remoteTag), nil
|
||||
}
|
||||
|
||||
// PushDisposition describes evidence from one exact tag push.
|
||||
type PushDisposition string
|
||||
|
||||
const (
|
||||
PushNotAttempted PushDisposition = "not_attempted"
|
||||
PushRejected PushDisposition = "rejected"
|
||||
PushUnknown PushDisposition = "unknown"
|
||||
)
|
||||
|
||||
// PushError preserves the cause and evidence about whether dispatch occurred.
|
||||
type PushError struct {
|
||||
Disposition PushDisposition
|
||||
Err error
|
||||
}
|
||||
|
||||
func (err *PushError) Error() string { return err.Err.Error() }
|
||||
func (err *PushError) Unwrap() error { return err.Err }
|
||||
|
||||
// PushTag publishes only the captured tag object to its exact tag ref.
|
||||
func (endpoint Endpoint) PushTag(ctx context.Context, repositoryDir, tag, tagObject string) error {
|
||||
if err := CheckTagRef(ctx, repositoryDir, tag); err != nil {
|
||||
return err
|
||||
return &PushError{PushNotAttempted, err}
|
||||
}
|
||||
if !validObjectID(tagObject) {
|
||||
return fmt.Errorf("invalid tag publication object %q at %q", tagObject, tag)
|
||||
return &PushError{PushNotAttempted, fmt.Errorf("invalid tag publication object %q at %q", tagObject, tag)}
|
||||
}
|
||||
refspec := tagObject + ":refs/tags/" + tag
|
||||
if _, err := run(ctx, repositoryDir, endpoint.scopedConfig(), "push", "--no-follow-tags", "--recurse-submodules=no", "--porcelain", endpoint.ScopedName, refspec); err != nil {
|
||||
return fmt.Errorf("push annotated tag %s to %s: %w", tag, DisplayURL(endpoint.PushURL), err)
|
||||
if err := ctx.Err(); err != nil {
|
||||
return &PushError{PushNotAttempted, err}
|
||||
}
|
||||
return nil
|
||||
ref := "refs/tags/" + tag
|
||||
output, err := runWithEnvironment(ctx, repositoryDir, endpoint.scopedConfig(), map[string]string{"LC_ALL": "C"}, "push", "--no-follow-tags", "--recurse-submodules=no", "--porcelain", endpoint.ScopedName, tagObject+":"+ref)
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
disposition := PushUnknown
|
||||
var start *exec.Error
|
||||
var pathError *os.PathError
|
||||
if errors.As(err, &start) || errors.As(err, &pathError) {
|
||||
disposition = PushNotAttempted
|
||||
} else if rejectedPush(output, ref) {
|
||||
disposition = PushRejected
|
||||
}
|
||||
return &PushError{disposition, fmt.Errorf("push annotated tag %s to %s: %w", tag, DisplayURL(endpoint.PushURL), err)}
|
||||
}
|
||||
|
||||
func rejectedPush(output, ref string) bool {
|
||||
count, rejected := 0, false
|
||||
for _, line := range strings.Split(output, "\n") {
|
||||
fields := strings.SplitN(line, "\t", 3)
|
||||
if len(fields) != 3 {
|
||||
continue
|
||||
}
|
||||
_, destination, ok := strings.Cut(fields[1], ":")
|
||||
if !ok || destination != ref {
|
||||
continue
|
||||
}
|
||||
count++
|
||||
rejected = fields[0] == "!" && (strings.HasPrefix(fields[2], "[rejected]") || strings.HasPrefix(fields[2], "[remote rejected]"))
|
||||
}
|
||||
return count == 1 && rejected
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user