Ruby 如何选择适当的差异/修补程序,以便使用
我尝试在git repo的本地副本中获取日期后完成的提交,然后提取对文件的相关修改 如果我想将其与git命令进行比较,它将是:Ruby 如何选择适当的差异/修补程序,以便使用,ruby,git,rugged,Ruby,Git,Rugged,我尝试在git repo的本地副本中获取日期后完成的提交,然后提取对文件的相关修改 如果我想将其与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(rep
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个文件被修改/删除/替换。而git日志-p-reverse-after=2016-10-22显示只有2个文件被修改
如何获得与git命令相同的结果?ie如何找到通过此提交修改的真实文件?当我克隆时,它告诉我有2400多个文件,因此您可以得到2463个,这让我感到震惊,因为所有文件都已修改
这与的正常行为不同,默认情况下,将当前提交与第一个父提交进行比较
检查是否有一些设置,如git config core.autocrlf设置为true,这可能会改变本地repo中的eol。由于我没有从Racking团队得到任何答案,我在这里为libgit2 glib做了一个ruby gobject内省加载程序 现在我可以找到与git命令行界面对应的diff和日志:
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
不幸的是,没有区别,看这里,这是我用于测试的回购协议。您可以很容易地看到,在2016-10-22日期之后,没有2463个文件被修改/删除/替换。@cedlemo:是的,git日志-name status-oneline-after=2016-10-22只显示3个文件。这意味着你的脚本有问题。是因为它在该日期之前计算提交,而不是在该日期之后计算提交?毕竟,git日志名status-oneline-before=2016-10-22-all-branchs | grep-e^M | sort | uniq | wc提供3362个文件。除非c_time>=Date.new2016,10,01.to_time是可疑的…除非c_time>=Date.new2016,10,01实际上并不重要,因为我显示的输出是提交上循环的最后一次迭代,这意味着最后一次提交修改了2463个文件,这是错误的。脚本可能有问题,但我不知道在哪里。@cedlemo脚本可能很好,但回购协议本身可能不好。查看我修改后的答案。我尝试了git config core.autocrlf true命令,但它没有改变任何内容。我将在Rocked github上发布一个问题,因为我看到有人已经抱怨Rocked::Commitdiff。我会随时通知你的。