Skip to content

Commit a79935e

Browse files
authored
Make Git::Base#ls_tree handle commit objects (ruby-git#643)
Signed-off-by: James Couball <jcouball@yahoo.com>
1 parent 6db3ea4 commit a79935e

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

lib/git/lib.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ def object_contents(sha, &block)
338338
end
339339

340340
def ls_tree(sha)
341-
data = {'blob' => {}, 'tree' => {}}
341+
data = { 'blob' => {}, 'tree' => {}, 'commit' => {} }
342342

343343
command_lines('ls-tree', sha).each do |line|
344344
(info, filenm) = line.split("\t")

tests/test_helper.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,4 +170,9 @@ def assert_command_line(expected_command_line, git_cmd, git_cmd_args)
170170

171171
assert_equal(expected_command_line, actual_command_line)
172172
end
173+
174+
def assert_child_process_success(&block)
175+
yield
176+
assert_equal 0, $CHILD_STATUS.exitstatus, "Child process failed with exitstatus #{$CHILD_STATUS.exitstatus}"
177+
end
173178
end

tests/units/test_ls_tree.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
require 'test_helper'
2+
3+
class TestLsTree < Test::Unit::TestCase
4+
def test_ls_tree_with_submodules
5+
in_temp_dir do
6+
submodule = Git.init('submodule', initial_branch: 'main')
7+
File.write('submodule/README.md', '# Submodule')
8+
submodule.add('README.md')
9+
submodule.commit('Add README.md')
10+
11+
repo = Git.init('repo', initial_branch: 'main')
12+
File.write('repo/README.md', '# Main Repository')
13+
repo.add('README.md')
14+
repo.commit('Add README.md')
15+
16+
Dir.chdir('repo') do
17+
assert_child_process_success { `git -c protocol.file.allow=always submodule add ../submodule submodule 2>&1` }
18+
assert_child_process_success { `git commit -am "Add submodule" 2>&1` }
19+
end
20+
21+
expected_submodule_sha = submodule.object('HEAD').sha
22+
23+
# Make sure the ls_tree command can handle submodules (which show up as a commit object in the tree)
24+
tree = assert_nothing_raised { repo.ls_tree('HEAD') }
25+
actual_submodule_sha = tree.dig('commit', 'submodule', :sha)
26+
27+
# Make sure the submodule commit was parsed correctly
28+
assert_equal(expected_submodule_sha, actual_submodule_sha, 'Submodule SHA was not returned')
29+
end
30+
end
31+
end

0 commit comments

Comments
 (0)