views:

164

answers:

4

Suppose I have a file "ChangeLog" in branch "master". I want to record information about all changes in any branch in this file (in more detail than in a commit message and with other descriptive information).

I git checkout -b revA, perform edits, update ChangeLog and git commit.

I then git checkout -b master. This checkout will replace ChangeLog with the version in branch "master".

What I would like is for ChangeLog to automagically be the most recently modified version irrespective of which branch I've checked out. I don't want to manually merge (or, most likely, forget to merge) ChangeLog from some other branch into my current branch.

I haven't found anything which seems to allow this. Is it possible to do this?

+1  A: 

Put it in a separate repository and use a submodule to link it into each branch.

Steven Schlansker
+1  A: 

Write a post-commit hook to merge the file into the other branches. http://kernel.org/pub/software/scm/git/docs/githooks.html

Mark Carey
This will work and does answer the question, so I'm reluctantly giving it +1, but this is a really bad idea. Doing this means that the ChangeLog for your latest release may contain information about changes that have not been released. Why would you want that?
William Pursell
I wouldn't do it, but the question presuposes the situation you are concerned with. I'd much rather branch and merge explicitly.
Mark Carey
@William - the ChangeLog information being kept is acceptable for release.
themis
+2  A: 

You could use a post-checkout hook:

#! /bin/sh

newref="$2"
isbranch="$3"

# ignore file checkouts
if test $isbranch -eq 0; then
  exit 0
fi

path=ChangeLog

git for-each-ref --sort=-committerdate \
                 --format='%(objectname) %(refname:short)' \
                 refs/heads/\* |
while read sha ref; do
  if git rev-parse --verify --quiet "$sha:$path" >/dev/null
  then
    if test "$newref" != "$sha"; then
      echo Checking out $path from $ref
      git checkout $sha -- "$path"
    fi
    break
  fi
done

It will still be up to you to add and commit the ChangeLog on your branch if appropriate.

This hook sorts branches by their heads' respective commit dates, which could be problematic. Say you create a topic branch rooted at a commit in master's history. Even though its snapshot of ChangeLog is older in the sense of calendar time, the hook above will treat it as the newest because it is referenced by a commit that was created more recently, so be careful that you don't accidentally lose work by switching branches when you have unstaged changes to ChangeLog.

Greg Bacon
This is just what I want. You rock! I am able to have a file describing what my branches contain and (at a high level) what changes they incorporate.
themis
You're welcome! I'm glad it helps.
Greg Bacon
+4  A: 

I know that this answer is begging the question, but I think that anything else is setting yourself up for a maintenance headache.

If you put sufficient description into your commit messages then git log fits the requirement. All the changes that are in a particular branch are described exactly by the commits that go into it and it is impossible for it to be out of date or have descriptions for changes that aren't in it ascribed to it.

Ever since I've moved to git I've given up maintaining a change log manually. For me, it had no value and significant cost.

There is no reason not to put full descriptions in commits. With --amend you can fix up spelling errors and add detail, and if you stick to the convention of having a brief subject line and a fuller description then you can choose between short and long log formats.

Charles Bailey
The ChangeLog is at a more high level than git log, indicating what a particular branch is about and other such information which would be more difficult to glean from massive git log output.
themis
@themis: Surely that depends on what you put in your commit message? In your question you said 'in more detail than in a commit message' but now you say 'at a more high level'. Are these compatible?
Charles Bailey