49 lines
1.0 KiB
Go
49 lines
1.0 KiB
Go
package git
|
|
|
|
import (
|
|
"fmt"
|
|
"os/exec"
|
|
"strings"
|
|
|
|
"coopcloud.tech/abra/pkg/log"
|
|
)
|
|
|
|
// 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, fname string) []string {
|
|
args := []string{
|
|
"-C",
|
|
repoPath,
|
|
"--no-pager",
|
|
"-c",
|
|
"color.diff=always",
|
|
"diff",
|
|
}
|
|
|
|
if fname != "" {
|
|
args = append(args, fname)
|
|
}
|
|
|
|
return args
|
|
}
|
|
|
|
// DiffUnstaged shows a `git diff`. Due to limitations in the underlying go-git
|
|
// library, this implementation requires the /usr/bin/git binary.
|
|
func DiffUnstaged(path, fname string) (string, error) {
|
|
if _, err := exec.LookPath("git"); err != nil {
|
|
return "", fmt.Errorf("missing /usr/bin/git command? cannot output diff")
|
|
}
|
|
|
|
gitDiffArgs := getGitDiffArgs(path, fname)
|
|
|
|
log.Debugf("running: git %s", strings.Join(gitDiffArgs, " "))
|
|
|
|
diff, err := exec.Command("git", gitDiffArgs...).Output()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
return string(diff), nil
|
|
}
|