如何获取文件夹历史中的所有重命名?

huangapple go评论54阅读模式
英文:

How to get all the renames in a folder's history?

问题

我将使用git filter-repo从存储库中提取一个文件夹及其历史记录。

它的文档说道,

请注意,就像git本身一样,不会跟踪重命名,因此您可能需要指定多个路径,例如--path olddir/ --path newdir/

这意味着我需要明确指定文件夹外部文件的先前名称,以便保留它们的历史记录。

是否有一种便捷的方法来获取文件夹中所有文件的旧名称?

git log --follow的帮助文档说,"(仅适用于单个文件)",因此一个可能的算法是:

  • 对于文件夹中的每个文件
    • 运行git log --follow
    • 解析git日志输出以查找历史中的任何重命名

我想我可以这样做 - 即编写一个脚本,为每个文件发出git log --follow命令,并解析输出 - 这是最佳方法还是唯一方法吗?

英文:

I will use git filter-repo to extract a folder and its history from a repository.

Its documentation says,

> Note that much like git itself, renames are NOT followed so you may need to specify multiple paths, e.g. --path olddir/ --path newdir/

That's saying that I should to specify explicitly the previous names of files outside the folder, so that their history is retained too.

Is there a convenient way to get the old names of all files in a folder?

The help for git log --follow says, "(works only for a single file)" -- so an algorithm might be:

  • For each file in folder
    • Run git log --follow
    • Parse the git log output to look for any renaming in the history

I suppose I can do that -- i.e. write a script to issue git log --follow commands for each file, and parse the output -- is this the best or only way?

答案1

得分: 1

最有效的方法可能是对 git log --oneline --name-status --diff-filter=R 进行后处理,

如果需要的话,添加 -m--graph 可以更好地了解你所查看的内容,它将显示重命名的整个合并历史,这可能会变得庞大,但如果你需要的话,这是必要的。我不知道有任何3D历史可视化工具,那将非常方便。

无论如何,关于后处理的入门套件,这比在大型历史上运行 git filter-repo --analyze 要快得多:

git log --reverse -m --first-parent --name-status --pretty= --diff-filter=R \
        | awk '{ kids[$2]=kids[$2]"\n\t->\t"$3} 
            END{ for (k in kids) print k,kids[k] }' FS=$'\t' OFS=$'\t'
英文:

Most efficient is probably postprocess git log --oneline --name-status --diff-filter=R,

Add -m --graph to get a better grip on what you're looking at if needed, it'll show the entire merge history of the renames, that can get voluminous but if you need it you need it. I don't know of any 3d history visualisation tools, that'd be handy.

Anyway, a starter kit on the postprocessing, this runs rather dramatically faster than git filter-repo --analyze on large histories:

git log --reverse -m --first-parent --name-status --pretty= --diff-filter=R \
        | awk '{ kids[$2]=kids[$2]"\n\t->\t"$3} 
            END{ for (k in kids) print k,kids[k] }' FS=$'\t' OFS=$'\t'

答案2

得分: 0

除了 "git log --follow" 之外,还有一种获取 Git 文件夹中所有文件的旧名称的替代方法,需要使用 "git blame" 命令。git blame 命令可用于查看文件的完整历史,包括重命名和其他更改,以一种以行为单位显示文件更改的格式。

请参考以下示例,了解如何使用 git blame 命令获取文件夹中所有文件的旧名称:

git ls-tree -r --name-only HEAD | while read filename; do git blame --line-porcelain $filename | grep "^filename "; done | sort | uniq -c | sort -n

在上述示例中,git ls-tree 命令用于列出仓库的内容,而 while 循环迭代处理文件夹中的每个文件。然后使用 git blame 命令查看每个文件的历史,通过 grep 命令过滤输出,只显示文件名。最后,使用 sort 和 uniq 命令对输出进行排序和处理,以显示文件夹中所有文件的旧名称列表,以及每个文件名在仓库历史中出现的次数。

谢谢。

英文:

There is an alternative way apart from "git log --follow" to get the old names of all files in a folder using Git, it requires using the "git blame" command. The git blame command can be used to view the entire history of a file, including renames and other changes, in a format that shows the line-by-line changes to the file.

Refer below example for how you can use the git blame command to get the old names of all files in a folder:

git ls-tree -r --name-only HEAD | while read filename; do git blame --line-porcelain $filename | grep "^filename "; done | sort | uniq -c | sort -n

In the above example, the git ls-tree command is used to list the contents of the repository, and the while loop iterates through each file in the folder. The git blame command is then used to view the history of each file, and the output is filtered to show only the file names using the grep command. The final output is sorted and processed using the sort and uniq commands to show a list of all the old names of the files in the folder, along with the number of times each file name has appeared in the history of the repository.

Thanks.

答案3

得分: 0

Running git filter-repo --analyze produces various files including renames.txt.

It's not filtered per-path, it's for the whole repository.

You can parse it to find/filter whatever folders interest you.

英文:

Running git filter-repo --analyze produces various files including renames.txt.

It's not filtered per-path, it's for the whole repository.

You can parse it to find/filter whatever folders interest you.

huangapple
  • 本文由 发表于 2023年2月14日 01:08:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/75439052.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定