Module: GitVersionBump

Defined in:
lib/git-version-bump.rb

Constant Summary collapse

VERSION =
version('git-version-bump')
MAJOR_VERSION =
major_version('git-version-bump')
MINOR_VERSION =
minor_version('git-version-bump')
PATCH_VERSION =
patch_version('git-version-bump')
INTERNAL_REVISION =
internal_revision('git-version-bump')
DATE =
date('git-version-bump')

Class Method Summary collapse

Class Method Details

.caller_gemspecObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/git-version-bump.rb', line 12

def self.caller_gemspec
  # First up, who called us?  Because this method gets called from other
  # methods within this file, we can't just look at
  # Gem.location_of_caller, but instead we need to parse the caller
  # stack ourselves to find which gem we're trying to version all over.
  caller_file = caller.
                  map  { |l| l.split(':')[0] }.
                  find { |l| l != __FILE__ }

  # Real paths, please.
  caller_file = File.realpath(caller_file)

  # Next we grovel through all the loaded gems to try and find the gem
  # that contains the caller's file.
  Gem.loaded_specs.values.each do |spec|
    if Dir.
        glob(spec.lib_dirs_glob).
        find { |d| caller_file.index(File.realpath(d)) == 0 }
      # The caller_file is in this
      # gem!  Woohoo!
      return spec
    end
  end

  nil
end

.date(gem = nil) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/git-version-bump.rb', line 130

def self.date(gem = nil)
  # Are we in a git tree?
  system("git status >/dev/null 2>&1")
  if $? == 0
    # Yes, we're in git.

    if dirty_tree?
      return Time.now.strftime("%F")
    else
      # Clean tree.  Date of last commit is needed.
      return `git show --format=format:%ad --date=short | head -n 1`.strip
    end
  else
    # Not in git; time to hit the gemspecs
    if gem
      return Gem.loaded_specs[gem].date.strftime("%F")
    end

    if spec = caller_gemspec
      return spec.date.strftime("%F")
    end

    raise RuntimeError,
        "GVB.date(#{gem.inspect}) called from mysterious, non-gem location."
  end
end

.dirty_tree?Boolean

Returns:

  • (Boolean)


5
6
7
8
9
10
# File 'lib/git-version-bump.rb', line 5

def self.dirty_tree?
  # Are we in a dirty, dirty tree?
  system("! git diff --no-ext-diff --quiet --exit-code || ! git diff-index --cached --quiet HEAD")

  $? == 0
end

.internal_revision(gem = nil) ⇒ Object



124
125
126
# File 'lib/git-version-bump.rb', line 124

def self.internal_revision(gem = nil)
  version(gem).split('.', 4)[3].to_s
end

.major_version(gem = nil) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/git-version-bump.rb', line 82

def self.major_version(gem = nil)
  ver = version(gem)
  v   = ver.split('.')[0]

  unless v =~ /^[0-9]+$/
    raise ArgumentError,
            "#{v} (part of #{ver.inspect}) is not a numeric version component.  Abandon ship!"
  end

  return v.to_i
end

.minor_version(gem = nil) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
# File 'lib/git-version-bump.rb', line 96

def self.minor_version(gem = nil)
  ver = version(gem)
  v   = ver.split('.')[1]

  unless v =~ /^[0-9]+$/
    raise ArgumentError,
            "#{v} (part of #{ver.inspect}) is not a numeric version component.  Abandon ship!"
  end

  return v.to_i
end

.patch_version(gem = nil) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
# File 'lib/git-version-bump.rb', line 110

def self.patch_version(gem = nil)
  ver = version(gem)
  v   = ver.split('.')[2]

  unless v =~ /^[0-9]+$/
    raise ArgumentError,
            "#{v} (part of #{ver.inspect}) is not a numeric version component.  Abandon ship!"
  end

  return v.to_i
end

.tag_version(v, release_notes = false) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/git-version-bump.rb', line 159

def self.tag_version(v, release_notes = false)
  if dirty_tree?
    puts "You have uncommitted files.  Refusing to tag a dirty tree."
  else
    if release_notes
      # We need to find the tag before this one, so we can list all the commits
      # between the two.  This is not a trivial operation.
      prev_tag = `git describe --always`.strip.gsub(/-\d+-g[0-9a-f]+$/, '')

      log_file = Tempfile.new('gvb')

      log_file.puts "\n\n\n        # Write your release notes above.  The first line should be the release name.\n        # To help you remember what's in here, the commits since your last release\n        # are listed below.\n        #\n      EOF\n\n      log_file.close\n      system(\"git log --format='# %h  %s' \#{prev_tag}..HEAD >>\#{log_file.path}\")\n\n      pre_hash = Digest::SHA1.hexdigest(File.read(log_file.path))\n      system(\"git config -e -f \#{log_file.path}\")\n      if Digest::SHA1.hexdigest(File.read(log_file.path)) == pre_hash\n        puts \"Release notes not edited; aborting\"\n        log_file.unlink\n        return\n      end\n\n      puts \"Tagging version \#{v}...\"\n      system(\"git tag -a -F \#{log_file.path} v\#{v}\")\n      log_file.unlink\n    else\n      # Crikey this is a lot simpler\n      system(\"git tag -a -m 'Version v\#{v}' v\#{v}\")\n    end\n\n    system(\"git push >/dev/null 2>&1\")\n    system(\"git push --tags >/dev/null 2>&1\")\n  end\nend\n".gsub(/^\t\t\t\t\t/, '')

.version(gem = nil) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/git-version-bump.rb', line 39

def self.version(gem = nil)
  @version_cache ||= {}

  return @version_cache[gem] if @version_cache[gem]

  git_ver = `git describe --dirty='.1.dirty.#{Time.now.strftime("%Y%m%d.%H%M%S")}' --match='v[0-9]*.[0-9]*.*[0-9]' 2>/dev/null`.
              strip.
              gsub(/^v/, '').
              gsub('-', '.')

  # If git returned success, then it gave us a described version.
  # Success!
  return git_ver if $? == 0

  # git failed us; we're either not in a git repo or else we've never
  # tagged anything before.

  # Are we in a git repo with no tags?  If so, dump out our
  # super-special version and be done with it.
  system("git status >/dev/null 2>&1")
  return "0.0.0.1.ENOTAG" if $? == 0

  if gem
    return Gem.loaded_specs[gem].version.to_s
  end

  # We're not in a git repo.  This means that we need to get version
  # information out of rubygems, given only the filename of who called
  # us.  This takes a little bit of effort.

  if spec = caller_gemspec
    return spec.version.to_s
  else
    # If we got here, something went *badly* wrong -- presumably, we
    # weren't called from within a loaded gem, and so we've got *no*
    # idea what's going on.  Time to bail!
    raise RuntimeError,
          "GVB.version(#{gem.inspect}) failed.  Is git installed?"
  end
end