Skip to content

Instantly share code, notes, and snippets.

@slaughtr
Created March 15, 2024 21:42
Show Gist options
  • Save slaughtr/cff093c383d71868d6bf4fbfe88437eb to your computer and use it in GitHub Desktop.
Save slaughtr/cff093c383d71868d6bf4fbfe88437eb to your computer and use it in GitHub Desktop.
mupen
# Thanks chat gippity!
# This zsh function simplifies jumping around many repos. And single repos, too
# It attempts to checkout the current dir's git repo main branch, fetch and pull, and open the directory in VS Code
# If there are unstaged changes in the current branch, it shows a diff and asks y/n if you want to discard those changes
# If yes, it continues as normal (checkout main, fetch, pull, VSC). If no, it aborts
# Why "mupen"? It's some amalgamation of main, pull, and open :shrug:
# Define the function
mupen() {
# Attempt to checkout the main branch. If there are no changes, this will succeed.
git checkout main &>/dev/null && {
echo "Switched to main branch successfully."
git fetch && git pull
code .
return
}
# If the checkout fails, it might be due to unstaged changes. Check for unstaged changes.
if ! git diff --exit-code --quiet; then
echo "Unstaged changes detected."
git diff
# Ask the user if they want to discard the changes.
read "response?Do you want to discard these changes? [y/N]: "
if [[ "$response" =~ ^[Yy]$ ]]; then
# Discard the changes
git checkout -- .
else
# Exit if the user doesn't want to discard the changes.
echo "Exiting without discarding changes."
return
fi
fi
# Checkout the main branch, fetch and pull updates, and open VS Code.
git checkout main
git fetch && git pull
echo "Switched to main branch and updated."
code .
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment