update: fix when HEAD doesn't exist.

e.g. when it's a detached `HEAD` through checking out a commit rather
than a branch.
This commit is contained in:
Mike McQuaid 2015-09-11 11:00:07 +01:00
parent 664e9e844a
commit b1ff9c42d6
2 changed files with 9 additions and 3 deletions

View File

@ -187,7 +187,13 @@ class Updater
@stashed = true @stashed = true
end end
@initial_branch = `git symbolic-ref --short HEAD`.chomp
begin
@initial_branch = `git symbolic-ref --short HEAD 2>/dev/null`.chomp
rescue ErrorDuringExecution
@initial_branch = ""
end
if @initial_branch != "master" && !@initial_branch.empty? if @initial_branch != "master" && !@initial_branch.empty?
safe_system "git", "checkout", "master", *quiet safe_system "git", "checkout", "master", *quiet
end end
@ -227,7 +233,7 @@ class Updater
ignore_interrupts { yield } ignore_interrupts { yield }
ensure ensure
if $?.signaled? && $?.termsig == 2 # SIGINT if $?.signaled? && $?.termsig == 2 # SIGINT
safe_system "git", "checkout", @initial_branch safe_system "git", "checkout", @initial_branch unless @initial_branch.empty?
safe_system "git", "reset", "--hard", @initial_revision safe_system "git", "reset", "--hard", @initial_revision
safe_system "git", "stash", "pop" if @stashed safe_system "git", "stash", "pop" if @stashed
end end

View File

@ -58,7 +58,7 @@ class UpdaterTests < Homebrew::TestCase
FormulaVersions.stubs(:new).returns(stub(:formula_at_revision => "2.0")) FormulaVersions.stubs(:new).returns(stub(:formula_at_revision => "2.0"))
@updater.diff = fixture(fixture_name) @updater.diff = fixture(fixture_name)
@updater.in_repo_expect("git diff --quiet", true) @updater.in_repo_expect("git diff --quiet", true)
@updater.in_repo_expect("git symbolic-ref --short HEAD", "master") @updater.in_repo_expect("git symbolic-ref --short HEAD 2>/dev/null", "master")
@updater.in_repo_expect("git rev-parse -q --verify HEAD", "1234abcd") @updater.in_repo_expect("git rev-parse -q --verify HEAD", "1234abcd")
@updater.in_repo_expect("git config core.autocrlf false") @updater.in_repo_expect("git config core.autocrlf false")
@updater.in_repo_expect("git pull --ff --no-rebase --quiet origin refs/heads/master:refs/remotes/origin/master") @updater.in_repo_expect("git pull --ff --no-rebase --quiet origin refs/heads/master:refs/remotes/origin/master")