3

I would like to mass-update files in a script, using the following command:

grep -l -r -E "..." . | while read -r FILE ; do
    # How to do the next line only for files that are not in submodules?
    sed -i '' -E '...' "${FILE}"
done

However, I have git submodules and do not want to update files contained in those submodules. Short of maintaining a blacklist of paths where the submodules are, is there any way to check if a file belongs to a submodule or skip them altogether?

Léo Natan
  • 56,823
  • 9
  • 150
  • 195
  • I'm not sure but maybe this helps: https://stackoverflow.com/questions/2405305/how-to-tell-if-a-file-is-git-tracked-by-shell-exit-code – kvantour Jan 08 '19 at 11:44
  • The problem with that approach is that all the files are tracked, just in a submodule rather than the main repo. – Léo Natan Jan 08 '19 at 11:45
  • 3
    You could use `git grep` rather than regular grep. – torek Jan 08 '19 at 11:50
  • @torek It works exactly as I need. Care to add an answer? – Léo Natan Jan 08 '19 at 12:31
  • Technically, it's not an answer to the question you *asked*. :-) (The linked question *is*, and should work: the test for "is a tracked file" will use the *current* Git repo's index, not the submodule repo's index. It's just less efficient than not looking at submodule files in the first place.) – torek Jan 08 '19 at 12:49
  • I changed the question. Your answer is what I _needed_. :-) – Léo Natan Jan 08 '19 at 12:51

1 Answers1

1

If you can use git grep directly—Git's grep is pretty capable, but there may be minor differences from the system variant to watch out for—this sidesteps the problem nicely since git grep only looks at the right files in the first place.

(Mostly, it should just be a matter of replacing grep with git grep.)

torek
  • 448,244
  • 59
  • 642
  • 775