Skip to content

Instantly share code, notes, and snippets.

@ryanjones
Forked from nathany/LICENSE
Created September 6, 2012 17:12
Show Gist options
  • Save ryanjones/3658654 to your computer and use it in GitHub Desktop.
Save ryanjones/3658654 to your computer and use it in GitHub Desktop.
Pruning remote branches that are already in master
namespace :git do
desc "prune"
task :prune do
# Prepare origin and ensure on master to do comparison
print "\nFetch and prune tracking branches? [y/N] "
yes_no = $stdin.gets.chomp
abort if yes_no.downcase != 'y'
system('git fetch && git remote prune origin')
abort "Must be on master to find already merged branches." if current_branch != 'master'
# Offer to delete remote branches that are already in (local) master
out = run_cmd("git branch -r --merged")
branches = out.split("\n").reject { |b| b =~ /(production|release|staging|master)$/i || b !~ /^\s*origin/i }
if branches.empty?
puts "No remote branches to prune."
else
puts "\nThe following remote branches are fully contained in HEAD of #{current_branch}:"
puts branches
print "\nDelete from origin? (GitHub) [y/N] "
yes_no = $stdin.gets.chomp
abort if yes_no.downcase != 'y'
branches.map! { |b| b.sub(/^\s*origin\//i, '') } # remove origin/
system("git push origin --delete #{branches.join(' ')} && git remote prune origin") # delete stuff!
end
# Local branches that could be deleted
out = run_cmd("git branch --merged")
branches = out.split("\n").reject { |b| b =~ /(production|release|staging|master)$/i }
unless branches.empty?
puts "\nThe following local branches are fully contained in HEAD of #{current_branch}:"
puts branches
puts "\nLocal pruning is left as an exercise for the reader."
end
end
# local branch currently checked out:
def current_branch
out = run_cmd('git branch')
local_branch = $1 if out =~ /\* (\S+)\s/m
end
def run_cmd(cmd)
out = `#{cmd}`
err = $?
raise "Running `#{cmd}' failed (#{err})." unless err == 0
out
end
end
Copyright (C) 2012 Nathan Youngman
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment