breakout feature into parts

This commit is contained in:
rkiel
2015-06-06 06:43:34 -04:00
parent d928791be0
commit 71f7a91065
7 changed files with 184 additions and 152 deletions

View File

@@ -1,159 +1,17 @@
#!/usr/bin/env ruby
def error ( msg )
puts
puts "ERROR: #{msg}"
puts
exit
end
class Base
attr_reader :argv
def initialize (argv)
@argv = argv
end
def run_cmd ( cmd, fallback_branch = current_branch )
puts
puts cmd
success = system cmd
unless success
system "git checkout #{fallback_branch}"
error "(see above)"
end
puts
end
def current_branch
@current_branch ||= `git branch|grep '\*'|sed 's/\*\s*//'`.strip
end
def remote_branch (branch)
@remote_branch ||= `git branch -r|grep origin|grep -v 'HEAD'|grep #{branch}`.strip
end
def standard_branches
['master','develop','integration']
end
def parse_branch (branch)
parts = branch.split('-')
error "invalid branch: user-standard-feature" unless parts.size > 2
user = parts.shift
standard = parts.shift
error "invalid user: #{user}" unless ENV['USER'] == user
error "invalid branch: #{standard}" unless standard_branches.include? standard
feature = parts.join('-')
{ user: user, standard: standard, feature: feature }
end
end
class Branch < Base
def execute
run_cmd "git branch"
end
end
class Rebase < Base
def execute
parts = parse_branch(current_branch)
standard_branch = parts[:standard]
feature_branch = current_branch
remote_branch = remote_branch(feature_branch)
error "USAGE: feature rebase" unless standard_branch
error "invalid feature branch: #{feature_branch}" if standard_branches.include? feature_branch
run_cmd "git checkout #{standard_branch}"
run_cmd "git pull origin #{standard_branch}"
run_cmd "git checkout #{feature_branch}"
run_cmd "git rebase #{standard_branch}"
if remote_branch != ""
run_cmd "git push origin :#{feature_branch}"
end
run_cmd "git push origin #{feature_branch}"
end
end
class MergeTo < Base
def execute
parts = parse_branch(current_branch)
if argv.size == 2
merge_to_branch = argv[1]
elsif argv.size == 1
merge_to_branch = parts[:standard]
else
error "USAGE: feature merge [branch]"
end
feature_branch = current_branch
error "invalid branch: #{merge_to_branch}" unless standard_branches.include? merge_to_branch
run_cmd "git checkout #{merge_to_branch}"
run_cmd "git pull origin #{merge_to_branch}"
run_cmd "git merge #{feature_branch}"
run_cmd "git push origin #{merge_to_branch}"
run_cmd "git checkout #{feature_branch}"
end
end
class Start < Base
def execute
error "USAGE: feature start feature_name" unless argv.size == 2
feature = argv[1]
feature_branch = "#{ENV['USER']}-#{current_branch}-#{feature}"
error "invalid base branch: #{current_branch}" unless standard_branches.include? current_branch
error "invalid feature branch: #{featureh}" if standard_branches.include? feature
run_cmd "git pull origin #{current_branch}"
run_cmd "git checkout -b #{feature_branch}"
run_cmd "git push origin #{feature_branch}"
end
end
class End < Base
def execute
error "USAGE: feature end" unless ARGV.size == 1
parts = parse_branch(current_branch)
standard_branch = parts[:standard]
feature_branch = current_branch
error "invalid feature branch: #{feature_branch}" if standard_branches.include? feature_branch
run_cmd "git checkout #{standard_branch}"
if remote_branch(feature_branch) != ""
run_cmd "git push origin :#{feature_branch}"
end
run_cmd "git branch -D #{feature_branch}"
run_cmd "git remote prune origin"
end
end
require_relative '../lib/feature/start'
require_relative '../lib/feature/end'
require_relative '../lib/feature/rebase'
require_relative '../lib/feature/merge_to'
require_relative '../lib/feature/branch'
command = case ARGV[0]
when "start" then Start.new(ARGV)
when "end" then End.new(ARGV)
when "rebase" then Rebase.new(ARGV)
when "merge" then MergeTo.new(ARGV)
when nil then Branch.new(ARGV)
else error "USAGE: feature cmd branch"
when "start" then Feature::Start.new(ARGV)
when "end" then Feature::End.new(ARGV)
when "rebase" then Feature::Rebase.new(ARGV)
when "merge" then Feature::MergeTo.new(ARGV)
else Feature::Branch.new(ARGV)
end
command.execute

