Snail

导航

 

Something about git attributes. Reference: here.

配置文件的查找顺序

  1. 当前仓库里的.gitattribute
  2. core.attributesFile所指定的文件(core.attributesFile的默认值为$XDG_CONFIG_HOME/git/attributes)
  3. 当前用户目录下的配置文件$HOME/.config/git/attributes
  4. 系统目录下的配置文件$(prefix)/etc/gitattributes

diff属性

用于配置如何生成diff信息.

1. Performing text diffs of binary files

git diff以文本的形式显示二进制文件.

比如, 一个ucs2编码的文件, git diff的时候是看不到具体改动的, 因为被当成了二进制文件.
这时候可以进行以下配置(假设文件后缀为".reg):

  1. 配置.gitattribute, 通过diff属性指定diff driver:
*.reg diff = ucs2
  1. ~/.gitconfig中配置diff driver:
[diff "ucs2"]
    textconv = ~/scripts/ucs2.sh

The textconv config option is used to define a program for performing such a conversion.
The program should take a single argument, the name of a file to convert, and produce the resulting text on stdout.

  1. 编码转换脚本~/scripts/ucs2.sh:
#!/bin/bash
cat $1 | iconv -f utf-16 -t utf-8

2. Defining a custom hunk-header

自定义diff头部信息.

git diff输出的diff信息中, 每块改动都是以@@ -k,l +n,m @@ TEXT的格式开头的,
其中的TEXT可以通过diff.*.xfuncname配置项来指定自定义的格式.

比如, qt的lupdate工具提取出来的翻译文件(.ts), 我想在diff的时候知道是每条改动对应的是哪个context下的,
而每个context都是以<name>.*</name>格式开头的, 这时候可以进行如下配置:

  1. .gitattribute
*.ts diff = ts
  1. ~/.gitconfig
[diff "ts"]
    xfuncname = "<name>.*</name>" # 正则表达式
posted on 2022-03-12 17:52  Snail-0304  阅读(283)  评论(0)    收藏  举报