Update roles/home/scripts/commits.sh

This commit is contained in:
Giulio De Pasquale 2024-12-11 16:33:12 +00:00
parent 2758be3937
commit a62e0f2869

View File

@ -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[@]}"
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=()
# Expand files and directories
for file in "${files[@]}"; do
if [ -d "$file" ]; then
while IFS= read -r line; do
all_files+=("$line")
done < <(git status -s | cut -d " " -f 3)
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}"