From a62e0f2869ca1b3be51fc91ec89ba2c8e10702a0 Mon Sep 17 00:00:00 2001 From: Giulio De Pasquale Date: Wed, 11 Dec 2024 16:33:12 +0000 Subject: [PATCH 1/2] Update roles/home/scripts/commits.sh --- roles/home/scripts/commits.sh | 97 +++++++++++++++++++++++++++-------- 1 file changed, 76 insertions(+), 21 deletions(-) diff --git a/roles/home/scripts/commits.sh b/roles/home/scripts/commits.sh index 2ff26cc..46e7fdb 100644 --- a/roles/home/scripts/commits.sh +++ b/roles/home/scripts/commits.sh @@ -5,19 +5,27 @@ create_pr_from_files() { local base_branch="development" local current_branch=$(git rev-parse --abbrev-ref HEAD) local files=() + local expanded_files=() + local temp_branch_created=false cleanup() { git checkout "${current_branch}" - git checkout "${temp_branch}" -- "${all_files[@]}" - git restore --staged . - git branch -D "${temp_branch}" + + if [ "$temp_branch_created" = true ]; then + git checkout "${temp_branch}" -- "${expanded_files[@]}" + git restore --staged . + git branch -D "${temp_branch}" + fi + git branch -D "${pr_branch}" } handle_error() { local error_msg="$1" + echo "Error: ${error_msg}" cleanup + return 1 } @@ -40,37 +48,83 @@ create_pr_from_files() { return 1 fi - # Backup all modified files - local all_files=() - while IFS= read -r line; do - all_files+=("$line") - done < <(git status -s | cut -d " " -f 3) + # Expand files and directories + for file in "${files[@]}"; do + if [ -d "$file" ]; then + while IFS= read -r line; do + expanded_files+=("$line") + done < <(find "$file" -type f) + else + expanded_files+=("$file") + fi + done - echo Our: "${files[@]}" - echo All: "${all_files[@]}" + echo "Original files: ${files[@]}" + echo "Expanded files: ${expanded_files[@]}" - git checkout -b "$temp_branch" || handle_error "Failed to create temporary branch" - git commit -am "Backup changes" || handle_error "Failed to commit changes to temporary branch" + # Check if there are any uncommitted changes + if [ -n "$(git status -s)" ]; then + # Only create temp branch and backup if there are uncommitted changes + git checkout -b "$temp_branch" || ( + handle_error "Failed to create temporary branch" + return $? + ) + git commit -am "Backup changes" || ( + handle_error "Failed to commit changes to temporary branch" + return $? + ) + + temp_branch_created=true + fi # Switch to base branch and create temporary branch if [ "$current_branch" != "$base_branch" ]; then - git checkout "$base_branch" || handle_error "Failed to checkout base branch" - git pull || handle_error "Failed to sync base branch" + git checkout "$base_branch" || ( + handle_error "Failed to checkout base branch" + return $? + ) + git pull || ( + handle_error "Failed to sync base branch" + return $? + ) fi - git checkout -b "$pr_branch" || handle_error "Failed to create PR branch" + git checkout -b "$pr_branch" || ( + handle_error "Failed to create PR branch" + return $? + ) - # Restore specified files from temporary branch - git checkout "$temp_branch" -- "${files[@]}" || handle_error "Failed to restore specified files" - git add "${files[@]}" || handle_error "Failed to stage specified files" + # Restore files either from temp branch or original branch + if [ "$temp_branch_created" = true ]; then + git checkout "$temp_branch" -- "${expanded_files[@]}" || ( + handle_error "Failed to restore specified files" + return $? + ) + else + git checkout "$current_branch" -- "${expanded_files[@]}" || ( + handle_error "Failed to restore specified files" + return $? + ) + fi - # # Generate commit message + git add "${expanded_files[@]}" || ( + handle_error "Failed to stage specified files" + return $? + ) + + # Verify files were staged + if [ -z "$(git diff --cached --name-only)" ]; then + handle_error "No files were staged. Aborting PR creation." + return $? + fi + + # Generate commit message echo "Generating commit message..." - local commit_message=$(git diff --staged | aichat -m ollama:pino-coder -r commitmessage || handle_error "Failed to generate commit message.") + local commit_message=$(git ais || handle_error "Failed to generate commit message.") local commit_subject=$(echo "$commit_message" | head -n 1) local commit_body=$(echo "$commit_message" | tail -n +2) - # # # Commit the specified files + # Commit the specified files git commit --edit -m "$commit_subject"$'\n\n'"$commit_body" || handle_error "Committing files failed." if [ $? -ne 0 ]; then handle_error "Committing files failed." @@ -90,6 +144,7 @@ create_pr_from_files() { cleanup } + create_pr_from_commit() { local commit_hash="$1" local base_branch="${2:-development}" From 165fa5f3cec2b760f8dd84d02e55abdbb8295bfa Mon Sep 17 00:00:00 2001 From: Giulio De Pasquale Date: Wed, 11 Dec 2024 16:56:19 +0000 Subject: [PATCH 2/2] Update roles/home/scripts/commits.sh --- roles/home/scripts/commits.sh | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/roles/home/scripts/commits.sh b/roles/home/scripts/commits.sh index 46e7fdb..94e7c18 100644 --- a/roles/home/scripts/commits.sh +++ b/roles/home/scripts/commits.sh @@ -3,13 +3,13 @@ create_pr_from_files() { local temp_branch="pr-${TIMESTAMP}-temp" local pr_branch="pr-${TIMESTAMP}" local base_branch="development" - local current_branch=$(git rev-parse --abbrev-ref HEAD) + local working_branch=$(git rev-parse --abbrev-ref HEAD) local files=() local expanded_files=() local temp_branch_created=false cleanup() { - git checkout "${current_branch}" + git checkout "${working_branch}" if [ "$temp_branch_created" = true ]; then git checkout "${temp_branch}" -- "${expanded_files[@]}" @@ -59,9 +59,6 @@ create_pr_from_files() { fi done - echo "Original files: ${files[@]}" - echo "Expanded files: ${expanded_files[@]}" - # Check if there are any uncommitted changes if [ -n "$(git status -s)" ]; then # Only create temp branch and backup if there are uncommitted changes @@ -77,7 +74,8 @@ create_pr_from_files() { temp_branch_created=true fi - # Switch to base branch and create temporary branch + # Get current branch and switch to base branch if needed + current_branch=$(git rev-parse --abbrev-ref HEAD) if [ "$current_branch" != "$base_branch" ]; then git checkout "$base_branch" || ( handle_error "Failed to checkout base branch" @@ -101,19 +99,14 @@ create_pr_from_files() { return $? ) else - git checkout "$current_branch" -- "${expanded_files[@]}" || ( + git checkout "$working_branch" -- "${expanded_files[@]}" || ( handle_error "Failed to restore specified files" return $? ) fi - git add "${expanded_files[@]}" || ( - handle_error "Failed to stage specified files" - return $? - ) - # Verify files were staged - if [ -z "$(git diff --cached --name-only)" ]; then + if [ -z "$(git diff --staged)" ]; then handle_error "No files were staged. Aborting PR creation." return $? fi @@ -145,6 +138,7 @@ create_pr_from_files() { } + create_pr_from_commit() { local commit_hash="$1" local base_branch="${2:-development}"