diff --git a/main.go b/main.go index 73f1af6..cf4c43d 100644 --- a/main.go +++ b/main.go @@ -5,16 +5,12 @@ import ( "fmt" "log" "os" - "path/filepath" "sort" "sync" - "time" "modify/utils" - "github.com/bmatcuk/doublestar/v4" "github.com/go-git/go-git/v5" - "github.com/go-git/go-git/v5/plumbing/object" "modify/logger" "modify/processor" @@ -75,7 +71,7 @@ func main() { // Resolve all the files for all the globs logger.Info("Found %d unique file patterns", len(globs)) - files, err := ExpandGLobs(globs) + files, err := utils.ExpandGLobs(globs) if err != nil { logger.Error("Failed to expand file patterns: %v", err) return @@ -222,116 +218,3 @@ func main() { stats.TotalModifications, stats.ProcessedFiles, stats.ProcessedFiles+stats.FailedFiles) } } - -func setupGit() error { - cwd, err := os.Getwd() - if err != nil { - return fmt.Errorf("failed to get current working directory: %w", err) - } - logger.Debug("Current working directory obtained: %s", cwd) - - logger.Debug("Attempting to open git repository at %s", cwd) - repo, err = git.PlainOpen(cwd) - if err != nil { - logger.Debug("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.Info("Successfully initialized a new git repository at %s", cwd) - } else { - logger.Info("Successfully opened existing git repository at %s", cwd) - } - - logger.Debug("Attempting to obtain worktree for 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.Debug("Successfully obtained worktree for repository at %s", cwd) - return nil -} - -func ExpandGLobs(patterns map[string]struct{}) ([]string, error) { - var files []string - filesMap := make(map[string]bool) - - cwd, err := os.Getwd() - if err != nil { - return nil, fmt.Errorf("failed to get current working directory: %w", err) - } - - logger.Debug("Expanding patterns from directory: %s", cwd) - for pattern, _ := range patterns { - logger.Trace("Processing pattern: %s", pattern) - matches, _ := doublestar.Glob(os.DirFS(cwd), pattern) - logger.Debug("Found %d matches for pattern %s", len(matches), pattern) - for _, m := range matches { - info, err := os.Stat(m) - if err != nil { - logger.Warning("Error getting file info for %s: %v", m, err) - continue - } - if !info.IsDir() && !filesMap[m] { - logger.Trace("Adding file to process list: %s", m) - filesMap[m], files = true, append(files, m) - } - } - } - - if len(files) > 0 { - logger.Debug("Found %d files to process: %v", len(files), files) - } - return files, nil -} - -func cleanupGitFiles(files []string) error { - for _, file := range files { - logger.Debug("Checking git status for file: %s", file) - status, err := worktree.Status() - if err != nil { - logger.Error("Error getting worktree status: %v", err) - 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.Info("Detected untracked file: %s. Adding to git index.", file) - _, err = worktree.Add(file) - if err != nil { - logger.Error("Error adding file to git: %v", err) - 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.Info("File %s added successfully. Committing 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 { - logger.Error("Error committing file: %v", err) - fmt.Fprintf(os.Stderr, "Error committing file: %v\n", err) - return fmt.Errorf("error committing file: %w", err) - } - logger.Info("Successfully committed file: %s", filename) - } else { - logger.Info("File %s is already tracked. Restoring it to the working tree.", file) - err := worktree.Restore(&git.RestoreOptions{ - Files: []string{file}, - Staged: true, - Worktree: true, - }) - if err != nil { - logger.Error("Error restoring file: %v", err) - fmt.Fprintf(os.Stderr, "Error restoring file: %v\n", err) - return fmt.Errorf("error restoring file: %w", err) - } - logger.Info("File %s restored successfully", file) - } - } - return nil -} diff --git a/utils/git.go b/utils/git.go new file mode 100644 index 0000000..db98cc4 --- /dev/null +++ b/utils/git.go @@ -0,0 +1,97 @@ +package utils + +import ( + "fmt" + "modify/logger" + "os" + "path/filepath" + "time" + + "github.com/go-git/go-git/v5/plumbing/object" + "github.com/go-git/go-git/v5" +) + +var ( + Repo *git.Repository + Worktree *git.Worktree +) + +func SetupGit() error { + cwd, err := os.Getwd() + if err != nil { + return fmt.Errorf("failed to get current working directory: %w", err) + } + logger.Debug("Current working directory obtained: %s", cwd) + + logger.Debug("Attempting to open git repository at %s", cwd) + Repo, err = git.PlainOpen(cwd) + if err != nil { + logger.Debug("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.Info("Successfully initialized a new git repository at %s", cwd) + } else { + logger.Info("Successfully opened existing git repository at %s", cwd) + } + + logger.Debug("Attempting to obtain worktree for 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.Debug("Successfully obtained worktree for repository at %s", cwd) + return nil +} + +func CleanupGitFiles(files []string) error { + for _, file := range files { + logger.Debug("Checking git status for file: %s", file) + status, err := Worktree.Status() + if err != nil { + logger.Error("Error getting worktree status: %v", err) + 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.Info("Detected untracked file: %s. Adding to git index.", file) + _, err = Worktree.Add(file) + if err != nil { + logger.Error("Error adding file to git: %v", err) + 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.Info("File %s added successfully. Committing 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 { + logger.Error("Error committing file: %v", err) + fmt.Fprintf(os.Stderr, "Error committing file: %v\n", err) + return fmt.Errorf("error committing file: %w", err) + } + logger.Info("Successfully committed file: %s", filename) + } else { + logger.Info("File %s is already tracked. Restoring it to the working tree.", file) + err := Worktree.Restore(&git.RestoreOptions{ + Files: []string{file}, + Staged: true, + Worktree: true, + }) + if err != nil { + logger.Error("Error restoring file: %v", err) + fmt.Fprintf(os.Stderr, "Error restoring file: %v\n", err) + return fmt.Errorf("error restoring file: %w", err) + } + logger.Info("File %s restored successfully", file) + } + } + return nil +} \ No newline at end of file diff --git a/utils/modifycommand.go b/utils/modifycommand.go index 3afd74a..8179720 100644 --- a/utils/modifycommand.go +++ b/utils/modifycommand.go @@ -3,6 +3,7 @@ package utils import ( "fmt" "modify/logger" + "os" "github.com/bmatcuk/doublestar/v4" ) @@ -73,6 +74,39 @@ func AggregateGlobs(commands []ModifyCommand) map[string]struct{} { return globs } +func ExpandGLobs(patterns map[string]struct{}) ([]string, error) { + var files []string + filesMap := make(map[string]bool) + + cwd, err := os.Getwd() + if err != nil { + return nil, fmt.Errorf("failed to get current working directory: %w", err) + } + + logger.Debug("Expanding patterns from directory: %s", cwd) + for pattern, _ := range patterns { + logger.Trace("Processing pattern: %s", pattern) + matches, _ := doublestar.Glob(os.DirFS(cwd), pattern) + logger.Debug("Found %d matches for pattern %s", len(matches), pattern) + for _, m := range matches { + info, err := os.Stat(m) + if err != nil { + logger.Warning("Error getting file info for %s: %v", m, err) + continue + } + if !info.IsDir() && !filesMap[m] { + logger.Trace("Adding file to process list: %s", m) + filesMap[m], files = true, append(files, m) + } + } + } + + if len(files) > 0 { + logger.Debug("Found %d files to process: %v", len(files), files) + } + return files, nil +} + func LoadCommands(args []string) ([]ModifyCommand, error) { commands := []ModifyCommand{}