git在合并冲突后更改change-id的位置

时间:2016-10-11 12:15:26

标签: git merge gerrit

我正在使用gitgerrit一段时间,但有一种行为让我烦恼。

如果我使用冲突执行cherry-pickmergechange-ID最初生成的commit-msg hook不在最后一行。这是一个例子。

Commit message

Change-ID: AAAAAAAAAA

Conflits:
     File1.cpp

如果我保留这样的信息,push会被gerrit视为最后一段所禁止的。{/ p>

我知道可以使用--amendinteractive rebase编辑提交消息,但我不想这样做。我希望git单独处理它。

我的问题很简单。有没有办法在Change-ID之前插入冲突线?

Git版本1.8.1

2 个答案:

答案 0 :(得分:3)

解决此问题的两种方法:

(I)prepare-commit-msg hook

使用此钩子的步骤:

  1. 复制现有样本: cp .git/hooks/prepare-commit-msg.sample .git/hooks/prepare-commit-msg

  2. 确保挂钩具有正确的权限: chmod 755 .git/hooks/prepare-commit-msg

  3. 如果您没有.git/hooks

    ,请执行此操作
    #!/bin/sh
    #
    # An example hook script to prepare the commit log message.
    # Called by "git commit" with the name of the file that has the
    # commit message, followed by the description of the commit
    # message's source.  The hook's purpose is to edit the commit
    # message file.  If the hook fails with a non-zero status,
    # the commit is aborted.
    #
    # To enable this hook, rename this file to "prepare-commit-msg".
    
    # This hook includes three examples.  The first comments out the
    # "Conflicts:" part of a merge commit.
    #
    # The second includes the output of "git diff --name-status -r"
    # into the message, just before the "git status" output.  It is
    # commented because it doesn't cope with --amend or with squashed
    # commits.
    #
    # The third example adds a Signed-off-by line to the message, that can
    # still be edited.  This is rarely a good idea.
    
    case "$2,$3" in
      merge,)
        /usr/bin/perl -i.bak -ne 's/^/# /, s/^# #/#/ if /^Conflicts/ .. /#/; print' "$1" ;;
    
    # ,|template,)
    #   /usr/bin/perl -i.bak -pe '
    #      print "\n" . `git diff --cached --name-status -r`
    #    if /^#/ && $first++ == 0' "$1" ;;
    
      *) ;;
    esac
    
    # SOB=$(git var GIT_AUTHOR_IDENT | sed -n 's/^\(.*>\).*$/Signed-off-by: \1/p')
    # grep -qs "^$SOB" "$1" || echo "$SOB" >> "$1"
    

    以下位应注释掉“冲突”部分,而不必为改进git本身而烦恼。

    case "$2,$3" in
      merge,)
        /usr/bin/perl -i.bak -ne 's/^/# /, s/^# #/#/ if /^Conflicts/ .. /#/; print' "$1" ;;
    

    (II)将git升级到2.1.3 +

    commit 96f78d3
    (由Junio C Hamano -- gitster --于2014年10月28日合并)

    查看提交历史记录:

    49c3e92 (tag: refs/tags/v2.1.3) Git 2.1.3
    ebc2e5a Merge branch 'jk/pack-objects-no-bitmap-when-splitting' into maint
    9db1838 Merge branch 'da/mergetool-meld' into maint
    af1b4e3 Merge branch 'rm/gitweb-start-form' into maint
    27c31d2 Merge branch 'bc/asciidoc-pretty-formats-fix' into maint
    a8f01f8 Merge branch 'rs/daemon-fixes' into maint
    5b509df Update draft release notes to 2.2
    9ce57f1 Merge branch 'da/difftool'
    e82935d Merge branch 'rb/pack-window-memory-config-doc'
    7654ca6 Merge branch 'mg/lib-gpg-ro-safety'
    ce71c1f Merge branch 'dm/port2zos'
    c1777a2 Merge branch 'oc/mergetools-beyondcompare'
    d70e331 Merge branch 'jk/prune-mtime'
    853878d Merge branch 'bc/asciidoctor'
    96ef1bd api-run-command: add missing list item marker
    8828f29 use child_process_init() to initialize struct child_process variables
    5d222c0 receive-pack: avoid minor leak in case start_async() fails
    261f315 merge & sequencer: turn "Conflicts:" hint into a comment
    

    从第2.1.3节开始,该更改应该是Git的一部分。因此,如果您升级到git版本2.1.3+,则应自动注释“冲突”,使“更改ID”成为提交消息中的最后一行,从而解决您的问题。

答案 1 :(得分:0)

编辑:由于您评论说要自动执行此操作,因此最好的办法是使用提交挂钩。特别是prepare-commit-msg看起来很有希望:

  

prepare-commit-msg挂钩在启动提交消息编辑器之前运行,但是在创建默认消息之后。它允许您在提交作者看到之前编辑默认消息。这个钩子需要一些参数:到目前为止保存提交消息的文件的路径,提交的类型,以及提交SHA-1(如果这是修改的提交)。这个钩子通常对正常提交没用;相反,它适用于自动生成默认消息的提交,例如模板化提交消息,合并提交,压缩提交和修改提交。您可以将它与提交模板结合使用,以编程方式插入信息。

有关如何开始的指示,请参阅官方文档 - https://git-scm.com/book/it/v2/Customizing-Git-Git-Hooks

如果您只想更改消息文本,则可以使用Amend Commit执行此操作,如果这是您最后一次提交的话。

如果它不是最后一次提交,您仍然可以使用交互式rebase和reword来完成。

对于Interactive Rebase,请参阅https://www.atlassian.com/git/tutorials/rewriting-history/。您将使用的命令是git rebase -i,它将在origin/master上重新定义。然后,将pick命令更改为reword,如屏幕截图所示:

enter image description here