Skip to content

Commit 48658e6

Browse files
committed
throw an exception if a system call fails and don't just plow through executing commands
1 parent d928ba2 commit 48658e6

3 files changed

Lines changed: 56 additions & 6 deletions

File tree

bin/code

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ command :push, :p do |c|
4747
end
4848
end
4949

50+
command :hotfix, :fix do |c|
51+
c.action do |global_options, options, args|
52+
hotfix_name = args.join('-')
53+
$git.hotfix hotfix_name
54+
end
55+
end
56+
5057
desc 'Cleanup after a feature has been merged through GitHub'
5158
command :finish do |c|
5259
c.action do |global_options, options, args|

lib/code/git.rb

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,36 @@ def path
1818
def start(feature)
1919
raise FeatureExistsError, "Feature #{feature} already exists" if Branch.exists?(feature)
2020

21-
development_branch.checkout unless current_branch.development?
22-
development_branch.pull
21+
with_stash do
22+
development_branch.checkout unless current_branch.development?
23+
development_branch.pull
24+
25+
Branch.create(feature).checkout
26+
end
27+
end
28+
29+
def stash
30+
System.call('stash')
31+
end
32+
33+
def unstash
34+
System.call('stash pop')
35+
end
2336

24-
Branch.create(feature).checkout
37+
def with_stash
38+
if uncommitted_changes?
39+
stash
40+
yield
41+
unstash
42+
else
43+
yield
44+
end
45+
end
46+
47+
def hotfix(name)
48+
start name
49+
commit name if uncommitted_changes?
50+
publish name
2551
end
2652

2753
def switch(*patterns)
@@ -38,6 +64,11 @@ def cancel
3864
end
3965
end
4066

67+
def commit(message)
68+
System.call 'add -A'
69+
System.call "commit -m \"#{message}\""
70+
end
71+
4172
def publish(message = '')
4273
raise NotOnFeatureBranchError, 'Must be on a feature branch to publish your code' unless current_branch.feature?
4374
raise UncommittedChangesError, 'You have uncommitted changes' if uncommitted_changes?
@@ -68,11 +99,11 @@ def finish
6899
fetch
69100
development_branch.pull
70101

71-
branch.delete
102+
branch.delete!
72103
end
73104

74105
def uncommitted_changes?
75-
System.result('git diff --name-status') != ''
106+
System.result('git status --porcelain') != ''
76107
end
77108

78109
def checkout(branch_name)

lib/code/system.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,15 @@ module Code
22
module System
33
extend self
44

5+
CommandFailedError = ::Class.new(StandardError)
6+
57
COLORS = {black: 30, red: 31, green: 32, yellow: 33, blue: 34, magenta: 35, teal: 36}
68

9+
def prompt(*args)
10+
print(*args)
11+
gets
12+
end
13+
714
def error(message)
815
abort red(message)
916
end
@@ -19,6 +26,11 @@ def open(item)
1926
def exec(script)
2027
puts green(script)
2128
%x[#{script}]
29+
raise CommandFailedError, red("command failed: #{script}") if command_failed?
30+
end
31+
32+
def command_failed?
33+
not $?.success?
2234
end
2335

2436
def puts(text)
@@ -43,7 +55,7 @@ def green(script)
4355
end
4456

4557
def red(script)
46-
color script, :magenta
58+
color script, :red
4759
end
4860

4961
end

0 commit comments

Comments
 (0)