Skip to content

Make Git::Base#ls_tree handle commit objects #643

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/git/lib.rb
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ def object_contents(sha, &block)
end

def ls_tree(sha)
data = {'blob' => {}, 'tree' => {}}
data = { 'blob' => {}, 'tree' => {}, 'commit' => {} }

command_lines('ls-tree', sha).each do |line|
(info, filenm) = line.split("\t")
Expand Down
5 changes: 5 additions & 0 deletions tests/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,9 @@ def assert_command_line(expected_command_line, git_cmd, git_cmd_args)

assert_equal(expected_command_line, actual_command_line)
end

def assert_child_process_success(&block)
yield
assert_equal 0, $CHILD_STATUS.exitstatus, "Child process failed with exitstatus #{$CHILD_STATUS.exitstatus}"
end
end
31 changes: 31 additions & 0 deletions tests/units/test_ls_tree.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
require 'test_helper'

class TestLsTree < Test::Unit::TestCase
def test_ls_tree_with_submodules
in_temp_dir do
submodule = Git.init('submodule', initial_branch: 'main')
File.write('submodule/README.md', '# Submodule')
submodule.add('README.md')
submodule.commit('Add README.md')

repo = Git.init('repo', initial_branch: 'main')
File.write('repo/README.md', '# Main Repository')
repo.add('README.md')
repo.commit('Add README.md')

Dir.chdir('repo') do
assert_child_process_success { `git -c protocol.file.allow=always submodule add ../submodule submodule 2>&1` }
assert_child_process_success { `git commit -am "Add submodule" 2>&1` }
end

expected_submodule_sha = submodule.object('HEAD').sha

# Make sure the ls_tree command can handle submodules (which show up as a commit object in the tree)
tree = assert_nothing_raised { repo.ls_tree('HEAD') }
actual_submodule_sha = tree.dig('commit', 'submodule', :sha)

# Make sure the submodule commit was parsed correctly
assert_equal(expected_submodule_sha, actual_submodule_sha, 'Submodule SHA was not returned')
end
end
end