package git import ( "fmt" "os/exec" "github.com/sirupsen/logrus" ) // getGitDiffArgs builds the `git diff` invocation args. It removes the usage // of a pager and ensures that colours are specified even when Git might detect // otherwise. func getGitDiffArgs(repoPath string) []string { return []string{ "-C", repoPath, "--no-pager", "-c", "color.diff=always", "diff", } } // DiffUnstaged shows a `git diff`. Due to limitations in the underlying go-git // library, this implementation requires the /usr/bin/git binary. It gracefully // skips if it cannot find the command on the system. func DiffUnstaged(path string) error { if _, err := exec.LookPath("git"); err != nil { logrus.Warnf("unable to locate git command, cannot output diff") return nil } gitDiffArgs := getGitDiffArgs(path) diff, err := exec.Command("git", gitDiffArgs...).Output() if err != nil { return nil } fmt.Print(string(diff)) return nil }