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

@ -45,3 +45,39 @@ func Commit(repoPath, commitMessage string, dryRun bool) error {
return nil
}
// CommitFile commits a specific file.
func CommitFile(repoPath, filePath, commitMessage string, dryRun bool) error {
if commitMessage == "" {
return fmt.Errorf("no commit message specified?")
}
commitRepo, err := git.PlainOpen(repoPath)
if err != nil {
return err
}
commitWorktree, err := commitRepo.Worktree()
if err != nil {
return err
}
if !dryRun {
if _, err := commitWorktree.Add(filePath); err != nil {
return fmt.Errorf("unable to add %s: %s", filePath, err)
}
}
opts := &git.CommitOptions{}
if !dryRun {
_, err = commitWorktree.Commit(commitMessage, opts)
if err != nil {
return err
}
log.Debug("git changes commited")
} else {
log.Debug("dry run: no changes commited")
}
return nil
}

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
}

35
pkg/git/dir.go Normal file
View File

@ -0,0 +1,35 @@
package git
import (
"os"
"path"
"path/filepath"
"coopcloud.tech/abra/pkg/log"
)
func FindDir(dir string) string {
dir, err := filepath.Abs(dir)
if err != nil {
return ""
}
realPath, err := filepath.EvalSymlinks(dir)
if err != nil {
log.Warn("unable to find git repo: broken symlink: %s", dir)
return ""
}
dir = realPath
if dir == os.ExpandEnv("$HOME/.abra") || dir == os.ExpandEnv("$ABRA_DIR") || dir == "/" {
return ""
}
p := path.Join(dir, ".git")
if _, err := os.Stat(p); err == nil {
return path.Dir(p)
}
return FindDir(filepath.Dir(dir))
}

41
pkg/git/pull.go Normal file
View File

@ -0,0 +1,41 @@
package git
import (
"errors"
"coopcloud.tech/abra/pkg/log"
"github.com/go-git/go-git/v5"
)
// Pull pulls the latest changes in.
func Pull(repoDir string, dryRun bool) error {
if dryRun {
log.Debugf("dry run: no git changes pulled in %s", repoDir)
return nil
}
repo, err := git.PlainOpen(repoDir)
if err != nil {
return err
}
opts := &git.PullOptions{
RemoteName: "origin", // NOTE(d1): what could go wrong 🤡
}
worktree, err := repo.Worktree()
if err != nil {
return err
}
if err := worktree.Pull(opts); err != nil && !errors.Is(err, git.NoErrAlreadyUpToDate) {
return err
} else if err != nil && errors.Is(err, git.NoErrAlreadyUpToDate) {
log.Debugf("skipping pulling changes at %s", repoDir)
return nil
}
log.Debugf("git changes pulled in at %s", repoDir)
return nil
}

View File

@ -27,7 +27,7 @@ func Push(repoDir string, remote string, tags bool, dryRun bool) error {
return err
}
log.Debugf("git changes pushed")
log.Debug("git changes pushed")
if tags {
opts.RefSpecs = append(opts.RefSpecs, config.RefSpec("+refs/tags/*:refs/tags/*"))
@ -36,7 +36,7 @@ func Push(repoDir string, remote string, tags bool, dryRun bool) error {
return err
}
log.Debugf("git tags pushed")
log.Debug("git tags pushed")
}
return nil