From e65b3a867a008533babb7f029741ace075173938 Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 25 Jun 2025 18:09:27 +1200 Subject: [PATCH] Modify gp/gp --- gp/gp | 86 ++++++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 64 insertions(+), 22 deletions(-) diff --git a/gp/gp b/gp/gp index 1648ee3..4c7f79a 100755 --- a/gp/gp +++ b/gp/gp @@ -101,7 +101,7 @@ generate_commit_message() { local single_file single_file=$(echo "$files_changed" | head -1) local change_type - change_type=$(git diff --cached --name-status "$single_file" 2>/dev/null || git diff --name-status "$single_file") + change_type=$(git diff --cached --name-status -- "$single_file" 2>/dev/null || git diff --name-status -- "$single_file") case "${change_type:0:1}" in A) message="Add $single_file" ;; M) message="Update $single_file" ;; @@ -137,19 +137,31 @@ check_git_repo() { # Function to check for uncommitted changes and unpushed commits check_for_changes() { - local has_staged - has_staged=$(! git diff --cached --quiet && echo "true" || echo "false") - local has_modified - has_modified=$(! git diff --quiet && echo "true" || echo "false") - local has_untracked - has_untracked=$([ -n "$(git ls-files --others --exclude-standard)" ] && echo "true" || echo "false") + local has_staged=false + local has_modified=false + local has_untracked=false + + # Check for staged changes + if ! git diff --cached --quiet; then + has_staged=true + fi + + # Check for modified files + if ! git diff --quiet; then + has_modified=true + fi + + # Check for untracked files + if [ -n "$(git ls-files --others --exclude-standard)" ]; then + has_untracked=true + fi # Check for unpushed commits local current_branch current_branch=$(git branch --show-current) local unpushed_commits="" if git rev-parse --verify "origin/$current_branch" >/dev/null 2>&1; then - unpushed_commits=$(git rev-list "origin/$current_branch..HEAD" 2>/dev/null || true) + unpushed_commits=$(git rev-list "origin/$current_branch..HEAD" -- 2>/dev/null || true) fi # If add-all is enabled, check if we have any changes at all @@ -214,7 +226,7 @@ show_status_and_confirm() { # Show staged changes if ! git diff --cached --quiet; then print_info "Staged changes:" - git diff --cached --name-only | while IFS= read -r line; do echo " $line"; done + git diff --cached --name-only -- | while IFS= read -r line; do echo " $line"; done has_staged_changes=true fi @@ -225,7 +237,7 @@ show_status_and_confirm() { else print_info "Modified files (unstaged, will NOT be included):" fi - git diff --name-only | while IFS= read -r line; do echo " $line"; done + git diff --name-only -- | while IFS= read -r line; do echo " $line"; done has_unstaged_changes=true fi @@ -283,21 +295,34 @@ do_commit_and_push() { # Add files if requested if [ "$ADD_ALL" = true ]; then print_info "Adding all files..." - git add . + git add -A fi # Commit print_info "Committing with message: '$commit_msg'" - git commit -m "$commit_msg" + git commit -m "$commit_msg" -- # Push print_info "Pushing to $target_branch..." - if git push origin "$target_branch"; then - print_success "Successfully pushed to origin/$target_branch" + # Check if branch exists on remote to determine if we need -u flag + if git ls-remote --exit-code --heads origin "$target_branch" >/dev/null 2>&1; then + # Branch exists on remote, normal push + if git push origin "$target_branch"; then + print_success "Successfully pushed to origin/$target_branch" + else + print_error "Failed to push to origin/$target_branch" + print_info "You may need to pull first: git pull origin $target_branch" + exit 1 + fi else - print_error "Failed to push to origin/$target_branch" - print_info "You may need to pull first: git pull origin $target_branch" - exit 1 + # Branch doesn't exist on remote, use -u to set upstream + print_info "Setting upstream branch (first push to origin/$target_branch)" + if git push -u origin "$target_branch"; then + print_success "Successfully pushed and set upstream to origin/$target_branch" + else + print_error "Failed to push to origin/$target_branch" + exit 1 + fi fi } @@ -354,6 +379,10 @@ while [[ $# -gt 0 ]]; do shift ;; -b|--branch) + if [ -z "${2:-}" ]; then + print_error "Option -b/--branch requires a branch name" + exit 1 + fi TARGET_BRANCH="$2" shift 2 ;; @@ -406,12 +435,25 @@ main() { # Execute push only print_info "Pushing existing commits to $TARGET_BRANCH..." - if git push origin "$TARGET_BRANCH"; then - print_success "Successfully pushed existing commits to origin/$TARGET_BRANCH" + # Check if branch exists on remote to determine if we need -u flag + if git ls-remote --exit-code --heads origin "$TARGET_BRANCH" >/dev/null 2>&1; then + # Branch exists on remote, normal push + if git push origin "$TARGET_BRANCH"; then + print_success "Successfully pushed existing commits to origin/$TARGET_BRANCH" + else + print_error "Failed to push to origin/$TARGET_BRANCH" + print_info "You may need to pull first: git pull origin $TARGET_BRANCH" + exit 1 + fi else - print_error "Failed to push to origin/$TARGET_BRANCH" - print_info "You may need to pull first: git pull origin $TARGET_BRANCH" - exit 1 + # Branch doesn't exist on remote, use -u to set upstream + print_info "Setting upstream branch (first push to origin/$TARGET_BRANCH)" + if git push -u origin "$TARGET_BRANCH"; then + print_success "Successfully pushed existing commits and set upstream to origin/$TARGET_BRANCH" + else + print_error "Failed to push to origin/$TARGET_BRANCH" + exit 1 + fi fi exit 0 fi