7

I'm running this shell script whenever I do a git commit -m "msg" which writes the commit log to a php file. I want it to include the current commit message as well that I am doing at that time.

My shell script:

#!/bin/sh
path="path/to/gitlog.php"
echo "<?php $git_log = array(" > $path
git log --date=iso --pretty=format:'array("%h","%an","%ad","%s"),' >> $path
echo ");" >> $path

The gitlog.php gets saved to my repository, which I will then git push.

I currently have it in 'pre-commit' hook, is there a way to get the committing message within this hook?

My use case

I am the only developer in the project. Time is short! it's for others who are involved in the project to see progress and read the descriptive commit logs without the overhead of me having to double up. The log is output to a dashboard that everyone has access to.

Git is not running on the dashboard server, and files are deployed from a repository hosting company (Beanstalk)

12
  • A pre-commit hook runs before the commit, so it can't possibly include the commit itself. You can make a new, additional commit after you've made the commit, and you can even make that new commit have a different parent than the most recent commit (this is how git commit --amend works), but you can't change the existing commit. If you're trying to include version information, the usual method with git is to keep that file outside git, especially if the entire file is generated from a commit (as this one would be). To view an old commit you check it out, then run your "generate version".
    – torek
    Commented Dec 5, 2015 at 0:16
  • @torek, thks - is there not a hook that gets called before the actual commit is performed, and gets passed the message data, that I can manually append the commit message and date to the log, re-perform the 'git add .' and finally continue with the committing?
    – digout
    Commented Dec 5, 2015 at 12:46
  • Aha, yes, there's the commit-msg hook. It's intended to let you alter the message (for formatting standards) or reject the commit if the message is wrong. I have never actually used this hook myself but it runs at the right point in the commit process. I would recommend not doing git add . (that might add files you are deliberately leaving modified-but-not-staged) but git add $path might well work here. (I'd suggest testing with commits of specific files as well, though, and/or git commit -a, to see if it works right there.)
    – torek
    Commented Dec 5, 2015 at 20:10
  • 4
    There are several problems with trying to use hooks for generating something like changelog. They will tend to generate a lot of changes, that will get in your way during merging and don't forget that they are local to repository and have to be set on each repository separately. Depending on where you want the changelog to appear, there is probably a more reliable way to get it.
    – Jan Hudec
    Commented Jan 12, 2016 at 20:14
  • 1
    Sounds scary to me. Might work if you are the only contributor for the project, however I still wouldn't do it. You already have the changelog in git, not sure what you hope to achieve by duplicating this into another file. In general the git changelog often documents changes to a very detailed level and is not suitable for the changelog file. Commented Jan 19, 2016 at 9:57

2 Answers 2

1

Another option could be to create an appropriate hook that writes the changelog to a file outside your repo, and then pushes the file to somewhere accessible by external users. No need to commit the changelog then, which is the cause of problems.

0

Given your use case, I would suggest :
move your pre-commit hook on your local machine to the post-checkout hook on the dashboard server (I imagine there is a clone of the repo behind that dashboard site ?)

1
  • No, the files are deployed to the 'dashboard server' from a repo hoster (Beanstalk)
    – digout
    Commented Jan 21, 2016 at 15:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.