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