推荐一个Git属性扩展

svn有相应keywords属性扩展,git的实现相对复杂一点。如何轻松的实现提交文件后能自动更新文件版本等其他信息呢?

github上发现了一个不错的扩展,扩展地址:https://github.com/turon/git-rcs-keywords,脚本是用perl编写。linux系统测试非常不错。

下面是原文介绍:

This module provides a means to add keyword expansion of the following
standard RCS tags to your git projects:

$Id$
$Date$
$File$
$Author$
$Revision$
$Source$

The mechanism used are filters. The smudge filter is run on checkout, and the
clean filter is run on commit. The tags are only expanded on the local disk,
not in the repository itself.

To start, you need to add the following to ~/.gitconfig:

[filter "rcs-keywords"]
clean = .git_filters/rcs-keywords.clean
smudge = .git_filters/rcs-keywords.smudge %f

Then, to add keyword expansion, simply add these files to your project:
/.gitattributes - *.c filter=rcs-keywords
/.git_filters/rcs-keywords.smudge - copy this file to project
/.git_filters/rcs-keywords.clean - copy companion to project

Note: This feature is known not to work in git 1.6.2.2 and 1.7.3.*, and
verified to work in git 1.7.4.4 and 1.7.4.msysgit.0

我的渣理解:
实现的原理就是在checkout的时候从log中提取对应的信息替换文件中设置的标签,在commit的时候再把这些标签替换回来。

  • 使用也很简单,编辑git的全局配置文件.gitconfiglinux一般在用户目录下~/.gitconfig,增加如下内容:
      [filter "rcs-keywords"]
          clean  = .git_filters/rcs-keywords.clean
          smudge = .git_filters/rcs-keywords.smudge %f````
    
    
  • 把插件中的.gitattributes和目录.git_filters复制到你的项目目录下面.
     cp ./{.gitattributes,./git_filters} -R ~/Project/
    
    
  • 编辑.gitattributes文件,把要替换的文件后缀加上。
    *.php filter=rcs-keywords
    *.ini filter=rcs-keywords
    *.html filter=rcs-keywords
    *.css filter=rcs-keywords
    

so easy。现在文件的版本等信息就可以直接在文件中查看了。

标签: git, keywords, 关键词扩展

添加新评论