Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added Repo#commit_deltas_from as a (fairly expensive and lazy) way of…

… getting

the commits that differ between two different repositories
  • Loading branch information...
commit 2adfc816945c1ea2c0f147eef8e46dc3b8596b35 1 parent ed76397
@js js authored
View
1  .gitignore
@@ -1 +1,2 @@
pkg
+.DS_Store
View
14 lib/grit/repo.rb
@@ -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 [])
View
8 test/fixtures/rev_list_delta_a
@@ -0,0 +1,8 @@
+e34590b7a2d186b3bb9a1170d02d52b36c791c78
+8977833d74f8681aa0d9a5e84b0dd3d81519774d
+6f5561530cb3a94e4c86454e84732197325be172
+ee419e04a961543444be6db66aef52e6e37936d6
+d845de9d438e1a249a0c2fcb778e8ea3b7e06cef
+0bba4a6c10060405a94d52533af2f9bdacd4f29c
+77711c0722964ead965e0ba2ee9ed4a03cb3d292
+501d23cac6dd911511f15d091ee031a15b90ebde
View
11 test/fixtures/rev_list_delta_b
@@ -0,0 +1,11 @@
+4c8124ffcf4039d292442eeccabdeca5af5c5017
+634396b2f541a9f2d58b00be1a07f0c358b999b3
+ab25fd8483882c3bda8a458ad2965d2248654335
+e34590b7a2d186b3bb9a1170d02d52b36c791c78
+8977833d74f8681aa0d9a5e84b0dd3d81519774d
+6f5561530cb3a94e4c86454e84732197325be172
+ee419e04a961543444be6db66aef52e6e37936d6
+d845de9d438e1a249a0c2fcb778e8ea3b7e06cef
+0bba4a6c10060405a94d52533af2f9bdacd4f29c
+77711c0722964ead965e0ba2ee9ed4a03cb3d292
+501d23cac6dd911511f15d091ee031a15b90ebde
View
26 test/test_repo.rb
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.