Allowing using the anchored diff from the Go stdlib as the diff implementation

This commit is contained in:
Kovid Goyal 2023-03-23 12:30:56 +05:30
parent 9c188096d0
commit 2ac170c1b1
No known key found for this signature in database
GPG key ID: 06BC317B515ACE7C
4 changed files with 321 additions and 31 deletions

View file

@ -30,27 +30,34 @@ var DiffExe = (&utils.Once[string]{Run: func() string {
return utils.FindExe("diff")
}}).Get
func find_differ() error {
func find_differ() {
if GitExe() != "git" && exec.Command(GitExe(), "--help").Run() == nil {
diff_cmd, _ = shlex.Split(GIT_DIFF)
return nil
}
if DiffExe() != "diff" && exec.Command(DiffExe(), "--help").Run() == nil {
} else if DiffExe() != "diff" && exec.Command(DiffExe(), "--help").Run() == nil {
diff_cmd, _ = shlex.Split(DIFF_DIFF)
return nil
} else {
diff_cmd = []string{}
}
return fmt.Errorf("Neither the git nor the diff programs were found in PATH")
}
func set_diff_command(q string) error {
if q == "auto" {
return find_differ()
}
c, err := shlex.Split(q)
if err == nil {
switch q {
case "auto":
find_differ()
case "builtin", "":
diff_cmd = []string{}
case "diff":
diff_cmd, _ = shlex.Split(DIFF_DIFF)
case "git":
diff_cmd, _ = shlex.Split(GIT_DIFF)
default:
c, err := shlex.Split(q)
if err != nil {
return err
}
diff_cmd = c
}
return err
return nil
}
type Center struct{ prefix_count, suffix_count int }
@ -273,10 +280,6 @@ func parse_patch(raw string, left_lines, right_lines []string) (ans *Patch, err
}
func run_diff(file1, file2 string, num_of_context_lines int) (ok, is_different bool, patch string, err error) {
context := strconv.Itoa(num_of_context_lines)
cmd := utils.Map(func(x string) string {
return strings.ReplaceAll(x, "_CONTEXT_", context)
}, diff_cmd)
// we resolve symlinks because git diff does not follow symlinks, while diff
// does. We want consistent behavior, also for integration with git difftool
// we always want symlinks to be followed.
@ -288,19 +291,40 @@ func run_diff(file1, file2 string, num_of_context_lines int) (ok, is_different b
if err != nil {
return
}
cmd = append(cmd, path1, path2)
c := exec.Command(cmd[0], cmd[1:]...)
stdout, stderr := bytes.Buffer{}, bytes.Buffer{}
c.Stdout, c.Stderr = &stdout, &stderr
err = c.Run()
if err != nil {
var e *exec.ExitError
if errors.As(err, &e) && e.ExitCode() == 1 {
return true, true, stdout.String(), nil
if len(diff_cmd) == 0 {
data1, err := data_for_path(path1)
if err != nil {
return false, false, "", err
}
return false, false, stderr.String(), err
data2, err := data_for_path(path2)
if err != nil {
return false, false, "", err
}
patchb := Diff(path1, data1, path2, data2, num_of_context_lines)
if patchb == nil {
return true, false, "", nil
}
return true, len(patchb) > 0, utils.UnsafeBytesToString(patchb), nil
} else {
context := strconv.Itoa(num_of_context_lines)
cmd := utils.Map(func(x string) string {
return strings.ReplaceAll(x, "_CONTEXT_", context)
}, diff_cmd)
cmd = append(cmd, path1, path2)
c := exec.Command(cmd[0], cmd[1:]...)
stdout, stderr := bytes.Buffer{}, bytes.Buffer{}
c.Stdout, c.Stderr = &stdout, &stderr
err = c.Run()
if err != nil {
var e *exec.ExitError
if errors.As(err, &e) && e.ExitCode() == 1 {
return true, true, stdout.String(), nil
}
return false, false, stderr.String(), err
}
return true, false, stdout.String(), nil
}
return true, false, stdout.String(), nil
}
func do_diff(file1, file2 string, context_count int) (ans *Patch, err error) {