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 22 23 24 25 26 |
# 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.(File.join(path, '..')) end @git_dir or raise "Invalid git repository #{@path}" end |
Instance Method Details
#bump(version) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# 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! ⇒ Object
79 80 81 |
# File 'lib/gitvers/repository.rb', line 79 def check! versions.count > 0 or raise NoVersionError end |
#check_version!(version) ⇒ Object
83 84 85 86 87 88 89 90 91 |
# 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 ⇒ Object
32 33 34 35 |
# File 'lib/gitvers/repository.rb', line 32 def full_version check! git(:describe).strip end |
#revision_number ⇒ Object
42 43 44 45 |
# File 'lib/gitvers/repository.rb', line 42 def revision_number check! git("rev-list #{@commit_name}").lines.count end |
#short_version ⇒ Object
37 38 39 40 |
# File 'lib/gitvers/repository.rb', line 37 def short_version check! git('describe --abbrev=0').strip end |
#summary ⇒ Object
28 29 30 |
# File 'lib/gitvers/repository.rb', line 28 def summary "#{full_version} (#{revision_number})" end |
#tag(version) ⇒ Object
73 74 75 76 77 |
# File 'lib/gitvers/repository.rb', line 73 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
47 48 49 50 |
# File 'lib/gitvers/repository.rb', line 47 def versions return @versions if defined?(@versions) @versions = git(:tag).lines.reverse end |