Skip to content

Commit e7c1c01

Browse files
committed
Better way to check whether commits in all branches have been pushed
The previous approach where you `git push --all -n $remote 2>&1` and check whether the result is 'Everything up-to-date' can (and often will) yield "To prevent you from losing history, non-fast-forward updates were rejected" Since such an approach does traffic with the remote anyway, I choose to fetch the origin and inspect it's branches. This is a bit inefficient network-wise, but couldn't see a better solution
1 parent 3a4c5f2 commit e7c1c01

File tree

1 file changed

+38
-12
lines changed

1 file changed

+38
-12
lines changed

git-remote-in-sync.sh

+38-12
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,37 @@ fi
3838

3939
for remote in $list;
4040
do
41-
# check commits and branches
42-
out=`git push --all -n $remote 2>&1`
43-
if [ "$out" != 'Everything up-to-date' ];
41+
# Check commits and branches
42+
# An approach where you `git push --all -n $remote 2>&1` and check whether the result is
43+
# 'Everything up-to-date' can (and often will) yield
44+
# "To prevent you from losing history, non-fast-forward updates were rejected"
45+
# Since such an approach does traffic with the remote anyway, I choose to fetch the origin
46+
# and inspect it's branches. This is a bit inefficient network-wise, but couldn't see a better solution
47+
if ! git fetch $remote
4448
then
45-
echo -e "***** Dirty commits/branches: *****\n$out" >&2
49+
echo "could not git fetch $remote" >&2
4650
ret=1
4751
fi
52+
IFS_BACKUP=$IFS
53+
IFS=$'\n'
54+
for branch in `git branch`
55+
do
56+
branch=${branch/\*/};
57+
branch_local=${branch// /}; # git branch prefixes branches with spaces. and spaces in branchnames are illegal.
58+
if ! git branch -r --contains $branch_local 2>/dev/null | grep -q "^ $remote/"
59+
then
60+
echo "Branch $branch_local is not contained within remote $remote!" >&2
61+
ret=1
62+
fi
63+
if ! git branch -r | grep -q "^ $remote/$branch_local"
64+
then
65+
echo "Branch $branch_local exists, but not $remote/$branch_local" >&2
66+
ret=1
67+
fi
68+
done
69+
IFS=$IFS_BACKUP
4870

49-
# check tags
71+
# Check tags
5072
out=`git push --tags -n $remote 2>&1`
5173
if [ "$out" != 'Everything up-to-date' ];
5274
then
@@ -55,21 +77,25 @@ do
5577
fi
5678
done
5779

58-
# added/deleted/modified files in the currently checked out branch or index.
59-
# this should do I think. (other branch, esp remote tracking branch should not be checked, right? maybe if this one is dirty..)
80+
# Check WC/index
6081
cur_branch=`git branch | grep '^\* ' | cut -d ' ' -f2`
6182
cur_branch=${cur_branch// /}
6283
exp="# On branch $cur_branch
6384
nothing to commit (working directory clean)"
6485
out=`git status`
65-
if [ "$out" != "$exp" ];
86+
if [ "$out" != "$exp" ]
6687
then
67-
echo "***** Dirty WC or index *****" >&2
68-
git status
69-
ret=1
88+
# usually i'd use bash regex, but this case is multiple-lines so bash regex is no go
89+
out=$(echo "$out" | egrep -v "^(# On branch $cur_branch|# Your branch is behind .* commits, and can be fast-forwarded.|#|nothing to commit \(working directory clean\))$")
90+
if [ -n "$out" ]
91+
then
92+
echo "***** Dirty WC or index *****" >&2
93+
git status
94+
ret=1
95+
fi
7096
fi
7197

72-
# stash
98+
# Check stash
7399
if [ `git stash list | wc -l` -gt 0 ];
74100
then
75101
echo "***** Dirty stash: *****" >&2

0 commit comments

Comments
 (0)