From b0080ad5e9da86f5896f9772c1593ff38c8bca49 Mon Sep 17 00:00:00 2001 From: Giulio De Pasquale Date: Tue, 26 Nov 2024 12:55:41 +0000 Subject: [PATCH 1/5] Update roles/home/aichat/roles/commitmessage.md --- roles/home/aichat/roles/commitmessage.md | 1 + 1 file changed, 1 insertion(+) diff --git a/roles/home/aichat/roles/commitmessage.md b/roles/home/aichat/roles/commitmessage.md index ff4a4e4..f07401e 100644 --- a/roles/home/aichat/roles/commitmessage.md +++ b/roles/home/aichat/roles/commitmessage.md @@ -29,6 +29,7 @@ Process: 4. Expand on the most promising thought(s) by generating sub-thoughts. 5. Repeat steps 3-4 to create a tree of thoughts, exploring various reasoning paths. 6. Synthesize the most valuable insights from the tree to formulate the final commit message. +7. DO NOT mention "useless" details such as added imports. For each thought and sub-thought, consider: - The type of change (e.g., feature, bug fix, refactor, style, docs, etc.) From 91d7bcbab19d877ae8354180c4ab165236eea556 Mon Sep 17 00:00:00 2001 From: Giulio De Pasquale Date: Wed, 4 Dec 2024 11:34:05 +0000 Subject: [PATCH 2/5] Update roles/home/scripts/commits.sh --- roles/home/scripts/commits.sh | 2 ++ 1 file changed, 2 insertions(+) diff --git a/roles/home/scripts/commits.sh b/roles/home/scripts/commits.sh index 29c758e..99f0d06 100644 --- a/roles/home/scripts/commits.sh +++ b/roles/home/scripts/commits.sh @@ -26,6 +26,8 @@ create_pr_from_files() { local commit_message="" local new_branch="pr-$(date +%Y%m%d%H%M%S)" + git checkout "${origin_branch}" + git checkout -b "$new_branch" git add "${files[@]}" From d91b3bc9a930b763f313c98ce3adb36c8891f9cc Mon Sep 17 00:00:00 2001 From: Giulio De Pasquale Date: Wed, 4 Dec 2024 16:13:24 +0000 Subject: [PATCH 3/5] 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() { From 5ebc68ad804ff50536c0d7c1438513677067eff7 Mon Sep 17 00:00:00 2001 From: Giulio De Pasquale Date: Thu, 5 Dec 2024 15:02:19 +0000 Subject: [PATCH 4/5] fix(create_pr_from_files): improved robustness che prima faceva schifo --- roles/home/scripts/commits.sh | 70 +++++++++++++++++------------------ 1 file changed, 35 insertions(+), 35 deletions(-) diff --git a/roles/home/scripts/commits.sh b/roles/home/scripts/commits.sh index 6d51f4d..9f05665 100644 --- a/roles/home/scripts/commits.sh +++ b/roles/home/scripts/commits.sh @@ -1,27 +1,23 @@ 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 base_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}" + git branch -D "${pr_branch}" } - # Function to handle errors handle_error() { local error_msg="$1" - echo "Error: ${error_msg}" cleanup - return 1 } @@ -29,7 +25,7 @@ create_pr_from_files() { while [[ $# -gt 0 ]]; do case $1 in -b | --base) - origin_branch="$2" + base_branch="$2" shift 2 ;; *) @@ -44,50 +40,54 @@ create_pr_from_files() { return 1 fi - asd=($(for f in "${files[@]}"; do find "${f}" | tr -s '/'; done)) - # Initialize an empty array - files_to_keep=() - - # 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=() - - # Use a while loop to read each line from the command substitution + # Backup all modified files + local all_files=() while IFS= read -r line; do all_files+=("$line") - done < <(git status --porcelain | cut -d " " -f 3) + done < <(git status -s | cut -d " " -f 3) - # Switch to origin branch if not already there - if [ "$current_branch" != "$origin_branch" ]; then - git checkout "${origin_branch}" + echo Our: "${files[@]}" + echo All: "${all_files[@]}" + # 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" fi - git checkout -b "${temp_branch}" && git commit -am "Saving changes" || cleanup + 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" - git checkout "${origin_branch}" && git checkout -b "${pr_branch}" && git checkout "${temp_branch}" -- "${files_to_keep[@]}" || cleanup + # Create PR branch from base branch + git checkout "$base_branch" || handle_error "Failed to checkout base branch" + git checkout -b "$pr_branch" || handle_error "Failed to create PR branch" + # 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" + + # # 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.") - # 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) - git commit -m "$commit_subject"$'\n\n'"$commit_body" || handle_error "Committing files failed." + # # # 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." + fi - git push origin "${pr_branch}" + # Push the PR branch to the remote repository + git push origin "$pr_branch" || handle_error "Failed to push PR branch" - # Create PR + # Create the pull request gh pr create \ - --base "${origin_branch}" \ - --head "${pr_branch}" \ - --title "${commit_subject}" \ - --body "${commit_body}" || - handle_error "Failed to create pull request" + --base "$base_branch" \ + --head "$pr_branch" || handle_error "Failed to create pull request" echo "Pull request created successfully." + + # Cleanup cleanup } From 5a95a015e865a97b38c1ab85da394df4f14f20c7 Mon Sep 17 00:00:00 2001 From: Giulio De Pasquale Date: Fri, 6 Dec 2024 12:03:58 +0000 Subject: [PATCH 5/5] Update roles/home/scripts/commits.sh --- roles/home/scripts/commits.sh | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/roles/home/scripts/commits.sh b/roles/home/scripts/commits.sh index 9f05665..2ff26cc 100644 --- a/roles/home/scripts/commits.sh +++ b/roles/home/scripts/commits.sh @@ -48,17 +48,16 @@ create_pr_from_files() { echo Our: "${files[@]}" echo All: "${all_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" + # 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" fi - 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" - - # Create PR branch from base branch - git checkout "$base_branch" || handle_error "Failed to checkout base branch" git checkout -b "$pr_branch" || handle_error "Failed to create PR branch" # Restore specified files from temporary branch