Skip to content

Commit

Permalink
Added Repo#commit_deltas_from as a (fairly expensive and lazy) way of…
Browse files Browse the repository at this point in the history
… getting

the commits that differ between two different repositories
  • Loading branch information
js committed Feb 14, 2009
1 parent ed76397 commit 2adfc81
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pkg
.DS_Store
14 changes: 14 additions & 0 deletions lib/grit/repo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,20 @@ def commit(id)
Commit.find_all(self, id, options).first
end

# Returns a list of commits that is in +other_repo+ but not in self
#
# Returns Grit::Commit[]
def commit_deltas_from(other_repo, ref = "master", other_ref = "master")
# TODO: we should be able to figure out the branch point, rather than
# rev-list'ing the whole thing
repo_refs = self.git.rev_list({}, ref).strip.split("\n")
other_repo_refs = other_repo.git.rev_list({}, other_ref).strip.split("\n")

(other_repo_refs - repo_refs).map do |ref|
Commit.find_all(other_repo, ref, {:max_count => 1}).first
end
end

# The Tree object for the given treeish reference
# +treeish+ is the reference (default 'master')
# +paths+ is an optional Array of directory paths to restrict the tree (deafult [])
Expand Down
8 changes: 8 additions & 0 deletions test/fixtures/rev_list_delta_a
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
e34590b7a2d186b3bb9a1170d02d52b36c791c78
8977833d74f8681aa0d9a5e84b0dd3d81519774d
6f5561530cb3a94e4c86454e84732197325be172
ee419e04a961543444be6db66aef52e6e37936d6
d845de9d438e1a249a0c2fcb778e8ea3b7e06cef
0bba4a6c10060405a94d52533af2f9bdacd4f29c
77711c0722964ead965e0ba2ee9ed4a03cb3d292
501d23cac6dd911511f15d091ee031a15b90ebde
11 changes: 11 additions & 0 deletions test/fixtures/rev_list_delta_b
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
4c8124ffcf4039d292442eeccabdeca5af5c5017
634396b2f541a9f2d58b00be1a07f0c358b999b3
ab25fd8483882c3bda8a458ad2965d2248654335
e34590b7a2d186b3bb9a1170d02d52b36c791c78
8977833d74f8681aa0d9a5e84b0dd3d81519774d
6f5561530cb3a94e4c86454e84732197325be172
ee419e04a961543444be6db66aef52e6e37936d6
d845de9d438e1a249a0c2fcb778e8ea3b7e06cef
0bba4a6c10060405a94d52533af2f9bdacd4f29c
77711c0722964ead965e0ba2ee9ed4a03cb3d292
501d23cac6dd911511f15d091ee031a15b90ebde
26 changes: 26 additions & 0 deletions test/test_repo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -318,4 +318,30 @@ def test_log_with_path_and_options
Git.any_instance.expects(:log).with({:pretty => 'raw', :max_count => 1}, 'master', '--', 'file.rb').returns(fixture('rev_list'))
@r.log('master', 'file.rb', :max_count => 1)
end

# commit_deltas_from

def test_commit_deltas_from_nothing_new
other_repo = Repo.new(GRIT_REPO)
@r.git.expects(:rev_list).with({}, "master").returns(fixture("rev_list_delta_b"))
other_repo.git.expects(:rev_list).with({}, "master").returns(fixture("rev_list_delta_a"))

delta_commits = @r.commit_deltas_from(other_repo)
assert_equal 0, delta_commits.size
end

def test_commit_deltas_from_when_other_has_new
other_repo = Repo.new(GRIT_REPO)
@r.git.expects(:rev_list).with({}, "master").returns(fixture("rev_list_delta_a"))
other_repo.git.expects(:rev_list).with({}, "master").returns(fixture("rev_list_delta_b"))
%w[
4c8124ffcf4039d292442eeccabdeca5af5c5017
634396b2f541a9f2d58b00be1a07f0c358b999b3
ab25fd8483882c3bda8a458ad2965d2248654335
].each do |ref|
Commit.expects(:find_all).with(other_repo, ref, :max_count => 1).returns([stub()])
end
delta_commits = @r.commit_deltas_from(other_repo)
assert_equal 3, delta_commits.size
end
end

0 comments on commit 2adfc81

Please sign in to comment.