Class: Gitvers::Repository
- Inherits:
-
Object
- Object
- Gitvers::Repository
- Defined in:
- lib/gitvers/repository.rb
Instance Method Summary collapse
- #bump(version) ⇒ Object
- #check! ⇒ Object
- #check_version!(version) ⇒ Object
- #full_version ⇒ Object
-
#initialize(path, commit: 'HEAD') ⇒ Repository
constructor
A new instance of Repository.
- #revision_number ⇒ Object
- #short_version ⇒ Object
- #summary ⇒ Object
- #tag(version) ⇒ Object
- #versions ⇒ Object
Constructor Details
#initialize(path, commit: 'HEAD') ⇒ Repository
Returns a new instance of Repository.
9 10 11 12 13 14 15 16 17 18 19 20 21 |
# 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") path = File.(File.join(path, '..')) next unless File.exist?(git_dir) || File.exists?(head_file) @git_dir = git_dir end @git_dir or raise "Invalid git repository #{@path}" end |
Instance Method Details
#bump(version) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/gitvers/repository.rb', line 47 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! ⇒ Object
74 75 76 |
# File 'lib/gitvers/repository.rb', line 74 def check! versions.count > 0 or raise NoVersionError end |
#check_version!(version) ⇒ Object
78 79 80 81 82 83 84 85 86 |
# File 'lib/gitvers/repository.rb', line 78 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 ⇒ Object
27 28 29 30 |
# File 'lib/gitvers/repository.rb', line 27 def full_version check! git(:describe).strip end |
#revision_number ⇒ Object
37 38 39 40 |
# File 'lib/gitvers/repository.rb', line 37 def revision_number check! git("rev-list #{@commit_name}").lines.count end |
#short_version ⇒ Object
32 33 34 35 |
# File 'lib/gitvers/repository.rb', line 32 def short_version check! git('describe --abbrev=0').strip end |
#summary ⇒ Object
23 24 25 |
# File 'lib/gitvers/repository.rb', line 23 def summary "#{full_version} (#{revision_number})" end |
#tag(version) ⇒ Object
68 69 70 71 72 |
# File 'lib/gitvers/repository.rb', line 68 def tag(version) check_version!(version) git("tag -a #{version} -m v#{version}") puts "Created tag #{version}. You need to push it with git push --tags" end |
#versions ⇒ Object
42 43 44 45 |
# File 'lib/gitvers/repository.rb', line 42 def versions return @versions if defined?(@versions) @versions = git(:tag).lines.reverse end |