将 git merge 驱动程序添加到存储库? [英] Add a git merge driver to the repository?

查看:34
本文介绍了将 git merge 驱动程序添加到存储库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个合并驱动程序.我已经定义了一个 .gitattributes 文件如下:

I'm creating a merge driver. I have defined a .gitattributes file as follows:

filename merge=mergeStrategy

我在 $PROJECT/.git/config 中创建了合并驱动程序,如下所示:

I have created the merge driver in $PROJECT/.git/config as follows:

[merge "mergeStrategy"]
    name = My merge strategy
    driver = scripts/mergeScript.sh

这在本地工作正常,但我想将此合并驱动程序提交到 git 存储库,以便合并策略对每个人都有效.

This works fine locally, but I would like to commit this merge driver to the git repository so that the merge strategy is in effect for everyone.

有没有办法将这个(或其他 Git 配置选项)添加到存储库本身?

Is there a way I can add this (or other Git configuration options) to the repository itself?

推荐答案

您可以简单地添加和提交(和推送)script/mergeScript(连同 .gitattributes文件,当然)

You could simply add and commit (and push) script/mergeScript (along with the .gitattributes file, of course)

只要:

  • mergeScript 不知何故在执行合并驱动程序的用户的 $PATH 中.
  • mergeScript 是可执行的,chmod +x
  • mergeScript 没有扩展名,允许您在需要时稍后更改其内容(从 bash shell 到 Perl 脚本到 C 可执行文件到...).
  • mergeScript is somehow in the $PATH of the user executing the merge driver.
  • mergeScript is executable, chmod +x
  • mergeScript is without extension, to allow you to later change its content (from a bash shell to a Perl script to a C executable to ...) if needed.

(感谢您,MestreLion,对于最后两点,正如他在评论中提到的那样)

(Thank you, MestreLion, for the last two points, as he mentioned them in the comment)

您在本地就是这种情况,因为我怀疑 '.' 在您的 $PATH 中,但您不能假设每个人都是如此.

That was the case for you locally, as I suspect that '.' was in your $PATH, but you cannot assume that for everybody.

但是,本地配置文件 (.git/config) 不会被推送/克隆(如 Alexandr Priymak 在评论中指出),所以用户仍然需要复制自定义合并驱动程序的声明.

However, the local config file (.git/config) won't be pushed/cloned (as Alexandr Priymak points out in the comment), so the users still need to replicate the declaration of the custom merge driver.

这是一项基本的安全措施,目的是让您不会将潜在的有害"产品推向高潮.脚本将在下一次合并时自动执行.

This is a basic safety measure, in order for you to not push a potential "harmful" script which would then be automatically executed at the next merge.

使用 Makefile

另一种选择是将所有 git 驱动程序和 .gitattributes 保留在版本历史记录中,并且 包含一些可以轻松激活所有驱动程序的内容.例如,一个项目使用了 Makefile,它有一个特殊的目标 make gitdrivers,它激活了 repo 中的所有 git 驱动程序.

An another option is to keep all the git drivers and .gitattributes in the version history and also include something that easily activates all the drivers. For example, one project used Makefile that had special target make gitdrivers that activated all the git drivers in the repo.

这是必需的,因为 git 将自定义驱动程序视为潜在的安全漏洞,您需要做一些事情来授予任何新驱动程序的信任.合并驱动程序和其他挂钩是在您的用户凭据上运行的可执行代码,它们会作为 git 操作的副作用自动启动,因此显然在运行该代码之前必须格外小心.

This is needed because git considers custom drivers as potential security vulnerability and you need to do something to grant trust to any new drivers. The merge drivers and other hooks are executable code running on your user credentials that start automatically as a side-effect on git actions, so obviously extra care must be taken before running that code.

运行改变 .git/config 的代码来激活驱动程序是授予信任的 git 风格.

Running code that changes .git/config to activate the drivers is the git style of granting the trust.

在实践中,您可以使用 Makefile(但您可以使用适合您项目的任何构建系统或脚本).对于 Ubuntu Linux 主机,您可以简单地执行如下操作(具有三个驱动程序的示例):

