3

I wrote some script that basically uses git log to extract some information for a file in the workspace. That works OK, but it fails if the file is part of a submodule.

As I found out, git log -- file returns nothing and exits with 0 if the file is part of a submodule; however when I cd into the submodule's directory, git log will work as expected.

So my idea was to detect whether a given file is part of a submodule, and if so: "Which submodule?". How can I get that information?

Update

Actually git log outputs data for the submodule once the new submodule is committed. But still the log info for a file in the submodule is different depending from where the git log command is started:

When started from the main workspace it refers to the commit of the submodule, but when run from the submodule directory, it refers to the commit inside the submodule.

So the question is still valid ;-)

U. Windl
  • 3,480
  • 26
  • 54
  • 1
    https://stackoverflow.com/q/59017611/6309 and https://stackoverflow.com/a/52047313/6309 can help. – VonC Aug 31 '21 at 07:17
  • @VonC This question is somewhat different (while still similar): I'm not in a subdirectory I want to check, but I have a filename to check. Of course I could `cd` to the `dirname` of each file and then do the check suggested, but that does not look like an elegant solution. Agreed, if nothing better appears, I'll try that. – U. Windl Aug 31 '21 at 09:16
  • OK. Using the folder of the file is the recommended approach. I don't know of a `git` command checking a submodule ownership directly from a file. – VonC Aug 31 '21 at 09:28
  • I'd argue that this is a bug: `git log -- path/to/file` where `path` or `to` is a submodule should produce at least a warning, maybe even an error. – torek Aug 31 '21 at 14:32

0 Answers0