GIT: search for a list of files (e.g. using git ls files), including submodules

I was trying to figure out how to get a list of all the files in the git repository, including those contained in the submodules. Currently, git ls-files will provide a directory of top-level submodules, but not the files contained in the submodule. In further research, I found that using git submodule , you can recursively find all submodules, and then go to git ls-files using:

 git submodule --quiet foreach --recursive "git ls-files" 

The only problem is that the results are the path from the submodule, but I need the full path from the repo. So for the following

eg. / some / path / to / gitrepo / source / submodule / [file1, file2]

What do I see:

 file1 file2 

What I would like to see:

 source/submodule/file1 source/submodule/file2 

Is there any way to do this? There are some predefined variables from the documentation ($ name, $ path, $ sha1 and $ toplevel), but I'm not sure how to use them to get the desired results.

+6
source share
2 answers

Another approach is possible with Git 2.11+ (Q4 2016)

 git ls-files --recurse-submodules 

See commit 75a6315 , commit 07c01b9 , commit e77aa33 , commit 74866d7 (October 7, 2016) Brandon Williams ( mbrandonw ) .
(merger of Junio ​​C Hamano - gitster - at commit 1c2b1f7 , October 26, 2016)

ls-files : possibly recursion into submodules

" git ls-files " recognized " --recurse-submodules ", which can be used to obtain a list of monitored files through submodules (i.e. this only works with the --cached option, and not to list unplayable or ignored files).

This would be a useful tool for sitting on the side upstream of the channel, which is read using xargs to work with all the working files of the tree from a top-level superproject.

As shown in this test , the output will include the full path to the file, starting with the main parent repo.

The git ls-files documentation now includes:

 --recurse-submodules 

Recursively calls ls files on each submodule in the repository.
Currently, only -cached mode is supported.


Git 2.13 (Q2 2017) adds ls-files --recurse-submodules :

See commit 2cfe66a , commit 2e5d650 (April 13, 2017) by Jacob Keller ( jacob-keller ) .
(merged Junio ​​C Hamano - gitster - in commit 2d646e3 , April 24, 2017)

ls-files : fix recurse-submodules with nested submodules

Since commit e77aa33 ("ls-files: it is not necessary to overwrite in submodules", 2016-10-07, Git 2.11) ls-files known as submodules when displaying files.

Unfortunately, this fails in some cases, including when nesting more than one submodule, called from within the submodule, which itself has submodules, or when the GIT_DIR environment variable is GIT_DIR .

Prior to committing b58a68c (" setup : allow the prefix to be passed to Git commands", 2017-03-17, Git 2.13-rc0), this led to an error indicating that --prefix and --super-prefix were incompatible.

After this commit, instead, the cycle loop is forever with the set of GIT_DIR for the parent and constantly reads the parent files of the submodules and is recursively maintained.

Fix this by properly preparing the environment for the submodules when setting up the child process. This is similar to how other commands, such as grep, behave.

+7
source

Take a look at the git submodule documentation that says:

foreach

Computes an arbitrary shell command in each computed submodule. The command has access to the variables $name , $path , $sha1 and $toplevel : $name is the name of the corresponding section of the submodule in .gitmodules, $path is the name of the submodule directory relative to the superproject, $sha1 is the commit written to the superproject and $toplevel is the absolute path to the top level of a super project.

Given the above information, you can do something like:

 git submodule foreach 'git ls-files | sed "s|^|$path/|"' 

In this example, we simply take the output from git ls-files in the submodule and use sed to add the value of $path , which is the path of the submodule relative to the parent project.

+3
source

All Articles