3

I am trying to write a script that will use git to get the files that have changed and run commands based on that. I do my development with docker-compose and here is the repo I am working on https://github.com/my-grocery-price-book/www

when running git diff-files from inside the container it lists all the files. when running git diff-files from outside the container list no files.

upgrading git inside the container to the latest version of 2.16 still gives the same output.

Steps to reproduce:

git clone https://github.com/my-grocery-price-book/www.git
docker-compose run --rm app bash
git diff-files

Anyone know what I need to do to get git to behave correctly inside docker?

Grant Speelman
  • 143
  • 1
  • 6
  • This behaviour is also covered by this question/answer: https://stackoverflow.com/a/3879077/358224 – Jason Nov 28 '19 at 04:20

1 Answers1

4

git diff-files uses a hashing algorithm to compare files in the working tree and the index. The files in the working tree have the same hashes as the ones in a local environment.

My hypothesis is that when you run git diff-files in the docker container, the index wasn't reflected properly due to the docker volume mount and hence the output. This does not happen if you were to re-clone the entire repository in the docker container itself. However, if you switch context back to your local environment right after this and do git diff-files, the behavior is reversed (i.e. it lists all the files when you are outside the container, but not inside the container).

Here's what you can do since you might be switching between the docker container and your local environment:

git diff > /dev/null && git diff-files

Use git diff to refresh the index and force the output to /dev/null, then git diff-files will reflect the right output. You need to do this in both environments, especially when switching from one to another. If you feel that the whole command is too long, you might find git aliases to be helpful.

Jay Lim
  • 402
  • 3
  • 6
  • Thanks Jay for the comment. Unfortunately, this did not seem to fix it :( – Grant Speelman Mar 11 '18 at 22:49
  • It's a quick hack. You have to type the entire command every time you want to use `git diff-files`. (i.e. `git diff > /dev/null && git diff-files`) – Jay Lim Mar 12 '18 at 02:12