module Packaging::GitHelpers
Helpers around various git commands
Public Instance Methods
Return the name of the current git branch or “detached” if in “head detached” state.
@return String
# File lib/packaging/git_helpers.rb, line 70 def branch_name branch = git("branch").grep(/^\* /).first.sub(/^\* /, "") return "detached" if branch =~ /HEAD detached/ branch end
Create a git tag based on the version and the current branch.
The optional block can specify the version number to use. If no block is given, this uses spec_version
, i.e. the version number of the first .spec file in the package/ subdirectory.
@return [Boolean] true if the tag was created, false if it already existed
# File lib/packaging/git_helpers.rb, line 31 def create_version_tag(&block) name = tag_name(&block) if tag_exists?(name) puts "Tag #{name} already exists" false else puts "Creating tag #{name}" git("tag #{name}") true end end
Call a git subcommand and return its output as an array of strings.
@return Array<String>
# File lib/packaging/git_helpers.rb, line 106 def git(subcmd) `/usr/bin/git #{subcmd}`.split("\n") end
Check if the current branch is “master”.
@return [Boolean]
# File lib/packaging/git_helpers.rb, line 80 def master? branch_name == "master" end
Read the package version from specfile_name. If specfile_name is ‘nil’, use the first .spec file in the packages/ subdirectory.
@return String
# File lib/packaging/git_helpers.rb, line 57 def spec_version(specfile_name = nil) specfile_name ||= Dir.glob("#{Packaging::Configuration.instance.package_dir}/*.spec").first # use the first *.spec file found, assume all spec files # contain the same version File.readlines(specfile_name) .grep(/^\s*Version:\s*/).first.sub("Version:", "").strip end
Check if a tag exists.
@return Boolean
# File lib/packaging/git_helpers.rb, line 47 def tag_exists?(name = nil) name ||= tag_name git("tag").include?(name) end
Return a suitable tag name based on version and branch. For “master”, this is only the version number. For branches, this is “branchname-version”. If in “detached head” state, this is “detached-version”.
Like in create_version_tag
, the optional block can specify the version number to use. If no block is given, this uses spec_version
, i.e. the version number of the first .spec file in the package/ subdirectory.
@return String
# File lib/packaging/git_helpers.rb, line 95 def tag_name(&block) branch = branch_name version = block_given? ? block.call : spec_version return version if branch == "master" branch + "-" + version end