From d91b3bc9a930b763f313c98ce3adb36c8891f9cc Mon Sep 17 00:00:00 2001 From: Giulio De Pasquale Date: Wed, 4 Dec 2024 16:13:24 +0000 Subject: [PATCH] scripts(zsh): update create_pr_from_files --- roles/home/scripts/commits.sh | 93 +++++++++++++++++++++-------------- 1 file changed, 55 insertions(+), 38 deletions(-) diff --git a/roles/home/scripts/commits.sh b/roles/home/scripts/commits.sh index 99f0d06..6d51f4d 100644 --- a/roles/home/scripts/commits.sh +++ b/roles/home/scripts/commits.sh @@ -1,7 +1,29 @@ create_pr_from_files() { + # Constants + local TIMESTAMP=$(date +%Y%m%d%H%M%S) + + # Branch names + local temp_branch="pr-${TIMESTAMP}-temp" + local pr_branch="pr-${TIMESTAMP}" local origin_branch="development" local current_branch=$(git rev-parse --abbrev-ref HEAD) - local files=() + + cleanup() { + git checkout "${current_branch}" + git checkout "${temp_branch}" -- "${all_files[@]}" + git restore --staged . + git branch -D "${temp_branch}" + } + + # Function to handle errors + handle_error() { + local error_msg="$1" + + echo "Error: ${error_msg}" + cleanup + + return 1 + } # Parse arguments while [[ $# -gt 0 ]]; do @@ -22,56 +44,51 @@ create_pr_from_files() { return 1 fi - # Checkout to the new branch and add the specified files - local commit_message="" - local new_branch="pr-$(date +%Y%m%d%H%M%S)" + asd=($(for f in "${files[@]}"; do find "${f}" | tr -s '/'; done)) + # Initialize an empty array + files_to_keep=() - git checkout "${origin_branch}" + # Use a while loop to read each line from the command substitution + while IFS= read -r line; do + files_to_keep+=("$line") + done < <(git status --porcelain | grep -f <(printf "%s\n" "${asd[@]}") | cut -d " " -f 3) + # Initialize an empty array + all_files=() - git checkout -b "$new_branch" - git add "${files[@]}" + # Use a while loop to read each line from the command substitution + while IFS= read -r line; do + all_files+=("$line") + done < <(git status --porcelain | cut -d " " -f 3) - # Generate commit message using aichat - echo "Generating commit message..." - commit_message=$(git diff --staged | aichat -m ollama:pino-coder -r commitmessage) - - if [ -z "$commit_message" ]; then - echo "Failed to generate commit message. Cleaning up." - git checkout - - git branch -D "$new_branch" - if $has_changes; then - echo "Restoring stashed changes..." - git stash pop - fi - git checkout "${current_branch}" - return 1 + # Switch to origin branch if not already there + if [ "$current_branch" != "$origin_branch" ]; then + git checkout "${origin_branch}" fi + git checkout -b "${temp_branch}" && git commit -am "Saving changes" || cleanup + + git checkout "${origin_branch}" && git checkout -b "${pr_branch}" && git checkout "${temp_branch}" -- "${files_to_keep[@]}" || cleanup + + echo "Generating commit message..." + local commit_message=$(git diff --staged | aichat -m ollama:pino-coder -r commitmessage || handle_error "Failed to generate commit message.") # Extract title and body from the generated commit message local commit_subject=$(echo "$commit_message" | head -n 1) local commit_body=$(echo "$commit_message" | tail -n +2) - if ! git commit -m "$commit_subject"$'\n\n'"$commit_body"; then - echo "Committing changes failed. Cleaning up." - git checkout - - git branch -D "$new_branch" - if $has_changes; then - echo "Restoring stashed changes..." - git stash pop - fi + git commit -m "$commit_subject"$'\n\n'"$commit_body" || handle_error "Committing files failed." - git checkout "${current_branch}" + git push origin "${pr_branch}" - return 1 - fi - - git push origin "$new_branch" - - gh pr create --base "$origin_branch" --head "$new_branch" --title "$commit_subject" --body "$commit_body" + # Create PR + gh pr create \ + --base "${origin_branch}" \ + --head "${pr_branch}" \ + --title "${commit_subject}" \ + --body "${commit_body}" || + handle_error "Failed to create pull request" echo "Pull request created successfully." - - git checkout "${current_branch}" + cleanup } create_pr_from_commit() {