class Gitvers::Repository

Public Class Methods

new(path, commit: 'HEAD') click to toggle source
# File lib/gitvers/repository.rb, line 9
def initialize(path, commit: 'HEAD')
  @path = path
  @commit_name = commit

  while path != '/'
    git_dir = File.join(path, "/.git")
    head_file = File.join(path, "/HEAD")
    if File.exist?(git_dir)
      @git_dir = git_dir
      break
    elsif File.exists?(head_file)
      @git_dir = path
      break
    end
    path = File.expand_path(File.join(path, '..'))
  end
  @git_dir or raise "Invalid git repository #{@path}"
end

Public Instance Methods

bump(version) click to toggle source
# File lib/gitvers/repository.rb, line 52
def bump(version)
  check!

  ver = check_version!(version) rescue nil

  if ver.nil?
    ver = check_version!(short_version)
    case version.to_sym
    when :major
      ver.major += 1
      ver.minor = ver.patch = 0
    when :minor
      ver.minor += 1
      ver.patch = 0
    when :patch
      ver.patch += 1
    end
  end
  tag("#{ver.major}.#{ver.minor}.#{ver.patch}")
end
check!() click to toggle source
# File lib/gitvers/repository.rb, line 79
def check!
  versions.count > 0 or raise NoVersionError
end
check_version!(version) click to toggle source
# File lib/gitvers/repository.rb, line 83
def check_version!(version)
  version =~ /(\d+)\.(\d+)\.(\d+)/ or raise InvalidVersionError, "Invalid version number #{version || '(null)'}"

  OpenStruct.new({
    :major => $1.to_i,
    :minor => $2.to_i,
    :patch => $3.to_i
  })
end
full_version() click to toggle source
# File lib/gitvers/repository.rb, line 32
def full_version
  check!
  git(:describe).strip.gsub(/^v/, '')
end
revision_number() click to toggle source
# File lib/gitvers/repository.rb, line 42
def revision_number
  check!
  git("rev-list #{@commit_name}").lines.count
end
short_version() click to toggle source
# File lib/gitvers/repository.rb, line 37
def short_version
  check!
  git('describe --abbrev=0').strip.gsub(/^v/, '')
end
summary() click to toggle source
# File lib/gitvers/repository.rb, line 28
def summary
  "#{full_version} (#{revision_number})"
end
tag(version) click to toggle source
# File lib/gitvers/repository.rb, line 73
def tag(version)
  check_version!(version)
  git("tag -a v#{version} -m v#{version}")
  puts "Created tag #{version}. You need to push it with git push --tags"
end
versions() click to toggle source
# File lib/gitvers/repository.rb, line 47
def versions
  return @versions if defined?(@versions)
  @versions = git(:tag).lines.reverse
end

Private Instance Methods

git(command) click to toggle source
# File lib/gitvers/repository.rb, line 94
def git(command)
  `git --git-dir="#{@git_dir}" --work-tree="#{@path}/" #{command}`
end