From 937516c5c002075fe070e327fa0b068f227ad0cc Mon Sep 17 00:00:00 2001 From: shatrughan mishra Date: Sat, 7 Feb 2026 15:11:05 +0530 Subject: [PATCH 1/3] feat: add safety warning Signed-off-by: shatrughan mishra --- cmd/gitx/main.go | 11 +++++++++- internal/git/init.go | 48 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/cmd/gitx/main.go b/cmd/gitx/main.go index ae5e114..ad5efe3 100644 --- a/cmd/gitx/main.go +++ b/cmd/gitx/main.go @@ -85,9 +85,18 @@ func ensureGitRepo(shouldInit bool) error { return fmt.Errorf("error: not a git repository\nrun gitx -i/--init to initialize a new git repository and open gitx") } + // Check if the directory is unsafe for git initialization + safe, err := git.WarnIfUnsafe(".") + if err != nil { + return fmt.Errorf("safety check failed: %w", err) + } + if !safe { + return fmt.Errorf("git initialization cancelled by user") + } + // Initialize a new git repository g := &git.GitCommands{} - _, err := g.InitRepository(".") + _, err = g.InitRepository(".") if err != nil { return fmt.Errorf("failed to initialize git repository: %w", err) } diff --git a/internal/git/init.go b/internal/git/init.go index 30240d3..c0b651d 100644 --- a/internal/git/init.go +++ b/internal/git/init.go @@ -1,10 +1,58 @@ package git import ( + "bufio" "fmt" + "os" "path/filepath" + "strings" ) +// UnsafeDirectories is a list of system directories that should not be initialized as git repositories. +var UnsafeDirectories = []string{"/", "/home", "/tmp"} + +// WarnIfUnsafe checks if the given path is potentially unsafe for git initialization. +// If unsafe, it prints a warning and prompts the user for confirmation. +// Returns true if the user confirmed or the path is safe, false otherwise. +func WarnIfUnsafe(path string) (bool, error) { + absPath, err := filepath.Abs(path) + if err != nil { + return false, fmt.Errorf("failed to resolve path: %w", err) + } + + // Check against unsafe system directories + for _, unsafe := range UnsafeDirectories { + if absPath == unsafe { + return promptConfirmation(absPath, "system root directory") + } + } + + // Check against home directory + homeDir, err := os.UserHomeDir() + if err == nil && absPath == homeDir { + return promptConfirmation(absPath, "home directory") + } + + return true, nil // Safe to proceed +} + +// promptConfirmation displays a warning and asks for user confirmation. +func promptConfirmation(path, reason string) (bool, error) { + fmt.Printf("\n⚠️ WARNING: You are about to initialize a git repository in a %s:\n", reason) + fmt.Printf(" Path: %s\n\n", path) + fmt.Printf("This may not be what you intended. Initializing git here could cause issues.\n") + fmt.Printf("Continue? [y/N]: ") + + reader := bufio.NewReader(os.Stdin) + response, err := reader.ReadString('\n') + if err != nil { + return false, fmt.Errorf("failed to read user input: %w", err) + } + + response = strings.TrimSpace(strings.ToLower(response)) + return response == "y", nil +} + // InitRepository initializes a new Git repository in the specified path. func (g *GitCommands) InitRepository(path string) (string, error) { if path == "" { From cb642a6afc0cf4c921664ac53f6d20e29ef1d20e Mon Sep 17 00:00:00 2001 From: shatrughan mishra Date: Sat, 7 Feb 2026 17:01:45 +0530 Subject: [PATCH 2/3] refactor: safety warning cleanup Signed-off-by: shatrughan mishra --- cmd/gitx/main.go | 54 ++++++++++++++++++++++++++++++++++++++++++-- internal/git/init.go | 48 --------------------------------------- 2 files changed, 52 insertions(+), 50 deletions(-) diff --git a/cmd/gitx/main.go b/cmd/gitx/main.go index ad5efe3..df0739f 100644 --- a/cmd/gitx/main.go +++ b/cmd/gitx/main.go @@ -1,11 +1,14 @@ package main import ( + "bufio" "errors" "fmt" "log" "os" "os/exec" + "path/filepath" + "strings" tea "github.com/charmbracelet/bubbletea" "github.com/gitxtui/gitx/internal/git" @@ -85,8 +88,8 @@ func ensureGitRepo(shouldInit bool) error { return fmt.Errorf("error: not a git repository\nrun gitx -i/--init to initialize a new git repository and open gitx") } - // Check if the directory is unsafe for git initialization - safe, err := git.WarnIfUnsafe(".") + // Check if the directory is safe for git initialization + safe, err := checkInitSafety() if err != nil { return fmt.Errorf("safety check failed: %w", err) } @@ -104,3 +107,50 @@ func ensureGitRepo(shouldInit bool) error { fmt.Println("Initialized new git repository in current directory") return nil } + +// unsafeDirs is a list of system directories that should not be initialized as git repositories. +var unsafeDirs = []string{"/", "/tmp"} + +// checkInitSafety verifies if initialization is safe in the current directory. +// If unsafe, it displays a warning and prompts for confirmation. +// Returns true if safe or user confirmed, false otherwise. +func checkInitSafety() (bool, error) { + absPath, err := filepath.Abs(".") + if err != nil { + return false, fmt.Errorf("failed to resolve path: %w", err) + } + + // Check against unsafe system directories + for _, unsafeDir := range unsafeDirs { + if absPath == unsafeDir { + return promptInitConfirmation(absPath, "system root directory") + } + } + + // Check against home directory + homeDir, err := os.UserHomeDir() + if err == nil && absPath == homeDir { + return promptInitConfirmation(absPath, "home directory") + } + + return true, nil // Safe to proceed +} + +// promptInitConfirmation displays a warning and asks for user confirmation before initialization. +func promptInitConfirmation(path, reason string) (bool, error) { + fmt.Println() + fmt.Println("WARNING: You are about to initialize a git repository in a " + reason + ":") + fmt.Printf(" Path: %s\n", path) + fmt.Println() + fmt.Println("This may not be what you intended. Initializing git here could cause issues.") + fmt.Print("Continue? [y/N]: ") + + reader := bufio.NewReader(os.Stdin) + response, err := reader.ReadString('\n') + if err != nil { + return false, fmt.Errorf("failed to read user input: %w", err) + } + + response = strings.TrimSpace(strings.ToLower(response)) + return response == "y", nil +} diff --git a/internal/git/init.go b/internal/git/init.go index c0b651d..30240d3 100644 --- a/internal/git/init.go +++ b/internal/git/init.go @@ -1,58 +1,10 @@ package git import ( - "bufio" "fmt" - "os" "path/filepath" - "strings" ) -// UnsafeDirectories is a list of system directories that should not be initialized as git repositories. -var UnsafeDirectories = []string{"/", "/home", "/tmp"} - -// WarnIfUnsafe checks if the given path is potentially unsafe for git initialization. -// If unsafe, it prints a warning and prompts the user for confirmation. -// Returns true if the user confirmed or the path is safe, false otherwise. -func WarnIfUnsafe(path string) (bool, error) { - absPath, err := filepath.Abs(path) - if err != nil { - return false, fmt.Errorf("failed to resolve path: %w", err) - } - - // Check against unsafe system directories - for _, unsafe := range UnsafeDirectories { - if absPath == unsafe { - return promptConfirmation(absPath, "system root directory") - } - } - - // Check against home directory - homeDir, err := os.UserHomeDir() - if err == nil && absPath == homeDir { - return promptConfirmation(absPath, "home directory") - } - - return true, nil // Safe to proceed -} - -// promptConfirmation displays a warning and asks for user confirmation. -func promptConfirmation(path, reason string) (bool, error) { - fmt.Printf("\n⚠️ WARNING: You are about to initialize a git repository in a %s:\n", reason) - fmt.Printf(" Path: %s\n\n", path) - fmt.Printf("This may not be what you intended. Initializing git here could cause issues.\n") - fmt.Printf("Continue? [y/N]: ") - - reader := bufio.NewReader(os.Stdin) - response, err := reader.ReadString('\n') - if err != nil { - return false, fmt.Errorf("failed to read user input: %w", err) - } - - response = strings.TrimSpace(strings.ToLower(response)) - return response == "y", nil -} - // InitRepository initializes a new Git repository in the specified path. func (g *GitCommands) InitRepository(path string) (string, error) { if path == "" { From 8f3fd927643723b5553ce54869b8b1bb6892b935 Mon Sep 17 00:00:00 2001 From: Ayush Date: Sat, 7 Feb 2026 22:24:01 +0530 Subject: [PATCH 3/3] chore: fix safety warning formatting Signed-off-by: Ayush --- cmd/gitx/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/gitx/main.go b/cmd/gitx/main.go index df0739f..e39d7c9 100644 --- a/cmd/gitx/main.go +++ b/cmd/gitx/main.go @@ -140,7 +140,7 @@ func checkInitSafety() (bool, error) { func promptInitConfirmation(path, reason string) (bool, error) { fmt.Println() fmt.Println("WARNING: You are about to initialize a git repository in a " + reason + ":") - fmt.Printf(" Path: %s\n", path) + fmt.Printf(" Path: %s\n", path) fmt.Println() fmt.Println("This may not be what you intended. Initializing git here could cause issues.") fmt.Print("Continue? [y/N]: ")