简体   繁体   English

为什么 git 描述挑选它做的标签

[英]Why is git describe picking out the tag that it does

I am developing an internal web application for a company I am doing some work for.我正在为我工作的公司开发内部 web 应用程序。 It is hosted on a private github repository, but most of my development is done on my linux desktop at home, and the web server for this is hosted in the companies office on a small raspberry pi.它托管在一个私有的 github 存储库中,但我的大部分开发是在我家里的 linux 桌面上完成的,而 web 服务器托管在公司办公室的一个小型树莓派上。 It is essentially nginx front end proxying the /api url to a nodejs application supported by pm2.它本质上是 nginx 前端将 /api url 代理到 pm2 支持的 nodejs 应用程序。

I deploy by git push ing to the github repository from my home and git pull ing the production branch in the server.我通过git push送到 github 存储库并通过git pull取服务器中的生产分支进行部署。 A git hook post-commit script runs a npm install in a build directory before temporarily stopping the production api server, copying over the node_modules directory and running a script which in essence does git describe --abbrev=0 --tags to get and store the version number in a .env file for the production server to announce its version to clients. git hook post-commit脚本在构建目录中运行 npm 安装,然后暂时停止生产 api 服务器,复制 node_modules 目录并运行一个脚本,该脚本本质上执行git describe --abbrev=0 --tags以获取和存储.env文件中的版本号,用于生产服务器向客户端宣布其版本。

If I run git describe at home it gives version v4.2.2 , but in the clinic I get v4.1.17 and I cannot see why如果我在家里运行 git describe,它会提供v4.2.2版本,但在诊所我得到v4.1.17 ,但我不明白为什么

In the image below the left hand part is the output of gitk --all on my home repository and with only terminal access in the clinic, you can see from the right hand part pretty much the same graph using git log - with commits missing that are not relevant to either the master or production branch.在下图中,左手部分是gitk --all的 output——全部在我的家庭存储库中,并且在诊所只有终端访问权限,您可以从右手部分看到几乎相同的图表,使用git log - 提交缺少那个与masterproduction分支无关。 v4.2.2 is only two commits away, whereas v4.1.17 is at least 10 away. v4.2.2 只有两次提交,而 v4.1.17 至少有 10 次。

Why is git describe behaving the way it is?为什么 git 描述了它的行为方式?

EDIT: I sort of know the answer - on the server there is a post-commit hook that ended up making a commit on the branch, even though it wasn't supposed to - it was only supposed to be doing it on the development machine.编辑:我有点知道答案 - 在服务器上有一个提交后挂钩,最终在分支上进行提交,即使它不应该 - 它只应该在开发机器上进行.

来自我的 git repo 的复合 gitk 图

answered by @TTT in his comment :<\/em> @TTT 在他的评论中回答:<\/em>

maybe the cause of the problem is that you intended the server local production branch to be the same as your home branch, but it isn't.也许问题的原因是您希望服务器本地生产分支与您的主分支相同,但事实并非如此。 So every time it does a pull it's making a new merge commit.因此,每次拉取时,它都会进行新的合并提交。 Maybe the server should push out its version (if you want to keep that), or, maybe the server should get the latest with a git fetch && git reset --hard @{u}<\/code> instead of with git pull<\/code> .也许服务器应该推出它的版本(如果你想保留它),或者,也许服务器应该使用git fetch && git reset --hard @{u}<\/code>而不是git pull<\/code>来获取最新版本。 (If the server shouldn't be making it's own commits.) (如果服务器不应该自己提交。)

"

Git 2.40 (Q1 2023), clarifies what you must do for environment variables set when hooks are invoked. Git 2.40(2023 年第一季度),阐明了在调用挂钩时必须对环境变量集执行的操作。
That would help git describe to behave as expected in a hook.这将有助于git describe在钩子中表现得像预期的那样。

See commit 772f8ff (09 Jan 2023) by Eric Sunshine ( sunshineco ) .请参阅Eric Sunshine ( sunshineco )提交 772f8ff (2023 年 1 月 9 日)。
(Merged by Junio C Hamano -- gitster -- in commit fc2735f , 21 Jan 2023) (由Junio C Hamano 合并gitster 提交 fc2735f ,2023 年 1 月 21 日)

githooks : discuss Git operations in foreign repositories githooks : 讨论 Git 在国外仓库的操作

Signed-off-by: Eric Sunshine签字人:Eric Sunshine
Acked-by: Jeff King确认人:Jeff King

Hook authors are periodically caught off-guard by difficult-to-diagnose errors when their hook invokes Git commands in a repository other than the local one.当钩子调用本地存储库以外的存储库中的 Git 命令时,钩子作者会定期因难以诊断的错误而措手不及。
In particular, Git environment variables, such as GIT_DIR and GIT_WORK_TREE, which reference the local repository cause the Git commands to operate on the local repository rather than on the repository which the author intended.特别是引用本地存储库的 Git 环境变量,例如GIT_DIRGIT_WORK_TREE,导致 Git 命令在本地存储库而不是作者想要的存储库上运行。
This is true whether the environment variables have been set manually by the user or automatically by Git itself.无论环境变量是由用户手动设置还是由 Git 本身自动设置,都是如此。
The same problem crops up when a hook invokes Git commands in a different worktree of the same repository, as well.当挂钩在同一存储库的不同工作树中调用 Git 命令时,也会出现同样的问题。

Recommended best-practice$ for avoiding this problem is for the hook to ensure that Git variables are unset before invoking Git commands in foreign repositories or other worktrees:避免此问题的推荐最佳实践 $ 是挂钩确保在调用外部存储库或其他工作树中的 Git 命令之前取消设置 Git 变量:

 unset $(git rev-parse --local-env-vars)

However, this advice is not documented anywhere.但是,此建议未在任何地方记录。
Rectify this shortcoming by mentioning it in githooks.txt documentation.通过在githooks.txt文档中提及它来纠正这个缺点。

See also:也可以看看:

githooks now includes in its man page : githooks现在包含在其手册页中:

Environment variables, such as GIT_DIR , GIT_WORK_TREE , etc., are exported so that Git commands run by the hook can correctly locate the repository.导出GIT_DIRGIT_WORK_TREE等环境变量,以便 hook 运行的 Git 命令可以正确定位到版本库。

If your hook needs to invoke Git commands in a foreign repository or in a different working tree of the same repository, then it should clear these environment variables so they do not interfere with Git operations at the foreign location.如果您的挂钩需要在外部存储库或同一存储库的不同工作树中调用 Git 命令,那么它应该清除这些环境变量,这样它们就不会干扰外部位置的 Git 操作。

For example:例如:

 local_desc=$(git describe) foreign_desc=$(unset $(git rev-parse --local-env-vars); git -C../foreign-repo describe)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM