git 转换git仓库文件编码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11052199/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 13:56:08  来源:igfitidea点击:

Convert git repository file encoding

gitutf-8character-encodingcvscvs2svn

提问by Bertram Nudelbach

I have a large CVS repository containing files in ISO-8859-1and want to convert this to git.

我有一个包含文件的大型 CVS 存储库,ISO-8859-1并希望将其转换为 git。

Sure I can configure git to use ISO-8859-1for encoding, but I would like to have it in utf8.

当然我可以配置 gitISO-8859-1用于编码,但我希望它在utf8.

Now with tools such as iconvor recodeI can convert the encoding for the files in my working tree. I could commit this with a message like converted encoding.

现在使用诸如iconv或 之类的工具,recode我可以转换工作树中文件的编码。我可以用类似converted encoding.

My question now is, is there a possibility to convert the complete history? Either when converting from cvs to git or afterwards. My idea would be to write a script that reads each commit in the git repository and to convert it to utf8and to commit it in a new git repository.

我现在的问题是,是否有可能转换完整的历史记录?从 cvs 转换为 git 时或之后。我的想法是编写一个脚本来读取 git 存储库中的每个提交,并将其转换为utf8并提交到一个新的 git 存储库中。

Is this possible (I am unsure about the hash codes and how to walk through the commits, branches and tags). Or is there a tool that can handle something like this?

这可能吗(我不确定哈希码以及如何遍历提交、分支和标签)。或者有没有工具可以处理这样的事情?

采纳答案by Celada

You can do this with git filter-branch. The idea is that you have to change the encoding of the files in every commit, rewriting each commit as you go.

您可以使用git filter-branch. 这个想法是你必须在每次提交中更改文件的编码,在你进行时重写每个提交。

First, write a script that changes the encoding of every file in the repository. It could look like this:

首先,编写一个脚本来更改存储库中每个文件的编码。它可能看起来像这样:

#!/bin/sh

find . -type f -print | while read f; do
        mv -i "$f" "$f.recode.$$"
        iconv -f iso-8859-1 -t utf-8 < "$f.recode.$$" > "$f"
        rm -f "$f.recode.$$"
done

Then use git filter-branchto run this script over and over again, once per commit:

然后用于git filter-branch一遍又一遍地运行此脚本,每次提交一次:

git filter-branch --tree-filter /tmp/recode-all-files HEAD

where /tmp/recode-all-filesis the above script.

/tmp/recode-all-files上面的脚本在哪里。

Right after the repository is freshly upgraded from CVS, you probably have just one branch in git with a linear history back to the beginning. If you have several branches, you may need to enhance the git filter-branchcommand to edit all the commits.

在存储库从 CVS 新升级之后,您可能在 git 中只有一个分支,其线性历史可以追溯到开始。如果您有多个分支,则可能需要增强git filter-branch命令以编辑所有提交。