Skip to content

Commit

Permalink
Simplify things
Browse files Browse the repository at this point in the history
Signed-off-by: kraem <[email protected]>
  • Loading branch information
kraem committed Oct 15, 2020
1 parent 7ef705b commit 600ca13
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 35 deletions.
2 changes: 0 additions & 2 deletions lib/bundix.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
require 'open-uri'
require 'open3'
require 'pp'
require 'tmpdir'

require_relative 'bundix/version'
require_relative 'bundix/source'
Expand All @@ -12,7 +11,6 @@
class Bundix
NIX_INSTANTIATE = 'nix-instantiate'
NIX_PREFETCH_URL = 'nix-prefetch-url'
GIT = 'git'
NIX_HASH = 'nix-hash'
NIX_SHELL = 'nix-shell'

Expand Down
60 changes: 29 additions & 31 deletions lib/bundix/source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,35 +72,6 @@ def nix_prefetch_url(url)
nil
end

def fetch_ref(uri, revision)
Dir.mktmpdir do |dir|
sh(
Bundix::GIT,
'clone',
uri,
dir,
)
rescue => ex
puts ex
return nil

sh(
Bundix::GIT,
'-C',
dir,
'name-rev',
revision,
) do |_si, so, _se|
rev, ref = so.read.lines.first.split
ref.gsub!(%r{^remotes/origin/}, '')
return ref
rescue => ex
puts ex
nil
end
end
end

def format_hash(hash)
sh(NIX_HASH, '--type', 'sha256', '--to-base32', hash)[SHA256_32]
end
Expand Down Expand Up @@ -175,8 +146,35 @@ def convert_git
revision = spec.source.options.fetch('revision')
uri = spec.source.options.fetch('uri')
submodules = !!spec.source.submodules
ref = fetcher.fetch_ref(uri, revision)
fail "couldn't fetch ref for #{spec.full_name}" unless ref
ref, branch, tag = spec.source.options.values_at('ref', 'branch', 'tag')
unless ref.nil?
# Didn't find a good enough solution for this since Bundler accepts
# SHA1's that are shorter than 40 characters, which means we don't know
# if the ref in the Gemfile is a hash or a branch/tag name.
#
# (builtins.fetchGit only takes a tag/branch as 'ref')
#
# One possible solution if we could discriminate between a short SHA1
# and tags/refs would be to clone the repo and do `git name-rev $ref`.
#
# Leaving this as a hint for anyone wanting to implement this.
fetcher.print_error(
"Please provide a 40 character SHA1 as 'ref' in Gemfile.\n" +
"If you tried providing a tag/branch name please use 'tag' or 'branch' respectively in the Gemfile.\n" +
"More info: https://bundler.io/guides/git.html"
)
raise
end
unless branch.nil?
ref = branch
end
unless tag.nil?
ref = tag
end
# Setting ref to master. This should work since Bundler doesn't handle
# repos without a master branch that hasn't 'branch' or 'tag' specified
# in the Gemfile explicitly
ref = 'master' unless ref

{ type: 'builtins-git',
url: uri.to_s,
Expand Down
3 changes: 1 addition & 2 deletions shell.nix
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,10 @@ in
mkdir -p $out
makeWrapper $src/bin/bundix $out/bin/bundix \
--prefix PATH : "${nix.out}/bin" \
--prefix PATH : "${git.out}/bin" \
--set GEM_PATH "${bundler}/${bundler.ruby.gemPath}"
'';

nativeBuildInputs = [makeWrapper];

buildInputs = [bundler ruby minitest rake git];
buildInputs = [bundler ruby minitest rake];
}

0 comments on commit 600ca13

Please sign in to comment.