Skip to content
This repository was archived by the owner on Mar 31, 2022. It is now read-only.

Commit 99d2b20

Browse files
committed
Merge pull request #26 from flavorjones/support-homedir-config
Support `.pairs` in the user's HOME directory, as advertised.
2 parents 007869a + 972f43f commit 99d2b20

File tree

2 files changed

+47
-14
lines changed

2 files changed

+47
-14
lines changed

Diff for: lib/pivotal_git_scripts/git_pair.rb

+14-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
require "pivotal_git_scripts/version"
22
require 'yaml'
33
require 'optparse'
4+
require 'pathname'
45

56
module PivotalGitScripts
67
module GitPair
@@ -126,15 +127,20 @@ def parse_cli_options(argv)
126127
end
127128

128129
def read_pairs_config
129-
pairs_file_path = nil
130-
candidate_file_path = '.pairs'
131-
until pairs_file_path || File.expand_path(candidate_file_path) == '/.pairs' do
132-
if File.exists?(candidate_file_path)
133-
pairs_file_path = candidate_file_path
134-
else
135-
candidate_file_path = File.join("..", candidate_file_path)
136-
end
130+
pairs_file_name = '.pairs'
131+
132+
directory = File.absolute_path(Dir.pwd)
133+
candidate_directories = [directory]
134+
while ! Pathname.new(directory).root? do
135+
directory = File.absolute_path(File.join(directory, ".."))
136+
candidate_directories << directory
137137
end
138+
home = File.absolute_path(ENV["HOME"])
139+
candidate_directories << home unless candidate_directories.include? home
140+
141+
pairs_file_path = candidate_directories.
142+
map { |d| File.join(d, ".pairs") }.
143+
find { |f| File.exists? f }
138144

139145
unless pairs_file_path
140146
raise GitPairException, <<-INSTRUCTIONS

Diff for: spec/cli_spec.rb

+33-6
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def write(file, content)
2727

2828
# use fake home for .ssh hacks
2929
run "mkdir #{dir}/home"
30-
ENV["HOME"] = File.expand_path("#{dir}/home")
30+
ENV["HOME"] = File.absolute_path("#{dir}/home")
3131

3232
Dir.chdir dir do
3333
run "touch a"
@@ -269,13 +269,40 @@ def git_config_value(name, global = false)
269269
end
270270
end
271271

272-
it "fails if it cannot find a pairs file" do
273-
run "git pair ab", :fail => true
272+
context "and without a .pairs file in the home directory" do
273+
it "fails if it cannot find a pairs file" do
274+
run "git pair ab", :fail => true
275+
end
276+
277+
it "prints instructions" do
278+
result = run "git pair ab", :fail => true
279+
result.should include("Could not find a .pairs file. Create a YAML file in your project or home directory.")
280+
end
274281
end
275282

276-
it "prints instructions" do
277-
result = run "git pair ab", :fail => true
278-
result.should include("Could not find a .pairs file. Create a YAML file in your project or home directory.")
283+
context "but a .pairs file in the home directory" do
284+
around do |example|
285+
file = File.join(ENV["HOME"], ".pairs")
286+
write file, <<-YAML.unindent
287+
pairs:
288+
ab: Aa Bb
289+
bc: Bb Cc
290+
cd: Cc Dd
291+
292+
email:
293+
prefix: the-pair
294+
domain: the-host.com
295+
YAML
296+
297+
example.run
298+
299+
FileUtils.rm file
300+
end
301+
302+
it "loads the file" do
303+
result = run "git pair ab"
304+
expect_config result, "Aa Bb", "ab", "[email protected]"
305+
end
279306
end
280307
end
281308
end

0 commit comments

Comments
 (0)