【问题标题】:How can I require (or urge) git commiters to provide relevant author info?我如何要求(或敦促)git 提交者提供相关的作者信息?
【发布时间】:2012-08-09 14:28:55
【问题描述】:

99% 的时间我们的团队在本地开发框上提交并推送提交,在这种情况下,提交的作者信息是正确的。然而,在压力大的时候,有人会直接在开发服务器上调整一些东西并从那里提交。发生这种情况时,提交中的作者信息默认为共享部署帐户。

有没有办法在提交时提示用户提供他们的作者信息?他们不会记得做--author。

我看过但未能完全通过终点线的一些东西是:

  1. 使用客户端挂钩拒绝作者字符串中包含“部署”的提交
  2. 寻找始终提示作者信息的设置
  3. 尝试根据用于登录盒子的 ssh-key 自动设置作者

有没有办法让其中一个工作或一个好的方法来做到这一点?

【问题讨论】:

  • 您的团队是否讨论过不允许直接在开发服务器上编辑代码?
  • 当然。我想这会发生,但现在它很少发生,而且只在“紧急情况”期间发生,并且完全防止发生的事情不是我作为“新人”可以完成的事情。
  • 不要寻求技术解决方案。惩罚违反公司发展惯例的员工。
  • @meagar 我同意,但有一个中间立场。如果我的同事(他们不是我的员工)进入这项新工作的三周后,就不能以他们过去所做的方式(很少而且仅在“紧急情况”中)承诺,因为“新家伙”放了一个钩子来防止它,你肯定明白这是次优的。一个从温和的教育开始,这需要知道谁在做以及多久做一次——这就是为什么这个问题要求设置好的作者信息而不是阻止提交(这很容易)。

标签: git


【解决方案1】:

好的,我找到了一种方法来做到这一点,但这只是因为我们使用基于密钥的身份验证通过 ssh 专门访问此框。

在共享部署用户的/home/deploy/.ssh/authorized_keys 文件中,我为每个键添加了一个环境变量,例如,我的如下所示:

environment="GIT_AUTHOR_EMAIL=ry4an@host.com",environment="GIT_AUTHOR_NAME=Ry4an on Dev" ssh-rsa AAAA...cXBcmHr ry4an@host.com

还需要添加:

PermitUserEnvironment yes

到/etc/ssh/sshd_config 文件。

这行得通,但如果存在这样的事情,我宁愿了解user.name=ASK 设置。

【讨论】:

    【解决方案2】:

    我不太确定它对你们来说是如何工作的,但你可以让你的开发人员设置一个别名,以便我将 --author 添加到每个正常的提交中。

    【讨论】:

    • 这是一个共享帐户,所以每个人的别名都是一样的。
    • 啊,好吧,我不太明白。
    【解决方案3】:

    很遗憾,我也找不到真正令人满意的解决方案。我将列出到目前为止我找到的所有解决方案(我认为您已经提出了问题中最好的 3 个)。

    (1) 没有 --author 禁止提交

    将以下脚本另存为pre-commit hook in in .git/hooks/pre-commit 以禁止:

    #!/bin/sh
    
    if echo $GIT_AUTHOR_NAME | grep -q "XXX" 2> /dev/null; then
        echo 'You tried to commit as XXX, please use  git commit --author="your name".'
        return 1
    fi
    

    设置用户名为XXX:

    git config user.name XXX
    

    警告:这仅适用于正常提交。合并提交将作为 XXX 提交。可能也有办法完成这项工作

    (2a) 在提交前提示作者信息(使用钩子)

    使用钩子设置作者的唯一方法,似乎是用post-commit钩子,see also here修改提交。

    这是一个带有丑陋副作用的奇怪 hack(例如,提交消息中的作者显示错误)。

    #!/bin/sh
    
    # env variable to prevent recursion
    test -z $AMMEND_COMMIT || exit 0
    export AMMEND_COMMIT=1
    
    exec < /dev/tty
    echo -n "Author for previous commit: "
    read author
    
    git commit -q --no-edit --amend --author "$author"
    

    使用以下脚本根据上次提交获取作者建议:

    #!/bin/sh
    
    # TODO this doesn't work for repos with <15 commits
    NUM_LAST_COMMITS=15  # how many commits in the past to look for authors
    
    # env variable to prevent recursion
    test -z $ammend_commit || exit 0
    export ammend_commit=1
    
    exec < /dev/tty
    
    last_authors=$(git shortlog -s -e HEAD~${NUM_LAST_COMMITS}..HEAD|sed 's/^\W*[0-9]*\W*//g')
    echo "Select an author from list or enter an author:"
    echo
    echo "$last_authors" | awk '{print "    [" NR "] " $s }'
    echo
    echo -n "Enter number or author: "
    read author
    
    if echo "$author" | egrep -q '^[0-9]+$'; then
        author=$(echo "$last_authors" | sed "${author}q;d")
    fi
    
    git commit -q --no-edit --amend --author "$author"
    

    看起来像这样:

    $ git commit -am "My message"
    Select an author from list or enter an author:
    
        [1] First Author <first_author@domain.com>
        [2] Second Author <second_author@domain.com>
        [3] Third Author <third_author@domain.com>
    
    Enter number or author: 3
    

    (2b) 在提交前提示作者信息(使用自定义脚本或别名)

    您可以创建一个脚本git-commit.sh 或git 别名git commit-author 提示使用作者,然后调用git commit --author=&lt;user's selection&gt;。不幸的是not possible to overwritegit commit。

    /usr/local/bin 中的 git-commit.sh 脚本可能如下所示:

    #!/bin/sh
    
    # TODO this doesn't work for repos with <15 commits
    NUM_LAST_COMMITS=15  # how many commits in the past to look for authors
    
    last_authors=$(git shortlog -s -e HEAD~${NUM_LAST_COMMITS}..HEAD|sed 's/^\W*[0-9]*\W*//g')
    echo "Select an author from list or enter an author:"
    echo
    echo "$last_authors" | awk '{print "    [" NR "] " $s }'
    echo
    echo -n "Enter number or author: "
    read author
    
    if echo "$author" | egrep -q '^[0-9]+$'; then
        author=$(echo "$last_authors" | sed "${author}q;d")
    fi
    
    git commit "$@" --author "$author"
    

    将脚本添加为别名运行:

    $ git config alias.commit-author '!git-commit.sh'
    

    (3)根据登录时使用的ssh-key自动设置作者

    这是你自己的答案,复制到这里以获得更好的概览:

    在共享部署用户的/home/deploy/.ssh/authorized_keys 文件中,我为每个键添加了一个环境变量,例如,我的如下所示:

    environment="GIT_AUTHOR_EMAIL=ry4an@host.com",environment="GIT_AUTHOR_NAME=Ry4an on Dev" ssh-rsa AAAA...cXBcmHr ry4an@host.com
    

    还需要添加:

    PermitUserEnvironment yes
    

    到/etc/ssh/sshd_config 文件。

    【讨论】:

      猜你喜欢
      • 2015-03-08
      • 2018-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多