From 511979315dd6daec4931bd2ccbb6362111823f41 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 22 Jun 2025 14:46:29 +1200 Subject: [PATCH] Make add-all default with user confirmation --- gp/gp | 85 ++++++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 67 insertions(+), 18 deletions(-) diff --git a/gp/gp b/gp/gp index f0116d8..b3a3b21 100755 --- a/gp/gp +++ b/gp/gp @@ -31,15 +31,17 @@ OPTIONS: -n, --dry-run Show what would be committed without actually doing it -f, --force Skip safety checks (use with caution) -b, --branch Specify branch to push to (default: current branch) - -a, --add-all Add all files including untracked (default: only staged + modified) + -a, --add-all Add all files including untracked (default) + --staged-only Only commit staged changes (don't add untracked files) ARGUMENTS: message Optional custom commit message (overrides auto-generation) EXAMPLES: - gp # Auto-generate commit message and push - gp "Fix bug in parser" # Use custom commit message + gp # Add all files, auto-generate commit message and push + gp "Fix bug in parser" # Add all files with custom commit message gp --dry-run # Preview what would be committed + gp --staged-only # Only commit staged changes (don't add untracked) gp -b develop # Push to develop branch instead of current EOF @@ -189,28 +191,72 @@ check_for_changes() { fi } -# Function to show what would be committed -show_status() { +# Function to show what would be committed and get user confirmation +show_status_and_confirm() { print_info "Current branch: $(git branch --show-current)" print_info "Repository: $(git remote get-url origin 2>/dev/null || echo 'No remote')" echo - if git diff --cached --quiet; then - print_info "Staged changes: None" - if ! git diff --quiet; then - print_info "Modified files (unstaged):" - git diff --name-only | sed 's/^/ /' - fi - else + local has_staged_changes=false + local has_unstaged_changes=false + local has_untracked_files=false + + # Show staged changes + if ! git diff --cached --quiet; then print_info "Staged changes:" git diff --cached --name-only | sed 's/^/ /' + has_staged_changes=true fi + # Show unstaged changes + if ! git diff --quiet; then + if [ "$ADD_ALL" = true ]; then + print_info "Modified files (will be added):" + else + print_info "Modified files (unstaged, will NOT be included):" + fi + git diff --name-only | sed 's/^/ /' + has_unstaged_changes=true + fi + + # Show untracked files if [ "$ADD_ALL" = true ]; then local untracked=$(git ls-files --others --exclude-standard) if [ -n "$untracked" ]; then print_info "Untracked files (will be added):" echo "$untracked" | sed 's/^/ /' + has_untracked_files=true + fi + fi + + # Show summary of what will happen + echo + if [ "$ADD_ALL" = true ]; then + if [ "$has_unstaged_changes" = true ] || [ "$has_untracked_files" = true ]; then + print_warning "Files will be automatically added before committing" + fi + if [ "$has_staged_changes" = false ] && [ "$has_unstaged_changes" = false ] && [ "$has_untracked_files" = false ]; then + print_info "No changes found to commit" + fi + else + if [ "$has_unstaged_changes" = true ]; then + print_warning "Unstaged changes will NOT be included (use -a to include them)" + fi + if [ "$has_staged_changes" = false ]; then + print_info "No staged changes to commit" + fi + fi + + # Confirmation for file additions (unless forced or dry-run) + if [ "$ADD_ALL" = true ] && [ "$FORCE" = false ] && [ "$DRY_RUN" = false ]; then + if [ "$has_unstaged_changes" = true ] || [ "$has_untracked_files" = true ]; then + echo + read -p "Add these files and continue? [y/N] " -n 1 -r + echo + if [[ ! $REPLY =~ ^[Yy]$ ]]; then + print_info "Aborted by user" + exit 0 + fi fi fi } @@ -245,14 +291,14 @@ do_commit_and_push() { autocomplete() { local args=("$@") if [ ${#args[@]} -eq 0 ]; then - printf "%s\n" "--help" "--dry-run" "--force" "--add-all" "--branch" "-h" "-n" "-f" "-a" "-b" + printf "%s\n" "--help" "--dry-run" "--force" "--add-all" "--staged-only" "--branch" "-h" "-n" "-f" "-a" "-b" fi } # Default values DRY_RUN=false FORCE=false -ADD_ALL=false +ADD_ALL=true # Default to adding all files CUSTOM_MESSAGE="" TARGET_BRANCH="" PUSH_ONLY=false @@ -286,7 +332,11 @@ while [[ $# -gt 0 ]]; do shift ;; -a|--add-all) - ADD_ALL=true + ADD_ALL=true # Explicitly set (though it's default) + shift + ;; + --staged-only) + ADD_ALL=false shift ;; -b|--branch) @@ -352,9 +402,8 @@ main() { exit 0 fi - # Show current status - show_status - echo + # Show current status and get confirmation for file additions + show_status_and_confirm # Generate or use custom commit message if [ -n "$CUSTOM_MESSAGE" ]; then