2

I am using git and gerrit for a while but there is a behavior annoying me.

If I do a cherry-pick or a merge with conflict, the change-ID initialy made by commit-msg hook is not on the last line. Here an example.

Commit message

Change-ID: AAAAAAAAAA

Conflits:
     File1.cpp

If I keep the message like that, the push is prohibited by gerrit which look under last paragraph.

I know it is possible to edit the commit message using --amend or interactive rebase but I would like to not have to do it. I want git to handle it alone.

My question is quite simple. Is there a way to make the conflict lines by inserted before the Change-ID ?


Git version 1.8.1

Flows
  • 3,675
  • 3
  • 28
  • 52
  • Why do you care which line the change-id is on? – larsks Oct 11 '16 at 12:29
  • Because if after a cherry-pick with conflict I push to gerrit, the push is prohibited because gerrit look for change -id in last paragraph. – Flows Oct 11 '16 at 12:33
  • Interesting; I didn't think Gerrit cared where the change-id was located. I guess I've learned something. – larsks Oct 11 '16 at 12:53
  • Which version of git are you using? Since some versions (I think [version 1.8.2](https://github.com/git/git/blob/master/Documentation/RelNotes/1.8.2.txt) introduced that change) the list of conflicted files should be commented out in the commit message template and thus not appear in the commit message by default. – lucash Oct 11 '16 at 13:19
  • I am using git 1.8.1. I didn't find any info of what you wrote but it may be interesting indeed. – Flows Oct 11 '16 at 13:41
  • 1
    In the linked release notes the paragraph starting with "Various "hint" lines Git gives" explains that change. – lucash Oct 11 '16 at 14:28

2 Answers2

3

Two ways to fix this problem:

(I) prepare-commit-msg hook

Steps to use this hook:

  1. Copy the existing sample: cp .git/hooks/prepare-commit-msg.sample .git/hooks/prepare-commit-msg

  2. Make sure the hook has the correct permissions: chmod 755 .git/hooks/prepare-commit-msg

Here it is in case you don't have it in .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"

That following bit should comment out the Conflict section without you having to fiddle around with upgrading git itself.

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

(II) Upgrade git to 2.1.3+

See commit 96f78d3
(Merged by Junio C Hamano -- gitster -- on 28 Oct 2014)

Looking at the commit history:

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

that change should be a part of Git from v. 2.1.3 onwards. So if you upgrade to git version 2.1.3+ the 'Conflict' should be automatically commented out making the 'Change-Id' the last line in the commit message thus solving your problem.

Ashutosh Jindal
  • 18,501
  • 4
  • 62
  • 91
0

Edit: Since you commented to say you want to do it automatically, your best bet is to do to use commit hooks. In particular, prepare-commit-msg looks promising:

The prepare-commit-msg hook is run before the commit message editor is fired up but after the default message is created. It lets you edit the default message before the commit author sees it. This hook takes a few parameters: the path to the file that holds the commit message so far, the type of commit, and the commit SHA-1 if this is an amended commit. This hook generally isn’t useful for normal commits; rather, it’s good for commits where the default message is auto-generated, such as templated commit messages, merge commits, squashed commits, and amended commits. You may use it in conjunction with a commit template to programmatically insert information.

See the official documentation for pointers on how to start - https://git-scm.com/book/it/v2/Customizing-Git-Git-Hooks


If you mean you just want to change the message text, you can do that with Amend Commit if this is the last commit you made.

If it's not the last commit, you can still do it using interactive rebase and reword.

For Interactive Rebase see https://www.atlassian.com/git/tutorials/rewriting-history/. The command you will use is git rebase -i, which will rebase on origin/master. You then change the pick command to reword as shown in the screenshot:

enter image description here

Community
  • 1
  • 1
sashoalm
  • 75,001
  • 122
  • 434
  • 781
  • Yes I know about that, thanks, but I would like to not have to make a second action (rebase,amend) after the conflict. – Flows Oct 11 '16 at 12:51
  • 1
    @Flows Your question did not state that you know how to do it by hand and you want to do it automatically. Can you edit your question to make that more clear? – sashoalm Oct 11 '16 at 12:56
  • I updated my question. I am not sure the hook is sufficient because if a change-ID is already existing in the commit message, nothing is done by the script (actually I am using `commit-msg`). – Flows Oct 11 '16 at 13:14
  • @Flows I am sorry, but I didn't understand that at all. The previous sentence just doesn't make sense to me - "nothing is done by the script (actually I am using commit-msg)" is extremely vague to the point of undecipherable. Please keep in mind that I can't read your mind. – sashoalm Oct 11 '16 at 13:21
  • I tried to be as clear as possible (I am not English fluent), but I understand it is not always readable. I edited my sentence. – Flows Oct 11 '16 at 13:43
  • Actually, a `commit-msg` add the change-id to the message if it is not always present. My idea based on your answer is to have a `prepare-commit-msg` calling my actual `commit-msg` in order to have a change-id. But it won't work – Flows Oct 11 '16 at 13:50