#!/bin/sh

# this test demonstrate that when working with multiple branches tracked from multiple remotes
# then 'git push' or 'git push [remote]' will try to push too many references to the remote repository

# git clone foo.git repo
# cd foo
# git remote add bar ../bar.git
# git fetch bar
# git checkout -b bar-master bar/master
# git push  =>> FAILED because trying to push origin/master to bar/master

# yet, the following is working fine
# git checkout master
# git push  => OK, doesn't try to push "bar" references to "origin"

set -x
set -e


rm -rf demo
mkdir demo
cd demo
ROOT_PATH=$(pwd)



# create our first bare repo
mkdir bare1.git
cd bare1.git
BARE1_PATH=$(pwd)
git init --bare


# create clone of bare1
cd $ROOT_PATH
git clone bare1.git work
cd work
WORK_PATH=$(pwd)

# push a first comit

echo "hello world" > foo.c
git add foo.c
git commit -m 'adding foo.c'
git push origin master


# create a bare clone of bare1
cd $ROOT_PATH
git clone --bare bare1.git bare2.git


# show that "git push" try to push "origin" references to all other remotes 
# if we try to branch/checkout a branch from other remote

# 1) add a new commit to bare2
git clone bare2.git bare2_work
cd bare2_work
echo "bye bye" > bar.c
git add bar.c
git commit -m 'adding bar.c'
git push

# 2) return to "work" and add bare2 as new remote
cd $WORK_PATH
git remote add bare2 ../bare2.git
git fetch bare2

# 3) create a tracking branch from bare2/master
git checkout -b bare2_master bare2/master

# 4) run "git push"
# "git help push" gives the example and says: 
#
#   git push
#       Works like git push <remote>, where <remote> is the current branch’s remote (or origin, if no remote is
#       configured for the current branch).
#
# so, we expect this command to do nothing as everything is up to date
# yet, it try to push 'master' reference to 'bare2/master'
# which obviously (and fortunatly) failed
# 'git push' failed but 'git push bare2' or 'git push --repo=bare2' are not better
# in any case the behavior is not good as trying to push bare1.git references to bare2.git


git push              
git push bare2        
git push --repo=bare2 

# if we are here, the bug is fixed
echo "fix done"
exit 0

