【问题标题】:Iterate with Ruby through git commits for a particular branch通过特定分支的 git 提交使用 Ruby 进行迭代
【发布时间】:2015-01-17 02:38:51
【问题描述】:

我想使用 Rugged 遍历特定分支上的所有提交,从最旧的(第一个)到最新的(最后一个)。我想检查一下 SHA1 和每个人的评论。

也许我最好只运行 'git log --reverse' 并解析结果,但只要有这个很好的 Ruby 库可以与 Git 一起使用,我想我会使用它。

请原谅我,但我无法从 Rugged 或 libgit2 文档中完全弄清楚如何做我想做的事情。

【问题讨论】:

    标签: git libgit2 rugged


    【解决方案1】:

    你可以创建一个commit walker,像这样:

    require 'rugged'
    
    repo = Rugged::Repository.new('/path/for/your/repo')
    walker = Rugged::Walker.new(repo)
    walker.sorting(Rugged::SORT_REVERSE)
    walker.push(repo.branches["master"].target_id)
    
    walker.each do |commit| 
      puts "commit #{commit.oid}"
      puts "Author: #{commit.author[:name]} <#{commit.author[:email]}>"
      puts "Date:   #{commit.author[:time]}"
      puts "\n    #{commit.message}\n"
    end
    walker.reset
    

    您可以查看documentation 并阅读存储库中的testing code,了解您可以使用 Rugged 做什么。

    【讨论】:

    • 谢谢@hector。最终我想通了,做的几乎和你写的一样,但我使用了 SORT_DATE 并扭转了整个事情。也许你的好一点。
    猜你喜欢
    • 2014-09-19
    • 1970-01-01
    • 2021-05-31
    • 2012-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多