我希望能够区分两个Grit::Commit
较新的对象。我所说的较新的意思是,如果commit_A
是 的父母(或父母的父母等)commit_B
,则commit_B
较新。这假设commit_A
和commit_B
在同一个分支上。
我考虑过使用Grit::Commit#date()
,但我认为这将是不准确的。
有任何想法吗?
这是我最终实施的。解释见评论。
性能很慢,但使用 repo.git.rev_list(通过 method_missing)时更糟。
require 'grit'
module Grit
class Commit
# Returns true if any commits in +commits+ are decendants of calling commit. True
# means that one or more commits in +commits+ are newer.
def has_decendant_in? *commits
total_commits = commits.flatten
raise ArgumentError "at least one commit required." if total_commits.empty?
total_commits.each do |commit|
return true if repo.commits_between(commit.id, id).empty?
end
return false
end
# Returns an Array of commits which tie for being the newest commits. Ties can
# occur when commits are in different branches.
def self.newest *commits
oldest_commits = []
total_commits = commits.flatten
raise ArgumentError "at least one commit required." if total_commits.empty?
(total_commits).each do |commit|
if commit.has_decendant_in?(total_commits - [commit])
oldest_commits << commit
end
end
return total_commits - oldest_commits
end
end
end
以下将对您有所帮助... git log --graph 或者您可以使用 gitk