如何在git中列出目录树的更改历史记录 [英] How to list the change history of the directory tree in git

查看:425
本文介绍了如何在git中列出目录树的更改历史记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Git不会像这样跟踪目录。它只跟踪生活在某个目录中的文件。
(请参阅如何添加一个空的目录到Git仓库?



然而,如果我有一定的提交历史,我隐含地也有对目录树进行更改的历史记录。 / p>

那么我该如何回答以下问题:


  1. when foo / bar创建(在git术语中:何时是该目录中创建的第一个文件)。如果foo / bar在历史的某个时间被删除并重新创建,可能会有多个限定提交。

  2. 何时删除了目录foo / bar(在git术语中:何时从该目录中删除最后一个文件)。如上所述,可能有多个合格提交。

  3. 历史上任何时间点存在的foo / bar的子目录是什么

我能想到的最接近的是伪代码:

 循环结束所有提交(git rev-list --all)
从repo根目录开始
递归执行到目前为止重建的目录树
调用git ls-tree和grep树行
重建目录树的下一级别
end
end

显然这可能是用你最喜欢的脚本语言编写。

然后我拥有所有的目录树,我仍然需要以一种聪明的方式搜索它们,以便回答类型1 - 3的问题。再次,可行但可能而不是在几分钟之内。



问题是:


  1. 有没有简单的方法?
  2. >
  3. 如果不是:是否适合网上已有的脚本? (我的谷歌搜索没有透露任何信息,但我没有找到完美的搜索词)
  4. >

    对于问题1和问题2,这很简单:




    • 何时创建目录foo / bar?

      git log --oneline - foo / bar | tail -n 1


    • 何时删除目录foo / bar?

      git log --oneline - foo / bar | head -n 1




    然而,第三部分有点棘手,

    上面的两个命令给你 $ FIRST_REV (已创建)和

    下面的代码片段提供了所有提交树的地方:

     用于转换$(git rev-list FIRST_REV..LAST_REV)
    do
    git ls-tree -r -t $ rev | grep树| cut -f 2
    done

    然后,你有一个目录列表。但仍有重复。将该列表传递给 sort -u 并完成:

     #!/ bin / sh 
    for $(git rev-list FIRST_REV..LAST_REV)
    do
    git ls-tree -r -t $ r | grep树| cut -f 2
    完成| sort -u

    但是,您将丢失这些目录受到影响的提交信息。这是缺点。



    并且,这假定 foo / bar 只创建一次,不再存在。

    Git does not track directories as such. It only tracks files that live in some directory. (See How can I add an empty directory to a Git repository?)

    However, if I have certain history of commits I implicitly also have history of changes to the directory tree.

    So how do I answer questions like:

    1. when was directory foo/bar created (in git terminology: when was the first file created in that directory). There could be more than one qualifying commit if foo/bar has been deleted some time in history and recreated later.
    2. when was directory foo/bar removed (in git terminology: when was the last file removed from that directory). As above there could be more than one qualifying commit.
    3. what are the subdirectories of foo/bar that existed in any point of time in history

    The closest I could come up with is in pseudo code:

    loop over all commits (git rev-list --all)
      start from repo root directory
      do recursively on the directory tree rebuilt so far
        call git ls-tree and grep the tree lines
        rebuild next level of directory tree
      end
    end
    

    Obviously this could be written in your favorite scripting language.

    Then I have all directory trees and I still need to search them in a smart way in order to be to answer questions of type 1 - 3. Again, doable but probably not in a couple of minutes.

    The questions are:

    1. Is there an easier way?
    2. If not: are suitable the scripts already on the net? (my googling didn't reveal any, but I did not come up the perfect search words either)

    解决方案

    For questions 1 and 2, it's quite easy:

    • when was directory foo/bar created?
      git log --oneline -- foo/bar | tail -n 1

    • when was directory foo/bar deleted?
      git log --oneline -- foo/bar | head -n 1

    However, the third part is a bit tricky and I cannot answer it completely.

    The two commands above give you $FIRST_REV (created) and $LAST_REV (deleted).

    The following snippet gives you all commits where the tree was modified:

    for rev in $(git rev-list FIRST_REV..LAST_REV)
    do
      git ls-tree -r -t $rev | grep tree | cut -f 2
    done
    

    Then, you have a list of directories that were present. But there are still duplicates. Pass that list to a sort -u and you're done:

    #!/bin/sh
    for r in $(git rev-list FIRST_REV..LAST_REV) 
    do 
        git ls-tree -r -t $r | grep tree | cut -f 2
    done | sort -u
    

    However, you lose the information of the commits where these directories were affected. That's the drawback.

    And, this assumes that foo/bar was created only once and is no longer present.

    这篇关于如何在git中列出目录树的更改历史记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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