54
lib/feature/base.rb Normal file
View File

@@ -0,0 +1,54 @@
module Feature
class Base
attr_reader :argv
def initialize (argv)
@argv = argv
end
def run_cmd ( cmd, fallback_branch = current_branch )
puts
puts cmd
success = system cmd
unless success
system "git checkout #{fallback_branch}"
error "(see above)"
end
puts
end
def current_branch
@current_branch ||= `git branch|grep '\*'|sed 's/\*\s*//'`.strip
end
def remote_branch (branch)
@remote_branch ||= `git branch -r|grep origin|grep -v 'HEAD'|grep #{branch}`.strip
end
def standard_branches
['master','develop','integration']
end
def parse_branch (branch)
parts = branch.split('-')
error "invalid branch: user-standard-feature" unless parts.size > 2
user = parts.shift
standard = parts.shift
error "invalid user: #{user}" unless ENV['USER'] == user
error "invalid branch: #{standard}" unless standard_branches.include? standard
feature = parts.join('-')
{ user: user, standard: standard, feature: feature }
end
def error ( msg )
puts
puts "ERROR: #{msg}"
puts
exit
end
end
end

11
lib/feature/branch.rb Normal file
View File

@@ -0,0 +1,11 @@
require_relative './base'
module Feature
class Branch < Feature::Base
def execute
run_cmd "git branch"
end
end
end

28
lib/feature/end.rb Normal file
View File

@@ -0,0 +1,28 @@
require_relative './base'
module Feature
class End < Feature::Base
def execute
error "USAGE: feature end" unless ARGV.size == 1
parts = parse_branch(current_branch)
standard_branch = parts[:standard]
feature_branch = current_branch
error "invalid feature branch: #{feature_branch}" if standard_branches.include? feature_branch
run_cmd "git checkout #{standard_branch}"
if remote_branch(feature_branch) != ""
run_cmd "git push origin :#{feature_branch}"
end
run_cmd "git branch -D #{feature_branch}"
run_cmd "git remote prune origin"
end
end
end

29
lib/feature/merge_to.rb Normal file
View File

@@ -0,0 +1,29 @@
require_relative './base'
module Feature
class MergeTo < Feature::Base
def execute
parts = parse_branch(current_branch)
if argv.size == 2
merge_to_branch = argv[1]
elsif argv.size == 1
merge_to_branch = parts[:standard]
else
error "USAGE: feature merge [branch]"
end
feature_branch = current_branch
error "invalid branch: #{merge_to_branch}" unless standard_branches.include? merge_to_branch
run_cmd "git checkout #{merge_to_branch}"
run_cmd "git pull origin #{merge_to_branch}"
run_cmd "git merge #{feature_branch}"
run_cmd "git push origin #{merge_to_branch}"
run_cmd "git checkout #{feature_branch}"
end
end
end

29
lib/feature/rebase.rb Normal file
View File

@@ -0,0 +1,29 @@
require_relative './base'
module Feature
class Rebase < Feature::Base
def execute
parts = parse_branch(current_branch)
standard_branch = parts[:standard]
feature_branch = current_branch
remote_branch = remote_branch(feature_branch)
error "USAGE: feature rebase" unless standard_branch
error "invalid feature branch: #{feature_branch}" if standard_branches.include? feature_branch
run_cmd "git checkout #{standard_branch}"
run_cmd "git pull origin #{standard_branch}"
run_cmd "git checkout #{feature_branch}"
run_cmd "git rebase #{standard_branch}"
if remote_branch != ""
run_cmd "git push origin :#{feature_branch}"
end
run_cmd "git push origin #{feature_branch}"
end
end
end

23
lib/feature/start.rb Normal file
View File

@@ -0,0 +1,23 @@
require_relative './base'
module Feature
class Start < Feature::Base
def execute
error "USAGE: feature start feature_name" unless argv.size == 2
feature = argv[1]
feature_branch = "#{ENV['USER']}-#{current_branch}-#{feature}"
error "invalid base branch: #{current_branch}" unless standard_branches.include? current_branch
error "invalid feature branch: #{featureh}" if standard_branches.include? feature
run_cmd "git pull origin #{current_branch}"
run_cmd "git checkout -b #{feature_branch}"
run_cmd "git push origin #{feature_branch}"
end
end
end