从PHP读取Git提交消息 [英] Reading a Git commit message from PHP

查看:342
本文介绍了从PHP读取Git提交消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法来读取使用PHP的Git commit消息。我怀疑我需要使用Git钩子,但之前我从未使用它们,所以我需要在正确的方向上使用 push 。具体来说,我想实现以下过程:


  • PHP脚本在每次提交后自动执行
  • 脚本捕获Git用户名,提交时间和提交内容 如果可能,我'喜欢坚持纯PHP。如果有教程或引用可以指出,那将是一个巨大的帮助。

    解决方案

    要获取提交哈希,你可以使用

      git rev-parse --verify HEAD 2> / dev / null 

    From php:

      exec('git rev-parse --verify HEAD 2> / dev / null',$ output); 
    $ hash = $ output [0];

    您可以获取提交信息,作者和时间(尽管时间将简单地现在如果它是作为post-commit钩子的一部分运行的话):

      exec(git show $ hash,$ output) ; 

    如果不是很明显,不管你用php做什么,都只会成为你'd用git做cli - 也就是说任何我怎么能用x从git来做php只是 exec('git answer',$ output)


    I'm looking for a way to read a Git commit message with PHP. I suspect I need to use a Git hook, but I've never worked with them before, so I need a push in the right direction. Specifically, I'd like to implement the following process:

    • A PHP script is executed automatically after every commit
    • The script captures the Git username, the time of the commit, and the commit content

    If at all possible, I'd like to stick with pure PHP. If there are tutorials or references that you could point out, that would be a huge help.

    解决方案

    To get the commit hash, you can use

    git rev-parse --verify HEAD 2> /dev/null
    

    From within php:

    exec('git rev-parse --verify HEAD 2> /dev/null', $output);
    $hash = $output[0];
    

    You can get the commit message, author and time (though - the time will simply be "now" if it's run as part of a post-commit hook) with:

    exec("git show $hash", $output);
    

    If it's not obvious, whatever you do with php is simply going to be a wrapper around the things you'd do with git on the cli - I.e. any "how can I do x with git from php" is just exec('the git answer', $output)

    这篇关于从PHP读取Git提交消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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