public
Last active

Script to prune any tags which exist locally but not on the remote

  • Download Gist
git-prune-tags.rb
Ruby
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
#!/usr/bin/env ruby
 
require 'readline'
def prompt(prompt="> ")
input = nil
prompt += " " unless prompt =~ /\s$/
loop do
input = Readline.readline(prompt)
break if input.length > 0
end
return input
end
 
remote = ARGV[0]
dry_run = ARGV.include?("--dry-run")
 
if !remote || remote == "--dry-run"
puts "no remote specified, assuming 'origin'"
remote = "origin"
end
 
local_tags = `git tag`.split("\n")
remote_tags = []
 
remote_tag_details = `git ls-remote --tags #{remote}`.split("\n")
remote_tag_details.each do |details|
sha, tag = details.split("\t")
next if tag[-3, 3] == "^{}"
remote_tags << tag.gsub(%r(^refs/tags/), '')
end
 
delete_tags = []
local_tags.each do |local_tag|
next if remote_tags.include?(local_tag)
puts "local tag '#{local_tag}' not found in '#{remote}'"
delete_tags << local_tag
end
 
if delete_tags.empty?
abort "No tags found locally which aren't on '#{remote}'"
end
 
if dry_run
puts "#{delete_tags.size} tags would have been deleted, run without --dry-run to do the deed"
else
response = prompt("Are you sure you want to delete these tags? (yn)")
if response.downcase.strip =~ /^y/
delete_tags.each do |local_tag|
`git tag -d #{local_tag}`
end
 
puts "#{delete_tags.size} tags have been deleted"
else
puts "Aborted"
end
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.