diff options
author | Matthieu Serrepuy <lotooo@gmail.com> | 2020-06-15 21:02:48 +0200 |
---|---|---|
committer | Matthieu Serrepuy <lotooo@gmail.com> | 2020-07-10 21:56:31 +0200 |
commit | d199db9228140e7877347fdaa916e67534a257e1 (patch) | |
tree | 155b05334e4640974e280ee6726ca362b0e20d1e /lib/chef/provider | |
parent | a7785683f2cc33af3a47898458cfd8b8a1239a69 (diff) | |
download | chef-d199db9228140e7877347fdaa916e67534a257e1.tar.gz |
Don't try a git branch -f if already on the same branch
Signed-off-by: Matthieu Serrepuy <lotooo@gmail.com>
Diffstat (limited to 'lib/chef/provider')
-rw-r--r-- | lib/chef/provider/git.rb | 16 |
1 files changed, 12 insertions, 4 deletions
diff --git a/lib/chef/provider/git.rb b/lib/chef/provider/git.rb index c8b48f5602..6f5a129820 100644 --- a/lib/chef/provider/git.rb +++ b/lib/chef/provider/git.rb @@ -154,6 +154,11 @@ class Chef sha_hash?(result) ? result : nil end + def already_on_target_branch? + current_branch = git("rev-parse", "--abbrev-ref", "HEAD", cwd: cwd, returns: [0, 128]).stdout.strip + current_branch == (new_resource.checkout_branch || new_resource.revision) + end + def add_remotes if new_resource.additional_remotes.length > 0 new_resource.additional_remotes.each_pair do |remote_name, remote_url| @@ -193,6 +198,9 @@ class Chef # detached head git("checkout", target_revision, cwd: cwd) logger.info "#{new_resource} checked out reference: #{target_revision}" + elsif already_on_target_branch? + # we are already on the proper branch + git("reset", "--hard", target_revision, cwd: cwd) else # need a branch with a tracking branch git("branch", "-f", new_resource.revision, target_revision, cwd: cwd) @@ -222,13 +230,13 @@ class Chef logger.trace "Fetching updates from #{new_resource.remote} and resetting to revision #{target_revision}" git("fetch", "--prune", new_resource.remote, cwd: cwd) git("fetch", new_resource.remote, "--tags", cwd: cwd) - if new_resource.checkout_branch + if sha_hash?(new_resource.revision) || is_tag? || already_on_target_branch? + # detached head or if we are already on the proper branch + git("reset", "--hard", target_revision, cwd: cwd) + elsif new_resource.checkout_branch # check out to a local branch git("branch", "-f", new_resource.checkout_branch, target_revision, cwd: cwd) git("checkout", new_resource.checkout_branch, cwd: cwd) - elsif sha_hash?(new_resource.revision) || is_tag? - # detached head - git("reset", "--hard", target_revision, cwd: cwd) else # need a branch with a tracking branch git("branch", "-f", new_resource.revision, target_revision, cwd: cwd) |