Skip to content

Instantly share code, notes, and snippets.

@nowells
Created November 18, 2011 01:02
Show Gist options
  • Save nowells/1375176 to your computer and use it in GitHub Desktop.
Save nowells/1375176 to your computer and use it in GitHub Desktop.
git merge master to all branches
#!/usr/bin/env ruby
require 'net/smtp'
def run(command)
output = `#{command} 2>&1`
if not $?.success?
raise Exception, "Command Failed '#{command}': #{output}"
end
output
end
def branch_list(command)
run(command)
.split("\n")
.select { |x| (x =~ / -> /).nil? }
.map { |x| x.strip.split('/', 2).last }
.select { |x| (x =~ /^master$/).nil? }
end
def send_email(from, from_alias, to, to_alias, subject, message)
msg = <<END_OF_MESSAGE
From: #{from_alias} <#{from}>
To: #{to_alias} <#{to}>
Subject: #{subject}
#{message}
END_OF_MESSAGE
Net::SMTP.start('localhost') do |smtp|
smtp.send_message msg, from, to
end
end
# Update repository and remove all stale branches
run "git remote update --prune"
branches = branch_list("git branch -r")
unclean_merges = []
branches.each do |branch|
begin
run "git reset --hard HEAD"
run "git checkout origin/#{branch}"
begin
run "git merge --no-ff origin/master"
#run "git push origin HEAD:refs/heads/#{branch}"
rescue Exception
unclean_merges << branch
raise
end
puts "Successfully updated branch '#{branch}'"
rescue Exception
puts "Failed to update branch '#{branch}'"
end
end
run "git reset --hard HEAD"
run "git checkout master"
if not unclean_merges.empty?
send_email(
"no-reply@drakerlabs.com", "Jenkins Merger",
"nowell@strite.org", "Dev Team",
"Unclean master merges",
"The following branches were unable to merge master cleanly:\n#{unclean_merges.join("\n")}"
)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment