abra/pkg/git/diff.go
decentral1se 4d7c812fe2
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is passing
WIP: operator collaboration MVP
See toolshed/organising#467
2024-12-30 00:46:25 +01:00

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
}