190 lines
4.6 KiB
Bash
190 lines
4.6 KiB
Bash
create_pr_from_files() {
|
|
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 working_branch=$(git rev-parse --abbrev-ref HEAD)
|
|
local files=()
|
|
local expanded_files=()
|
|
local temp_branch_created=false
|
|
|
|
cleanup() {
|
|
git checkout "${working_branch}"
|
|
|
|
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
|
|
}
|
|
|
|
# Parse arguments
|
|
while [[ $# -gt 0 ]]; do
|
|
case $1 in
|
|
-b | --base)
|
|
base_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
|
|
|
|
# Expand files and directories
|
|
for file in "${files[@]}"; do
|
|
if [ -d "$file" ]; then
|
|
while IFS= read -r line; do
|
|
expanded_files+=("$line")
|
|
done < <(find "$file" -type f)
|
|
else
|
|
expanded_files+=("$file")
|
|
fi
|
|
done
|
|
|
|
# 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
|
|
|
|
# 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
|
|
|
|
git checkout -b "$pr_branch" || (
|
|
handle_error "Failed to create PR branch"
|
|
return $?
|
|
)
|
|
|
|
# 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..."
|
|
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
|
|
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
|
|
|
|
# Push the PR branch to the remote repository
|
|
git push origin "$pr_branch" || handle_error "Failed to push PR branch"
|
|
|
|
# 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."
|
|
|
|
# Cleanup
|
|
cleanup
|
|
}
|
|
|
|
|
|
|
|
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
|
|
}
|