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?

random
  • 9,774
  • 10
  • 66
  • 83
mammenx
  • 21
  • 2
  • 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. – Oliver Charlesworth Jun 07 '14 at 08: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. – Oliver Charlesworth Jun 07 '14 at 08:49
  • 1
    possible duplicate of [In Git, how can I write the current commit hash to a file in the same commit](http://stackoverflow.com/questions/3442874/in-git-how-can-i-write-the-current-commit-hash-to-a-file-in-the-same-commit) – Oliver Charlesworth Jun 07 '14 at 08: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) –  Jun 07 '14 at 09: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? – Oliver Charlesworth Jun 07 '14 at 09:05
  • @OliCharlesworth Yes, the commit will indeed change, but that doesn't need to be a problem here, because there will be no reference to the original commit. –  Jun 07 '14 at 09:06
  • @hvd: Ah, I see what you mean now. – Oliver Charlesworth Jun 07 '14 at 09:07
  • You can try to use smudge/clean filters (in `gitattributes`), adding changelog at checkout and removing it on checking. Then files in working area would have changelog appended, but version stored in repository would be clean (no changelog). – Jakub Narębski Jun 07 '14 at 09:29
  • Thanks for the suggestions. I agree, using git log would suffice. But is it possible to update the hash/index to the modified file and then proceed with commit ? – mammenx Jun 07 '14 at 15:48

2 Answers2

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.

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.

nirmalraj17
  • 494
  • 7
  • 20