0

我希望能够区分两个Grit::Commit较新的对象。我所说的较新的意思是,如果commit_A是 的父母(或父母的父母等)commit_B,则commit_B较新。这假设commit_Acommit_B在同一个分支上。

我考虑过使用Grit::Commit#date(),但我认为这将是不准确的。

有任何想法吗?

4

2 回答 2

1

这是我最终实施的。解释见评论。

性能很慢,但使用 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
于 2012-06-13T22:18:46.223 回答
0

以下将对您有所帮助... git log --graph 或者您可以使用 gitk

于 2012-06-05T07:21:35.837 回答