Skip to content

Instantly share code, notes, and snippets.

@phatblat
Created May 4, 2016 17:42
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save phatblat/ad41059a0d7a045f32067897fd28282d to your computer and use it in GitHub Desktop.
Script for creating a matching topic branch in each of repos in the current directory
#!/usr/bin/env ruby
# create_branch.rb
base_branch="development"
ARGV.each do|a|
puts "Argument: #{a}"
end
# Script requires at least 1 argument
if ARGV.length < 1
puts "Usage: create_branch.sh new-branch-name [base-branch]"
puts "If not provided, 'base-branch' is assumed to be '#{base_branch}'."
exit 1
end
# 1st argument: new-branch-name
new_branch_name = ARGV[0]
# 2nd argument: base-branch (optional)
if ARGV.length == 2
base_branch = ARGV[1]
end
def branch_exists(branch_name)
puts "Checking whether #{branch_name} exists in local repo"
success = system("git rev-parse --verify #{branch_name} > /dev/null")
puts "Branch exists" if success
return success
end
root_dir = File.dirname(__FILE__)
Dir.foreach(root_dir) do |item|
next if item == '.' or item == '..' or item == '_old' or File.file?(item)
# do work on real items
puts item
Dir.chdir(root_dir + '/' + item)
`git checkout master`
`git pull`
`git checkout #{base_branch} || git checkout -t origin/#{base_branch}`
`git pull`
if !branch_exists(new_branch_name)
# Only create if branch doesn't already exist
`git checkout -b #{new_branch_name}`
end
Dir.chdir("..") # pop
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment