Last active
November 5, 2024 03:01
-
-
Save reececomo/0e4e7d17c98def722d5f9bfe7291e91b to your computer and use it in GitHub Desktop.
zsh git aliases/functions: fin, rebase
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| alias reload="source ~/.zshrc" | |
| alias editzsh="code ~/.zshrc" | |
| alias editbash="code ~/.zshrc" # legacy | |
| # git: | |
| branch() { | |
| git branch --show-current 2> /dev/null | |
| } | |
| defaultbranch() { | |
| git symbolic-ref refs/remotes/origin/HEAD | sed 's|^refs/remotes/origin/||' 2> /dev/null | |
| } | |
| fin() { | |
| WARN='\033[0;33m' | |
| SUCCESS='\033[0;32m' | |
| RESET='\033[0m' | |
| defaultbranch=$(defaultbranch) | |
| if [ "$(branch)" = "$defaultbranch" ]; then | |
| if ! git diff-index --quiet HEAD --; then | |
| echo -e "${WARN}err: already on default branch (\"${defaultbranch}\")${RESET}" | |
| return 1 | |
| fi | |
| echo -e "${WARN}warn:${RESET} already on default branch (\"${defaultbranch}\"). ${SUCCESS}fetching latest...${RESET}" | |
| git pull | |
| return 0 | |
| fi | |
| if ! git diff-index --quiet HEAD --; then | |
| echo -e "${WARN}err: current branch (\"$(branch)\") contains uncommitted changes (hint: 'finforce' to discard changes)${RESET}" | |
| return 1 | |
| fi | |
| echo -e "${SUCCESS}updating $defaultbranch from remote${RESET}" | |
| git fetch origin $defaultbranch:$defaultbranch | |
| echo -e "${SUCCESS}checking out $defaultbranch${RESET}" | |
| git checkout $defaultbranch | |
| } | |
| finforce() { | |
| git reset HEAD --hard | |
| fin | |
| } | |
| rebase() { | |
| WARN='\033[0;33m' | |
| SUCCESS='\033[0;32m' | |
| RESET='\033[0m' | |
| defaultbranch="$(defaultbranch)" | |
| targetbranch="${1:-$(defaultbranch)}" | |
| # Check for uncommitted changes | |
| if ! git diff-index --quiet HEAD --; then | |
| echo -e "${WARN}err: current branch (\"$(branch)\") contains uncommitted changes.${RESET}" | |
| return 1 | |
| fi | |
| echo -e "${SUCCESS}rebasing onto $targetbranch${RESET}" | |
| if [ "$targetbranch" = "$defaultbranch" ]; then | |
| # Fetch the latest changes from the default branch | |
| git fetch origin $defaultbranch:$defaultbranch | |
| fi | |
| # Attempt to rebase | |
| if ! git rebase $targetbranch; then | |
| echo -e "${WARN}err: rebase failed. Aborting and resetting to original state...${RESET}" | |
| git rebase --abort | |
| git reset --hard | |
| return 1 | |
| fi | |
| echo -e "${SUCCESS}rebase completed successfully onto $targetbranch${RESET}" | |
| return 0 | |
| } | |
| # Enhanced checkout with ordered tab completion for recent branches | |
| # Usage: | |
| # $ checkout <tab> | |
| checkout() { | |
| git checkout "$@" | |
| } | |
| co() { | |
| git checkout "$@" | |
| } | |
| _branch_suggestions() { | |
| local current_branch=$(git branch --show-current) | |
| local default_branch=$(git symbolic-ref refs/remotes/origin/HEAD | sed 's|^refs/remotes/origin/||') | |
| local grey=$'\e[90m' | |
| local success=$'\e[32m' | |
| local reset=$'\e[0m' | |
| local suggestion_count=5 | |
| local -a branches | |
| branches=() | |
| # default branch | |
| if [[ "$current_branch" != "$default_branch" ]]; then | |
| local default_time=$(git log -1 --format=%cr "$default_branch") | |
| branches+=("$default_branch:$default_branch ${success}(default)${reset}") | |
| fi | |
| # suggest recent branches | |
| branches+=(${(f)"$(git branch --sort=-committerdate --format='%(refname:short):%(refname:short) - '"$grey"'%(committerdate:relative)'"$reset" | | |
| grep -v "^${current_branch}:" | | |
| grep -v "^${default_branch}:" | | |
| head -n "${suggestion_count}" | |
| )"}) | |
| _describe -V 'branches' branches | |
| } | |
| compdef _branch_suggestions checkout | |
| compdef _branch_suggestions co | |
| compdef _branch_suggestions rebase | |
| function openpr() { | |
| WARN='\033[0;33m' | |
| RESET='\033[0m' | |
| local branch=$(branch) | |
| if [ "$branch" = "" ]; then | |
| echo -e "${WARN}err: not a git repository (\"$(pwd)\") ${RESET}" | |
| return 1 | |
| fi | |
| # on default branch? | |
| # -> open readme | |
| local defaultbranch=$(defaultbranch) | |
| if [ "$branch" = "$defaultbranch" ]; then | |
| open "${repo_url}" | |
| return 0 | |
| fi | |
| # on feature branch? | |
| # -> open compare page | |
| local repo_url=$(git config --get remote.origin.url | sed -e 's/git@github.com:/https:\/\/github.com\//' -e 's/\.git$//') | |
| open "${repo_url}/compare/${defaultbranch}...${branch}" | |
| } | |
| alias gh="openpr" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment