Ditch go-git completely

This commit is contained in:
system
2025-10-01 21:58:04 +02:00
parent f1e7f748d0
commit 128db358fc
2 changed files with 86 additions and 116 deletions

23
go.mod
View File

@@ -6,27 +6,4 @@ toolchain go1.24.3
require ( require (
git.site.quack-lab.dev/dave/cylogger v1.2.2 git.site.quack-lab.dev/dave/cylogger v1.2.2
github.com/go-git/go-git/v5 v5.16.0
)
require (
dario.cat/mergo v1.0.0 // indirect
github.com/Microsoft/go-winio v0.6.2 // indirect
github.com/ProtonMail/go-crypto v1.1.6 // indirect
github.com/cloudflare/circl v1.6.1 // indirect
github.com/cyphar/filepath-securejoin v0.4.1 // indirect
github.com/emirpasic/gods v1.18.1 // indirect
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
github.com/go-git/go-billy/v5 v5.6.2 // indirect
github.com/golang/groupcache v0.0.0-20241129210726-2c02b8208cf8 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/kevinburke/ssh_config v1.2.0 // indirect
github.com/pjbgf/sha1cd v0.3.2 // indirect
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 // indirect
github.com/skeema/knownhosts v1.3.1 // indirect
github.com/xanzy/ssh-agent v0.3.3 // indirect
golang.org/x/crypto v0.37.0 // indirect
golang.org/x/net v0.39.0 // indirect
golang.org/x/sys v0.32.0 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
) )

127
main.go
View File

@@ -3,17 +3,35 @@ package main
import ( import (
"flag" "flag"
"os" "os"
"os/exec"
"path" "path"
"path/filepath" "path/filepath"
"strings"
"time" "time"
logger "git.site.quack-lab.dev/dave/cylogger" logger "git.site.quack-lab.dev/dave/cylogger"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing/object"
) )
var ROOT string var ROOT string
// executeGitCommand runs a git command in the ROOT directory and returns the output
func executeGitCommand(args ...string) (string, error) {
cmd := exec.Command("git", args...)
cmd.Dir = ROOT
output, err := cmd.CombinedOutput()
if err != nil {
logger.Error("Git command failed: %s", string(output))
return "", err
}
return strings.TrimSpace(string(output)), nil
}
// isGitRepository checks if the current directory is a git repository
func isGitRepository() bool {
_, err := executeGitCommand("rev-parse", "--git-dir")
return err == nil
}
func main() { func main() {
scanIntervalF := flag.String("interval", "2s", "scan interval") scanIntervalF := flag.String("interval", "2s", "scan interval")
flag.Parse() flag.Parse()
@@ -49,12 +67,10 @@ func main() {
} }
func doRun() { func doRun() {
// Open the repository // Check if repository exists
r, err := git.PlainOpen(ROOT) if !isGitRepository() {
if err != nil { // Initialize a new repository
// If the repository does not exist, create it _, err := executeGitCommand("init")
if err == git.ErrRepositoryNotExists {
r, err = git.PlainInit(ROOT, false) // Initialize a new repository
if err != nil { if err != nil {
logger.Error("Error creating repository: %v", err) logger.Error("Error creating repository: %v", err)
return return
@@ -68,75 +84,62 @@ func doRun() {
return return
} }
// Get the worktree for initial commit // Set identity
w, err := r.Worktree() if err := setIdentity(); err != nil {
if err != nil { logger.Error("Error setting identity: %v", err)
logger.Error("Error getting worktree: %v", err)
return return
} }
// Add .gitignore // Add .gitignore
if _, err := w.Add(".gitignore"); err != nil { if _, err := executeGitCommand("add", ".gitignore"); err != nil {
logger.Error("Error adding .gitignore: %v", err) logger.Error("Error adding .gitignore: %v", err)
return return
} }
// Create initial commit // Create initial commit
if _, err := w.Commit("Initial commit", &git.CommitOptions{ if _, err := executeGitCommand("commit", "-m", "Initial commit"); err != nil {
Author: &object.Signature{
Name: "system",
Email: "system@localhost",
When: time.Now(),
},
}); err != nil {
logger.Error("Error creating initial commit: %v", err) logger.Error("Error creating initial commit: %v", err)
return return
} }
logger.Info("Initial commit created with .gitignore") logger.Info("Initial commit created with .gitignore")
return return
} else {
logger.Error("Error opening repository: %v", err)
return
}
}
// Get the worktree
w, err := r.Worktree()
if err != nil {
logger.Error("Error getting worktree: %v", err)
return
} }
// Check status // Check status
status, err := w.Status() statusOutput, err := executeGitCommand("status", "--porcelain")
if err != nil { if err != nil {
logger.Error("Error getting status: %v", err) logger.Error("Error getting status: %v", err)
return return
} }
if status.IsClean() { if statusOutput == "" {
logger.Info("Nothing to commit") logger.Info("Nothing to commit")
return return
} }
// Count changes by type // Count changes by type
lines := strings.Split(statusOutput, "\n")
var added, modified, deleted int var added, modified, deleted int
for _, s := range status { for _, line := range lines {
switch s.Worktree { if len(line) >= 2 {
case git.Modified: status := line[:2]
modified++ if strings.Contains(status, "A") {
case git.Added:
added++ added++
case git.Deleted: }
if strings.Contains(status, "M") {
modified++
}
if strings.Contains(status, "D") {
deleted++ deleted++
} }
} }
}
logger.Info("Changes detected: %d added, %d modified, %d deleted", added, modified, deleted) logger.Info("Changes detected: %d added, %d modified, %d deleted", added, modified, deleted)
// Add all changes // Add all changes
err = w.AddWithOptions(&git.AddOptions{All: true}) _, err = executeGitCommand("add", ".")
if err != nil { if err != nil {
logger.Error("Error adding changes: %v", err) logger.Error("Error adding changes: %v", err)
return return
@@ -144,55 +147,45 @@ func doRun() {
logger.Info("Changes added to index") logger.Info("Changes added to index")
// Set identity // Set identity
err = setIdentity(r) if err := setIdentity(); err != nil {
if err != nil {
logger.Error("Error setting identity: %v", err) logger.Error("Error setting identity: %v", err)
return return
} }
// Get current branch // Get current branch
head, err := r.Head() branch, err := executeGitCommand("branch", "--show-current")
if err != nil { if err != nil {
logger.Error("Error getting HEAD: %v", err) logger.Error("Error getting current branch: %v", err)
return return
} }
// Commit changes // Commit changes
commit, err := w.Commit("Update", &git.CommitOptions{ commitHash, err := executeGitCommand("commit", "-m", "Update")
Author: &object.Signature{
Name: "system",
Email: "system@localhost",
When: time.Now(),
},
})
if err != nil { if err != nil {
logger.Error("Error committing changes: %v", err) logger.Error("Error committing changes: %v", err)
return return
} }
// Get commit details // Get commit details
commitObj, err := r.CommitObject(commit) authorName, _ := executeGitCommand("log", "-1", "--format=%an", commitHash)
if err != nil { authorEmail, _ := executeGitCommand("log", "-1", "--format=%ae", commitHash)
logger.Error("Error getting commit details: %v", err) commitDate, _ := executeGitCommand("log", "-1", "--format=%ai", commitHash)
return commitMessage, _ := executeGitCommand("log", "-1", "--format=%s", commitHash)
}
logger.Info("Changes committed successfully:") logger.Info("Changes committed successfully:")
logger.Info(" Branch: %s", head.Name().Short()) logger.Info(" Branch: %s", branch)
logger.Info(" Commit: %s", commitObj.Hash.String()) logger.Info(" Commit: %s", commitHash)
logger.Info(" Author: %s <%s>", commitObj.Author.Name, commitObj.Author.Email) logger.Info(" Author: %s <%s>", authorName, authorEmail)
logger.Info(" Date: %s", commitObj.Author.When.Format(time.RFC3339)) logger.Info(" Date: %s", commitDate)
logger.Info(" Message: %s", commitObj.Message) logger.Info(" Message: %s", commitMessage)
} }
func setIdentity(r *git.Repository) error { func setIdentity() error {
cfg, err := r.Config() _, err := executeGitCommand("config", "user.name", "system")
if err != nil { if err != nil {
return err return err
} }
cfg.User.Name = "system" _, err = executeGitCommand("config", "user.email", "system@localhost")
cfg.User.Email = "system@localhost" return err
return r.SetConfig(cfg)
} }