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 }