mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-05-10 20:05:50 +02:00
This makes it easier to copy diff hunks and paste them into code. We only strip the prefixes if the copied lines are either all '+' or all '-' (possibly including context lines), otherwise we keep them. We also keep them when parts of a hunk header is included in the selection; this is useful for copying a diff hunk and pasting it into a github comment, for example. A not-quite-correct edge case is when you select the '--- a/file.txt' line of a diff header on its own; in this case we copy it as '-- a/file.txt' (same for the '+++' line). This is probably uncommon enough that it's not worth fixing (it's not trivial to fix because we don't know that we're in a header).
89 lines
1.2 KiB
Go
89 lines
1.2 KiB
Go
package controllers
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func Test_dropDiffPrefix(t *testing.T) {
|
|
scenarios := []struct {
|
|
name string
|
|
diff string
|
|
expectedResult string
|
|
}{
|
|
{
|
|
name: "empty string",
|
|
diff: "",
|
|
expectedResult: "",
|
|
},
|
|
{
|
|
name: "only added lines",
|
|
diff: `+line1
|
|
+line2
|
|
`,
|
|
expectedResult: `line1
|
|
line2
|
|
`,
|
|
},
|
|
{
|
|
name: "added lines with context",
|
|
diff: ` line1
|
|
+line2
|
|
`,
|
|
expectedResult: `line1
|
|
line2
|
|
`,
|
|
},
|
|
{
|
|
name: "only deleted lines",
|
|
diff: `-line1
|
|
-line2
|
|
`,
|
|
expectedResult: `line1
|
|
line2
|
|
`,
|
|
},
|
|
{
|
|
name: "deleted lines with context",
|
|
diff: `-line1
|
|
line2
|
|
`,
|
|
expectedResult: `line1
|
|
line2
|
|
`,
|
|
},
|
|
{
|
|
name: "only context",
|
|
diff: ` line1
|
|
line2
|
|
`,
|
|
expectedResult: `line1
|
|
line2
|
|
`,
|
|
},
|
|
{
|
|
name: "added and deleted lines",
|
|
diff: `+line1
|
|
-line2
|
|
`,
|
|
expectedResult: `+line1
|
|
-line2
|
|
`,
|
|
},
|
|
{
|
|
name: "hunk header lines",
|
|
diff: `@@ -1,8 +1,11 @@
|
|
line1
|
|
`,
|
|
expectedResult: `@@ -1,8 +1,11 @@
|
|
line1
|
|
`,
|
|
},
|
|
}
|
|
for _, s := range scenarios {
|
|
t.Run(s.name, func(t *testing.T) {
|
|
assert.Equal(t, s.expectedResult, dropDiffPrefix(s.diff))
|
|
})
|
|
}
|
|
}
|