diff options
author | David RodrÃguez <deivid.rodriguez@riseup.net> | 2023-06-30 13:45:39 +0200 |
---|---|---|
committer | Hiroshi SHIBATA <hsbt@ruby-lang.org> | 2023-07-04 14:54:26 +0900 |
commit | 09382135de5e706d7b100900cc81ddf91f4d3314 (patch) | |
tree | a6f5dce3db76854deffdf1d01ce0ee67febf4793 /lib | |
parent | 0ae2709dd71eaefe4a316bdbc9e97eaef3c7ae69 (diff) | |
download | ruby-09382135de5e706d7b100900cc81ddf91f4d3314.tar.gz |
[rubygems/rubygems] Fix git source lockfile unstability
We have some flags that limit running git commit commands under certain
situations, for example, when running under `--local`. However, those
should only affect remote git operations, not local read-only operations
like `git --version`, or `git rev-parse --abbrev-ref HEAD`.
This commit refactors things to achieve that.
By doing this, the `#to_s` representation of a source is more
consistent, since we don't get any errors when reading the checked out
branch, and we avoid some flip-flop lockfile issues.
https://github.com/rubygems/rubygems/commit/4a529fce81
Diffstat (limited to 'lib')
-rw-r--r-- | lib/bundler/source/git/git_proxy.rb | 44 |
1 files changed, 31 insertions, 13 deletions
diff --git a/lib/bundler/source/git/git_proxy.rb b/lib/bundler/source/git/git_proxy.rb index 926c9b8ead..53a9a78ddf 100644 --- a/lib/bundler/source/git/git_proxy.rb +++ b/lib/bundler/source/git/git_proxy.rb @@ -67,8 +67,8 @@ module Bundler end def current_branch - @current_branch ||= allowed_with_path do - git("rev-parse", "--abbrev-ref", "HEAD", :dir => path).strip + @current_branch ||= with_path do + git_local("rev-parse", "--abbrev-ref", "HEAD", :dir => path).strip end end @@ -84,7 +84,7 @@ module Bundler end def full_version - @full_version ||= git("--version").sub(/git version\s*/, "").strip + @full_version ||= git_local("--version").sub(/git version\s*/, "").strip end def checkout @@ -253,15 +253,15 @@ module Bundler end def git(*command, dir: nil) - command_with_no_credentials = check_allowed(command) - - out, err, status = capture(command, dir) - - raise GitCommandError.new(command_with_no_credentials, dir || SharedHelpers.pwd, err) unless status.success? - - Bundler.ui.warn err unless err.empty? + run_command(*command, :dir => dir) do |unredacted_command| + check_allowed(unredacted_command) + end + end - out + def git_local(*command, dir: nil) + run_command(*command, :dir => dir) do |unredacted_command| + redact_and_check_presence(unredacted_command) + end end def has_revision_cached? @@ -330,12 +330,30 @@ module Bundler end def check_allowed(command) - require "shellwords" - command_with_no_credentials = URICredentialsFilter.credential_filtered_string("git #{command.shelljoin}", uri) + command_with_no_credentials = redact_and_check_presence(command) raise GitNotAllowedError.new(command_with_no_credentials) unless allow? command_with_no_credentials end + def redact_and_check_presence(command) + raise GitNotInstalledError.new unless Bundler.git_present? + + require "shellwords" + URICredentialsFilter.credential_filtered_string("git #{command.shelljoin}", uri) + end + + def run_command(*command, dir: nil) + command_with_no_credentials = yield(command) + + out, err, status = capture(command, dir) + + raise GitCommandError.new(command_with_no_credentials, dir || SharedHelpers.pwd, err) unless status.success? + + Bundler.ui.warn err unless err.empty? + + out + end + def capture(cmd, dir, ignore_err: false) SharedHelpers.with_clean_git_env do require "open3" |