From fe8a0569ca1fc419f80196bea22cc1de0ff3b93a Mon Sep 17 00:00:00 2001 From: Mike McQuaid Date: Mon, 7 Dec 2015 09:30:19 +0000 Subject: [PATCH] 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 --- Library/Homebrew/cmd/update.rb | 57 +++++++++++++++++---------- Library/Homebrew/test/test_updater.rb | 2 +- 2 files changed, 37 insertions(+), 22 deletions(-) diff --git a/Library/Homebrew/cmd/update.rb b/Library/Homebrew/cmd/update.rb index 10b4da661e..6ee2726a94 100644 --- a/Library/Homebrew/cmd/update.rb +++ b/Library/Homebrew/cmd/update.rb @@ -203,15 +203,6 @@ class Updater end 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; # check refs/remotes/origin/HEAD to see what the default # origin branch name is, and use that. If not set, fall back to "master". @@ -228,6 +219,16 @@ class Updater @initial_branch = "" 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 # renaming it in the currently checked-out branch. To test run # "brew update --simulate-from-current-branch" @@ -243,7 +244,7 @@ class Updater end 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 @initial_revision = read_current_revision @@ -263,18 +264,29 @@ class Updater @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 + pop_stash + else + pop_stash_message end + end - if @stashed - safe_system "git", "stash", "pop", *@quiet_args - if ARGV.verbose? - puts "Restored your changes:" - system "git", "status", "--short", "--untracked-files" - end - @stashed = false + def pop_stash + return unless @stashed + safe_system "git", "stash", "pop", *@quiet_args + if ARGV.verbose? + puts "Restoring your stashed changes to #{repository}:" + system "git", "status", "--short", "--untracked-files" 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 def reset_on_interrupt @@ -282,9 +294,12 @@ class Updater ensure if $?.signaled? && $?.termsig == 2 # SIGINT safe_system "git", "checkout", @initial_branch unless @initial_branch.empty? - safe_system "git", "reset", "--hard", @initial_revision - safe_system "git", "stash", "pop", *@quiet_args if @stashed - @stashed = false + safe_system "git", "reset", "--hard", @initial_revision, *@quiet_args + if @initial_branch + pop_stash + else + pop_stash_message + end end end diff --git a/Library/Homebrew/test/test_updater.rb b/Library/Homebrew/test/test_updater.rb index 20d26e6e90..16efdaa1d4 100644 --- a/Library/Homebrew/test/test_updater.rb +++ b/Library/Homebrew/test/test_updater.rb @@ -57,9 +57,9 @@ class UpdaterTests < Homebrew::TestCase Formulary.stubs(:factory).returns(stub(:pkg_version => "1.0")) FormulaVersions.stubs(:new).returns(stub(:formula_at_revision => "2.0")) @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 --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 config core.autocrlf false") @updater.in_repo_expect("git pull --ff --no-rebase --quiet origin refs/heads/master:refs/remotes/origin/master")