tags:

views:

58

answers:

3

Every time I do a merge I need for a merge commit to be generated and I would like it to have more than just the summary of all the commits.

My question is how can I format git-fmt-merge-msg or what determines this automated message (I can do this manually after a commit by amending it and using git-log --pretty=format:'...')

For example I would like to format it as such:

Merge branch 'test'
  * test:
   [BZ: #123] fifth commit subject
   [BZ: #123] fourth commit subject
   [BZ: #123] third commit subject
   [BZ: #123] second commit subject
   [BZ: #123] first commit subject


Merge details:
   [BZ: #123] fifth commit subject
        at 2010-06-30 11:29:00 +0100
    - fifth commit body

   [BZ: #123] fourth commit subject
        at 2010-06-30 11:22:17 +0100
    - fourth commit body

   [BZ: #123] third commit subject
        at 2010-06-30 11:21:43 +0100
    - third commit body

   [BZ: #123] second commit subject
        at 2010-06-30 11:21:30 +0100
    - second commit body

   [BZ: #123] first commit subject
        at 2010-06-30 11:29:57 +0100
    - first commit body

edit:

for non-ideal fix see answer below

but the real question is how to modify the information generated by the 'fmt-merge-msg'?

+1  A: 

You could try defining a prepare-commit-msg hook (the sample one does generate some custom "default commit messages")

VonC
Works great up to a point, I have to merge with --no-commit and only then commit, otherwise the prepare-commit-msg hook doesn't seem to run (merge_msg is standard) .. I would like for the merge full details to be added automatically, as these are the commits that i submit to CVS
shil88
@shil88: did you try and see if the `commit-msg` hook was activated during a `git merge`?
VonC
@shil88: just realized I have already answered your first question (http://stackoverflow.com/questions/2817897/use-hooks-in-git-to-import-and-export-to-cvs): did you finally manage to implement a hook for keeping git and cvs repo in synch?
VonC
@VonC sorry did not had enough reputation to comment on answers, but I implemented a CVSNT postcommand hook that after a small delay forces the 'main' Git repo to perform a cvsimport... still tweaking it though, I will post my solution when it is fully usable (committing automatically is a really bad idea and for now I prefer committing from my local repo)
shil88
@shil88: posting your solution to this other question would be very helpful :) And you will be able to select it as the official solution too. (Even if it is your own solution)
VonC
@VonC commit-msg hook does not activate
shil88
@shil88: darn... Is there any hook alive during a git merge?!
VonC
@VonC doesn't seem to be any, tried post-commit and update, neither seemed to work (used a simple echo "bla" in the hooks to check if they are active)
shil88
@VonC what I need is to modify the git-fmt-merge-msg command, but I'm guessing that is not customizable (at least easily)
shil88
@VonC I'm just going to create a command that produces my full commit msg and run it with -m option... I'm sure that will be the workable solution
shil88
@shil88: interesting. if you can update your question with yuyr solution, I will post the main principle in my edited answer.
VonC
I didn't finish it today, cause work got in the way, but as soon as there is a working solution I'll post it here
shil88
@shil88: no problem, and no rush (at least on my side ;) I'll be there)
VonC
A: 

There's also a --log option for git-merge now, which does half of what you want - it places the shortlog (commit summaries) in the merge message. A full solution will have to use a hook like in VonC's answer, though.

Jefromi
well I already have that option set globally, hooks won't do the trick as they do not activate during the merge (unless you stop it with --no-commit)
shil88
"hooks won't do the trick as they do not activate during the merge (unless you stop it with --no-commit)"; that a good question in itself: why no hook is being activating during the commit part of a merge?
VonC
@VonC: I think that's deliberate, actually - from a quick skim of the source, merge calls `commit_tree` directly.
Jefromi
+1  A: 

I've found there are two ways for solving this problem

note: don't use both at the same time, as if the commit fails to merge it will add the log again to the bottom.

personal note: I'm using the first solution as it relies entirely on git's hooks and config properties, instead of an external script.
For a real solution one would have to extend a git command named 'fmt-merge-msg' that generates the oneline descriptions when passing the --log option (if you really need this solution you'll have to create your own patch (for git) and compile it from source).

1. using prepare-commit-message as VonC suggested
this solution has the problem that you need to interrupt the commit and then commit manually

setting an alias that will build the desired commit message:

[alias]  
lm = log --pretty=format:'%s%n   by %C(yellow)%an%Creset (%ad)%n %n%b' --date=local

creating the prepare-commit-msg hook by creating an executable prepare-commit-msg in $GIT_DIR/hooks/ (example script below)

#!/bin/sh
#...

case "$2,$3" in  
  merge,)  
  echo "Merge details:" >> $1  
  echo "" >> $1  
  git lm ORIG_HEAD..MERGE_HEAD >> "$1" ;;  
*) ;;  
esac  

one should define an alias commit msg such as

[alias]  
m = merge --no-ff --no-commit

2. using a custom command that will generate the merge automatically
(using the lm alias created in 1.)

#!/bin/sh

echo ""
echo "merge with commit details -- HEAD..$1"
git merge --no-ff --no-log -m "`git lm HEAD..$1`" --no-commit $1

and then execute a rather rigid command:

./cmd-name <branch to merge>

if you still wish to have the oneline description of the commits you'll need to add new commands or whatever to the -m argument (if you use --log then it will be generated on the bottom)

shil88
Excellent feedback: +1
VonC