In practice, you can use Makefile for that (but you could use whatever build system or script that suits your project). For Ubuntu Linux hosts, you can simply do it like follows (example with three drivers):

在文件 .gitattributes 中:

[attr]POFILE merge=merge-po-files
[attr]IMAGE diff=image
[attr]BINARY diff=binary -merge -text

locale/*.po POFILE
data/*.img BINARY
*.png IMAGE

和在 Makefile 中(缩进应该只用 U+0009 TAB 字符完成,但 stackoverflow.com 在这里不支持它):

and in Makefile (indent should be done with U+0009 TAB character only but stackoverflow.com doesn't support it here):

developer-dependencies:
        sudo apt install required-package1 required-package2

submodules:
        @echo "Overwriting submodules with committed versions..."
        git submodule sync
        git submodule update --init

gitdrivers: developer-dependencies submodules
        @echo "Overwriting git drivers with current version..."
        git config merge.merge-po-files.driver "./bin/merge-po-files %A %O %B"
        # show rough thumbnails in text mode for images
        git config diff.image.textconv "./bin/image-textconv"
        git config diff.image.binary "true"
        git config diff.image.cachetextconv "true"
        # show some extra information about binary files
        git config diff.binary.textconv "./bin/binary-textconv"
        git config diff.binary.binary "true"
        @echo "All git drivers done."

其中文件merge-po-filesimage-textconvbinary-textconv 是项目子目录bin 中合适的可执行文件.如果您有一个需要在多个平台上工作的项目,您可能需要额外的依赖来为当前平台构建/链接正确的二进制文件.

Where files merge-po-files, image-textconv and binary-textconv are suitable executables in the project subdirectory bin. If you have a project that needs to work on multiple platforms, you could have an extra dependency to build/link correct binary for the current platform.

示例配置告诉 git 在 diffs 中呈现图像的 ASCII 表示,合并具有特殊合并驱动程序的 gettext .PO 文件,并防止合并选定的二进制文件,即使 git 启发式将它们声明为文本.而是需要为标记为 BINARY 的文件选择一个或另一个版本.并在差异中显示 BINARY 文件的特殊用户可见摘要.

The example config tells git to render ASCII presentation of images in diffs, merge gettext .PO files with special merge driver and prevent merging selected binary files even if git heuristics declare those as text. Instead require selecting one version or the other for files marked as BINARY. And show special user visible summary for the BINARY files in diffs.

这样所有开发人员只需要在完成初始克隆后运行一次 make gitdrivers.并且他们会在合并时自动获取已安装的驱动程序的更新或稍后重新设定他们的工作副本.如果他们不确定他们是否有最新的驱动程序,他们可以随时重新运行相同的命令.请注意,上面的示例将重置您拥有的所有子模块,这些子模块可能是您真正想要的,也可能不是.

This way all developers just need to run make gitdrivers once after doing the initial clone. And they automatically get updates to drivers that they've already installed when they merge or rebase their working copy later. And if they're not sure if they have the latest drivers, they can simply re-run the same command at any time. Note that the example above will reset all submodules you have which may or may not what you really want.

TL;DR: .git/config 未包含在存储库中,但您可以包含一个使用 git config<重新创建合适配置的脚本/code> 命令.对于类 UNIX 系统,shell 脚本非常有用.如果您的项目需要构建代码,您可以根据需要挂钩安装所需的驱动程序以构建过程.另请注意,使用 git config ... 命令比尝试直接修改配置文件要安全得多,因为即使将来该配置文件的文件格式发生更改,这些命令也会继续工作.

TL;DR: .git/config is not included in the repository but you can include a script that re-creates suitable config using git config commands. For UNIX-like systems a shell script is great. If your project needs building the code you can hook installing the required drivers to build process if needed. Also note that it's much safer to use git config ... commands instead of trying to directly modify the config file because those commands will keep working even if the file format of that config file changed in the future.

这篇关于将 git merge 驱动程序添加到存储库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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