From a93c6aad9d6450ad87aaa3d2b4148cf128c90c8c Mon Sep 17 00:00:00 2001 From: Arti Zirk Date: Tue, 2 Apr 2019 16:22:27 +0300 Subject: [PATCH] Add script to update all local branches --- .bin/git-ffwd-update | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100755 .bin/git-ffwd-update diff --git a/.bin/git-ffwd-update b/.bin/git-ffwd-update new file mode 100755 index 0000000..ca2b0a8 --- /dev/null +++ b/.bin/git-ffwd-update @@ -0,0 +1,35 @@ +#!/bin/bash +# src: https://stackoverflow.com/a/9076361 + +main() { + REMOTES="$@"; + if [ -z "$REMOTES" ]; then + REMOTES=$(git remote); + fi + REMOTES=$(echo "$REMOTES" | xargs -n1 echo) + CLB=$(git rev-parse --abbrev-ref HEAD); + echo "$REMOTES" | while read REMOTE; do + git remote update $REMOTE + git remote show $REMOTE -n \ + | awk '/merges with remote/{print $5" "$1}' \ + | while read RB LB; do + ARB="refs/remotes/$REMOTE/$RB"; + ALB="refs/heads/$LB"; + NBEHIND=$(( $(git rev-list --count $ALB..$ARB 2>/dev/null) +0)); + NAHEAD=$(( $(git rev-list --count $ARB..$ALB 2>/dev/null) +0)); + if [ "$NBEHIND" -gt 0 ]; then + if [ "$NAHEAD" -gt 0 ]; then + echo " branch $LB is $NBEHIND commit(s) behind and $NAHEAD commit(s) ahead of $REMOTE/$RB. could not be fast-forwarded"; + elif [ "$LB" = "$CLB" ]; then + echo " branch $LB was $NBEHIND commit(s) behind of $REMOTE/$RB. fast-forward merge"; + git merge -q $ARB; + else + echo " branch $LB was $NBEHIND commit(s) behind of $REMOTE/$RB. resetting local branch to remote"; + git branch -f $LB -t $ARB >/dev/null; + fi + fi + done + done +} + +main $@