125 lines
3.0 KiB
Bash
125 lines
3.0 KiB
Bash
create_pr_from_files() {
|
|
local origin_branch="development"
|
|
local current_branch=$(git rev-parse --abbrev-ref HEAD)
|
|
local files=()
|
|
|
|
# Parse arguments
|
|
while [[ $# -gt 0 ]]; do
|
|
case $1 in
|
|
-b | --base)
|
|
origin_branch="$2"
|
|
shift 2
|
|
;;
|
|
*)
|
|
files+=("$1")
|
|
shift
|
|
;;
|
|
esac
|
|
done
|
|
|
|
if [ ${#files[@]} -eq 0 ]; then
|
|
echo "Usage: create_pr_from_files <file1> [<file2> ...] [-b <base-branch>]"
|
|
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)"
|
|
|
|
git checkout -b "$new_branch"
|
|
git add "${files[@]}"
|
|
|
|
# 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
|
|
fi
|
|
|
|
# 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 checkout "${current_branch}"
|
|
|
|
return 1
|
|
fi
|
|
|
|
git push origin "$new_branch"
|
|
|
|
gh pr create --base "$origin_branch" --head "$new_branch" --title "$commit_subject" --body "$commit_body"
|
|
|
|
echo "Pull request created successfully."
|
|
|
|
git checkout "${current_branch}"
|
|
}
|
|
|
|
create_pr_from_commit() {
|
|
local commit_hash="$1"
|
|
local base_branch="${2:-development}"
|
|
|
|
if [ -z "$commit_hash" ]; then
|
|
echo "Usage: create_pr_from_commit <commit-hash> [<base-branch>]"
|
|
return 1
|
|
fi
|
|
|
|
# Check for uncommitted changes and stash them if any
|
|
local has_changes=false
|
|
if ! git diff --quiet || ! git diff --cached --quiet; then
|
|
echo "Stashing uncommitted changes..."
|
|
git stash push -u
|
|
has_changes=true
|
|
fi
|
|
|
|
# Create a new branch name based on the commit hash
|
|
local new_branch="pr-${commit_hash}"
|
|
|
|
# Checkout to the new branch and cherry-pick the commit
|
|
git checkout "$base_branch" && git checkout -b "$new_branch"
|
|
if ! git cherry-pick "$commit_hash"; then
|
|
echo "Cherry-picking failed. Cleaning up."
|
|
git checkout -
|
|
git branch -D "$new_branch"
|
|
if $has_changes; then
|
|
echo "Restoring stashed changes..."
|
|
git stash pop
|
|
fi
|
|
return 1
|
|
fi
|
|
|
|
git push origin "$new_branch"
|
|
|
|
local commit_message=$(git show -s --format=%B "$commit_hash")
|
|
local commit_subject=$(echo "$commit_message" | head -n 1)
|
|
local commit_body=$(echo "$commit_message" | tail -n +2)
|
|
|
|
gh pr create --base "$base_branch" --head "$new_branch" --title "$commit_subject" --body "$commit_body"
|
|
|
|
echo "Pull request created successfully."
|
|
|
|
if $has_changes; then
|
|
echo "Restoring stashed changes..."
|
|
git stash pop
|
|
fi
|
|
}
|
|
|
|
|