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

我是研究僧i 提交于 2019-11-28 08:53:45
Jordi Bunster

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.

emk

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.

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.

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.

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!