Observe rehearsal resources and support safe cleanup
This commit is contained in:
80
internal/git/diagnostic.go
Normal file
80
internal/git/diagnostic.go
Normal file
@@ -0,0 +1,80 @@
|
||||
package git
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func checkDiagnosticTag(ctx context.Context, repo, tag, object, commit string) error {
|
||||
if !strings.HasPrefix(tag, "releasekit-test/") || !ValidObjectID(object) || !ValidObjectID(commit) {
|
||||
return errors.New("a diagnostic tag and exact tag object and commit are required")
|
||||
}
|
||||
return CheckTagRef(ctx, repo, tag)
|
||||
}
|
||||
|
||||
// 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 {
|
||||
if err := checkDiagnosticTag(ctx, repo, tag, object, commit); err != nil {
|
||||
return err
|
||||
}
|
||||
local, err := ReadAnnotatedTag(ctx, repo, tag)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !EqualObjectID(local.Object, object) || !EqualObjectID(local.Commit, commit) {
|
||||
return errors.New("local diagnostic tag differs from recorded object or commit")
|
||||
}
|
||||
remote, err := endpoint.QueryTag(ctx, repo, tag)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if remote.State == RefPresent && EqualObjectID(remote.Object, object) && EqualObjectID(remote.Peeled, commit) {
|
||||
return nil
|
||||
}
|
||||
if remote.State != RefAbsent {
|
||||
return fmt.Errorf("diagnostic tag already exists with different evidence: %w", ErrRemoteTagConflict)
|
||||
}
|
||||
return endpoint.PushTag(ctx, repo, tag, object)
|
||||
}
|
||||
|
||||
// DeleteDiagnosticTag deletes only the recorded remote annotated tag object.
|
||||
// A failed push remains uncertain; callers must query the endpoint again.
|
||||
func (endpoint Endpoint) DeleteDiagnosticTag(ctx context.Context, repo, tag, object, commit string) error {
|
||||
if err := checkDiagnosticTag(ctx, repo, tag, object, commit); err != nil {
|
||||
return err
|
||||
}
|
||||
observed, err := endpoint.QueryTag(ctx, repo, tag)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if observed.State == RefAbsent {
|
||||
return nil
|
||||
}
|
||||
if observed.State != RefPresent || !EqualObjectID(observed.Object, object) || !EqualObjectID(observed.Peeled, commit) {
|
||||
return fmt.Errorf("diagnostic tag differs from recorded object or commit: %w", ErrRemoteTagConflict)
|
||||
}
|
||||
ref := "refs/tags/" + tag
|
||||
if _, err := run(ctx, repo, endpoint.scopedConfig(), "push", "--no-follow-tags", "--recurse-submodules=no", "--porcelain", "--force-with-lease="+ref+":"+object, endpoint.ScopedName, ":"+ref); err != nil {
|
||||
return fmt.Errorf("delete diagnostic tag %s at %s: %w", tag, DisplayURL(endpoint.PushURL), err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeleteLocalDiagnosticTag uses Git's atomic expected-old-object check.
|
||||
func DeleteLocalDiagnosticTag(ctx context.Context, repo, tag, object, commit string) error {
|
||||
if err := checkDiagnosticTag(ctx, repo, tag, object, commit); err != nil {
|
||||
return err
|
||||
}
|
||||
observed, err := ReadAnnotatedTag(ctx, repo, tag)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !EqualObjectID(observed.Object, object) || !EqualObjectID(observed.Commit, commit) {
|
||||
return errors.New("local diagnostic tag differs from recorded object or commit")
|
||||
}
|
||||
_, err = run(ctx, repo, nil, "update-ref", "-d", "refs/tags/"+tag, object)
|
||||
return err
|
||||
}
|
||||
43
internal/git/diagnostic_test.go
Normal file
43
internal/git/diagnostic_test.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package git
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestDiagnosticTagDeletionRequiresRecordedObjectAndCommit(t *testing.T) {
|
||||
fixture := newRemoteFixture(t)
|
||||
ctx := t.Context()
|
||||
endpoint, err := Resolve(ctx, fixture.work, "origin")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
tag := "releasekit-test/root/" + strings.Repeat("a", 32)
|
||||
object, err := CreateAnnotatedTag(ctx, fixture.work, tag, fixture.commit, []byte("diagnostic\n"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := endpoint.PushDiagnosticTag(ctx, fixture.work, tag, object, fixture.commit); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := endpoint.PushDiagnosticTag(ctx, fixture.work, tag, object, fixture.commit); err != nil {
|
||||
t.Fatalf("identical push: %v", err)
|
||||
}
|
||||
if err := endpoint.DeleteDiagnosticTag(ctx, fixture.work, tag, object, strings.Repeat("b", 40)); !errors.Is(err, ErrRemoteTagConflict) {
|
||||
t.Fatalf("incorrect commit error=%v", err)
|
||||
}
|
||||
if err := DeleteLocalDiagnosticTag(ctx, fixture.work, tag, strings.Repeat("b", 40), fixture.commit); err == nil {
|
||||
t.Fatal("deleted tag with incorrect object")
|
||||
}
|
||||
if err := endpoint.DeleteDiagnosticTag(ctx, fixture.work, tag, object, fixture.commit); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ref, err := endpoint.QueryTag(ctx, fixture.work, tag)
|
||||
if err != nil || ref.State != RefAbsent {
|
||||
t.Fatalf("remote=%+v error=%v", ref, err)
|
||||
}
|
||||
if err := DeleteLocalDiagnosticTag(ctx, fixture.work, tag, object, fixture.commit); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user