Module: GitVersionBump

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

Defined Under Namespace

Classes: VersionUnobtainable

Constant Summary collapse

VERSION_TAG_GLOB =
'v[0-9]*.[0-9]*.*[0-9]'
DEVNULL =
Gem.win_platform? ? "NUL" : "/dev/null"
VERSION =
GVB.version
MAJOR_VERSION =
GVB.major_version
MINOR_VERSION =
GVB.minor_version
PATCH_VERSION =
GVB.patch_version
INTERNAL_REVISION =
GVB.internal_revision
DATE =
GVB.date

Class Method Summary collapse

Class Method Details

.commit_date_version(use_local_git = false) ⇒ Object

Calculate a version number based on the date of the most recent git commit.

Return a version format string of the form ‘“0.YYYYMMDD.N”`, where `YYYYMMDD` is the date of the “top-most” commit in the tree, and `N` is the number of other commits also made on that date.

This version format is not recommented for general use. It has benefit only in situations where the principles of Semantic Versioning have no real meaning, such as packages where there is little or no concept of “backwards compatibility” (eg packages which only contain images and other assets), or where the package can, for reasons outside that of the package itself, never break backwards compatibility (definitions of binary-packed structures shared amongst multiple systems).

The format of this commit-date-based version format allows for a strictly monotonically-increasing version number, aligned with the progression of the underlying git commit log.

One limitation of the format is that it doesn’t deal with the issue of package builds made from multiple divergent trees. Unlike ‘git-describe`-based output, there is no “commit hash” identity included in the version string. This is because of (ludicrous) limitations of the Rubygems format definition – the moment there’s a letter in the version number, the package is considered a “pre-release” version. Since hashes are hex, we’re boned. Sorry about that. Don’t make builds off a branch, basically.



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/git-version-bump.rb', line 201

def self.commit_date_version(use_local_git = false)
	if use_local_git
		unless git_available?
			raise RuntimeError,
			      "GVB.commit_date_version(use_local_git=true) called, but git isn't installed"
		end

		sq_git_dir = shell_quoted_string(Dir.pwd)
	else
		sq_git_dir = shell_quoted_string((File.dirname(caller_file) rescue nil || Dir.pwd))
	end

	commit_dates = `git -C #{sq_git_dir} log --format=%at`.
	               split("\n").
	               map { |l| Time.at(Integer(l)).strftime("%Y%m%d") }

	if $? == 0
		# We got a log; calculate our version number and we're done.
		version_date = commit_dates.first
		commit_count = commit_dates.select { |d| d == version_date }.length - 1
		dirty_suffix = if dirty_tree?
			".dirty.#{Time.now.strftime("%Y%m%d.%H%M%S")}"
		else
			""
		end

		return "0.#{version_date}.#{commit_count}#{dirty_suffix}"
	end

	# git failed us; either we're not in a git repo or else it's a git
	# repo that's not got any commits.

	# Are we in a git repo with no commits?  If so, try to use the gemspec
	# and if that fails then abort
	begin
		return gem_version(use_local_git)
	rescue VersionUnobtainable
		return "0.0.0.1.ENOCOMMITS"
	end
end

.date(use_local_git = false) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/git-version-bump.rb', line 88

def self.date(use_local_git=false)
	if use_local_git
		unless git_available?
			raise RuntimeError,
			      "GVB.date(use_local_git=true), but git is not installed"
		end

		sq_git_dir = shell_quoted_string(Dir.pwd)
	else
		sq_git_dir = shell_quoted_string((File.dirname(caller_file) rescue nil || Dir.pwd))
	end

	# Are we in a git tree?
	system("git -C #{sq_git_dir} status > #{DEVNULL} 2>&1")
	if $? == 0
		# Yes, we're in git.

		if dirty_tree?(sq_git_dir)
			return Time.now.strftime("%F")
		else
			# Clean tree.  Date of last commit is needed.
			return `git -C #{sq_git_dir} show --no-show-signature --format=format:%cd --date=short`.lines.first&.strip
		end
	else
		if use_local_git
			raise RuntimeError,
			      "GVB.date(use_local_git=true) called from non-git location"
		end

		# Not in git; time to hit the gemspecs
		if spec = caller_gemspec
			return spec.date.strftime("%F")
		end

		raise RuntimeError,
		      "GVB.date called from mysterious, non-gem location."
	end
