重写git历史记录以使crlf保持一致 [英] Rewriting git history to make crlf consistent

查看:136
本文介绍了重写git历史记录以使crlf保持一致的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在做出可能的切换之前,我想重写提交,其中父代和当前提交有不同的LR / CRLF编码。



因此,我试着运行 todos以下单行程 如果文件的父版本包含CR。

  $ git filter-branch --tree -filter'
echo
P = $ GIT_COMMIT ^;
FILES = $(git diff --name-only $ P);在$ FILES中
;做
如果! git cat-file -e $ P:$ a;然后回显没有父母;继续;网络连接;
如果git显示$:$ a | grep -qUP'\r';那么
回显父母是dos;
todos $ a;
else
echoparent is unix;
fromdos $ a;
fi;
done'23498f..HEAD

它不起作用。任何人都可以发现错误或给出解决这个问题的方法吗?

解决方案

我正在回答我自己的问题。我原来的解决方案中的错误是, FILES 集合是错误的,因为diff不是在重写的父节点和当前的提交节点之间进行的,而是在原始父母和当前提交。



在遍历一组提交时,需要更改的文件不是一组文件触及一个提交,但也有一些父提交混乱的行结束的文件集。



这意味着我没有得到正确的集合的文件。为过滤分支表达式提供了一个 map 函数,可以将原始转换转换为重写的转换。当我使用该功能时,它可以正常工作。



由此产生的单行如下所示:

  $ git filter-branch -f --tree-filter'
echo\ $ $ GIT_COMMIT;
P = $(git rev-parse $ GIT_COMMIT ^);
echo $ P;
P = $(map $ P);
echo $ P;
git cat-file commit $ GIT_COMMIT;
FILES = $(git diff --name-only $ GIT_COMMIT $ P);
回显文件:\ $ $文件;在$ FILES中
;做
git cat-file -e $ P:$ a> / dev / null 2>& 1 ||继续;
如果git显示$ P:$ a | grep -qUP'\r';那么
echoparent is dos $ a;
todos $ a;
else
echoparent is unix $ a;
fromdos $ a;
fi;
完成;
git add $ FILES;'a6d9e..HEAD

大部分aI都没有认为最后一个'git add $ FILES'是必要的,但这是我用过的命令,我不想提供不正确的答案。



注意:也应该可以定义 FILES = $(git diff --name-only a6d9e HEAD),因此在遍历提交时使用固定集合。这可能简单得多,但我没有那样做。


I have a git repository that has a fine variation of LF and CRLF files.

Before making a possible switch, I want to rewrite commits where the parent and the current commit have different LR/CRLF encodings.

So I tried the following "one-liner" (slightly edited) where I try to run todos if the parent version of a file contains a CR.

$ git filter-branch --tree-filter '
  echo
  P=$GIT_COMMIT^;
  FILES=$(git diff --name-only $P);
  for a in $FILES; do
     if ! git cat-file -e $P:$a; then echo "no parent"; continue; fi;
     if git show $:$a | grep -qUP '\r'; then
        echo "parent is dos";
        todos $a;
     else
        echo "parent is unix";
        fromdos $a;
     fi;
  done' 23498f..HEAD

It doesn't work. Can anyone spot the error or give a solution to this problem?

解决方案

I am answering my own question. The bug in my original solution is that the FILES set is wrong because the diff is not taken between the rewritten parent and the current commit, but between the original parent and the current commit.

When traversing a set of commits like this, the files that need to be changed isn't the set of files touched by a commit, but also the set of files where some parent commit messed up the line endings.

This means that I don't get the correct set of files. There is a map function provided to filter-branch expressions that can transform an "original" rev to the rewritten rev. When I use that function, it works correctly.

The resulting "one-liner" looks like this:

$ git filter-branch -f --tree-filter '                         
    echo "\n $GIT_COMMIT";
    P=$(git rev-parse $GIT_COMMIT^);
    echo $P;
    P=$(map $P);
    echo $P;
    git cat-file commit $GIT_COMMIT;
    FILES=$(git diff --name-only $GIT_COMMIT $P);
    echo "FILES:\n$FILES"; 
    for a in $FILES; do
        git cat-file -e $P:$a > /dev/null 2>&1 || continue;
        if git show $P:$a | grep -qUP '\r'; then
           echo "parent is dos $a";
           todos $a;
        else
           echo "parent is unix $a";
           fromdos $a;
        fi;
    done;
    git add $FILES;' a6d9e..HEAD 

Most of the aI don't think the last 'git add $FILES' is needed, but that is the command that I used, and I don't want to provide an incorrect answer.

Note: It should also be possible to define FILES=$(git diff --name-only a6d9e HEAD) and thus use a fixed set when traversing the commits. That is probably much simpler, but I didn't do that.

这篇关于重写git历史记录以使crlf保持一致的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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