package git import ( "context" "fmt" "strings" ) func snapshotPrefix(taskID string) string { return "refs/byre/tasks/" + taskID + "/snap/" } func SnapshotRef(taskID string, n int) string { return fmt.Sprintf("%s%d", snapshotPrefix(taskID), n) } func CommitSnapshot(ctx context.Context, dir, tree, ref, message string, env []string) (string, error) { head, err := Head(ctx, dir) if err != nil { return "", err } commit, err := runEnv(ctx, dir, env, "commit-tree", tree, "-p", head, "-m", message) if err != nil { return "", err } _, err = run(ctx, dir, "update-ref", ref, commit, "") if err != nil { return "", err } return commit, nil } func SnapshotRefs(ctx context.Context, repo, taskID string) ([]string, error) { out, err := run(ctx, repo, "for-each-ref", "--format=%(refname)", snapshotPrefix(taskID)) if err != nil { return nil, err } if out == "" { return []string{}, nil } return strings.Split(out, "\n"), nil } func DeleteSnapshotRefs(ctx context.Context, repo, taskID string) error { refs, err := SnapshotRefs(ctx, repo, taskID) if err != nil { return err } if len(refs) == 0 { return nil } b := &strings.Builder{} for _, ref := range refs { fmt.Fprintf(b, "delete %s\n", ref) } _, err = runInput(ctx, repo, nil, b.String(), "update-ref", "--stdin") return err }