-
Notifications
You must be signed in to change notification settings - Fork 0
/
rmtag.rb
123 lines (93 loc) · 2.95 KB
/
rmtag.rb
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
#!/usr/bin/env ruby
# handles command line options
require 'getoptlong'
class Deleter
# the public entry point
def run
options = parseOptions()
options['help'] && outputHelp()
validateArgs()
minimum = Gem::Version.new( ARGV[0] )
maximum = Gem::Version.new( ARGV[1] )
deleteTags( minimum, maximum, options['execute'] )
end
protected
#
# deletes the tags, optionally only outputs the
# commands that would be executed, for the truly paranoid
#
def deleteTags min, max, execute
tagsToDelete = `git tag`.split( /\s+/ ).select do |tag|
tag = Gem::Version.new(tag)
tag >= min && tag <= max
end
commands = [
"git tag -d " + tagsToDelete.join(' '),
"git push origin " + tagsToDelete.map { |tag| ':' + tag }.join(' ')
]
commands.each do | command |
puts command
if execute
`#{command}`
puts 'executed !'
end
end
end
#
# application requires that two arguments be provided; both
# start tag and end tag
#
def validateArgs
if ( ! ARGV[0] || ! ARGV[1])
abort "must provide two version numbers. try: tagdeleter.rb --help"
end
end
#
# uses the getoptlog library to parse
# the --test and --help options
#
def parseOptions
opts = GetoptLong.new(
[ '--test', '-t', GetoptLong::NO_ARGUMENT ],
[ '--help', '-h', GetoptLong::NO_ARGUMENT ],
[ '--execute', '-e', GetoptLong::NO_ARGUMENT ]
)
options = Hash.new
opts.each do | optionName, argument |
options[optionName.gsub('--', '')] = true
end
options['test'] = options['test'] || false
options['execute'] = options['execute'] || false
options['help'] = options['help'] || false
return options
end
#
# outputs help text
#
def outputHelp
output = <<-EOF
\033[1mNAME\033[0m
rmtag
\033[1mSYNOPSIS\033[0m
rmtag --execute minVersion maxVersion
rmtag --test minVersion maxVersion
\033[1mDESCRIPTION\033[0m
when run within a directory version controlled by git, deletes all tags
between minVersion and maxVersion, \033[1mincluding\033[0m exact
matches for minVersion and maxVersion
\033[1mOPTIONS\033[0m
-h, --help
show this help file
-e, --execute
run the commands generated by the program
-t, --test
only output the commands to stdout, do not run them. this is the default mode
if --execute is not specified.
EOF
puts output.gsub( /^ /, '' )
abort
end
end
# do stuff
deletions = Deleter.new
deletions.run()