0

Our customer insists of having a file history as comments at the top of the file, even though we use git for version control. As a lazy developer (and having serious concerns about the usefulness of in-file histories compared to Version Control systems), I am wondering if I can automate this. Since it is a frequent request, I am wondering if anybody has done this before.

Is it possible to write the commit message automatically to a file, e.g. as a post-commit hook? I've read about git attributes, but they are only expanded when the file is checked out and will not be part of the content of the file in the repository.

I found an example to modify a file in a pre-commit hook, but since I need the commit message, it would have to be done in the post-commit hook. I am not sure if I can amend the commit then.

Jens
  • 9,058
  • 2
  • 26
  • 43
  • Just to prevent discussions: we tried to argue that the in-file history is useless, pointing out that version control is used and is much more applicable, but they have a rule saying that there must be a in-file history. My domain is very conservative and rules are not easily changed ... – Jens May 03 '16 at 09:01
  • Putting the commit message in the file would mean that the file gets modified, and you'd have to commit it again, which means... – 1615903 May 03 '16 at 09:02
  • You are right that it is a bad idea. That said, git's most direct support for this is via "smudge" and "clean" filters, where you can implement arbitrary keyword expansion. It has numerous flaws though. See http://stackoverflow.com/questions/1476979/git-smudge-filter for some, and an alternative. – torek May 03 '16 at 09:05
  • @1615903 It's possible to do it in the pre-commit hook. I've included an example. – Jens May 03 '16 at 09:05
  • @1615903 As far as I understand, a pre-commit hook cannot add the commit message because it has not been entered at that stage. – Jens May 03 '16 at 09:06
  • True, sorry. But a `commit-msg` hook could do that, see here: https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks#Client-Side-Hooks – 1615903 May 03 '16 at 09:07
  • @torek smudge and clean work during staging or checkout, right? So the file would be modified when we check it out, but I cannot add the text during commit. I need the changes in the file in the repository. – Jens May 03 '16 at 09:08
  • Correct, the version in the repository would not contain the text. However, if you examine the internals of other VCSes like RCS or CVS that do satisfy these customers, you find the in-repo versions *also* have keywords "cleaned" (though there are variations on this theme). – torek May 03 '16 at 09:11
  • A reference to CVS could be a way to go. We convinced them to let us use git instead, so using it a gold-standard could work. I will give it a try. – Jens May 03 '16 at 09:13
  • In that case (though I admit this is a long shot) perhaps you can convince them that since git records information on a per-*commit* basis (not per-file), a file included in builds that contains the complete "git log" history up to that point of the build gets them all the information they would want. From there it's a smaller step to convince them that the SHA1 of that file gets them the same information, and from there they learn that the SHA1 of the commit gets that info as well, and then ... voila! :-) (Well, it's a nice fantasy, at least.) – torek May 03 '16 at 09:18

0 Answers0