From 71f7a9106588bec3b15ef6c875443a54c71372b7 Mon Sep 17 00:00:00 2001 From: rkiel Date: Sat, 6 Jun 2015 06:43:34 -0400 Subject: [PATCH] breakout feature into parts --- bin/feature | 162 +++------------------------------------- lib/feature/base.rb | 54 ++++++++++++++ lib/feature/branch.rb | 11 +++ lib/feature/end.rb | 28 +++++++ lib/feature/merge_to.rb | 29 +++++++ lib/feature/rebase.rb | 29 +++++++ lib/feature/start.rb | 23 ++++++ 7 files changed, 184 insertions(+), 152 deletions(-) create mode 100644 lib/feature/base.rb create mode 100644 lib/feature/branch.rb create mode 100644 lib/feature/end.rb create mode 100644 lib/feature/merge_to.rb create mode 100644 lib/feature/rebase.rb create mode 100644 lib/feature/start.rb diff --git a/bin/feature b/bin/feature index 596d6d3..429f2d3 100755 --- a/bin/feature +++ b/bin/feature @@ -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 diff --git a/lib/feature/base.rb b/lib/feature/base.rb new file mode 100644 index 0000000..ceb6430 --- /dev/null +++ b/lib/feature/base.rb @@ -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 diff --git a/lib/feature/branch.rb b/lib/feature/branch.rb new file mode 100644 index 0000000..ffc308d --- /dev/null +++ b/lib/feature/branch.rb @@ -0,0 +1,11 @@ +require_relative './base' + +module Feature + + class Branch < Feature::Base + def execute + run_cmd "git branch" + end + end + +end diff --git a/lib/feature/end.rb b/lib/feature/end.rb new file mode 100644 index 0000000..a090d97 --- /dev/null +++ b/lib/feature/end.rb @@ -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 diff --git a/lib/feature/merge_to.rb b/lib/feature/merge_to.rb new file mode 100644 index 0000000..5e5ad17 --- /dev/null +++ b/lib/feature/merge_to.rb @@ -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 diff --git a/lib/feature/rebase.rb b/lib/feature/rebase.rb new file mode 100644 index 0000000..53e640d --- /dev/null +++ b/lib/feature/rebase.rb @@ -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 diff --git a/lib/feature/start.rb b/lib/feature/start.rb new file mode 100644 index 0000000..7d38271 --- /dev/null +++ b/lib/feature/start.rb @@ -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