jenkins-build-common.sh: fix being stuck on branch

It's hard to allow both branch names and git hashes. For a branch, we want to
prepend 'origin' to use the upstream branch. For a git hash, we don't.

We so far prepend 'origin/' if the current branch name doesn't resolve, but
that's not enough. If a local 'master' branch exists, we would stay on that
branch instead of origin/master.

Rather, prepend 'origin/' if 'origin/$branch' exists. Git hashes should not
exist as 'origin/123abc...' and used as-is, where branches from origin should
be updated to upstream by prepending 'origin/'.

Also create a local branch to build. Always force-remove the branch and
re-create from the origin/name or git hash. Keep the reset --hard for paranoia.

Change-Id: I2e610b357f8559c6b6ffb544eb0a952f04dd9f70
diff --git a/contrib/jenkins-build-common.sh b/contrib/jenkins-build-common.sh
index 921434d..63e0ba8 100644
--- a/contrib/jenkins-build-common.sh
+++ b/contrib/jenkins-build-common.sh
@@ -81,10 +81,12 @@
   git fetch origin
 
   # Figure out whether we need to prepend origin/ to find branches in upstream
-  if ! git rev-parse "$branch"; then
+  if git rev-parse "origin/$branch"; then
     branch="origin/$branch"
   fi
 
+  git branch -D build_branch || true
+  git checkout -b build_branch "$branch"
   git reset --hard "$branch"
 
   git rev-parse HEAD