Skip to content

Commit

Permalink
zsh: replace git-up with better script
Browse files Browse the repository at this point in the history
slight changes to aanand/git-up#121 (comment)
  • Loading branch information
kthibodeaux committed Jul 2, 2024
1 parent 9a3be83 commit 2b629a8
Showing 1 changed file with 56 additions and 12 deletions.
68 changes: 56 additions & 12 deletions bin/git-up
Original file line number Diff line number Diff line change
@@ -1,17 +1,61 @@
#!/bin/sh
#!/bin/zsh

original_branch=$(git branch --show-current)
# Add the following to your ~/.gitconfig
#
# [alias]
# up = !zsh /path-to-this-script/git-up.sh
#
# Then you can call
#
# `git up`

git fetch --all
branches=$(git branch | sed 's/^\* //')
# Remember current branch.
START_BRANCH=$(git branch --show-current --format='%(refname:short)')

for branch in $branches; do
echo "Rebasing branch \"$branch\""
git checkout "$branch"
git pull --rebase --autostash
echo ""
done
get_local_branches() {
# Gets local, which tracks origin.
git branch -vv | grep origin | sed 's/*/ /g' | awk '{print $1}'
}

get_remote_branches() {
git branch -r --format='%(refname:short)' | sed 's/origin\///' | grep -v HEAD
}

get_deleted_branches() {
get_remote_branches | grep -Evf /dev/fd/0 <(get_local_branches) | awk '{print $1}'
}

# Fetch latest.
# Updates only the view of remote branches and their commits.
# Does not fast forward local branches.
# Does not clone missing local remote branches.
git fetch --all -p >/dev/null

# Delete pruned local branches.
for branch in $(get_deleted_branches); do
if [ "$branch" = "$START_BRANCH" ]; then
printf "\n\nDeleting current branch \"%s\". Stashing changes and switching to master.\n" "$branch"

git checkout "$original_branch"
# Go to the root dir in case branch switch removes current working dir.
cd "$(git rev-parse --show-toplevel)" || exit 1
git stash >/dev/null
git checkout master >/dev/null
START_BRANCH=master

echo "All branches have been updated."
git branch -D "$branch" >/dev/null
else
git branch -D "$branch"
fi

done

# Fast-forward all branches.
for branch in $(get_local_branches); do
upstream=$(git config --get "branch.$branch.merge" | sed 's:refs/heads/::')
echo "Updating $branch..."
if [ "$branch" = "$START_BRANCH" ]; then
git merge --ff-only "origin/$upstream" | grep -v "Already up to date"
else
git fetch . "origin/$upstream:$branch"
fi
done

0 comments on commit 2b629a8

Please sign in to comment.