Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reserved branch warning #82

Merged
merged 13 commits into from
May 17, 2016
1 change: 1 addition & 0 deletions .ruby-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2.2.4
20 changes: 18 additions & 2 deletions lib/octopolo/git.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ class Git
NO_BRANCH = "(no branch)"
DEFAULT_DIRTY_MESSAGE = "Your Git index is not clean. Commit, stash, or otherwise clean up the index before continuing."
DIRTY_CONFIRM_MESSAGE = "Your Git index is not clean. Do you want to continue?"
RESERVED_BRANCH_MESSAGE = "Please choose another name for your new branch."
RESERVED_BRANCH_CONFIRM_MESSAGE = "Your new branch may be misidentified as a reserved branch based on its name. Do you want to continue?"
# we use date-based tags, so look for anything starting with a 4-digit year
RELEASE_TAG_FILTER = /^\d{4}.*/
RECENT_TAG_LIMIT = 9
Expand All @@ -17,6 +19,9 @@ class Git
STAGING_PREFIX = "staging"
QAREADY_PREFIX = "qaready"

# To check if the new branch's name starts with one of these
RESERVED_BRANCH_PREFIXES = [ DEPLOYABLE_PREFIX, STAGING_PREFIX, QAREADY_PREFIX ]

@resolver_used = nil

include CLIWrapper
Expand Down Expand Up @@ -66,8 +71,8 @@ def self.current_branch
# Public: Determine if current_branch is reserved
#
# Returnsa boolean value
def self.reserved_branch?
!(current_branch =~ /^(?:#{Git::STAGING_PREFIX}|#{Git::DEPLOYABLE_PREFIX}|#{Git::QAREADY_PREFIX})/).nil?
def self.reserved_branch?(branch=current_branch)
!(branch =~ /^(?:#{Git::RESERVED_BRANCH_PREFIXES.join('|')})/).nil?
end

# Public: Check out the given branch name
Expand Down Expand Up @@ -129,6 +134,16 @@ def self.alert_dirty_index(message)
raise DirtyIndex
end

def self.alert_reserved_branch(message)
cli.say " "
cli.say message
cli.say " "
cli.say "Here's the list of the reserved branch prefixes:"
cli.say RESERVED_BRANCH_PREFIXES.join(" ")
cli.say " "
raise ReservedBranch
end

# Public: Merge the given remote branch into the current branch
def self.merge(branch_name)
Git.if_clean do
Expand Down Expand Up @@ -291,5 +306,6 @@ def self.stale_branches_to_ignore(additional_branches=[])
MergeFailed = Class.new(StandardError)
NoBranchOfType = Class.new(StandardError)
DirtyIndex = Class.new(StandardError)
ReservedBranch = Class.new(StandardError)
end
end
9 changes: 8 additions & 1 deletion lib/octopolo/scripts/new_branch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module Scripts
class NewBranch
include ConfigWrapper
include GitWrapper
include CLIWrapper

attr_accessor :new_branch_name
attr_accessor :source_branch_name
Expand All @@ -22,7 +23,13 @@ def initialize(new_branch_name=nil, source_branch_name=nil)
# Public: Perform the script
def execute
raise ArgumentError unless new_branch_name
git.new_branch(new_branch_name, source_branch_name)
if !git.reserved_branch?(new_branch_name) || cli.ask_boolean(Git::RESERVED_BRANCH_CONFIRM_MESSAGE)
git.new_branch(new_branch_name, source_branch_name)
else
message = Git::RESERVED_BRANCH_MESSAGE
git.alert_reserved_branch message
exit 1
end
end

# Public: Provide a default value if none is given
Expand Down
28 changes: 25 additions & 3 deletions spec/octopolo/scripts/new_branch_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ module Scripts
describe NewBranch do
let(:config) { stub(:config, :deploy_branch => "production") }
let(:git) { stub(:Git) }
let(:cli) { stub(:Cli) }
let(:new_branch_name) { stub(:string) }
let(:custom_source_branch) { stub(:string) }

subject { NewBranch }

before do
NewBranch.any_instance.stub(:config => config, :git => git)
NewBranch.any_instance.stub(:config => config, :git => git, :cli => cli)
end

context "::execute" do
Expand All @@ -22,15 +23,36 @@ module Scripts
end
end

context "with a only new branch name given" do

context "with reserved new branch name" do
it "exits when aborted" do
allow(git).to receive(:reserved_branch?) { true }
allow(cli).to receive(:ask_boolean) { false }
allow(cli).to receive(:say).with(anything)
git.should_receive(:alert_reserved_branch)
expect { subject.execute(new_branch_name) }.to raise_error(SystemExit)
end

it "proceeds when confirmed" do
allow(git).to receive(:reserved_branch?) { true }
allow(cli).to receive(:ask_boolean) { true }
allow(cli).to receive(:say).with(anything)
git.should_receive(:new_branch).with(new_branch_name, "production")
subject.execute(new_branch_name)
end
end

context "with only new branch name given" do
it "delegates to Git.new_branch" do
allow(git).to receive(:reserved_branch?) { false }
git.should_receive(:new_branch).with(new_branch_name, "production")
subject.execute(new_branch_name)
end
end

context "with a only new branch name given" do
context "with new and source branch names given" do
it "delegates to Git.new_branch" do
allow(git).to receive(:reserved_branch?) { false }
git.should_receive(:new_branch).with(new_branch_name, custom_source_branch)
subject.execute(new_branch_name, custom_source_branch)
end
Expand Down