Git在制表符和空格之间转换,但仅在某些情况下会转换 [英] Git convert between tabs and spaces but only sometimes

查看:68
本文介绍了Git在制表符和空格之间转换,但仅在某些情况下会转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我是git的新手.就像,如果索引在暂存区中命中我,我几乎无法从索引中判断出该缓存.或类似的东西.有了这种方式,我的问题是:

First, I'm a newbie to git. Like, I could barely tell a cache from an index if it hit me in the staging area. Or something like that. With that out of the way, my problem is this:

假设我想从事一个项目,该项目的编码风格要求缩进空间,但是我喜欢制表符.看来我可以使用清洁和涂抹功能,但是有一个陷阱.编码风格未始终遵循,并且有些文件在同一行上混合了制表符和空格.因此,幼稚的方法会导致我改变一行,但是偶然地创建了一个庞大的提交,使该项目完全符合其自身的标准.这将是很棒的,只不过差异不那么有用,所以我最终遇到了新的敌人.

Suppose I want to work on a project whose coding style mandates spaces for indentation, but I like tabs. It seems that I can use the clean and smudge features, but there's a catch. The coding style is not followed consistently, and there some files that mix tabs and spaces on the same line. Thus a naïve approach would result in me making a one line change, but accidentally creating a massive commit which brings the project into full compliance with its own standards. This would be great except that the diffs would be less useful so I end up with new enemies.

所以问题是:是否有一种方法可以使这种魔术发挥作用,使得如果我不触摸文件,它就不会出现在画面中?(即使我只更改一个字符,我也愿意对我触摸的文件的空白承担全部责任.)

So the question is: is there a way to get this magic working in such a way that if I don't touch a file, it stays out of the picture? (I'm willing to take full responsibility for the whitespace of files I do touch, even if I change only a single character.)

好的,我刚刚接受了昨天接受的答案.我很确定这对我很无礼.我的借口是我今天才开始进行测试.由于显然已经有两个人误解了我,所以让我清楚我的实际做法,所以也许有人可以告诉我我是否感到困惑和/或困惑.

Okay, I just in-accepted the answer I accepted yesterday. I'm pretty sure this is very rude of me. My excuse is that I only got around to testing it today. Since apparently two people misunderstood me already, let me be clear on what I actually did, so maybe someone can tell me if I'm being confused and/or confusing.

$ ls -a
.  ..  t.txt
$ hd t.txt # file contains 3 bytes: a tab, a capital A, and a newline
00000000  09 41 0a                                          |.A.|
00000003
$ git init
Initialized empty Git repository in /home/marvy/test/.git/
$ git config --local git config --local user.name me
$ git config --local user.email me@example.com
$ git add t.txt
$ git commit
[master (root-commit) 959bf99] testing cleverness of git status
 1 file changed, 1 insertion(+)
 create mode 100644 t.txt
$ echo '*.txt filter=tabspace' > .git/info/attributes
$ cat .git/info/attributes
*.txt filter=tabspace
$ git config --local filter.tabspace.smudge unexpand
$ git config --local filter.tabspace.clean expand
$ rm t.txt
$ git checkout t.txt
$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   t.txt

no changes added to commit (use "git add" and/or "git commit -a")
$ git help --stackoverflow

正如我们在这里看到的,即使我刚刚签出,git status也报告t.txt已被修改.如果您运行git diff,它将声称我想将制表符转换为空格.我在做错什么吗?

As we can see here, git status reports that t.txt is modified, even though I just checked it out. If you run git diff, it will claim I want to convert tabs to spaces. Am I doing something wrong?

推荐答案

您可以使用预提交钩子,并且仅循环浏览已编辑的文件,并用空格替换选项卡.类似于以下内容:

You could use a pre-commit hook and only loop through your edited files and replaces the tabs with spaces. Something like the below:

FILES=`git status -s -uno | egrep '^M' | sed 's/^M//'`

for FILE in $FILES
do
    (sed -i 's/[[:space:]]*$//' "$FILE" > /dev/null 2>&1 || sed -i '' -E 's/[[:space:]]*$//' "$FILE")
fi

这篇关于Git在制表符和空格之间转换,但仅在某些情况下会转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