Add support for git ie. automatically resetting changes to ensure clean slate

This commit is contained in:
2025-03-26 03:22:05 +01:00
parent bb14087598
commit 0d8c447ff6
3 changed files with 219 additions and 8 deletions

99
main.go
View File

@@ -5,9 +5,13 @@ import (
"fmt"
"log"
"os"
"path/filepath"
"sync"
"time"
"github.com/bmatcuk/doublestar/v4"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing/object"
"modify/processor"
)
@@ -23,8 +27,12 @@ var stats GlobalStats
var logger *log.Logger
var (
jsonFlag = flag.Bool("json", false, "Process JSON files")
xmlFlag = flag.Bool("xml", false, "Process XML files")
jsonFlag = flag.Bool("json", false, "Process JSON files")
xmlFlag = flag.Bool("xml", false, "Process XML files")
gitFlag = flag.Bool("git", false, "Use git to manage files")
resetFlag = flag.Bool("reset", false, "Reset files to their original state")
repo *git.Repository
worktree *git.Worktree
)
func init() {
@@ -54,6 +62,10 @@ func main() {
fmt.Fprintf(os.Stderr, " Process JSON files\n")
fmt.Fprintf(os.Stderr, " -xml\n")
fmt.Fprintf(os.Stderr, " Process XML files\n")
fmt.Fprintf(os.Stderr, " -git\n")
fmt.Fprintf(os.Stderr, " Use git to manage files\n")
fmt.Fprintf(os.Stderr, " -reset\n")
fmt.Fprintf(os.Stderr, " Reset files to their original state\n")
fmt.Fprintf(os.Stderr, " -mode string\n")
fmt.Fprintf(os.Stderr, " Processing mode: regex, xml, json (default \"regex\")\n")
fmt.Fprintf(os.Stderr, "\nExamples:\n")
@@ -97,6 +109,14 @@ func main() {
logger.Printf("Transformed Lua expression from %q to %q", originalLuaExpr, luaExpr)
}
if *gitFlag {
err := setupGit()
if err != nil {
fmt.Fprintf(os.Stderr, "Error setting up git: %v\n", err)
return
}
}
// Expand file patterns with glob support
files, err := expandFilePatterns(filePatterns)
if err != nil {
@@ -109,6 +129,14 @@ func main() {
return
}
if *gitFlag {
err := cleanupGitFiles(files)
if err != nil {
fmt.Fprintf(os.Stderr, "Error cleaning up git files: %v\n", err)
return
}
}
// Create the processor based on mode
var proc processor.Processor
switch {
@@ -158,6 +186,33 @@ func main() {
}
}
func setupGit() error {
cwd, err := os.Getwd()
if err != nil {
return fmt.Errorf("failed to get current working directory: %w", err)
}
logger.Printf("Current working directory obtained: %s", cwd)
repo, err = git.PlainOpen(cwd)
if err != nil {
logger.Printf("No existing git repository found at %s, attempting to initialize a new git repository.", cwd)
repo, err = git.PlainInit(cwd, false)
if err != nil {
return fmt.Errorf("failed to initialize a new git repository at %s: %w", cwd, err)
}
logger.Printf("Successfully initialized a new git repository at %s", cwd)
} else {
logger.Printf("Successfully opened existing git repository at %s", cwd)
}
worktree, err = repo.Worktree()
if err != nil {
return fmt.Errorf("failed to obtain worktree for repository at %s: %w", cwd, err)
}
logger.Printf("Successfully obtained worktree for repository at %s", cwd)
return nil
}
func expandFilePatterns(patterns []string) ([]string, error) {
var files []string
filesMap := make(map[string]bool)
@@ -176,3 +231,43 @@ func expandFilePatterns(patterns []string) ([]string, error) {
}
return files, nil
}
func cleanupGitFiles(files []string) error {
for _, file := range files {
status, err := worktree.Status()
if err != nil {
fmt.Fprintf(os.Stderr, "Error getting worktree status: %v\n", err)
return fmt.Errorf("error getting worktree status: %w", err)
}
if status.IsUntracked(file) {
logger.Printf("Detected untracked file: %s. Attempting to add it to the git index.", file)
_, err = worktree.Add(file)
if err != nil {
fmt.Fprintf(os.Stderr, "Error adding file to git: %v\n", err)
return fmt.Errorf("error adding file to git: %w", err)
}
filename := filepath.Base(file)
logger.Printf("File %s added successfully. Now committing it with message: 'Track %s'", filename, filename)
_, err = worktree.Commit("Track "+filename, &git.CommitOptions{
Author: &object.Signature{
Name: "Big Chef",
Email: "bigchef@bigchef.com",
When: time.Now(),
},
})
if err != nil {
fmt.Fprintf(os.Stderr, "Error committing file: %v\n", err)
return fmt.Errorf("error committing file: %w", err)
}
logger.Printf("Successfully committed file: %s with message: 'Track %s'", filename, filename)
} else {
logger.Printf("File %s is already tracked. Restoring it to the working tree.", file)
worktree.Restore(&git.RestoreOptions{
Files: []string{file},
})
logger.Printf("File %s restored successfully.", file)
}
}
return nil
}