git - Hook 还是不 Hook - git

标签 git hook tr gitattributes

我们定制的 IDE 输出带有编码的 XML 文件,使它们看起来像二进制文件。这些文件的差异和 merge 失败。

我们可以使用 tr 命令创建这些文件的 ASCII 版本。我希望这些文件在提交之前始终自动转换为 ascii。

我拿起了使用 Git 进行版本控制的副本,它全心全意地警告我不要使用钩子(Hook),除非我真的需要。

我应该为此目的使用钩子(Hook)吗?或者我可以做其他事情来确保文件在提交之前总是被转换吗?

带有 msysgit 1.7.4 的 Windows XP

--=更新=--

感谢大家的帮助和耐心。期待this question我尝试了以下方法,但它不起作用:

echo "*.xrp    filter=xrp" > .git/info/attributes
git config --global filter.xrp.clean 'tr -cd '\''\11\12\15\40-\176'\'''
git config --global filter.xrp.smudge cat
git checkout --force

此配置更改后文件保持不变。即使我删除并重新 checkout 。

配置为清理任务的 tr 命令确实独立工作。证明:

$ head -n 1 cashflow/repo/C_GMM_CashflowRepo.xrp
ÿþ< ! - -   X M L   R e p o s i t o r y   f i l e   1 . 0   - - >

$ tr -cd '\''\11\12\15\40-\176'\' < cashflow/repo/C_GMM_CashflowRepo.xrp | head -n 1
<!-- XML Repository file 1.0 -->

任何人都可以看到我的配置有什么问题吗?

最佳答案

钩子(Hook)的一个问题是它们不是分布式的。

.gitattributes 有一些指令来管理文件的差异和内容,但另一个选项是 attribute filter (仍在 .gitattributes 中),并且可以在提交时自动转换这些文件。
(也就是说,如果干净的脚本能够 detect those files based on their content alone )


根据此聊天讨论,OP Synesso报告成功:

.gitattributes:
*.xrp filter=xrp

~/.gitconfig:
[filter "xrp"]
clean = \"C:/Program Files/Git/bin/tr.exe\" -cd "\\''\\11\\12\\15\\40-\\176'\\'"
smudge = cat

Then I had to modify the file, add, commit, delete, checkout ... and THEN it was fixed. :)

请注意,对于任何不只涉及一个用户的修改,但可能是任何克隆该 repo 的用户,我更喜欢添加(并提交)一个额外的 .gitattributes 文件,其中过滤器是声明,而不是修改 .git/info/attribute 文件(它没有被克隆)。

来自gitattributes man page :

  • If you wish to affect only a single repository (i.e., to assign attributes to files that are particular to one user’s workflow for that repository), then attributes should be placed in the $GIT_DIR/info/attributes file.
  • Attributes which should be version-controlled and distributed to other repositories (i.e., attributes of interest to all users) should go into .gitattributes files.
  • Attributes that should affect all repositories for a single user should be placed in a file specified by the core.attributesfile configuration option.
  • Attributes for all users on a system should be placed in the $(prefix)/etc/gitattributes file.

http://git-scm.com/docs/gitattributes


phyatt添加 in the comments :

I made an example similar to this for sqlite3.
You can add it into the correct files with two lines:

git config diff.sqlite3.textconv 'sqlite3 $1 .dump'
echo '*.db diff=sqlite3' >> $(git rev-parse --show-toplevel)/.gitattributes 

Similar lines can be used for writing other git config paths.

关于git - Hook 还是不 Hook - git,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6517186/

相关文章:

适用于 Windows 的 GitHub : Login Failed

git - 在 smartgit "git-am is in progress"中重新定位

git - checkout 时忽略 .gitattributes

c - 全局使用WH_MOUSE,无需DLL

linux - 使用命令行工具获取apache服务器版本

bash - 使用 sed 删除标点符号和制表符

regex - 删除额外的管道(使用 sed 或 tr)

windows - 如何在 Windows 上将 man 和 zip 添加到 "git bash"安装

从接收后 Hook 更新 Git 子模块

C# Hook vmtable