Remove QA Git Tags

- - posted in Uncategorized

We use a hybrid of semantic versioning at work, and tag releases with each deployment to QA.  After a production deployment, the QA tags are fairly useless, so I like to clean them up to keep the tags fairly clean.

Here’s a little script that will delete QA tags (which are denoted by a 4th decimal #):

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

#!/usr/bin/env /Users/chris/.rbenv/shims/ruby

\# Script to delete all but a specified list of tags  
\# from a git repo, both locally and remotely  
\# Run like \`mode=test ruby git\_tag\_cleanup\` to test;  
\# use ‘execute’ mode to actually delete the tags

def prompt(*args)  
print(*args)  
gets.strip  
end

mode = ENV['mode'] || ‘test’

tags\_to\_keep = %w[v2.0.0 v2.0.1]  
tags\_to\_delete = \`git tag\`.split(“\n”) – tags\_to\_keep

puts “Manual Keeping: #{tags\_to\_keep.inspect}”

deleted_tags = []  
tags\_to\_delete.each do |tag|  
\# Keep Major Release Tags  
if tag !~ /^v\d{1,3}\.\d{1,3}\.{0,1}\d{0,3}[a-z]{0,3}$/  
deleted_tags << tag  
end  
end

puts “Tags Marked for deletion: #{deleted_tags}”

answer = prompt “Continue [y/N]? ”  
if answer && answer == “y”  
deleted\_tags.each\_with_index do |tag, index|  
delete_locally = “git tag -d ‘#{tag}’”  
delete_remote = “git push origin :’#{tag}’”  
puts delete_locally # to verify output  
system(delete_locally)  
puts delete_remote  
system(delete_remote)

if index % 10 == 0  
sleep(30)  
end  
end

puts “\nDone!\n”  
else  
puts “\nCanceled\n”  
end

puts “”