Files
git-vc/main.go
2025-10-01 21:58:04 +02:00

192 lines
4.7 KiB
Go

package main
import (
"flag"
"os"
"os/exec"
"path"
"path/filepath"
"strings"
"time"
logger "git.site.quack-lab.dev/dave/cylogger"
)
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()
scanInterval, err := time.ParseDuration(*scanIntervalF)
if err != nil {
logger.Error("error parsing SCAN_INTERVAL: %v", err)
return
}
cwd, err := os.Getwd()
if err != nil {
logger.Error("error getting cwd: %v", err)
return
}
ROOT = path.Clean(cwd)
ROOT, err = filepath.Abs(ROOT)
if err != nil {
logger.Error("error getting absolute path for ROOT: %v", err)
return
}
logger.Info("Input args parsed as:")
logger.Info("ROOT: %s", ROOT)
logger.Info("SCAN_INTERVAL: %.0fs", scanInterval.Seconds())
for {
logger.Info("Running at %s", time.Now().Format(time.RFC3339))
doRun()
time.Sleep(scanInterval)
}
}
func doRun() {
// 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)
// 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
statusOutput, err := executeGitCommand("status", "--porcelain")
if err != nil {
logger.Error("Error getting status: %v", err)
return
}
if statusOutput == "" {
logger.Info("Nothing to commit")
return
}
// Count changes by type
lines := strings.Split(statusOutput, "\n")
var added, modified, deleted int
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 = executeGitCommand("add", ".")
if err != nil {
logger.Error("Error adding changes: %v", err)
return
}
logger.Info("Changes added to index")
// Set identity
if err := setIdentity(); err != nil {
logger.Error("Error setting identity: %v", err)
return
}
// Get current branch
branch, err := executeGitCommand("branch", "--show-current")
if err != nil {
logger.Error("Error getting current branch: %v", err)
return
}
// Commit changes
commitHash, err := executeGitCommand("commit", "-m", "Update")
if err != nil {
logger.Error("Error committing changes: %v", err)
return
}
// Get commit details
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", 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() error {
_, err := executeGitCommand("config", "user.name", "system")
if err != nil {
return err
}
_, err = executeGitCommand("config", "user.email", "system@localhost")
return err
}