Merge branch 'master' of ssh://git.giugl.io/peperunas/nixos

This commit is contained in:
Giulio De Pasquale 2024-12-16 10:38:02 +00:00
commit 4d6a5292d9

View File

@ -3,21 +3,29 @@ create_pr_from_files() {
local temp_branch="pr-${TIMESTAMP}-temp" local temp_branch="pr-${TIMESTAMP}-temp"
local pr_branch="pr-${TIMESTAMP}" local pr_branch="pr-${TIMESTAMP}"
local base_branch="development" 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 files=()
local expanded_files=()
local temp_branch_created=false
cleanup() { cleanup() {
git checkout "${current_branch}" git checkout "${working_branch}"
git checkout "${temp_branch}" -- "${all_files[@]}"
git restore --staged . if [ "$temp_branch_created" = true ]; then
git branch -D "${temp_branch}" git checkout "${temp_branch}" -- "${expanded_files[@]}"
git restore --staged .
git branch -D "${temp_branch}"
fi
git branch -D "${pr_branch}" git branch -D "${pr_branch}"
} }
handle_error() { handle_error() {
local error_msg="$1" local error_msg="$1"
echo "Error: ${error_msg}" echo "Error: ${error_msg}"
cleanup cleanup
return 1 return 1
} }
@ -40,37 +48,76 @@ create_pr_from_files() {
return 1 return 1
fi fi
# Backup all modified files # Expand files and directories
local all_files=() for file in "${files[@]}"; do
while IFS= read -r line; do if [ -d "$file" ]; then
all_files+=("$line") while IFS= read -r line; do
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[@]}" # Check if there are any uncommitted changes
echo All: "${all_files[@]}" 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 $?
)
git checkout -b "$temp_branch" || handle_error "Failed to create temporary branch" temp_branch_created=true
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 fi
git checkout -b "$pr_branch" || handle_error "Failed to create PR 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"
return $?
)
git pull || (
handle_error "Failed to sync base branch"
return $?
)
fi
# Restore specified files from temporary branch git checkout -b "$pr_branch" || (
git checkout "$temp_branch" -- "${files[@]}" || handle_error "Failed to restore specified files" handle_error "Failed to create PR branch"
git add "${files[@]}" || handle_error "Failed to stage specified files" return $?
)
# # Generate commit message # 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 "$working_branch" -- "${expanded_files[@]}" || (
handle_error "Failed to restore specified files"
return $?
)
fi
# Verify files were staged
if [ -z "$(git diff --staged)" ]; then
handle_error "No files were staged. Aborting PR creation."
return $?
fi
# Generate commit message
echo "Generating 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_subject=$(echo "$commit_message" | head -n 1)
local commit_body=$(echo "$commit_message" | tail -n +2) 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." git commit --edit -m "$commit_subject"$'\n\n'"$commit_body" || handle_error "Committing files failed."
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
handle_error "Committing files failed." handle_error "Committing files failed."
@ -90,6 +137,8 @@ create_pr_from_files() {
cleanup cleanup
} }
create_pr_from_commit() { create_pr_from_commit() {
local commit_hash="$1" local commit_hash="$1"
local base_branch="${2:-development}" local base_branch="${2:-development}"