This patch handles the detached HEAD case, by using git branch --contains HEAD to get the branch (or a list of possible branches).

It also adds a option to specify the branch in case this logic fails. Also there is an option to override the base source url for downloading.

While I was at it, I adjusted some checks so that the script will run even if the name of the checkout directory is not $packagename.