Module: GitVersionBump

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

Defined Under Namespace

Classes: CommandFailure, VersionUnobtainable

Class Method Summary collapse

Class Method Details

.commit_date_version(use_local_dir = 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.



157
158
159
160
161
162
163
# File 'lib/git-version-bump.rb', line 157

def self.commit_date_version(use_local_dir = false)
	if use_local_dir
		commit_date_version_string(true)
	else
		gem_version || commit_date_version_string(false)
	end
end

.commit_date_version_string(use_local_dir = false) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/git-version-bump.rb', line 165

def self.commit_date_version_string(use_local_dir = false)
	commit_dates = run_command(["git", "-C", git_dir(use_local_dir).to_s, "log", "--no-show-signature", "--format=%at"], "getting dates of all commits").
	               split("\n").
	               map { |l| Time.at(Integer(l)).strftime("%Y%m%d") }

	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}"
rescue CommandFailure => ex
	p :GVB_CDVS_CMD_FAIL, ex.output if debug?
	if ex.output =~ /fatal: your current branch .* does not have any commits yet/
		return "0.0.0.1.ENOCOMMITS"
	else
		raise VersionUnobtainable, "Could not get commit date-based version from git repository at #{git_dir(use_local_dir)}"
	end
end

.const_missing(c) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/git-version-bump/version.rb', line 26

def self.const_missing(c)
	if self.respond_to?(c) && c =~ /\A[A-Z_]+\z/
		public_send c
	else
		super
	end
end

.date(use_local_dir = false, include_lite_tags = false) ⇒ Object



71
72
73
74
75
76
77
# File 'lib/git-version-bump.rb', line 71

def self.date(use_local_dir=false, include_lite_tags = false)
	if use_local_dir
		repo_date(true, include_lite_tags)
	else
		gem_date || repo_date(false, include_lite_tags)
	end
end

.DATEObject



22
23
24
# File 'lib/git-version-bump/version.rb', line 22

def self.DATE
	GVB.date
end

.gem_dateObject



308
309
310
311
312
313
# File 'lib/git-version-bump.rb', line 308

def self.gem_date
	return nil if caller_gemspec.nil?
	return nil if caller_gemspec.date.nil?

	caller_gemspec.date.strftime("%F")
end

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



67
68
69
# File 'lib/git-version-bump.rb', line 67

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

.INTERNAL_REVISIONObject



18
19
20
# File 'lib/git-version-bump/version.rb', line 18

def self.INTERNAL_REVISION
	GVB.internal_revision
end

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



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/git-version-bump.rb', line 31

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

.MAJOR_VERSIONObject



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

def self.MAJOR_VERSION
	GVB.major_version
end

.minor_version(use_local_dir = false, include_lite_tags = false) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/git-version-bump.rb', line 43

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

.MINOR_VERSIONObject



10
11
12
# File 'lib/git-version-bump/version.rb', line 10

def self.MINOR_VERSION
	GVB.minor_version
end

.patch_version(use_local_dir = false, include_lite_tags = false) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/git-version-bump.rb', line 55

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

.PATCH_VERSIONObject



14
15
16
# File 'lib/git-version-bump/version.rb', line 14

def self.PATCH_VERSION
	GVB.patch_version
end

.tag_version(v, release_notes = false, include_lite_tags = false) ⇒ Object



79
80
81
82
83
84
85
86
87
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
126
127
128
# File 'lib/git-version-bump.rb', line 79

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."
		return false
	end
	if release_notes
		log_file = Tempfile.new('gvb')

		begin
			# 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 = run_command(git_cmd, "getting previous release tag").strip.gsub(/-\d+-g[0-9a-f]+$/, '')

			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.puts run_command(["git", "log", "--no-show-signature", "--format=# %h  %s", "#{prev_tag}..HEAD"], "getting commit range of release")

			log_file.close

			pre_hash = Digest::SHA1.hexdigest(File.read(log_file.path))
			run_command(["git", "config", "-e", "-f", log_file.path], "editing release notes", false)
			if Digest::SHA1.hexdigest(File.read(log_file.path)) == pre_hash
				puts "Release notes not edited; not making release"
				log_file.unlink
				return
			end

			puts "Tagging version #{v}..."
			run_command(["git", "tag", "-a", "-F", log_file.path, "v#{v}"], "tagging release with annotations")
		ensure
			log_file.unlink
		end
	else
		# Crikey this is a lot simpler
		run_command(["git", "tag", "-a", "-m", "Version v#{v}", "v#{v}"], "tagging release")
	end

	run_command(["git", "push"], "pushing commits to the default remote repository")
	run_command(["git", "push", "--tags"], "pushing tags to the default remote repository")
end

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



23
24
25
26
27
28
29
# File 'lib/git-version-bump.rb', line 23

def self.version(use_local_dir=false, include_lite_tags=false)
	if use_local_dir
		repo_version(true, include_lite_tags)
	else
		gem_version || repo_version(false, include_lite_tags)
	end.tap { |v| p :GVB_VERSION, v if debug? }
end

.VERSIONObject



2
3
4
# File 'lib/git-version-bump/version.rb', line 2

def self.VERSION
	GVB.version
end