英文:
Creating or ignoring missing empty directories in Eclipse
问题
我最近将一个项目从SVN迁移到Git。
现在我的Eclipse抱怨目录src/main/resources
丢失了。
在SVN中,该目录是一个空目录,但是Git似乎完全忽略空目录。
我可以告诉Eclipse:
- 要么在检出项目时创建该目录
- 要么完全忽略这个“错误”
英文:
I recently moved a project from SVN to Git.
Now my Eclipse complains that the directory src/main/resources
is missing.
The directory was an empty directory in SVN, but Git seems to ignore empty directories entirely.
Can I tell Eclipse to
- either create the directory when checking out the project
- or ignore the "error" completely
?
答案1
得分: 1
Sure, here is the translation of the provided text:
我不想将虚假文件放入一个目录。我想在Eclipse端解决这个问题。
You can tell Eclipse to ignore certain problems.
- 转到
Window -> Preferences -> Validation
。 - 在验证页面,您可以启用或禁用各个验证器,设置验证消息的严重程度以及自定义其他验证行为。找到导致问题的验证器并禁用它,或根据需要调整其设置。
但如果没有匹配的验证器,那么...解决方案在Git端。
Git提供了一种称为 gitattributes
的机制,可以用于在检出和其他事件发生时指定自定义操作。filter
属性 允许您定义一对 "smudge" 和 "clean" 脚本。"smudge" 脚本在检出时运行,而 "clean" 脚本在文件被暂存(即添加到索引)时运行。
在您的情况下,您可以创建一个 "smudge" 脚本来创建所需的目录。
-
首先,在您的存储库的
.git/config
文件中定义一个filter
:[filter "createDir"] smudge = 'mkdir -p src/main/resources && cat' clean = cat
这设置了一个名为 "createDir" 的过滤器。"smudge" 命令创建
src/main/resources
目录(如果尚不存在),然后通过cat
未修改地传递文件内容。"clean" 命令只是不改变地传递文件内容。 -
然后,在您的
.gitattributes
文件中将过滤器应用于存储库中的已知文件(例如,如果您有一个README.md
文件):README.md filter=createDir
这将 "createDir" 过滤器应用于您的文件
README.md
。
由于涉及到 本地 Git 配置,这个过程需要由开发人员激活:
git config --local filter.createDir.smudge 'mkdir -p src/main/resources && cat'
git config --local filter.createDir.clean cat
git restore --source=HEAD .
英文:
> I don't want to put fake files into a directory. I want to solve the problem on the Eclipse side
You can tell Eclipse to ignore certain problems.
- Go to
Window -> Preferences -> Validation
. - In the Validation page, you can enable or disable individual validators, set the severity of validation messages, and customize other validation behavior.
Find the validator that's causing the issue and disable it or adjust its settings as necessary.
But if there is no matching validator, then... the solution is on Git side.
Git provides a mechanism called gitattributes
that can be used to specify custom actions upon checkout and other events.
The filter
attribute allows you to define a pair of "smudge
" and "clean
" scripts. The smudge
script is run on checkout, and the clean
script is run when files are staged (i.e., added to the index).
In your case, you could create a smudge
script that creates the directory you need.
-
Start with defining a
filter
in your repository's.git/config
file:[filter "createDir"] smudge = 'mkdir -p src/main/resources && cat' clean = cat
This sets up a filter named "createDir". The smudge command creates the
src/main/resources
directory (if it doesn't exist already) and then passes the file content throughcat
unmodified. The clean command just passes the file content through unchanged. -
Then Apply the filter to a known file of your repository in your
.gitattributes
file (for instanceREADME.md
if you have one):README.md filter=createDir
This applies the "
createDir
" filter to your fileREADME.md
.
Since it involves the local Git config, this process needs to be activated by the developer:
git config --local filter.createDir.smudge 'mkdir -p src/main/resources && cat'
git config --local filter.createDir.clean cat
git restore --source=HEAD .
答案2
得分: 0
以下是翻译好的部分:
一般来说,你可以通过各种方式来利用Git,但是在这里,看起来你试图将Git用作部署服务器,但实际上它不是(就像Blender离CAD程序只差一点一样,重点不够准确,如果朝这个方向发展会损害其主要业务)。
让Git创建一个空目录的方法是告诉它应该有一个单独的工作树在那里。
z8=00000000
git update-index --add --cacheinfo 160000,$z8$z8$z8${z8}00000001,path/to/empty/dir
然后,当你检出该目录的提交版本时,Git会在那里创建一个空目录。我是否已经足够使用"hack"这个词了?正确的做法是使用设置脚本,我在我的makefiles中有一个"setup"目标,所有其他目标最终都依赖于它。
编辑:要查找所有空目录,可以使用find -empty
。
find -type d -name .git -prune -o -type d -empty -exec git update-index etc {} \;
或者使用-exec
执行你想要对它们执行的操作。
英文:
You can generally hack Git into doing anything you want, and so it is here, but it looks like you're trying to use Git as a deployment server, which it is not (in much the same way as Blender is tantalizingly close to a CAD program, the focus is just wrong enough that moving that direction would hurt its main business).
The hack for getting Git to create an empty directory is to tell it there's supposed to be a separate work tree there.
z8=00000000
git update-index --add --cacheinfo 160000,$z8$z8$z8${z8}00000001,path/to/empty/dir
and then when you check the committed version of that out Git will make an empty directory there. Have I used the word "hack" enough? The right way to do this is to have a setup script, I have a "setup" target in my makefiles and all the other targets eventually depend on that.
edit: to find all empty directories, find -empty
.
find -type d -name .git -prune -o -type d -empty -exec git update-index etc {} \;
or -exec
whatever you want to do with them.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论