Skip to content

Commit

Permalink
Support building from git-archive tarballs (vagrant-libvirt#1187)
Browse files Browse the repository at this point in the history
Adds support for exposing the correct version via export-subst for
git-archive constructed tarballs to make it easier to consume directly
from source.

Will check in the following order of preference:
 - version file
 - format string containing "Tag:"
 - clone remote to describe commit
  • Loading branch information
electrofelix authored Dec 14, 2020
1 parent 5e62973 commit 20067be
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
lib/vagrant-libvirt/version.rb export-subst
66 changes: 57 additions & 9 deletions lib/vagrant-libvirt/version.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,49 @@
require 'open3'
require 'tmpdir'

module VagrantPlugins
module ProviderLibvirt
VERSION_FILE = File.dirname(__FILE__) + "/version"

GIT_ARCHIVE_VERSION = "$Format:%H %D$"

HOMEPAGE = 'https://github.com/vagrant-libvirt/vagrant-libvirt'

def self.get_version
if File.exist?(VERSION_FILE)
# built gem
version = File.read(VERSION_FILE)
else
elsif self.inside_git_repository
# local repo
git_version = `git describe --tags`
version_parts = git_version.split('-').first(2) # drop the git sha if it exists
if version_parts.length > 1
# increment the patch number so that this is marked as a pre-release of the
# next possible release
main_version_parts = Gem::Version.new(version_parts[0]).segments
main_version_parts[-1] = main_version_parts.last + 1
version_parts = main_version_parts + ["pre", version_parts[1]]
version = self.version_from_describe(git_version)
elsif !GIT_ARCHIVE_VERSION.start_with?('$Format')
# archive - format string replaced during export
hash, refs = GIT_ARCHIVE_VERSION.split(' ', 2)

tag = refs.split(',').select { |ref| ref.strip.start_with?("tag:") }.first
if tag != nil
# tagged
version = tag.strip.split(' ').last
else
version = ""
# arbitrary branch/commit
Dir.mktmpdir do |dir|
stdout_and_stderr, status = Open3.capture2e("git -C #{dir} clone --bare #{HOMEPAGE}")
raise "failed to clone original to resolve version: #{stdout_and_stderr}" unless status.success?

stdout_and_stderr, status = Open3.capture2e("git --git-dir=#{dir}/vagrant-libvirt.git describe --tags #{hash}")
raise "failed to determine version for #{hash}: #{stdout_and_stderr}" unless status.success?

version = version_from_describe(stdout_and_stderr)
end

# in this case write the version file to avoid cloning a second time
File.write(VERSION_FILE, version)
end
version = version_parts.join(".")
else
# no idea
version = "9999"
end

return version.freeze
Expand All @@ -24,5 +52,25 @@ def self.get_version
def self.write_version
File.write(VERSION_FILE, self.get_version)
end

private

def self.inside_git_repository
_, status = Open3.capture2e("git rev-parse --git-dir")

status.success?
end

def self.version_from_describe(describe)
version_parts = describe.split('-').first(2) # drop the git sha if it exists
if version_parts.length > 1
# increment the patch number so that this is marked as a pre-release of the
# next possible release
main_version_parts = Gem::Version.new(version_parts[0]).segments
main_version_parts[-1] = main_version_parts.last + 1
version_parts = main_version_parts + ["pre", version_parts[1]]
end
version = version_parts.join(".")
end
end
end
4 changes: 2 additions & 2 deletions vagrant-libvirt.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ Gem::Specification.new do |s|
s.license = 'MIT'
s.description = %q{libvirt provider for Vagrant.}
s.summary = %q{libvirt provider for Vagrant.}
s.homepage = 'https://github.com/vagrant-libvirt/vagrant-libvirt'
s.homepage = VagrantPlugins::ProviderLibvirt::HOMEPAGE

s.files = Dir.glob("{lib,locales}/**/*") + %w(LICENSE README.md)
s.executables = Dir.glob("bin/*.*").map{ |f| File.basename(f) }
s.test_files = Dir.glob("{test,spec,features}/**/*.*")
s.name = 'vagrant-libvirt'
s.require_paths = ['lib']
s.version = VagrantPlugins::ProviderLibvirt.get_version()
s.version = VagrantPlugins::ProviderLibvirt.get_version

s.add_development_dependency "rspec-core", "~> 3.5.0"
s.add_development_dependency "rspec-expectations", "~> 3.5.0"
Expand Down

0 comments on commit 20067be

Please sign in to comment.