Skip to content

Commit 14fae4a

Browse files
James Couballjcouball
James Couball
authored andcommitted
Calculate the default for index relative to git_dir instead of work_tree
Signed-off-by: James Couball <jcouball@yahoo.com>
1 parent d1908f6 commit 14fae4a

File tree

3 files changed

+88
-4
lines changed

3 files changed

+88
-4
lines changed

lib/git/base.rb

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def self.init(working_dir, opts = {})
5858
# Submodules have a .git *file* not a .git folder.
5959
# This file's contents point to the location of
6060
# where the git refs are held (In the parent repo)
61-
if File.file?('.git')
61+
if opts[:working_directory] && File.file?(File.join(opts[:working_directory], '.git'))
6262
git_file = File.open('.git').read[8..-1].strip
6363
opts[:repository] = git_file
6464
opts[:index] = git_file + '/index'
@@ -72,13 +72,24 @@ def self.init(working_dir, opts = {})
7272
# opens a new Git Project from a working directory
7373
# you can specify non-standard git_dir and index file in the options
7474
def self.open(working_dir, opts={})
75-
self.new({:working_directory => working_dir}.merge(opts))
75+
opts[:working_directory] ||= working_dir
76+
opts[:repository] ||= File.join(opts[:working_directory], '.git')
77+
78+
# Submodules have a .git *file* not a .git folder.
79+
# This file's contents point to the location of
80+
# where the git refs are held (In the parent repo)
81+
if opts[:working_directory] && File.file?(File.join(opts[:working_directory], '.git'))
82+
git_file = File.open('.git').read[8..-1].strip
83+
opts[:repository] = git_file
84+
opts[:index] = git_file + '/index'
85+
end
86+
self.new(opts)
7687
end
7788

7889
def initialize(options = {})
7990
if working_dir = options[:working_directory]
8091
options[:repository] ||= File.join(working_dir, '.git')
81-
options[:index] ||= File.join(working_dir, '.git', 'index')
92+
options[:index] ||= File.join(options[:repository], 'index')
8293
end
8394
if options[:log]
8495
@logger = options[:log]

tests/test_helper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def set_file_paths
2626

2727
teardown
2828
def git_teardown
29-
if @tmp_path
29+
if instance_variable_defined?(:@tmp_path)
3030
FileUtils.rm_r(@tmp_path)
3131
end
3232
end

tests/units/test_git_dir.rb

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#!/usr/bin/env ruby
2+
3+
require File.dirname(__FILE__) + '/../test_helper'
4+
5+
class TestGitDir < Test::Unit::TestCase
6+
def test_index_calculated_from_git_dir
7+
Dir.mktmpdir do |work_tree|
8+
Dir.mktmpdir do |git_dir|
9+
git = Git.open(work_tree, repository: git_dir)
10+
11+
assert_equal(work_tree, git.dir.path)
12+
assert_equal(git_dir, git.repo.path)
13+
14+
# Since :index was not given in the options to Git#open, index should
15+
# be defined automatically based on the git_dir.
16+
#
17+
index = File.join(git_dir, 'index')
18+
assert_equal(index, git.index.path)
19+
end
20+
end
21+
end
22+
23+
# Test the case where the git-dir is not a subdirectory of work-tree
24+
#
25+
def test_git_dir_outside_work_tree
26+
Dir.mktmpdir do |work_tree|
27+
Dir.mktmpdir do |git_dir|
28+
# Setup a bare repository
29+
#
30+
source_git_dir = File.expand_path(File.join('tests', 'files', 'working.git'))
31+
FileUtils.cp_r(Dir["#{source_git_dir}/*"], git_dir, preserve: true)
32+
git = Git.open(work_tree, repository: git_dir)
33+
34+
assert_equal(work_tree, git.dir.path)
35+
assert_equal(git_dir, git.repo.path)
36+
37+
# Reconstitute the work tree from the bare repository
38+
#
39+
branch = 'master'
40+
git.checkout(branch, force: true)
41+
42+
# Make sure the work tree contains the expected files
43+
#
44+
expected_files = %w[ex_dir example.txt]
45+
actual_files = Dir[File.join(work_tree, '*')].map { |f| File.basename(f) }
46+
assert_equal(expected_files, actual_files)
47+
48+
# None of the expected files should have a status that says it has been changed
49+
#
50+
expected_files.each do |file|
51+
assert_equal(false, git.status.changed?(file))
52+
end
53+
54+
# Change a file and make sure it's status says it has been changed
55+
#
56+
file = 'example.txt'
57+
File.open(File.join(work_tree, file), "a") { |f| f.write("A new line") }
58+
assert_equal(true, git.status.changed?(file))
59+
60+
# Add and commit the file and then check that:
61+
# * the file is not flagged as changed anymore
62+
# * the commit was added to the log
63+
#
64+
max_log_size = 100
65+
assert_equal(64, git.log(max_log_size).size)
66+
git.add(file)
67+
git.commit('This is a new commit')
68+
assert_equal(false, git.status.changed?(file))
69+
assert_equal(65, git.log(max_log_size).size)
70+
end
71+
end
72+
end
73+
end

0 commit comments

Comments
 (0)