Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何有效地查看git下维护的重命名文件的内容?_Git_Rename_Git Log - Fatal编程技术网

如何有效地查看git下维护的重命名文件的内容?

如何有效地查看git下维护的重命名文件的内容?,git,rename,git-log,Git,Rename,Git Log,使用git命令 我得到了影响文件foo.c的更改列表。然后,给定git日志输出中的hash H,我可以运行 git show H:foo.c 查看该时间点的文件内容 如果文件已重命名,则此操作无效;git抱怨: fatal: Path 'foo.c' exists on disk, but not in '34cd81d75f398ee455e61969b118639dacbfd7a6'. 获取重命名文件内容的最有效方法是什么?编辑:我刚刚意识到,您可以将--diff filter=R添加到

使用git命令

我得到了影响文件
foo.c
的更改列表。然后,给定git日志输出中的hash H,我可以运行

git show H:foo.c
查看该时间点的文件内容

如果文件已重命名,则此操作无效;git抱怨:

fatal: Path 'foo.c' exists on disk, but not in '34cd81d75f398ee455e61969b118639dacbfd7a6'.

获取重命名文件内容的最有效方法是什么?

编辑:我刚刚意识到,您可以将
--diff filter=R
添加到
git日志--follow
命令中。(我已将其编辑到答案中。)这将减少要检查的哈希数,仅限于重命名相关文件的哈希数。但是,仍然必须单独提取文件重命名


下面是上面的评论变成了一个脚本。。。没有关于它有多漂亮的说法。:-)它需要更多的工作才能变成真正的命令

#! /bin/sh

# Find git commits that rename the given path.
identify()
{
    local path="$1"
    local IFS=$'\t'

    # 1) log with follow to extract hashes that rename the file
    #
    # 2) diff-tree each hash to get the actual rename involved
    #
    # 3) extract the hash and :<...>R line with old/new paths
    git log --follow --pretty='format:%H' --diff-filter=R -- "$path" |
    while read hash; do
        echo $hash
        git diff-tree -M -r --diff-filter=R $hash
    done |
    while read line; do
        case "$line" in
        :*)
            set -- $line
            # $2 is oldname, $3 is new name
            # we already have the new name, we need to back
            # up to the old one.
            [ "$3" != "$path" ] && continue
            # at and before this commit, use $2, the old name
            echo at and before $hash use $2
            path=$2;;
        *)
            hash=$line;;
        esac
    done
}

identify builtin/var.c
#/垃圾箱/垃圾箱
#Find git提交重命名给定路径。
识别()
{
本地路径=“$1”
本地IFS=$'\t'
#1)使用follow进行日志记录,以提取重命名文件的哈希值
#
#2)对每个散列进行diff树,以获得实际涉及的重命名
#
#3)使用旧/新路径提取哈希和:R行
git日志--follow--pretty='format:%H'--diff filter=R--“$path”|
读散列时;做
echo$hash
git diff tree-M-r--diff filter=r$hash
完成|
边读边做
大写“$line”
:*)
设置--$line
#$2为旧名,$3为新名
#我们已经有了新名字,我们需要回去
#直到老的那个。
[“$3”!=“$path”]&继续(&C)
#在此提交之前,使用$2,旧名称
在$hash处和之前回显使用$2
路径=$2;;
*)
哈希=$line;;
以撒
完成
}
识别内置/变量c

您需要按文件当时的名称显示文件。你怎么知道那个名字的?好吧,这远非完美,但可能是实现这一目标的一个好步骤:
git log--follow--pretty='format:%H'内置/var.c |同时读取哈希;do git diff tree-M-r--diff filter=r$hash;完成
(应用于git源代码树,发现builtin/var.c来自var.c和builtin-var.c)。但是它非常冗长,因为它在这些提交中发现了其他重命名…谢谢!我希望在git日志之后避免第二个昂贵的循环,这也需要大量资源。似乎没有有效的方法来做到这一点。git log命令知道重命名,但不提供直接使用它的方法。
git log
实际上直接从diffcore机器中获取它。不幸的是,这需要在启用重命名检测的情况下遍历所有差异,因此无论发生什么情况,这都有点昂贵,因此不是默认值;但是,如果能够说服git log--follow通过某个旁道偷偷地传递信息,那就太好了。否则,正如你所注意到的(和我所编码的),你必须重复所有那些昂贵的工作!
#! /bin/sh

# Find git commits that rename the given path.
identify()
{
    local path="$1"
    local IFS=$'\t'

    # 1) log with follow to extract hashes that rename the file
    #
    # 2) diff-tree each hash to get the actual rename involved
    #
    # 3) extract the hash and :<...>R line with old/new paths
    git log --follow --pretty='format:%H' --diff-filter=R -- "$path" |
    while read hash; do
        echo $hash
        git diff-tree -M -r --diff-filter=R $hash
    done |
    while read line; do
        case "$line" in
        :*)
            set -- $line
            # $2 is oldname, $3 is new name
            # we already have the new name, we need to back
            # up to the old one.
            [ "$3" != "$path" ] && continue
            # at and before this commit, use $2, the old name
            echo at and before $hash use $2
            path=$2;;
        *)
            hash=$line;;
        esac
    done
}

identify builtin/var.c