1

I am using Git for the version control system.
We have a requirement to keep the commit message to be added into a new file and place in the parent folder.

eg : if the new file is placed in the /pr/ab/cd/ef/a.java location. A file should be created and placed with the commit message on the folder "ef".

I have tried to use the hooks to serve this purpose, but it is not working.

I had used it in the commit-msg/ pre-commit-msg template like the below.

To List the untracked files -->

git ls-files --others --exclude-standard > D:/temp.txt

if [ -f D:/temp.txt ] ; then
    for line in `cat D:/temp.txt`;
        do
            if [ -f $line ] ; then 
                cp_path=`dirname $line`
                echo `date +%d/%m/%Y:%H:%M`-$1-$line >> ${cp_path}/version1.txt
            fi
        done
fi

This is working fine when this is created as a shell script using git bash instead of hooks.

I am trying to make this work automatically on adding/commiting any new check-in's using TortoiseGit.

Kindly guide me.

nirmalraj17
  • 494
  • 7
  • 20
  • 1
    Out of curiosity, *why* do you want to do this? Why can't the commit message just live inside the git repository? Do you also want this new file added to git as well? – PatrickSteele Jan 02 '17 at 14:13
  • We have 2 separate branches one for release and one for development. In the development version, I am building those checked in files and adding it for deployment to the test systems. For release branch, I need to take the entire content for building and place it for deployment. Since I am taking the entire content I wont be able to capture what was the changes done. I need to go to git and check for the logs to see what was the changes done. So thought to add like this automatically by adding the checked in files to a file. – nirmalraj17 Jan 03 '17 at 05:50
  • I am trying to achieve some result which is similar to http://stackoverflow.com/questions/24095474/append-the-commit-message-automatically-to-the-file-being-committed-in-git or http://stackoverflow.com/questions/3442874/in-git-how-can-i-write-the-current-commit-hash-to-a-file-in-the-same-commit – nirmalraj17 Jan 03 '17 at 05:51
  • One idea: Get a list of hashes of the commits going in to a deployment and then use `git log` with a few options to [pull the commit message](http://stackoverflow.com/questions/3357280/print-commit-message-of-a-given-commit-in-git) for each hash. Might be easier than trying to mess with post-commit hooks and additional files in your repository. – PatrickSteele Jan 03 '17 at 13:52

1 Answers1

3

I finally managed to find a post-commit hook and modify it according to my requirement and it works perfectly.

#!/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