jesseduffield.lazygit/pkg/commands/patch/patch_builder_test.go
Stefan Haller da4dfa17d5 Render the custom-patch secondary pane through the pager, via a real git diff
The secondary pane showed the custom patch with a bespoke in-memory render
(PatchBuilder.RenderAggregatedPatch / FormatView). That had two problems: it
could never be fed to a pager the way the main view's diff is (a stdin pager
might have worked, but an external diff tool like difftastic, which diffs two
files rather than a unified diff, could not), and its hand-rolled hunk/context
handling differed subtly from git's.

Materialize the patch instead as two real file trees under a temp dir — a/ holds
each patched file's "from"-side content, b/ that content with the patch applied —
and render it with `git diff --no-index`, reusing the exact pager wiring the main
view uses (stdin pager, external diff, or git's own colour as the raw fallback).
`--no-index` honors both GIT_PAGER and --ext-diff, so every pager type now renders
the custom patch like any other diff, and git computes the context, fixing the
quirks. Because the secondary is now an async diff task, the post-removal
selection reveal (which rides a task's restore) finally takes effect.

The trees are named a/b so that with --no-prefix the diff shows the real
repo-relative paths; added files are seeded empty in a/ so they pair up and show
their real paths rather than git's directory-comparison "added in b" form. The
patch builder owns the temp dir's lifetime (created on Start, removed on Reset)
and bumps a generation counter on every change, so the trees are rebuilt only
when the patch actually changes — covering the focused-main view and the old
explorer alike, without rebuilding on mere navigation.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-08-08 12:59:00 +02:00

54 lines
1.8 KiB
Go

package patch
import (
"testing"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
)
// newTestPatchBuilder returns a PatchBuilder whose files all resolve to the given
// diff, started for a dummy commit.
func newTestPatchBuilder(diff string) *PatchBuilder {
pb := NewPatchBuilder(logrus.New().WithField("test", "test"),
func(from, to string, reverse bool, filename string, previousPath string, plain bool) (string, error) {
return diff, nil
},
nil)
pb.Start("from", "to", false, true)
return pb
}
// In simpleDiff the deletion "-orange" is patch line index 6 (old-file line 2) and the
// addition "+grape" is index 7 (new-file line 2).
func TestPatchLineIndicesForLines(t *testing.T) {
pb := newTestPatchBuilder(simpleDiff)
indices, err := pb.PatchLineIndicesForLines("filename", []LineIdentity{
{LineNumber: 2, IsDeletion: true}, // -orange
{LineNumber: 2, IsDeletion: false}, // +grape
{LineNumber: 1, IsDeletion: false}, // " apple" — a context line, no change index
})
assert.NoError(t, err)
assert.Equal(t, []int{6, 7}, indices, "context-line identity is skipped; change lines map to their indices")
}
func TestIncludedLineIdentities(t *testing.T) {
pb := newTestPatchBuilder(simpleDiff)
// Nothing included yet.
assert.Empty(t, pb.IncludedLineIdentities("filename"))
// Include only the deletion: it comes back as its identity, the addition does not.
assert.NoError(t, pb.AddFileLineRange("filename", "", []int{6}))
assert.Equal(t,
[]LineIdentity{{LineNumber: 2, IsDeletion: true}},
pb.IncludedLineIdentities("filename"))
// Including the addition too yields both identities (order-independent).
assert.NoError(t, pb.AddFileLineRange("filename", "", []int{7}))
assert.ElementsMatch(t,
[]LineIdentity{{LineNumber: 2, IsDeletion: true}, {LineNumber: 2, IsDeletion: false}},
pb.IncludedLineIdentities("filename"))
}