跨平台 git hooks

发布于 2024-10-21 00:57:02 字数 75 浏览 8 评论 0原文

如何跨各种平台(例如,*nix 和 windows)管理 git 提交前/提交后挂钩?

针对这种情况有推荐的最佳实践吗?

How do you manage git pre/post commit hooks across various platforms (say, *nix and windows)?

Any best practices recommended for this scenario?

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

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

发布评论

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

评论(1

紫﹏色ふ单纯 2024-10-28 00:57:02

git hooks 实际上与任何 shell 脚本没有什么不同,因为保持它们“跨平台”的最佳实践同样适用。细微的区别是,您也可能将 Windows *nix 模拟层作为这些钩子的主要用户,并且与大多数实际的 Unix 环境相比,这些可能会相对削弱。

我刚刚将一个提交后挂钩(并编写了一个预提交挂钩)移植到 Windows 的默认 Git,它使用 MINGW32。自从上一个版本对可用的脚本环境做了一些假设以来,我必须做出一些更改:

  • 没有可用的 readlink ,但它在 Windows 上无论如何都是无效的,所以我只是侵入了 readlink 的检查code>which readlink 和 no-oped if there was none
  • 重定向到 /dev/null 在 Windows 中不起作用,而是必须重定向到 nul ,为了做到这一点,我只是做了一个 uname -s 检查并设置一个变量来重定向到
  • 没有 mktemp 可用的目录,但由于它是一个目录,我们最终修剪了无论如何,出于某种原因,我们使用了天真的硬编码目录名称
  • ,通过 exec >>$out 重定向 STDOUT 在 MINGW32 中工作正常,但 exec 2>>$err< STDERR 的 /code> 不会(它似乎默默地死掉),所以不幸的是我们不得不避开该重定向

除此之外,它按原样工作得很好,因为其中没有很多特定于平台的“东西”它。因此,只要坚持制作跨平台脚本的最佳实践,这应该会让您至少完成 80% 的工作。之后,在您所针对的所有各种环境中进行测试测试。

The git hooks are really no different than any shell script in that the best-practices for keeping them "cross-platform" apply just the same. The minor difference is that you may also be targeting the Windows *nix emulation layers as major users of these hooks as well, and these can be comparatively crippled by comparison to most actual Unix environments.

I just ported a post-commit hook (and wrote a pre-commit hook) to the default Git for Windows, which uses MINGW32. A few of the changes I had to make since the previous version made some assumptions about the scripting environment available:

  • no readlink available, but it was ineffectual on Windows anyway, so I just hacked in a check of which readlink and no-oped if there was none
  • redirecting to /dev/null doesn't work in Windows, instead you have to redirect to nul, to do this I just did a uname -s check and set a variable for what to redirect to
  • no mktemp available, but since it was a directory that we ended up pruning anyway, we used a naively hard-coded directory name
  • for some reason, redirecting of STDOUT via exec >>$out works fine in MINGW32, but exec 2>>$err for STDERR does not (it seems to die silently), so unfortunately we just had to eschew that redirection

Other than that, it worked pretty well as-is because there wasn't a lot of platform-specific "stuff" in it. So just stick to best practices for making cross-platform scripts and that should get you at least 80% of the way there. After that, test test test in all of the various environments you're targeting.

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