linuxgitshellgit-plumbing

Grep all the same name files' properties across all the git branches using pipelines


I have a git repo of configuration files segregated by branches, e.g.:

refs/heads/branch1, file - settings.properties
refs/heads/branch2, file - settings.properties

etc.

I'm trying to grep certain property of every each of the settings.properties file in every each of the repository:

git for-each-ref refs/heads --shell --format=‘%(refname:short)’ | xargs -n1 git checkout | cat settings.properties | grep ‘host.name’

The first command gives me the list of my branches, the second one checks me out to every branch one after another and I expect 3rd command cat the file and 4th to grep certain property. First 2 commands work just fine but if I run the whole thing it just greps host.name only for the first branch.

I'm obviously missing something essential about the pipelines. I know I can write it as a shell script and do all this in a loop, but I would like to keep the 'pipeline' approach, because I may often need to cat different files and grep different properties and wouldn't want to deal with passing parameters into the script


Solution

  • You don't need to check out each branch to get the information about that file. You can instead use git cat-file to show the contents of the file on that branch instead.

    So you could do something a little like this (untested):

    git for-each-ref refs/heads --shell --format='%(refname:short)' | \
        xargs -n1 -I{} git cat-file blob {}:settings.properties | grep 'host.name'
    

    Or if you wanted it to be even shorter, you could just use git grep directly:

    git for-each-ref refs/heads --shell --format='%(refname:short)' | \
        xargs -n1 -I{} git --no-pager grep host.name {}:settings.properties