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

后端 未结 5 1989
一整个雨季
一整个雨季 2020-12-05 22:30

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 copyrigh

相关标签:
5条回答
  • 2020-12-05 23:11

    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.

    0 讨论(0)
  • 2020-12-05 23:13

    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.

    0 讨论(0)
  • 2020-12-05 23:21

    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
    
    0 讨论(0)
  • 2020-12-05 23:21

    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.

    0 讨论(0)
  • 2020-12-05 23:31

    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.

    0 讨论(0)
提交回复
热议问题