修复git diff-files列出docker中所有文件的问题 [英] fix git diff-files listing all files in docker

查看:76
本文介绍了修复git diff-files列出docker中所有文件的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个脚本,该脚本将使用git获取已更改的文件并基于该命令运行命令。我使用docker-compose进行开发,这是我正在处理的仓库 https:/ /github.com/my-grocery-price-book/www

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

在运行 git diff-files 从容器内部列出所有文件。从容器外部运行 git diff-files
没有文件。

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.

升级git里面的git容器到最新版本2.16仍会提供相同的输出。

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

重现步骤:

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

有人知道我需要做些什么才能使git在docker中正确运行吗?

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

推荐答案

git diff-files 使用哈希算法比较工作树和索引中的文件。工作树中的文件与本地环境中的文件具有相同的哈希值。

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.

我的假设是,当您运行 git diff-files时在docker容器中,由于docker卷安装和输出,索引无法正确反映。如果要在Docker容器本身中重新克隆整个存储库,则不会发生这种情况。但是,如果在此之后立即将上下文切换回本地环境并执行 git diff-files ,则行为会相反(即,当您不在列表中时,它将列出所有文件。

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).

这是您可以做的,因为您可能正在docker容器和本地环境之间切换:

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

使用 git diff 刷新索引并将输出强制为 / dev / null ,然后 git diff-files 将反映正确的输出。您需要在两种环境中都执行此操作,尤其是从一种环境切换到另一种环境时。如果您觉得整个命令太长,则可能会发现 git别名以提供帮助。

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.

这篇关于修复git diff-files列出docker中所有文件的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