diff --git a/bin/git-pullpr b/bin/git-pullpr new file mode 100755 index 00000000..025d71c8 --- /dev/null +++ b/bin/git-pullpr @@ -0,0 +1,89 @@ +#!/bin/bash + +set -euo pipefail + +if [[ -n "${GIT_PILE_VERBOSE:-}" ]]; then + set -x +fi + +if [[ $# -lt 1 ]]; then + echo "usage: $0 " >&2 + exit 1 +fi + +export GIT_SEQUENCE_EDITOR=true + +# determine branch name +# fetch it +# pull it +# take commit(s)? that aren't part of the given sha and --fixup them +# rebase -i to autosquash them +readonly commit_to_update=$1 +if ! git cat-file -e "$commit_to_update" 2> /dev/null; then + echo "error: invalid commit sha: '$commit_to_update'" >&2 + exit 1 +fi + +branch_name="${GIT_PILE_PREFIX:-}$(git show --no-patch --format=%f "$commit_to_update" | tr '[:upper:]' '[:lower:]')" +if ! git show-ref --quiet "$branch_name"; then + echo "error: branch '$branch_name' doesn't exist" >&2 + exit 1 +fi + +worktree_name=$(git roothash) +readonly worktree_dir="$HOME/.cache/git-pile/$worktree_name" +if [[ ! -d "$worktree_dir" ]]; then + git worktree add -f "$worktree_dir" "$branch_name" +else + git -C "$worktree_dir" switch "$branch_name" +fi + +_detach_branch() { + git -C "$worktree_dir" switch --detach --quiet +} + +trap _detach_branch EXIT +git -C "$worktree_dir" pull --quiet + + +# branch_with_remote=$(git -C "$worktree_dir" rev-parse --abbrev-ref --symbolic-full-name "@{upstream}") +# remote_name="${branch_with_remote%%/*}" +# remote_branch_name="${branch_with_remote#*/}" + +# git -C "$worktree_dir" fetch "$remote_name" "$remote_branch_name" +# if ! git -C "$worktree_dir" diff --quiet "HEAD...@{upstream}"; then +# git -C "$worktree_dir" diff "HEAD...@{upstream}" +# answer=$(_ask "warning: upstream has new commits, would you like to pull those (or abort)? (y/n) ") +# if [[ "$answer" == y ]]; then +# git -C "$worktree_dir" pull +# else +# echo "warning: not updating PR, checkout '$branch_name', pull and try again" >&2 +# exit 1 +# fi +# fi + +# if ! git -C "$worktree_dir" cherry-pick "$new_refspec^..$base_refspec"; then +# # TODO if you exit in vim with :cq, it asks you if it was successful, i think if you hit yes it will continue even if it's not? +# if git -C "$worktree_dir" mergetool; then +# git -C "$worktree_dir" cherry-pick --continue +# else +# git -C "$worktree_dir" cherry-pick --abort +# # TODO what do i do? i think we have to reset branch state? maybe just abort cherry pick? +# exit 1 +# fi +# fi + +# if [[ "$squash" == true ]]; then +# if [[ "$signoff" == true ]]; then +# git -C "$worktree_dir" commit -s --amend --fixup=HEAD~ +# else +# git -C "$worktree_dir" commit --amend --fixup=HEAD~ +# fi +# git -C "$worktree_dir" rebase --interactive --autosquash HEAD~2 +# git -C "$worktree_dir" push --force-with-lease --quiet || echo "warning: failed to force push '$branch_name'" >&2 +# else +# git -C "$worktree_dir" push --quiet || echo "warning: failed to push '$branch_name'" >&2 +# fi + +# git rebase --interactive --exec "git commit --amend --fixup '$commit_to_update'" "$new_refspec"^ +# git rebase --interactive --autosquash "$commit_to_update"^