Fixes #5361 - Simplify repo-merge.sh by using function instead of duplicated code

Fixes #5361 - Simplify repo-merge.sh by using function instead of duplicated code

cf http://www.rudder-project.org/redmine/issues/5361
The indentation here looks pretty messed up... Can you align everything using <whatever conventoin the file already has> (I guess tabs)?