Skip to content

Commit 7200b26

Browse files
author
Mike Foley
committed
Don't abort if repo is in a detached HEAD state
When HEAD is detached (e.g., during an interactive rebase) Fit Commit should still run. Since the branch name is unknown, validators that specify branch name will not get run. Fixes #21
1 parent ebbed76 commit 7200b26

File tree

4 files changed

+31
-16
lines changed

4 files changed

+31
-16
lines changed

lib/fit_commit.rb

+1-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ def self.message_path
1717
end
1818

1919
def self.branch_name
20-
ENV["GIT_BRANCH_NAME"] ||
21-
fail("Git branch not found. You might need to re-run `fit-commit install`")
20+
ENV.fetch("GIT_BRANCH_NAME")
2221
end
2322
end

templates/hooks/commit-msg

+1-7
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,7 @@ if [ ! -r "$COMMIT_MESSAGE_PATH" ]; then
1818
exit 0
1919
fi
2020

21-
export GIT_BRANCH_NAME=`git symbolic-ref --short HEAD`
22-
if [ -z "$GIT_BRANCH_NAME" ]; then
23-
>&2 echo "fit-commit: WARNING: Skipping checks because the Git branch cannot be determined."
24-
>&2 echo "fit-commit: Please submit a bug report with the project:"
25-
>&2 echo "fit-commit: https://github.com/m1foley/fit-commit/issues"
26-
exit 0
27-
fi
21+
export GIT_BRANCH_NAME=`git symbolic-ref --short HEAD 2> /dev/null`
2822

2923
# find appropriate Ruby command
3024
if which rbenv > /dev/null 2>&1; then

test/unit/runner_test.rb

+7
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,13 @@ def stringio.tty?
101101
assert_equal FitCommit::Runner::EXIT_CODE_REJECT_COMMIT, call_runner
102102
assert_error_output
103103
end
104+
describe "empty branch name" do
105+
let(:branch_name) { "" }
106+
it "prints errors to stderr and rejects commit" do
107+
assert_equal FitCommit::Runner::EXIT_CODE_REJECT_COMMIT, call_runner
108+
assert_error_output
109+
end
110+
end
104111
end
105112
describe "user forces commit" do
106113
let(:stdin) { fake_tty("y") }

test/unit/validator_loader_test.rb

+22-7
Original file line numberDiff line numberDiff line change
@@ -23,30 +23,45 @@
2323
end
2424

2525
it "loads enabled validators" do
26-
assert validators.any? { |v| v.is_a? FitCommit::Validators::Wip }
26+
assert validators.one? { |v| v.is_a? FitCommit::Validators::Wip }
2727
end
2828

2929
it "doesn't load disabled validators" do
3030
assert validators.none? { |v| v.is_a? FitCommit::Validators::LineLength }
3131
end
3232

3333
describe "non-boolean options for Enabled" do
34-
it "doesn't load validators with a non-matching string/regex Enabled values" do
35-
assert validators.none? { |v| v.is_a? FitCommit::Validators::Frathouse }
34+
describe "branch_name does not match validator" do
35+
it "doesn't load validator" do
36+
assert validators.none? { |v| v.is_a? FitCommit::Validators::Frathouse }
37+
end
3638
end
3739

38-
describe "validator has a matching string Enabled value" do
40+
describe "branch_name matches validator via String" do
3941
let(:branch_name) { "bar" }
4042
it "loads validator" do
41-
assert validators.any? { |v| v.is_a? FitCommit::Validators::Frathouse }
43+
assert validators.one? { |v| v.is_a? FitCommit::Validators::Frathouse }
4244
end
4345
end
4446

45-
describe "validator has a matching regex Enabled value" do
47+
describe "branch_name matches validator via regex" do
4648
let(:branch_name) { "bazzz" }
4749
it "loads validator" do
48-
assert validators.any? { |v| v.is_a? FitCommit::Validators::Frathouse }
50+
assert validators.one? { |v| v.is_a? FitCommit::Validators::Frathouse }
4951
end
5052
end
5153
end
54+
55+
describe "branch_name is blank" do
56+
let(:branch_name) { "" }
57+
it "loads enabled validators" do
58+
assert validators.one? { |v| v.is_a? FitCommit::Validators::Wip }
59+
end
60+
it "doesn't load disabled validators" do
61+
assert validators.none? { |v| v.is_a? FitCommit::Validators::LineLength }
62+
end
63+
it "doesn't load validators that have non-boolean options for Enabled" do
64+
assert validators.none? { |v| v.is_a? FitCommit::Validators::Frathouse }
65+
end
66+
end
5267
end

0 commit comments

Comments
 (0)