Skip to content

Instantly share code, notes, and snippets.

@nathany
Created January 21, 2012 07:19
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save nathany/1651882 to your computer and use it in GitHub Desktop.
Save nathany/1651882 to your computer and use it in GitHub Desktop.
Pruning remote branches that are already in master
namespace :git do
desc "Remove remote branches that are already merged into master"
task :prune do
# never prune these branches, even if they are fully contained in master
PRESERVE_BRANCHES = %w{production staging master}
ORIGIN = 'origin' # remote name
MASTER = 'master' # LOCAL branch to compare against
# Get remote-tracking branches to match origin
abort unless prompt "Fetch and prune #{ORIGIN}/tracking branches to match #{ORIGIN}?"
GitHelper.update_tracking_branches(ORIGIN)
# Ensure on master to do comparison
abort "Must be on #{MASTER} to find already merged branches." if GitHelper.current_branch != MASTER
preserve_re = PRESERVE_BRANCHES.join('|')
# Offer to delete remote branches that are already in (local) master
branches = GitHelper.merged_branches(:remotes).
reject { |b| b =~ /(#{preserve_re})$/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 #{GitHelper.current_branch}:"
puts branches
abort unless prompt "Delete from #{ORIGIN}? (GitHub)"
GitHelper.delete!(branches, ORIGIN)
GitHelper.update_tracking_branches(ORIGIN)
end
# Local branches that could be deleted
branches = GitHelper.merged_branches.reject { |b| b =~ /(#{preserve_re})$/i }
unless branches.empty?
puts "\nThe following local branches are fully contained in HEAD of #{GitHelper.current_branch}:"
puts branches
puts "\nLocal pruning is left as an exercise for the reader."
end
end
def prompt(question)
print "\n#{question} [y/N] "
yes_no = $stdin.gets.chomp
yes_no.downcase == 'y'
end
module GitHelper
def self.update_tracking_branches(remote='origin')
# Prune removes remote-tracking branches that no longer exist on the remote
system("git fetch #{remote} && git remote prune #{remote}")
end
# list branches that are already merged into the current branch
def self.merged_branches(remotes=false)
out = if remotes
run_cmd("git branch --remotes --merged") # origin/ tracking branches
else
run_cmd("git branch --merged")
end
out.split("\n")
end
def self.delete!(branches, remote)
branch_list = branches.map { |b| b.sub(/^\s*#{remote}\//i, '') }.join(' ') # remove origin/
system("git push #{remote} --delete #{branch_list}")
end
# local branch currently checked out:
def self.current_branch
out = run_cmd('git branch')
$1 if out =~ /\* (\S+)\s/m
end
def self.run_cmd(cmd)
out = `#{cmd}`
err = $?
raise "Running `#{cmd}' failed (#{err})." unless err == 0
out
end
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.
@nathany
Copy link
Author

nathany commented Dec 4, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment