diff --git a/revgrep.go b/revgrep.go index ec60584..d122a55 100644 --- a/revgrep.go +++ b/revgrep.go @@ -411,8 +411,50 @@ func GitPatch(revisionFrom, revisionTo string) (io.Reader, []string, error) { func gitDiff(extraArgs ...string) *exec.Cmd { cmd := exec.Command("git", "diff", "--color=never", "--no-ext-diff") + if isSupportedByGit(2, 41, 0) { + cmd.Args = append(cmd.Args, "--default-prefix") + } + cmd.Args = append(cmd.Args, "--relative") cmd.Args = append(cmd.Args, extraArgs...) return cmd } + +func isSupportedByGit(major, minor, patch int) bool { + output, err := exec.Command("git", "version").CombinedOutput() + if err != nil { + return false + } + + fmt.Println(string(output)) + + parts := bytes.Split(bytes.TrimSpace(output), []byte(" ")) + if len(parts) < 3 { + return false + } + + v := string(parts[2]) + if v == "" { + return false + } + + vp := regexp.MustCompile(`^(\d+)\.(\d+)(?:\.(\d+))?$`).FindStringSubmatch(v) + + currentMajor, err := strconv.Atoi(vp[1]) + if err != nil { + return false + } + + currentMinor, err := strconv.Atoi(vp[2]) + if err != nil { + return false + } + + currentPatch, err := strconv.Atoi(vp[3]) + if err != nil { + return false + } + + return currentMajor*1_000_000_000+currentMinor*1_000_000+currentPatch*1_000 >= major*1_000_000_000+minor*1_000_000+patch*1_000 +}