breakout feature into parts
This commit is contained in:
162
bin/feature
162
bin/feature
@@ -1,159 +1,17 @@
|
|||||||
#!/usr/bin/env ruby
|
#!/usr/bin/env ruby
|
||||||
|
|
||||||
def error ( msg )
|
require_relative '../lib/feature/start'
|
||||||
puts
|
require_relative '../lib/feature/end'
|
||||||
puts "ERROR: #{msg}"
|
require_relative '../lib/feature/rebase'
|
||||||
puts
|
require_relative '../lib/feature/merge_to'
|
||||||
exit
|
require_relative '../lib/feature/branch'
|
||||||
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
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
command = case ARGV[0]
|
command = case ARGV[0]
|
||||||
when "start" then Start.new(ARGV)
|
when "start" then Feature::Start.new(ARGV)
|
||||||
when "end" then End.new(ARGV)
|
when "end" then Feature::End.new(ARGV)
|
||||||
when "rebase" then Rebase.new(ARGV)
|
when "rebase" then Feature::Rebase.new(ARGV)
|
||||||
when "merge" then MergeTo.new(ARGV)
|
when "merge" then Feature::MergeTo.new(ARGV)
|
||||||
when nil then Branch.new(ARGV)
|
else Feature::Branch.new(ARGV)
|
||||||
else error "USAGE: feature cmd branch"
|
|
||||||
end
|
end
|
||||||
|
|
||||||
command.execute
|
command.execute
|
||||||
|
|||||||
54
lib/feature/base.rb
Normal file
54
lib/feature/base.rb
Normal 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
11
lib/feature/branch.rb
Normal 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
28
lib/feature/end.rb
Normal 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
29
lib/feature/merge_to.rb
Normal 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
29
lib/feature/rebase.rb
Normal 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
23
lib/feature/start.rb
Normal 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
|
||||||
Reference in New Issue
Block a user