#!/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 = < 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