Ditch go-git completely
This commit is contained in:
179
main.go
179
main.go
@@ -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,94 +67,79 @@ 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
|
||||
if err != nil {
|
||||
logger.Error("Error creating repository: %v", err)
|
||||
return
|
||||
}
|
||||
logger.Info("New repository created at: %s", ROOT)
|
||||
|
||||
// Create .gitignore file
|
||||
gitignorePath := filepath.Join(ROOT, ".gitignore")
|
||||
if err := os.WriteFile(gitignorePath, []byte("*.log\n"), 0644); err != nil {
|
||||
logger.Error("Error creating .gitignore: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
// Get the worktree for initial commit
|
||||
w, err := r.Worktree()
|
||||
if err != nil {
|
||||
logger.Error("Error getting worktree: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
// Add .gitignore
|
||||
if _, err := w.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 {
|
||||
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)
|
||||
// Check if repository exists
|
||||
if !isGitRepository() {
|
||||
// Initialize a new repository
|
||||
_, err := executeGitCommand("init")
|
||||
if err != nil {
|
||||
logger.Error("Error creating repository: %v", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
logger.Info("New repository created at: %s", ROOT)
|
||||
|
||||
// Get the worktree
|
||||
w, err := r.Worktree()
|
||||
if err != nil {
|
||||
logger.Error("Error getting worktree: %v", err)
|
||||
// Create .gitignore file
|
||||
gitignorePath := filepath.Join(ROOT, ".gitignore")
|
||||
if err := os.WriteFile(gitignorePath, []byte("*.log\n"), 0644); err != nil {
|
||||
logger.Error("Error creating .gitignore: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
// Set identity
|
||||
if err := setIdentity(); err != nil {
|
||||
logger.Error("Error setting identity: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
// Add .gitignore
|
||||
if _, err := executeGitCommand("add", ".gitignore"); err != nil {
|
||||
logger.Error("Error adding .gitignore: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
// Create initial commit
|
||||
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
|
||||
}
|
||||
|
||||
// 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:
|
||||
added++
|
||||
case git.Deleted:
|
||||
deleted++
|
||||
for _, line := range lines {
|
||||
if len(line) >= 2 {
|
||||
status := line[:2]
|
||||
if strings.Contains(status, "A") {
|
||||
added++
|
||||
}
|
||||
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
|
||||
}
|
||||
|
Reference in New Issue
Block a user