WIP: operator collaboration MVP

See toolshed/organising#467
This commit is contained in:
2024-12-30 00:46:25 +01:00
parent 74108b0dd9
commit 4d7c812fe2
13 changed files with 319 additions and 20 deletions

View File

@ -3,6 +3,7 @@ package git
import (
"fmt"
"os/exec"
"strings"
"coopcloud.tech/abra/pkg/log"
)
@ -10,8 +11,8 @@ import (
// 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{
func getGitDiffArgs(repoPath, fname string) []string {
args := []string{
"-C",
repoPath,
"--no-pager",
@ -19,24 +20,29 @@ func getGitDiffArgs(repoPath string) []string {
"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. It gracefully
// skips if it cannot find the command on the system.
func DiffUnstaged(path string) error {
// library, this implementation requires the /usr/bin/git binary.
func DiffUnstaged(path, fname string) (string, error) {
if _, err := exec.LookPath("git"); err != nil {
log.Warnf("unable to locate git command, cannot output diff")
return nil
return "", fmt.Errorf("missing /usr/bin/git command? cannot output diff")
}
gitDiffArgs := getGitDiffArgs(path)
gitDiffArgs := getGitDiffArgs(path, fname)
log.Debugf("running: git %s", strings.Join(gitDiffArgs, " "))
diff, err := exec.Command("git", gitDiffArgs...).Output()
if err != nil {
return nil
return "", err
}
fmt.Print(string(diff))
return nil
return string(diff), nil
}