Git:如何处理不同的shebang

发布于 2024-10-18 10:06:21 字数 329 浏览 2 评论 0原文

人们如何处理本地和远程之间的不同 shebang?

例如,我的本地 python 是 /usr/bin/python,而我的 Web 主机是位于 ~/local/bin/python 的专用 python。首席开发人员的 ruby​​ 可能位于 /usr/bin/ruby,而我的则位于 /usr/local/bin/ruby。

我手动编辑 shebang,但随后 git 将其标记为更改。理想情况下,我希望 git 忽略文件的第一行,或者忽略文件中行的正则表达式匹配。

在我看来,这一定是一个非常普遍的问题,但我找不到任何提及。

我使用 git,但无论如何我都不会称自己为专家。

How do people deal with different shebangs between local and remote?

For example, my local python is /usr/bin/python, whereas my web host is a purpose-built python at ~/local/bin/python. A lead developer may have ruby at /usr/bin/ruby, whereas mine is /usr/local/bin/ruby.

I manually edit the shebang, but then git marks it as a change. Ideally, I would like git to ignore the first line of the file, or perhaps to ignore a regex match of lines within the file.

It seems to me this must be a very common problem, but I cannot find any mention of it.

I use git, but I would not call myself an expert by any stretch.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

请止步禁区 2024-10-25 10:06:21

将其更改为

#!/usr/bin/env python

#!/usr/bin/env ruby

那么它应该可以在您的所有系统上运行,前提是您的 PATH 环境变量中有 python 和 ruby​​。

Change it to

#!/usr/bin/env python

or

#!/usr/bin/env ruby

Then it should work on all your systems, provided you have python and ruby in your PATH environment variable.

东北女汉子 2024-10-25 10:06:21

解决你的问题的方法是 git 的 smudge/clean 过滤规则。这允许您设置过滤器,在签出时修改文件并在签入时撤消这些更改。这是一个很好的图形:

在此处输入图像描述

首先设置一个可以在两个方向上进行更改的过滤器,方法是添加类似以下内容的.git/config。 smudge 过滤器将存储库中的文件转换为工作副本,clean 过滤器会撤消该更改。重要的是运行涂抹-> clean 产生的正是原始文件。如果工作副本中的 #!/usr/bin/env python 是,此处给出的过滤器将用 #!~/local/bin/python 替换工作副本中的第一行 现在,

[filter "pyshebang"]
    smudge = sed '1s?^#!/usr/bin/env python$?#!~/local/bin/python?'
    clean = sed '1s?^#!~/local/bin/python$?#!/usr/bin/env python?'

通过向 .git/info/attributes 添加如下行来激活此过滤器(如果该文件不存在,则创建该文件):

*.py filter=pyshebang

如果您的 python 文件不以 .py 结尾,只需在正确的文件/整个文件夹/所有文件上配置过滤器。如果你正确设置了过滤器,它只会用 python shebang 更改文件。

我建议阅读涂抹过滤器,了解发生的事情的细节。

The solution to your problem is git’s smudge/clean filter rule. This allows you to set up filters that will modify files on checkout and undo those changes on checkin. Here’s a nice graphic:

enter image description here

First set up a the filters that can do the change in both directions, by adding something like the following to you .git/config. The smudge filter transforms the file in the repo to the working copy, the clean filter undoes that change. It is important that running smudge -> clean yields exactly the original file. The filters given here will replace the first line with #!~/local/bin/python in the working copy if it is #!/usr/bin/env python in the repo

[filter "pyshebang"]
    smudge = sed '1s?^#!/usr/bin/env python$?#!~/local/bin/python?'
    clean = sed '1s?^#!~/local/bin/python$?#!/usr/bin/env python?'

Now activate this filter by adding a line like this to .git/info/attributes (create that file if it doesn’t exist):

*.py filter=pyshebang

If your python files don’t end in .py, just configure the filter on the correct files / a whole folder / all the files. If you set up the filter correctly it will only change files with a python shebang anyways.

I would recommend to read up on smudge filters, to understand the details of what’s going.

奈何桥上唱咆哮 2024-10-25 10:06:21

这属于评论,但我还没有足够的声誉...您可以删除哈希爆炸,并在从命令行运行它时始终使用 python 运行它...也许吧。 ..

This belongs in the comments but I don't have enough reputation yet... You could just remove the hash bang and always run it with python when you run it from the command line... maybe...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文