From 27d29bd98596add617c1386a8f46a6f262f15e5a Mon Sep 17 00:00:00 2001 From: Josh Hagins Date: Mon, 1 Jun 2015 16:00:43 -0400 Subject: [PATCH] Only check against existing files if not initial commit Otherwise you get 'fatal: Not a valid object name HEAD' since `git ls-tree` requires an existing rev. --- lib/overcommit/hook/pre_commit/case_conflicts.rb | 9 +++++++-- spec/overcommit/hook/pre_commit/case_conflicts_spec.rb | 1 + 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/overcommit/hook/pre_commit/case_conflicts.rb b/lib/overcommit/hook/pre_commit/case_conflicts.rb index 2adb618e..edac2679 100644 --- a/lib/overcommit/hook/pre_commit/case_conflicts.rb +++ b/lib/overcommit/hook/pre_commit/case_conflicts.rb @@ -3,8 +3,13 @@ module Overcommit::Hook::PreCommit # Adapted from https://github.com/pre-commit/pre-commit-hooks class CaseConflicts < Base def run - paths = Set.new(applicable_files.map { |file| File.dirname(file) + File::SEPARATOR }) - repo_files = Set.new(Overcommit::GitRepo.list_files(paths.to_a) + applicable_files) + repo_files = Set.new(applicable_files) + + unless Overcommit::GitRepo.initial_commit? + paths = repo_files.map { |file| File.dirname(file) + File::SEPARATOR } + repo_files += Overcommit::GitRepo.list_files(paths) + end + conflict_hash = repo_files.classify(&:downcase). select { |_, files| files.size > 1 } conflict_files = applicable_files. diff --git a/spec/overcommit/hook/pre_commit/case_conflicts_spec.rb b/spec/overcommit/hook/pre_commit/case_conflicts_spec.rb index 8d63f014..0d7281d0 100644 --- a/spec/overcommit/hook/pre_commit/case_conflicts_spec.rb +++ b/spec/overcommit/hook/pre_commit/case_conflicts_spec.rb @@ -6,6 +6,7 @@ subject { described_class.new(config, context) } before do + Overcommit::GitRepo.stub(:initial_commit?).and_return(false) Overcommit::GitRepo.stub(:list_files).and_return(%w[foo]) end