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.) diff --git a/roles/home/scripts/commits.sh b/roles/home/scripts/commits.sh index 29c758e..2ff26cc 100644 --- a/roles/home/scripts/commits.sh +++ b/roles/home/scripts/commits.sh @@ -1,13 +1,31 @@ create_pr_from_files() { - local origin_branch="development" + local TIMESTAMP=$(date +%Y%m%d%H%M%S) + 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 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}" + } + + handle_error() { + local error_msg="$1" + echo "Error: ${error_msg}" + cleanup + return 1 + } + # Parse arguments while [[ $# -gt 0 ]]; do case $1 in -b | --base) - origin_branch="$2" + base_branch="$2" shift 2 ;; *) @@ -22,54 +40,54 @@ 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)" + # Backup all modified files + local all_files=() + while IFS= read -r line; do + all_files+=("$line") + done < <(git status -s | cut -d " " -f 3) - git checkout -b "$new_branch" - git add "${files[@]}" + echo Our: "${files[@]}" + echo All: "${all_files[@]}" - # Generate commit message using aichat - echo "Generating commit message..." - commit_message=$(git diff --staged | aichat -m ollama:pino-coder -r commitmessage) + 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" - 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 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 - # Extract title and body from the generated commit message + 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.") 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 checkout "${current_branch}" - - return 1 + # # # 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 "$new_branch" + # Push the PR branch to the remote repository + git push origin "$pr_branch" || handle_error "Failed to push PR branch" - gh pr create --base "$origin_branch" --head "$new_branch" --title "$commit_subject" --body "$commit_body" + # Create the pull request + gh pr create \ + --base "$base_branch" \ + --head "$pr_branch" || handle_error "Failed to create pull request" echo "Pull request created successfully." - git checkout "${current_branch}" + # Cleanup + cleanup } create_pr_from_commit() {