如何 select 适当的 diff /patch 用于 rugged 的​​提交

How to select the appropriate diff /patch for a commit with rugged

我尝试在 git 存储库的本地副本中获取某个日期之后完成的提交,然后提取文件的相关修改。

如果我想将其与 git 命令进行比较,它将是:

git log -p --reverse --after="2016-10-01"

这是我使用的脚本:

require "rugged"
require "date"

git_dir = "../ruby-gnome2/"

repo = Rugged::Repository.new(git_dir)
walker = Rugged::Walker.new(repo)
walker.sorting(Rugged::SORT_DATE| Rugged::SORT_REVERSE)
walker.push(repo.head.target)

walker.each do |commit|
  c_time = Time.at(commit.time)
  next unless c_time >= Date.new(2016,10,01).to_time

    puts c_time
    puts commit.diff.size
    puts commit.diff.stat.inspect
end

问题是好像修改了很多文件这里是这个脚本输出的结尾:

2016-10-22 17:33:37 +0200
2463
[2463, 0, 271332]

也就是说有2463个文件modified/deleted/replaced。而 git log -p --reverse --after="2016-10-22" 显示只有 2 个文件被修改。

如何获得与 git 命令相同的结果?即我怎样才能找到这个提交修改的真实文件?

当我克隆 ruby-gnome2/ruby-gnome2 时,它告诉我有 2400 多个文件,所以你得到 2463,我觉得 所有 文件都已被修改.

这不同于 rugged#commit.diff, which diff by default the current commit (returned by the Walker) 针对第一个父提交的正常行为。

检查您是否将 git config core.autocrlf 等设置设置为 true(这可能会更改您本地存储库中的 eol)。

由于我没有从 rugged 团队那里得到任何答复,所以我在 https://github.com/ruby-gnome2/ggit 此处为 libgit2-glib 做了一个 ruby gobject-introspection 加载程序 https://github.com/ruby-gnome2/ggit

现在我可以找到对应于git命令行界面的差异和日志:

require "ggit"

PATH = File.expand_path(File.dirname(__FILE__))

repo_path = "#{PATH}/ruby-gnome2/.git"

file = Gio::File.path(repo_path)

begin
  repo = Ggit::Repository.open(file)
  revwalker = Ggit::RevisionWalker.new(repo)
  revwalker.sort_mode = [:time, :topological, :reverse]
  head = repo.head
  revwalker.push(head.target)
rescue => error
  STDERR.puts error.message
  exit 1
end

def signature_to_string(signature)
  name = signature.name
  email = signature.email
  time = signature.time.format("%c")

  "#{name} <#{email}> #{time}"
end

while oid = revwalker.next do
  commit = repo.lookup(oid, Ggit::Commit.gtype)

  author = signature_to_string(commit.author)
  date = commit.committer.time
  next unless (date.year >= 2016 && date.month >= 11 && date.day_of_month > 5)
  committer = signature_to_string(commit.committer)

  subject = commit.subject
  message = commit.message

  puts "SHA: #{oid}"
  puts "Author:  #{author}"
  puts "Committer: #{committer}"
  puts "Subject: #{subject}"
  puts "Message: #{message}"
  puts "----------------------------------------"

  commit_parents = commit.parents
  if commit_parents.size > 0
    parent_commit = commit_parents.get(0)
    commit_tree = commit.tree
    parent_tree = parent_commit.tree

    diff = Ggit::Diff.new(repo, :old_tree => parent_tree,
                          :new_tree => commit_tree, :options => nil)

    diff.print( Ggit::DiffFormatType::PATCH ).each do |_delta, _hunk, line|
      puts "\t | #{line.text}"
      0
    end

  end

end