简体   繁体   English

在项目内包含git commit信息

[英]Include git commit information inside a project

I am working on php project. 我正在研究php项目。 Is it possible to integrate git into the project to have the info from last commit in the footer ? 是否可以将git集成到项目中,以将来自上一次提交的信息添加到footer

I am not using build framework 我没有使用构建框架

I'd use so called git hook to achieve this. 我会使用所谓的git hook来实现这一点。 Hooks are literally scripts that execute automatically on specific git actions. 挂钩实际上是在特定git动作上自动执行的脚本。 The one that you might be interested in is called git post commit hook , which is a script that is automatically executed after every commit. 您可能会感兴趣的一个叫做git post commit hook ,它是一个脚本,在每次提交后都会自动执行。 Here's how to add one: 以下是添加方法:

  1. Create a script .git/hooks/post-commit : 创建一个脚本.git/hooks/post-commit

     #!/bin/sh git log -1 --format=%cd --date=local > version.txt 
  2. Make it executable: 使它可执行:

      chmod +x .git/hooks/post-commit 

That's it. 而已。 Now after every commit your version.txt will be updated with info from your last commit. 现在,在每次提交之后,您的version.txt都将使用上一次提交的信息进行更新。

There's multiple options for doing that, but I think the best one would be to do it in during your build. 这样做有多种选择,但我认为最好的选择是在构建过程中进行选择。

If you're using something like Jenkins, Bamboo or any other CI/CD system, you can create a task to retrieve the git commit, and save it in a readable location by the application. 如果您正在使用诸如Jenkins,Bamboo或任何其他CI / CD系统之类的东西,则可以创建一个任务来检索git commit,并将其保存在应用程序可读的位置。 This could be: 可能是:

  • Set it as part of your configuration file 将其设置为配置文件的一部分
  • Set it as an environment variable that you can read 将其设置为您可以阅读的环境变量
  • Save it in a file and read it for each request 将其保存在文件中,并为每个请求读取
  • ... ...

Really, the options are endless, but it depends on the way you're building your project, and how it's being deployed. 确实,选项无穷无尽,但这取决于您构建项目的方式以及部署方式。 As you can see, you're not lacking options! 如您所见,您不缺少选择!

If you don't use a build system, maybe you should be using one then! 如果您不使用构建系统,那么也许您应该使用一个构建系统!

Still, you have the option to get the latest commit by executing something like: 不过,您仍然可以选择执行以下命令来获取最新的提交:

echo exec('git rev-parse --short HEAD');

Which will give you the short commit hash. 这将为您提供简短的提交哈希。 I'd really recommend using one of the alternative options though. 我确实建议您使用其他选项之一。

Actually after searching what I found something like that without using builds 实际上,在搜索了内容之后,我没有使用构建就发现了类似的内容

echo exec('git log -1 --format=%cd --date=local');

That will display the time and date of last commit 这将显示上次提交的时间和日期

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

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