Skip to content

Instantly share code, notes, and snippets.

@robCrawford
Last active May 10, 2022 09:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robCrawford/8e697a27b437c48e7830a1d6ada7f0a3 to your computer and use it in GitHub Desktop.
Save robCrawford/8e697a27b437c48e7830a1d6ada7f0a3 to your computer and use it in GitHub Desktop.
Open a PR in VS Code
# Loads a feature branch as unstaged changes to master (or `$2`) and opens the files in VS Code.
# Usage: `code-pr feature-branch`
# Does not run if working directory is not clean.
# `code-pr -r` resets working directory and returns to previous branch/directory
code-pr() {
base="${2:-master}"
if [ "$1" = "-r" ]; then
code-pr-r
else
prePr="$(git rev-parse --abbrev-ref HEAD)";
if [ -z "$(git status --porcelain)" ]; then
cd $(git rev-parse --show-toplevel) && git checkout $base && git pull && git fetch origin $1:$1 && git merge --squash $1 && git reset HEAD
if [ "$2" != "-n" ]; then code $(git diff --diff-filter=d --name-only) $(git status -suall | grep '??' | cut -c 4-); fi
else
printf "\n $(tput setaf 1)Working directory is not clean!$(tput sgr 0)\n\n"
fi
fi
}
code-pr-r() {
if [ -n "$prePr" ]; then
if read -q "y?Reset working directory (y/n)?"; then
echo "\n"
git reset --hard && git clean -df && git checkout $prePr && cd -;
prePr="";
fi
else
printf "Not found!\n"
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment