Skip to content

Commit

Permalink
Support pointing git sources to repos with default branches not named…
Browse files Browse the repository at this point in the history
… master

Without having to pass an explicit `:branch` argument.

Now git source string representation no longer shows branch information
unless the repository has already been cloned and we know what the
default branch is.
  • Loading branch information
deivid-rodriguez committed Jan 5, 2021
1 parent 7c075e7 commit de963bc
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 23 deletions.
34 changes: 18 additions & 16 deletions bundler/lib/bundler/source/git.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def initialize(options)
@uri = options["uri"] || ""
@safe_uri = URICredentialsFilter.credential_filtered_uri(@uri)
@branch = options["branch"]
@ref = options["ref"] || options["branch"] || options["tag"] || "master"
@ref = options["ref"] || options["branch"] || options["tag"]
@submodules = options["submodules"]
@name = options["name"]
@version = options["version"].to_s.strip.gsub("-", ".pre.")
Expand Down Expand Up @@ -60,25 +60,27 @@ def eql?(other)
alias_method :==, :eql?

def to_s
at = if local?
path
elsif user_ref = options["ref"]
if ref =~ /\A[a-z0-9]{4,}\z/i
shortref_for_display(user_ref)
begin
at = if local?
path
elsif user_ref = options["ref"]
if ref =~ /\A[a-z0-9]{4,}\z/i
shortref_for_display(user_ref)
else
user_ref
end
elsif ref
ref
else
user_ref
git_proxy.branch
end
else
ref
end

rev = begin
"@#{shortref_for_display(revision)}"
rescue GitError
nil
end
rev = " (at #{at}@#{shortref_for_display(revision)})"
rescue GitError
""
end

"#{@safe_uri} (at #{at}#{rev})"
"#{@safe_uri}#{rev}"
end

def name
Expand Down
2 changes: 1 addition & 1 deletion bundler/lib/bundler/source/git/git_proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ def remove_cache

def find_local_revision
allowed_with_path do
git("rev-parse", "--verify", ref, :dir => path).strip
git("rev-parse", "--verify", ref || "HEAD", :dir => path).strip
end
rescue GitCommandError => e
raise MissingGitRevisionError.new(e.command, path, ref, URICredentialsFilter.credential_filtered_uri(uri))
Expand Down
4 changes: 2 additions & 2 deletions bundler/spec/bundler/source/git_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@

describe "#to_s" do
it "returns a description" do
expect(subject.to_s).to eq "https://github.com/foo/bar.git (at master)"
expect(subject.to_s).to eq "https://github.com/foo/bar.git"
end

context "when the URI contains credentials" do
let(:uri) { "https://my-secret-token:x-oauth-basic@github.com/foo/bar.git" }

it "filters credentials" do
expect(subject.to_s).to eq "https://x-oauth-basic@github.com/foo/bar.git (at master)"
expect(subject.to_s).to eq "https://x-oauth-basic@github.com/foo/bar.git"
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions bundler/spec/install/deploy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@
bundle "config set --local deployment true"
bundle :install, :raise_on_error => false
expect(err).to include("deployment mode")
expect(err).to include("You have added to the Gemfile:\n* source: git://hubz.com (at master)")
expect(err).to include("You have added to the Gemfile:\n* source: git://hubz.com")
expect(err).not_to include("You have changed in the Gemfile")
end

Expand All @@ -361,7 +361,7 @@
bundle "config set --local deployment true"
bundle :install, :raise_on_error => false
expect(err).to include("deployment mode")
expect(err).to include("You have deleted from the Gemfile:\n* source: #{lib_path("rack-1.0")} (at master@#{revision_for(lib_path("rack-1.0"))[0..6]}")
expect(err).to include("You have deleted from the Gemfile:\n* source: #{lib_path("rack-1.0")}")
expect(err).not_to include("You have added to the Gemfile")
expect(err).not_to include("You have changed in the Gemfile")
end
Expand All @@ -385,7 +385,7 @@
bundle "config set --local deployment true"
bundle :install, :raise_on_error => false
expect(err).to include("deployment mode")
expect(err).to include("You have changed in the Gemfile:\n* rack from `no specified source` to `#{lib_path("rack")} (at master@#{revision_for(lib_path("rack"))[0..6]})`")
expect(err).to include("You have changed in the Gemfile:\n* rack from `no specified source` to `#{lib_path("rack")}`")
expect(err).not_to include("You have added to the Gemfile")
expect(err).not_to include("You have deleted from the Gemfile")
end
Expand Down
11 changes: 11 additions & 0 deletions bundler/spec/install/git_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,17 @@
expect(the_bundle).to include_gems "foo 1.0", :source => "git@#{lib_path("foo")}"
end

it "displays the correct default branch" do
build_git "foo", "1.0", :path => lib_path("foo"), :default_branch => "main"

install_gemfile <<-G, :verbose => true
gem "foo", :git => "#{file_uri_for(lib_path("foo"))}"
G

expect(out).to include("Using foo 1.0 from #{file_uri_for(lib_path("foo"))} (at main@#{revision_for(lib_path("foo"))[0..6]})")
expect(the_bundle).to include_gems "foo 1.0", :source => "git@#{lib_path("foo")}"
end

it "displays the ref of the gem repository when using branch~num as a ref" do
skip "maybe branch~num notation doesn't work on Windows' git" if Gem.win_platform?

Expand Down
2 changes: 1 addition & 1 deletion bundler/spec/lock/lockfile_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1134,7 +1134,7 @@
G

expect(bundled_app_lock).not_to exist
expect(err).to include "rack (>= 0) should come from an unspecified source and git://hubz.com (at master)"
expect(err).to include "rack (>= 0) should come from an unspecified source and git://hubz.com"
end

it "works correctly with multiple version dependencies" do
Expand Down
2 changes: 2 additions & 0 deletions bundler/spec/support/builders.rb
Original file line number Diff line number Diff line change
Expand Up @@ -547,9 +547,11 @@ def platform_string

class GitBuilder < LibBuilder
def _build(options)
default_branch = options[:default_branch] || "master"
path = options[:path] || _default_path
source = options[:source] || "git@#{path}"
super(options.merge(:path => path, :source => source))
@context.git("config --global init.defaultBranch #{default_branch}", path)
@context.git("init", path)
@context.git("add *", path)
@context.git("config user.email lol@wut.com", path)
Expand Down

0 comments on commit de963bc

Please sign in to comment.