Module: Packaging::GitHelpers

Defined in:
lib/packaging/git_helpers.rb

Overview

Helpers around various git commands

Instance Method Summary collapse

Instance Method Details

#branch_nameObject

Return the name of the current git branch or “detached” if in “head detached” state.

Returns:

  • String



70
71
72
73
74
# 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_version_tag(&block) ⇒ Boolean

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.

Returns:

  • (Boolean)

    true if the tag was created, false if it already existed



31
32
33
34
35
36
37
38
39
40
41
# 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

#git(subcmd) ⇒ Object

Call a git subcommand and return its output as an array of strings.



106
107
108
# File 'lib/packaging/git_helpers.rb', line 106

def git(subcmd)
  `/usr/bin/git #{subcmd}`.split("\n")
end

#master?Boolean

Check if the current branch is “master”.

Returns:

  • (Boolean)


80
81
82
# File 'lib/packaging/git_helpers.rb', line 80

def master?
  branch_name == "master"
end

#spec_version(specfile_name = nil) ⇒ Object

Read the package version from specfile_name. If specfile_name is ‘nil’, use the first .spec file in the packages/ subdirectory.

Returns:

  • String



57
58
59
60
61
62
63
# 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

#tag_exists?(name = nil) ⇒ Boolean

Check if a tag exists.

Returns:

  • (Boolean)

    Boolean



47
48
49
50
# File 'lib/packaging/git_helpers.rb', line 47

def tag_exists?(name = nil)
  name ||= tag_name
  git("tag").include?(name)
end

#tag_name(&block) ⇒ Object

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.

Returns:

  • String



95
96
97
98
99
100
# 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