Add unpushed commits detection to gp script
Some checks failed
Build-Test-Publish / test-install-from-scratch (linux/amd64) (push) Has been cancelled
Build-Test-Publish / test-install-from-scratch (linux/arm64) (push) Has been cancelled
Build-Test-Publish / build (linux/amd64) (push) Has been cancelled
Build-Test-Publish / build (linux/arm64) (push) Has been cancelled

This commit is contained in:
Your Name 2025-06-22 14:42:38 +12:00
parent 7dc2ed1e99
commit 551b062b71

85
gp/gp
View File

@ -129,28 +129,62 @@ check_git_repo() {
fi
}
# Function to check for uncommitted changes
# Function to check for uncommitted changes and unpushed commits
check_for_changes() {
local has_staged=$(! git diff --cached --quiet && echo "true" || echo "false")
local has_modified=$(! git diff --quiet && echo "true" || echo "false")
local has_untracked=$([ -n "$(git ls-files --others --exclude-standard)" ] && echo "true" || echo "false")
# Check for unpushed commits
local 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)
fi
# If add-all is enabled, check if we have any changes at all
if [ "$ADD_ALL" = true ]; then
if [ "$has_staged" = false ] && [ "$has_modified" = false ] && [ "$has_untracked" = false ]; then
print_info "No changes to commit (working tree clean)"
exit 0
# No working tree changes, but check for unpushed commits
if [ -n "$unpushed_commits" ]; then
local commit_count=$(echo "$unpushed_commits" | wc -l)
print_info "No working tree changes, but found $commit_count unpushed commit(s)"
print_info "Latest unpushed commit: $(git log --oneline -1)"
# Set a flag to indicate we should only push, not commit
PUSH_ONLY=true
return 0
else
print_info "No changes to commit (working tree clean)"
exit 0
fi
fi
else
# If add-all is disabled, only check staged changes
if [ "$has_staged" = false ]; then
if [ "$has_modified" = true ] || [ "$has_untracked" = true ]; then
print_warning "No staged changes found"
print_info "Use 'gp -a' to add all files, or stage changes first with 'git add'"
# No staged changes, but check for unpushed commits
if [ -n "$unpushed_commits" ]; then
local commit_count=$(echo "$unpushed_commits" | wc -l)
print_info "No staged changes, but found $commit_count unpushed commit(s)"
print_info "Latest unpushed commit: $(git log --oneline -1)"
if [ "$has_modified" = true ] || [ "$has_untracked" = true ]; then
print_warning "You have unstaged changes that won't be included"
print_info "Use 'gp -a' to include all changes, or stage them first with 'git add'"
fi
# Set a flag to indicate we should only push, not commit
PUSH_ONLY=true
return 0
else
print_info "No changes to commit (working tree clean)"
if [ "$has_modified" = true ] || [ "$has_untracked" = true ]; then
print_warning "No staged changes found"
print_info "Use 'gp -a' to add all files, or stage changes first with 'git add'"
else
print_info "No changes to commit (working tree clean)"
fi
exit 0
fi
exit 0
fi
fi
}
@ -221,6 +255,7 @@ FORCE=false
ADD_ALL=false
CUSTOM_MESSAGE=""
TARGET_BRANCH=""
PUSH_ONLY=false
# Handle special commands first
case "${1:-}" in
@ -283,6 +318,40 @@ main() {
# Check for changes
check_for_changes
# Handle push-only case (unpushed commits but no working tree changes)
if [ "$PUSH_ONLY" = true ]; then
echo
# Dry run mode for push-only
if [ "$DRY_RUN" = true ]; then
print_warning "DRY RUN MODE - No changes will be made"
print_info "Would push existing commits to: origin/$TARGET_BRANCH"
exit 0
fi
# Safety confirmation for push-only (unless forced)
if [ "$FORCE" = false ]; then
echo
read -p "Push existing commits to origin/$TARGET_BRANCH? [y/N] " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
print_info "Aborted by user"
exit 0
fi
fi
# 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"
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
exit 0
fi
# Show current status
show_status
echo