get-upstream-head-branch call failed on my system.
I changed it to something that worked on my system, which follows the $output convention elsewhere.
Fixed issues with error handling in RUN().
Regarding local in: local foo=$(some command), local has it's own return code which overwrites the return code of the command.  So the existing error handling was only checking for failure of "local".
Introduced more if control structures, to prevent cascading error messages in trouble spots.
Merge pull request #19 from AndrewSmart/bugfix

3 bugfixes