Skip to content

Commit 2e14aab

Browse files
committed
Support SHA-256 attribution object IDs
1 parent 70d1cae commit 2e14aab

2 files changed

Lines changed: 28 additions & 4 deletions

File tree

cmd/entire/cli/attribution.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -274,8 +274,8 @@ func resolveFileAttribution(ctx context.Context, file string, fetchOnMiss bool)
274274
}
275275
continue
276276
}
277-
if ctx, ok := resolver.checkpointCache[candidate.CheckpointID]; ok {
278-
result.Checkpoints[candidate.CheckpointID] = ctx
277+
if checkpointCtx, ok := resolver.checkpointCache[candidate.CheckpointID]; ok {
278+
result.Checkpoints[candidate.CheckpointID] = checkpointCtx
279279
}
280280
}
281281
}
@@ -545,7 +545,7 @@ func runGitBlame(ctx context.Context, repoRoot, file string) ([]rawBlameLine, er
545545
return parseBlamePorcelain(string(out))
546546
}
547547

548-
var blameHeaderRe = regexp.MustCompile(`^([0-9a-f]{40})\s+\d+\s+(\d+)(?:\s+\d+)?$`)
548+
var blameHeaderRe = regexp.MustCompile(`^([0-9a-f]{40}|[0-9a-f]{64})\s+\d+\s+(\d+)(?:\s+\d+)?$`)
549549

550550
func parseBlamePorcelain(output string) ([]rawBlameLine, error) {
551551
scanner := bufio.NewScanner(strings.NewReader(output))
@@ -1061,7 +1061,7 @@ func appendUniqueString(values []string, value string) []string {
10611061
}
10621062

10631063
func isZeroCommit(sha string) bool {
1064-
return sha == "" || sha == plumbing.ZeroHash.String()
1064+
return sha == "" || strings.Trim(sha, "0") == ""
10651065
}
10661066

10671067
func writeJSON(w io.Writer, value any) error {

cmd/entire/cli/attribution_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,30 @@ func TestParseBlamePorcelain(t *testing.T) {
4949
require.Equal(t, 2, lines[1].LineNumber)
5050
}
5151

52+
func TestParseBlamePorcelainSupportsSHA256ObjectIDs(t *testing.T) {
53+
sha256ID := strings.Repeat("a", 64)
54+
output := strings.Join([]string{
55+
fmt.Sprintf("%s 1 1 1", sha256ID),
56+
"author Ada Lovelace",
57+
"author-time 1700000000",
58+
"\tprint('hello')",
59+
"",
60+
}, "\n")
61+
62+
lines, err := parseBlamePorcelain(output)
63+
require.NoError(t, err)
64+
require.Len(t, lines, 1)
65+
require.Equal(t, sha256ID, lines[0].CommitSHA)
66+
require.Equal(t, 1, lines[0].LineNumber)
67+
require.Equal(t, "print('hello')", lines[0].Content)
68+
}
69+
70+
func TestIsZeroCommitSupportsSHA256ObjectIDs(t *testing.T) {
71+
require.True(t, isZeroCommit(strings.Repeat("0", 40)))
72+
require.True(t, isZeroCommit(strings.Repeat("0", 64)))
73+
require.False(t, isZeroCommit(strings.Repeat("0", 63)+"1"))
74+
}
75+
5276
func TestParseAttributionLineRange(t *testing.T) {
5377
got, err := parseAttributionLineRange("12-20")
5478
require.NoError(t, err)

0 commit comments

Comments
 (0)