Skip to content

Commit 08c9dda

Browse files
committed
fix(gitx): never run a repo's textconv program from blame or log
1 parent faaa35c commit 08c9dda

2 files changed

Lines changed: 38 additions & 3 deletions

File tree

‎internal/gitx/gitx.go‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1453,8 +1453,11 @@ func (r *Repo) BlameRevision(ctx context.Context, rev, path string, start, end i
14531453
return r.blame(ctx, rev, path, start, end, extra...)
14541454
}
14551455

1456+
// blame and log pass --no-textconv first, so a caller can still opt back in:
1457+
// git enables a repository's diff.<driver>.textconv program on both, and
1458+
// rgit reports lines of the real bytes its extents were resolved against.
14561459
func (r *Repo) blame(ctx context.Context, rev, path string, start, end int, extra ...string) ([]byte, error) {
1457-
args := append([]string{"blame", fmt.Sprintf("-L%d,%d", start, end)}, extra...)
1460+
args := append([]string{"blame", "--no-textconv", fmt.Sprintf("-L%d,%d", start, end)}, extra...)
14581461
if rev != "" {
14591462
args = append(args, rev)
14601463
}
@@ -1491,7 +1494,7 @@ func (r *Repo) LogLineRange(ctx context.Context, path string, start, end int, ex
14911494
if strings.ContainsAny(path, ":\n\x00") {
14921495
return nil, &LineRangePathError{Path: path}
14931496
}
1494-
args := append([]string{"log", fmt.Sprintf("-L%d,%d:%s", start, end, path)}, extra...)
1497+
args := append([]string{"log", "--no-textconv", fmt.Sprintf("-L%d,%d:%s", start, end, path)}, extra...)
14951498
return r.checked(ctx, args...)
14961499
}
14971500

@@ -1552,7 +1555,7 @@ func (r *Repo) FindRename(ctx context.Context, rev, path string) (commit, oldPat
15521555
// unparseable date or an unwalkable path is a genuine failure -- so any
15531556
// non-zero exit is a *GitError.
15541557
func (r *Repo) Log(ctx context.Context, since, until string, paths []string, extra ...string) ([]byte, error) {
1555-
args := []string{"log"}
1558+
args := []string{"log", "--no-textconv"}
15561559
if since != "" {
15571560
args = append(args, "--since="+since)
15581561
}

‎internal/gitx/gitx_test.go‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1029,3 +1029,35 @@ func TestRepoRunsNoRepoConfiguredPrograms(t *testing.T) {
10291029
t.Fatal("a repo-configured program ran during a read diff")
10301030
}
10311031
}
1032+
1033+
func TestHistoryReadsRunNoRepoConfiguredTextconv(t *testing.T) {
1034+
ctx := t.Context()
1035+
dir, repo := gittest.New(ctx, t)
1036+
write := func(name, body string) {
1037+
t.Helper()
1038+
if err := os.WriteFile(filepath.Join(dir, name), []byte(body), 0o600); err != nil {
1039+
t.Fatal(err)
1040+
}
1041+
}
1042+
write(".gitattributes", "*.txt diff=evil\n")
1043+
write("a.txt", "one\ntwo\n")
1044+
gittest.Commit(ctx, t, dir, "init")
1045+
write("a.txt", "one\nthree\n")
1046+
gittest.Commit(ctx, t, dir, "edit")
1047+
1048+
marker := filepath.Join(t.TempDir(), "ran")
1049+
gittest.Git(ctx, t, dir, "config", "diff.evil.textconv", "touch "+marker+"; cat")
1050+
1051+
if _, err := repo.Blame(ctx, "a.txt", 1, 2); err != nil {
1052+
t.Fatal(err)
1053+
}
1054+
if _, err := repo.LogLineRange(ctx, "a.txt", 1, 2); err != nil {
1055+
t.Fatal(err)
1056+
}
1057+
if _, err := repo.Log(ctx, "", "", []string{"a.txt"}, "-p"); err != nil {
1058+
t.Fatal(err)
1059+
}
1060+
if _, err := os.Stat(marker); err == nil {
1061+
t.Fatal("a repo-configured textconv program ran during a history read")
1062+
}
1063+
}

0 commit comments

Comments
 (0)