2

My aim is to maintain a log of all the commit history/messages at the end of each file in my repository. I used the commit-msg hook to get the commit message, prepare it and append it to the file.

However, I notice that since the file changes after staging, git status still shows as modified. What is the proper way to do this?

9
  • 5
    You already have a log of the commit history, it's the Git log. Committing the Git log to Git makes very little sense. Commented Jun 7, 2014 at 8:48
  • 1
    To answer your question though, what you want to do involves modifying the file, which of course will then appear as a (new) unstaged change. Commented Jun 7, 2014 at 8:49
  • 1
    possible duplicate of In Git, how can I write the current commit hash to a file in the same commit Commented Jun 7, 2014 at 8:54
  • @OliCharlesworth It's not quite the same: changing a commit will change its hash, so that other question could never work. Changing a commit does not need to change its commit message, so this question should be answerable (even though it's probably a bad idea) Commented Jun 7, 2014 at 9:01
  • @hvd: It sounds like the user wants to embed the commit message into the file being committed, in which case the commit would have to change, no? Commented Jun 7, 2014 at 9:05

2 Answers 2

2

Although I agree with Oli Charlesworth's comments that you should not be doing this, it is actually possible. Here is a simple post-commit hook that rewrites the commit, appending the commit message to the file "changelog".

if ! test ${GIT_BYPASS_POST_COMMIT+set}
then
    export GIT_BYPASS_POST_COMMIT=1

    git show --format=%B -s >>changelog
    git add changelog
    git commit --amend -C HEAD
fi

If you try this, I expect you will quickly find that it does not play nice with normal use of git. The simplest example is that if you amend a commit, you will be amending the commit that already changes changelog, so the hook ends up duplicating the commit message.

It's up to you to say whether you want to make an attempt to get this to work, or just give up on it, but I recommend the latter.

Sign up to request clarification or add additional context in comments.

Comments

1

I adapted the post-commit hook from @hvd and was able to modify the code to automatically add the commit message details to the checked in file location.

#!/bin/sh
path="D:/temp.txt"
git diff HEAD~1 --name-only > ${path}
if ! test ${GIT_BYPASS_POST_COMMIT+set}
then
    export GIT_BYPASS_POST_COMMIT=1
    for line in `cat $path`; do
        if [[ ! $line =~ version.txt ]];then
            file_path=`dirname $line`
            git show --format=%B -s | cut -d '#' -f2 > ${file_path}/version.txt
            echo " - " >> ${file_path}/version.txt
            echo $line >> ${file_path}/version.txt
            git add ${file_path}/version.txt
        fi  
    done    
    git commit --amend -C HEAD
fi

Initially it will capture all the files changed during the commit and it will save to a file. Now this will read each files in the file list excluding the version.txt file and will add the version.txt which contains *"commit message - file name"* And it will commit again to the last commit.

Note : if there are changes to specific directories, those directories will have a version file added.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.