update: always reset to origin/master.

Do a reset to `origin/master` and then stash but don't pop the stash
after running update (unless you were on a branch). This may be mildly
more annoying for Homebrew developers but means it's easier for our
users who don't understand Git (and particularly when they don't
understand that every tap is a separate Git repository).

Closes Homebrew/homebrew#45825.

Signed-off-by: Mike McQuaid <mike@mikemcquaid.com>
This commit is contained in:
Mike McQuaid 2015-12-07 09:30:19 +00:00
parent 3d3c3e0131
commit fe8a0569ca
2 changed files with 37 additions and 22 deletions

View File

@ -203,15 +203,6 @@ class Updater
end end
def pull!(options = {}) def pull!(options = {})
unless system "git", "diff", "--quiet"
if ARGV.verbose?
puts "Stashing your changes:"
system "git", "status", "--short", "--untracked-files"
end
safe_system "git", "stash", "save", "--include-untracked", *@quiet_args
@stashed = true
end
# The upstream repository's default branch may not be master; # The upstream repository's default branch may not be master;
# check refs/remotes/origin/HEAD to see what the default # check refs/remotes/origin/HEAD to see what the default
# origin branch name is, and use that. If not set, fall back to "master". # origin branch name is, and use that. If not set, fall back to "master".
@ -228,6 +219,16 @@ class Updater
@initial_branch = "" @initial_branch = ""
end end
unless `git status --untracked-files=all --porcelain 2>/dev/null`.chomp.empty?
if ARGV.verbose?
puts "Stashing uncommitted changes to #{repository}."
system "git", "status", "--short", "--untracked-files=all"
end
safe_system "git", "stash", "save", "--include-untracked", *@quiet_args
safe_system "git", "reset", "--hard", *@quiet_args
@stashed = true
end
# Used for testing purposes, e.g., for testing formula migration after # Used for testing purposes, e.g., for testing formula migration after
# renaming it in the currently checked-out branch. To test run # renaming it in the currently checked-out branch. To test run
# "brew update --simulate-from-current-branch" # "brew update --simulate-from-current-branch"
@ -243,7 +244,7 @@ class Updater
end end
if @initial_branch != @upstream_branch && !@initial_branch.empty? if @initial_branch != @upstream_branch && !@initial_branch.empty?
safe_system "git", "checkout", @upstream_branch, *@quiet_args safe_system "git", "checkout", "--force", "-B", @upstream_branch, "origin/#{@upstream_branch}", *@quiet_args
end end
@initial_revision = read_current_revision @initial_revision = read_current_revision
@ -263,18 +264,29 @@ class Updater
@current_revision = read_current_revision @current_revision = read_current_revision
if @initial_branch != "master" && !@initial_branch.empty? if @initial_branch != @upstream_branch && !@initial_branch.empty?
safe_system "git", "checkout", @initial_branch, *@quiet_args safe_system "git", "checkout", @initial_branch, *@quiet_args
pop_stash
else
pop_stash_message
end end
end
if @stashed def pop_stash
safe_system "git", "stash", "pop", *@quiet_args return unless @stashed
if ARGV.verbose? safe_system "git", "stash", "pop", *@quiet_args
puts "Restored your changes:" if ARGV.verbose?
system "git", "status", "--short", "--untracked-files" puts "Restoring your stashed changes to #{repository}:"
end system "git", "status", "--short", "--untracked-files"
@stashed = false
end end
@stashed = false
end
def pop_stash_message
return unless @stashed
puts "To restore the stashed changes to #{repository} run:"
puts " `cd #{repository} && git stash pop`"
@stashed = false
end end
def reset_on_interrupt def reset_on_interrupt
@ -282,9 +294,12 @@ class Updater
ensure ensure
if $?.signaled? && $?.termsig == 2 # SIGINT if $?.signaled? && $?.termsig == 2 # SIGINT
safe_system "git", "checkout", @initial_branch unless @initial_branch.empty? safe_system "git", "checkout", @initial_branch unless @initial_branch.empty?
safe_system "git", "reset", "--hard", @initial_revision safe_system "git", "reset", "--hard", @initial_revision, *@quiet_args
safe_system "git", "stash", "pop", *@quiet_args if @stashed if @initial_branch
@stashed = false pop_stash
else
pop_stash_message
end
end end
end end

View File

@ -57,9 +57,9 @@ class UpdaterTests < Homebrew::TestCase
Formulary.stubs(:factory).returns(stub(:pkg_version => "1.0")) Formulary.stubs(:factory).returns(stub(:pkg_version => "1.0"))
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 symbolic-ref refs/remotes/origin/HEAD 2>/dev/null", "refs/remotes/origin/master") @updater.in_repo_expect("git symbolic-ref refs/remotes/origin/HEAD 2>/dev/null", "refs/remotes/origin/master")
@updater.in_repo_expect("git symbolic-ref --short HEAD 2>/dev/null", "master") @updater.in_repo_expect("git symbolic-ref --short HEAD 2>/dev/null", "master")
@updater.in_repo_expect("git status --untracked-files=all --porcelain 2>/dev/null", "")
@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")