-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathgit-version.cr
More file actions
226 lines (195 loc) · 5.92 KB
/
Copy pathgit-version.cr
File metadata and controls
226 lines (195 loc) · 5.92 KB
1
2
3
4
5
6
7
8
9
10
11
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
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
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
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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
require "file_utils"
require "semantic_version"
module GitVersion
extend self
BASE_VERSION_STRING = "0.0.0"
BASE_VERSION = SemanticVersion.parse(BASE_VERSION_STRING)
DEV_BRANCH_SUFFIX = "SNAPSHOT"
class Git
def initialize(@dev_branch : String, @release_branch : String, @minor_identifier : String, @major_identifier : String,
@folder = FileUtils.pwd, @prefix : String = "", @log_paths : String = "")
@major_id_is_regex = false
@minor_id_is_regex = false
if match = /\/(.*)\//.match(@major_identifier)
@major_identifier = match[1]
@major_id_is_regex = true
end
if match = /\/(.*)\//.match(@minor_identifier)
@minor_identifier = match[1]
@minor_id_is_regex = true
end
#
end
private def add_prefix(version : String) : String
return "#{@prefix}#{version}"
end
private def strip_prefix(version : String) : String | Nil
version.lchop?(@prefix)
end
private def exec(cmd)
strout = IO::Memory.new
if !Process.run(
command: cmd,
shell: true,
output: strout,
error: Process::Redirect::Close,
chdir: @folder
).success?
raise "[ERROR] Command #{cmd} failed."
end
return strout.to_s.split('\n', remove_empty: true)
end
def dev_branch
return @dev_branch
end
def log_paths_filter
@log_paths.empty? ? "" : "-- #{@log_paths}"
end
def release_branch
return @release_branch
end
def tags_by_branch(branch)
return exec "git tag --merged #{branch}"
end
def current_branch_or_tag
return (exec "git symbolic-ref --short HEAD")[0]
rescue
return (exec "git describe --tags")[0]
end
def current_commit_hash : String
cmd = "git rev-parse --verify HEAD --short"
sha = (exec cmd)[0].strip
return "sha." + sha
end
def current_commit_hash_without_prefix : String
cmd = "git rev-parse --verify HEAD --short"
return (exec cmd)[0].strip
end
def commits_distance(tag : String | Nil)
if tag.nil?
return (exec "git rev-list --count HEAD")[0]
else
return (exec "git rev-list --count HEAD ^#{tag}")[0]
end
rescue
return 0
end
def get_commits_since(tag : String | Nil)
if !tag.nil? && (exec "git tag -l #{tag}").any?
last_commit = (exec "git show-ref -s #{tag}")[0]
return (exec "git log --pretty=%B #{last_commit}..HEAD #{log_paths_filter}")
else
return (exec "git log --pretty=%B")
end
rescue
return [] of String
end
def get_previous_tag_and_version : Tuple(String | Nil, SemanticVersion)
cb = current_branch_or_tag
branch_tags = tags_by_branch(cb)
previous_version = BASE_VERSION
previous_tag = nil
branch_tags.each do |tag|
begin
tag_without_prefix = strip_prefix(tag)
if tag_without_prefix.nil?
next
end
current_version = SemanticVersion.parse(tag_without_prefix)
if !current_version.prerelease.identifiers.empty?
next
elsif (previous_version < current_version)
previous_version = current_version
previous_tag = tag
end
rescue
#
end
end
return {previous_tag, previous_version}
end
def get_previous_version : String
lt, lv = get_previous_tag_and_version
return lt ? lt : add_prefix(lv.to_s)
end
def get_new_version
previous_tag, previous_version = get_previous_tag_and_version
previous_version =
SemanticVersion.new(
previous_version.major,
previous_version.minor,
previous_version.patch + 1,
nil,
nil,
)
major = false
get_commits_since(previous_tag).each do |c|
commit = c.downcase
match = if @major_id_is_regex
/#{@major_identifier}/.match(commit)
else
commit.includes?(@major_identifier)
end
if match
previous_version =
SemanticVersion.new(
previous_version.major + 1,
0,
0,
previous_version.prerelease,
previous_version.build,
)
major = true
break
end
end
if !major
get_commits_since(previous_tag).each do |c|
commit = c.downcase
match = if @minor_id_is_regex
/#{@minor_identifier}/.match(commit)
else
commit.includes?(@minor_identifier)
end
if match
previous_version =
SemanticVersion.new(
previous_version.major,
previous_version.minor + 1,
0,
previous_version.prerelease,
previous_version.build,
)
break
end
end
end
cb = current_branch_or_tag
if cb == @release_branch
#
elsif cb == @dev_branch
prerelease = [DEV_BRANCH_SUFFIX, commits_distance(previous_tag), current_commit_hash()] of String | Int32
previous_version =
SemanticVersion.new(
previous_version.major,
previous_version.minor,
previous_version.patch,
SemanticVersion::Prerelease.new(prerelease),
nil
)
else
branch_sanitized_name = cb.downcase.gsub(/[^a-zA-Z0-9]/, "")[0,30]
prerelease = [branch_sanitized_name, commits_distance(previous_tag), current_commit_hash()] of String | Int32
previous_version =
SemanticVersion.new(
previous_version.major,
previous_version.minor,
previous_version.patch,
SemanticVersion::Prerelease.new(prerelease),
nil
)
end
return add_prefix(previous_version.to_s)
end
end
end