end

.internal_revision(use_local_git = false, include_lite_tags = false) ⇒ Object



84
85
86
# File 'lib/git-version-bump.rb', line 84

def self.internal_revision(use_local_git=false, include_lite_tags=false)
	version(use_local_git, include_lite_tags).split('.', 4)[3].to_s
end

.major_version(use_local_git = false, include_lite_tags = false) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/git-version-bump.rb', line 48

def self.major_version(use_local_git=false, include_lite_tags=false)
	ver = version(use_local_git, include_lite_tags)
	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(use_local_git = false, include_lite_tags = false) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/git-version-bump.rb', line 60

def self.minor_version(use_local_git=false, include_lite_tags=false)
	ver = version(use_local_git, include_lite_tags)
	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(use_local_git = false, include_lite_tags = false) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/git-version-bump.rb', line 72

def self.patch_version(use_local_git=false, include_lite_tags=false)
	ver = version(use_local_git, include_lite_tags)
	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, include_lite_tags = false) ⇒ Object



127
128
129
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/git-version-bump.rb', line 127

def self.tag_version(v, release_notes = false, include_lite_tags=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.
			git_cmd = "git describe --match '#{VERSION_TAG_GLOB}' --always"
			git_cmd << ' --tags' if include_lite_tags
			prev_tag = `#{git_cmd}`.strip.gsub(/-\d+-g[0-9a-f]+$/, '')

			log_file = Tempfile.new('gvb')

			log_file.puts <<-EOF.gsub(/^\t\t\t\t\t/, '')



				# Write your release notes above.  The first line should be the release name.
				# To help you remember what's in here, the commits since your last release
				# are listed below. This will become v#{v}
				#
			EOF

			log_file.close
			system("git log --no-show-signature --format='# %h  %s' #{prev_tag}..HEAD >>#{log_file.path}")

			pre_hash = Digest::SHA1.hexdigest(File.read(log_file.path))
			system("git config -e -f #{log_file.path}")
			if Digest::SHA1.hexdigest(File.read(log_file.path)) == pre_hash
				puts "Release notes not edited; aborting"
				log_file.unlink
				return
			end

			puts "Tagging version #{v}..."
			system("git tag -a -F #{log_file.path} v#{v}")
			log_file.unlink
		else
			# Crikey this is a lot simpler
			system("git tag -a -m 'Version v#{v}' v#{v}")
		end

		system("git push > #{DEVNULL} 2>&1")
		system("git push --tags > #{DEVNULL} 2>&1")
	end
end

.version(use_local_git = false, include_lite_tags = false) ⇒ Object



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
38
39
40
41
42
43
44
45
46
# File 'lib/git-version-bump.rb', line 12

def self.version(use_local_git=false, include_lite_tags=false)
	if use_local_git
		unless git_available?
			raise RuntimeError,
			      "GVB.version(use_local_git=true) called, but git isn't installed"
		end

		sq_git_dir = shell_quoted_string(Dir.pwd)
	else
		sq_git_dir = shell_quoted_string((File.dirname(caller_file) rescue nil || Dir.pwd))
	end

	git_cmd = "git -C #{sq_git_dir} describe --dirty='.1.dirty.#{Time.now.strftime("%Y%m%d.%H%M%S")}' --match='#{VERSION_TAG_GLOB}'"
	git_cmd << " --tags" if include_lite_tags

	git_ver = `#{git_cmd} 2> #{DEVNULL}`.
	            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, try to use the gemspec
	# and if that fails then abort
	begin
		return gem_version(use_local_git)
	rescue VersionUnobtainable
		return "0.0.0.1.ENOTAG"
	end
end