Does git have anything like `svn propset svn:keywords` or pre-/post-commit hooks?
Asked Answered
J

5

39

Browsing through the git documentation, I can't see anything analogous to SVN's commit hooks or the "propset" features that can, say, update a version number or copyright notice within a file whenever it is committed to the repository.

Are git users expected to write external scripts for this sort of functionality (which doesn't seem out of the question) or have I just missed something obvious?

Edit : Just to be clear, I'm more interested in, e.g.,

svn propset svn:keywords "Author Date Id Revision" expl3.dtx

where a string like this:

$Id: expl3.dtx 780 2008-08-30 12:32:34Z morten $

is kept up-to-date with the relevant info whenever a commit occurs.

Jacey answered 2/9, 2008 at 15:5 Comment(0)
S
14

Quoting from the Git FAQ:

Does git have keyword expansion?

Not recommended. Keyword expansion causes all sorts of strange problems and isn't really useful anyway, especially within the context of an SCM. Outside git you may perform keyword expansion using a script. The Linux kernel export script does this to set the EXTRA_VERSION variable in the Makefile.

See gitattributes(5) if you really want to do this. If your translation is not reversible (eg SCCS keyword expansion) this may be problematic.

Sommer answered 2/9, 2008 at 16:25 Comment(4)
I have found this that works fairly well with git projects: github.com/turon/git-rcs-keywordsAdventurous
@PA If the free answer doesn't suit you, please ignore it. From the links given here, it is easy enough to find a better explanation. Complaining also doesn't help you gain knowledge. And it is called Git, not GIT.Ballottement
Thank you for the pointer to the Git FAQ. I found this quote amusing: "Keyword expansion...isn't really useful anyway." As a support developer, I refer to source code attributes daily. For example, our database stored procedures are in source control with revision attributes at the top. I can use a revision attribute to verify which version of the stored procedure is installed on a database.Cyrus
@Paul Williams This is rather arrogant. I understand that feature will not be implemented because of technical difficulties. But imputing users what is useful for them and what is not is a bit offensive. This feature was requested many times by many people so probably it is very useful.Duchy
T
18

I wrote up a fairly complete answer to this elsewhere, with code showing how to do it. A summary:

  1. You probably don't want to do this. Using git describe is a reasonable alternative.
  2. If you do need to do this, $Id$ and $Format$ are fairly easy.
  3. Anything more advanced will require using gitattributes and a custom filter. I provide an example implementation of $Date$.

Solutions based on hook functions are generally not helpful, because they make your working copy dirty.

Tut answered 17/9, 2008 at 1:19 Comment(0)
S
14

Quoting from the Git FAQ:

Does git have keyword expansion?

Not recommended. Keyword expansion causes all sorts of strange problems and isn't really useful anyway, especially within the context of an SCM. Outside git you may perform keyword expansion using a script. The Linux kernel export script does this to set the EXTRA_VERSION variable in the Makefile.

See gitattributes(5) if you really want to do this. If your translation is not reversible (eg SCCS keyword expansion) this may be problematic.

Sommer answered 2/9, 2008 at 16:25 Comment(4)
I have found this that works fairly well with git projects: github.com/turon/git-rcs-keywordsAdventurous
@PA If the free answer doesn't suit you, please ignore it. From the links given here, it is easy enough to find a better explanation. Complaining also doesn't help you gain knowledge. And it is called Git, not GIT.Ballottement
Thank you for the pointer to the Git FAQ. I found this quote amusing: "Keyword expansion...isn't really useful anyway." As a support developer, I refer to source code attributes daily. For example, our database stored procedures are in source control with revision attributes at the top. I can use a revision attribute to verify which version of the stored procedure is installed on a database.Cyrus
@Paul Williams This is rather arrogant. I understand that feature will not be implemented because of technical difficulties. But imputing users what is useful for them and what is not is a bit offensive. This feature was requested many times by many people so probably it is very useful.Duchy
S
4

Git does have pre-commit and post-commit hooks, they are located inside each .git/hooks directory. Just modify the files and chmod them to make them executable.

Statocyst answered 2/9, 2008 at 16:36 Comment(0)
C
2

Although an age-old Q&A. I thought I'd throw one in since this has been bugging me for a long time.

I am used to list the files in a directory by reverse-time order (funny me, heh?). The reason is that I would like to see which files I have (or anyone else has) changed recently.

Git will mess my plans because when switching a branch the local repo will completely overwrite the tracked files from the (incremental... I know...) copies that sit in the packed local repo.

This way all files that were checked out will carry the time stamp of the checkout and will not reflect their last modification time..... How so annoying.

So, I've devised a one-liner in bash that will update a $Date:$ property inside any file WITH THE TIME OF LAST MODIFICATION ACCORDING TO WHAT IT HAS ON FILE SYSTEM such that I will have an immediate status telling of last modification without having to browse the git log , git show or any other tool that gives the commit times in blame mode.

The following procedure will modify the $Date: $ keyword only in tracked files that are going to be committed to the repo. It uses git diff --name-only which will list files that were modified, and nothing else....

I use this one-liner manually before committing the code. One thing though is that I have to navigate to the repo's root directory before applying this.

Here's the code variant for Linux (pasted as a multi-line for readability)

git diff --name-only | xargs stat -c "%n %Y" 2>/dev/null | \
perl -pe 's/[^[:ascii:]]//g;' | while read l; do \
   set -- $l; f=$1;  shift; d=$*; modif=`date -d "@$d"`; \
   perl -i.bak -pe 's/\$Date: [\w \d\/:,.)(+-]*\$/\$Date: '"$modif"'\$/i' $f; \
   git add $f; done

and OSX

git diff --name-only | xargs stat -f "%N %Sm" | while read l; do \
   set -- $l; f=$1; shift; d=$*; modif=`date -j -f "%b %d %T %Y" "$d"`; \
   perl -i.bak -pe 's/\$Date: [\w \d\/:,.)(+-]*\$/\$Date: '"$modif"'\$/i' $f; \
   git add $f; done
Clavicle answered 23/1, 2017 at 21:13 Comment(0)
R
1

Perhaps the most common SVN property, 'svn:ignore' is done through the .gitignore file, rather than metadata. I'm afraid I don't have anything more helpful for the other kinds of metadata.

Romaic answered 2/9, 2008 at 15:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.