File tree 4 files changed +27
-4
lines changed
4 files changed +27
-4
lines changed Original file line number Diff line number Diff line change @@ -236,6 +236,9 @@ g.index.writable?
236
236
g.repo
237
237
g.dir
238
238
239
+ # ls-tree with recursion into subtrees (list files)
240
+ g.ls_tree(" head" , recursive: true )
241
+
239
242
# log - returns a Git::Log object, which is an Enumerator of Git::Commit objects
240
243
# default configuration returns a max of 30 commits
241
244
g.log
Original file line number Diff line number Diff line change @@ -642,8 +642,8 @@ def revparse(objectish)
642
642
self . lib . revparse ( objectish )
643
643
end
644
644
645
- def ls_tree ( objectish )
646
- self . lib . ls_tree ( objectish )
645
+ def ls_tree ( objectish , opts = { } )
646
+ self . lib . ls_tree ( objectish , opts )
647
647
end
648
648
649
649
def cat_file ( objectish )
Original file line number Diff line number Diff line change @@ -374,10 +374,15 @@ def object_contents(sha, &block)
374
374
end
375
375
end
376
376
377
- def ls_tree ( sha )
377
+ def ls_tree ( sha , opts = { } )
378
378
data = { 'blob' => { } , 'tree' => { } , 'commit' => { } }
379
379
380
- command_lines ( 'ls-tree' , sha ) . each do |line |
380
+ ls_tree_opts = [ ]
381
+ ls_tree_opts << '-r' if opts [ :recursive ]
382
+ # path must be last arg
383
+ ls_tree_opts << opts [ :path ] if opts [ :path ]
384
+
385
+ command_lines ( 'ls-tree' , sha , *ls_tree_opts ) . each do |line |
381
386
( info , filenm ) = line . split ( "\t " )
382
387
( mode , type , sha ) = info . split
383
388
data [ type ] [ filenm ] = { :mode => mode , :sha => sha }
Original file line number Diff line number Diff line change @@ -13,11 +13,26 @@ def test_ls_tree_with_submodules
13
13
repo . add ( 'README.md' )
14
14
repo . commit ( 'Add README.md' )
15
15
16
+ Dir . mkdir ( "repo/subdir" )
17
+ File . write ( 'repo/subdir/file.md' , 'Content in subdir' )
18
+ repo . add ( 'subdir/file.md' )
19
+ repo . commit ( 'Add subdir/file.md' )
20
+
21
+ # ls_tree
22
+ default_tree = assert_nothing_raised { repo . ls_tree ( 'HEAD' ) }
23
+ assert_equal ( default_tree . dig ( "blob" ) . keys . sort , [ "README.md" ] )
24
+ assert_equal ( default_tree . dig ( "tree" ) . keys . sort , [ "subdir" ] )
25
+ # ls_tree with recursion into sub-trees
26
+ recursive_tree = assert_nothing_raised { repo . ls_tree ( 'HEAD' , recursive : true ) }
27
+ assert_equal ( recursive_tree . dig ( "blob" ) . keys . sort , [ "README.md" , "subdir/file.md" ] )
28
+ assert_equal ( recursive_tree . dig ( "tree" ) . keys . sort , [ ] )
29
+
16
30
Dir . chdir ( 'repo' ) do
17
31
assert_child_process_success { `git -c protocol.file.allow=always submodule add ../submodule submodule 2>&1` }
18
32
assert_child_process_success { `git commit -am "Add submodule" 2>&1` }
19
33
end
20
34
35
+
21
36
expected_submodule_sha = submodule . object ( 'HEAD' ) . sha
22
37
23
38
# Make sure the ls_tree command can handle submodules (which show up as a commit object in the tree)
You can’t perform that action at this time.
0 commit comments