Capture diagnostic identities and rehearsal records

This commit is contained in:
2026-12-05 22:45:28 +00:00
parent 6f01e9ef0a
commit 181e5a030c
19 changed files with 1284 additions and 60 deletions

View File

@@ -47,6 +47,22 @@ func RepositoryRoot(ctx context.Context, directory string) (string, error) {
return normalizeRepositoryRoot(root)
}
// ResolveCommit resolves a revision to its full commit ID without changing the checkout.
func ResolveCommit(ctx context.Context, repositoryDir, revision string) (string, error) {
if revision == "" {
revision = "HEAD"
}
output, err := run(ctx, repositoryDir, nil, "rev-parse", "--verify", "--end-of-options", revision+"^{commit}")
if err != nil {
return "", fmt.Errorf("resolve selected revision: %w", err)
}
commit := strings.TrimSpace(output)
if !validObjectID(commit) {
return "", fmt.Errorf("selected revision did not resolve to a full commit ID")
}
return commit, nil
}
// CheckTaggerIdentity checks Git's native committer identity without creating a tag.
func CheckTaggerIdentity(ctx context.Context, directory string) error {
if _, err := run(ctx, directory, nil, "var", "GIT_COMMITTER_IDENT"); err != nil {

View File

@@ -9,6 +9,79 @@ import (
"strings"
)
// AnnotatedTag is the immutable tag object and its direct commit target.
type AnnotatedTag struct {
Object string
Commit string
Message []byte
}
// ReadAnnotatedTag reads an exact local tag and requires a direct commit target.
func ReadAnnotatedTag(ctx context.Context, repositoryDir, tag string) (AnnotatedTag, error) {
if err := CheckTagRef(ctx, repositoryDir, tag); err != nil {
return AnnotatedTag{}, err
}
ref := "refs/tags/" + tag
object, err := run(ctx, repositoryDir, nil, "rev-parse", "--verify", ref)
if err != nil {
return AnnotatedTag{}, fmt.Errorf("resolve annotated tag: %w", err)
}
object = strings.TrimSpace(object)
if !validObjectID(object) {
return AnnotatedTag{}, errors.New("invalid annotated tag object")
}
tagType, err := run(ctx, repositoryDir, nil, "cat-file", "-t", object)
if err != nil {
return AnnotatedTag{}, fmt.Errorf("inspect annotated tag: %w", err)
}
if strings.TrimSpace(tagType) != "tag" {
return AnnotatedTag{}, errors.New("diagnostic reference is not an annotated tag")
}
contents, err := run(ctx, repositoryDir, nil, "cat-file", "tag", object)
if err != nil {
return AnnotatedTag{}, fmt.Errorf("read annotated tag: %w", err)
}
header, message, ok := strings.Cut(contents, "\n\n")
if !ok {
return AnnotatedTag{}, errors.New("annotated tag has no message")
}
var target, targetType, tagName string
for _, line := range strings.Split(header, "\n") {
key, value, ok := strings.Cut(line, " ")
if !ok {
continue
}
switch key {
case "object":
if target != "" {
return AnnotatedTag{}, errors.New("annotated tag repeats its target")
}
target = value
case "type":
if targetType != "" {
return AnnotatedTag{}, errors.New("annotated tag repeats its type")
}
targetType = value
case "tag":
if tagName != "" {
return AnnotatedTag{}, errors.New("annotated tag repeats its name")
}
tagName = value
}
}
if !validObjectID(target) || targetType != "commit" || tagName != tag {
return AnnotatedTag{}, errors.New("annotated tag target or name does not match")
}
actualType, err := run(ctx, repositoryDir, nil, "cat-file", "-t", target)
if err != nil {
return AnnotatedTag{}, fmt.Errorf("inspect annotated tag target: %w", err)
}
if strings.TrimSpace(actualType) != "commit" {
return AnnotatedTag{}, errors.New("annotated tag target is not a commit")
}
return AnnotatedTag{Object: object, Commit: target, Message: []byte(message)}, nil
}
// CreateAnnotatedTag records the captured note as an unsigned annotated tag
// on the captured commit. Once its type and ID are established, the ID is
// returned even if a later verification fails.