From c16862b8cb39c0487b38331fdf93a455c3303e2f Mon Sep 17 00:00:00 2001 From: Joshua Liebowitz Date: Thu, 19 Apr 2018 09:59:47 -0700 Subject: [PATCH 1/6] Allow fetch operation to receive a `ref` param addresses https://github.com/ruby-git/ruby-git/issues/361 --- README.md | 1 + lib/git/base.rb | 4 ++-- lib/git/lib.rb | 3 ++- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 6758d5ee..dc9bd476 100644 --- a/README.md +++ b/README.md @@ -227,6 +227,7 @@ And here are the operations that will need to write to your git repository. g.fetch g.fetch(g.remotes.first) + g.fetch('origin', 'some/ref/head') g.pull g.pull(Git::Repo, Git::Branch) # fetch and a merge diff --git a/lib/git/base.rb b/lib/git/base.rb index ad3e52ec..8ef535cb 100644 --- a/lib/git/base.rb +++ b/lib/git/base.rb @@ -313,8 +313,8 @@ def checkout_file(version, file) # fetches changes from a remote branch - this does not modify the working directory, # it just gets the changes from the remote if there are any - def fetch(remote = 'origin', opts={}) - self.lib.fetch(remote, opts) + def fetch(remote = 'origin', ref = nil, opts={}) + self.lib.fetch(remote, ref, opts) end # pushes changes to a remote repository - easiest if this is a cloned repository, diff --git a/lib/git/lib.rb b/lib/git/lib.rb index 00cc0e72..a6a0fb15 100644 --- a/lib/git/lib.rb +++ b/lib/git/lib.rb @@ -724,8 +724,9 @@ def tag(name, *opts) end - def fetch(remote, opts) + def fetch(remote, ref, opts) arr_opts = [remote] + arr_opts << ref unless ref.nil? arr_opts << '--tags' if opts[:t] || opts[:tags] arr_opts << '--prune' if opts[:p] || opts[:prune] From d9f7da0171e99dada6018f7f3e00efc951a58811 Mon Sep 17 00:00:00 2001 From: Joshua Liebowitz Date: Thu, 19 Apr 2018 10:27:39 -0700 Subject: [PATCH 2/6] Update remote object --- .ruby-version | 1 + lib/git/lib.rb | 2 +- lib/git/remote.rb | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) create mode 100644 .ruby-version diff --git a/.ruby-version b/.ruby-version new file mode 100644 index 00000000..0bee604d --- /dev/null +++ b/.ruby-version @@ -0,0 +1 @@ +2.3.3 diff --git a/lib/git/lib.rb b/lib/git/lib.rb index a6a0fb15..cce0a73b 100644 --- a/lib/git/lib.rb +++ b/lib/git/lib.rb @@ -724,7 +724,7 @@ def tag(name, *opts) end - def fetch(remote, ref, opts) + def fetch(remote, ref = nil, opts) arr_opts = [remote] arr_opts << ref unless ref.nil? arr_opts << '--tags' if opts[:t] || opts[:tags] diff --git a/lib/git/remote.rb b/lib/git/remote.rb index 73556a7c..a3a3947b 100644 --- a/lib/git/remote.rb +++ b/lib/git/remote.rb @@ -12,7 +12,7 @@ def initialize(base, name) end def fetch(opts={}) - @base.fetch(@name, opts) + @base.fetch(@name, nil, opts) end # merge this remote locally From 37fede4c9c5462c656a0113f2334d0d8e00ff13b Mon Sep 17 00:00:00 2001 From: Joshua Liebowitz Date: Thu, 19 Apr 2018 10:40:20 -0700 Subject: [PATCH 3/6] remove .ruby-version --- .ruby-version | 1 - 1 file changed, 1 deletion(-) delete mode 100644 .ruby-version diff --git a/.ruby-version b/.ruby-version deleted file mode 100644 index 0bee604d..00000000 --- a/.ruby-version +++ /dev/null @@ -1 +0,0 @@ -2.3.3 From 1510bce8ef85ccb8209d95ba19388a98d8b7789a Mon Sep 17 00:00:00 2001 From: Joshua Liebowitz Date: Tue, 19 Jun 2018 11:00:40 -0700 Subject: [PATCH 4/6] Use options instead of separate param --- README.md | 2 +- lib/git/base.rb | 4 ++-- lib/git/lib.rb | 4 ++-- lib/git/remote.rb | 2 +- tests/units/test_remotes.rb | 28 ++++++++++++++++++++++++++++ 5 files changed, 34 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index dc9bd476..9f158936 100644 --- a/README.md +++ b/README.md @@ -227,7 +227,7 @@ And here are the operations that will need to write to your git repository. g.fetch g.fetch(g.remotes.first) - g.fetch('origin', 'some/ref/head') + g.fetch('origin', {:ref => 'some/ref/head'} ) g.pull g.pull(Git::Repo, Git::Branch) # fetch and a merge diff --git a/lib/git/base.rb b/lib/git/base.rb index 8ef535cb..ad3e52ec 100644 --- a/lib/git/base.rb +++ b/lib/git/base.rb @@ -313,8 +313,8 @@ def checkout_file(version, file) # fetches changes from a remote branch - this does not modify the working directory, # it just gets the changes from the remote if there are any - def fetch(remote = 'origin', ref = nil, opts={}) - self.lib.fetch(remote, ref, opts) + def fetch(remote = 'origin', opts={}) + self.lib.fetch(remote, opts) end # pushes changes to a remote repository - easiest if this is a cloned repository, diff --git a/lib/git/lib.rb b/lib/git/lib.rb index cce0a73b..f3f23da9 100644 --- a/lib/git/lib.rb +++ b/lib/git/lib.rb @@ -724,9 +724,9 @@ def tag(name, *opts) end - def fetch(remote, ref = nil, opts) + def fetch(remote, opts) arr_opts = [remote] - arr_opts << ref unless ref.nil? + arr_opts << opts[:ref] unless opts[:ref].nil? arr_opts << '--tags' if opts[:t] || opts[:tags] arr_opts << '--prune' if opts[:p] || opts[:prune] diff --git a/lib/git/remote.rb b/lib/git/remote.rb index a3a3947b..73556a7c 100644 --- a/lib/git/remote.rb +++ b/lib/git/remote.rb @@ -12,7 +12,7 @@ def initialize(base, name) end def fetch(opts={}) - @base.fetch(@name, nil, opts) + @base.fetch(@name, opts) end # merge this remote locally diff --git a/tests/units/test_remotes.rb b/tests/units/test_remotes.rb index 0f73fda3..fec59758 100644 --- a/tests/units/test_remotes.rb +++ b/tests/units/test_remotes.rb @@ -107,6 +107,34 @@ def test_fetch assert(loc.tags.map(&:name).include?('test-tag-in-deleted-branch')) end end + + def test_fetch_ref_adds_ref_option + in_temp_dir do |path| + loc = Git.clone(@wbare, 'local') + rem = Git.clone(@wbare, 'remote', :config => 'receive.denyCurrentBranch=ignore') + loc.add_remote('testrem', rem) + + loc.chdir do + new_file('test-file1', 'gonnaCommitYou') + loc.add + loc.commit('master commit 1') + first_commit = loc.log.first.sha + + new_file('test-file2', 'gonnaCommitYouToo') + loc.add + loc.commit('master commit 2') + second_commit = loc.log.first.sha + + # Make sure fetch message only has the first commit when we fetch the first commit + assert(loc.fetch('origin', {:ref => first_commit}).include?(first_commit)) + assert(!loc.fetch('origin', {:ref => first_commit}).include?(second_commit)) + + # Make sure fetch message only has the second commit when we fetch the second commit + assert(loc.fetch('origin', {:ref => second_commit}).include?(second_commit)) + assert(!loc.fetch('origin', {:ref => second_commit}).include?(first_commit)) + end + end + end def test_push in_temp_dir do |path| From c413f2125ad7641bdee34a3c06162c78bae9d18e Mon Sep 17 00:00:00 2001 From: Joshua Liebowitz Date: Tue, 19 Jun 2018 16:13:50 -0700 Subject: [PATCH 5/6] Update for comments --- tests/units/test_remotes.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/units/test_remotes.rb b/tests/units/test_remotes.rb index 2261a241..3e90c3b5 100644 --- a/tests/units/test_remotes.rb +++ b/tests/units/test_remotes.rb @@ -133,20 +133,20 @@ def test_fetch_ref_adds_ref_option new_file('test-file1', 'gonnaCommitYou') loc.add loc.commit('master commit 1') - first_commit = loc.log.first.sha + first_commit_sha = loc.log.first.sha new_file('test-file2', 'gonnaCommitYouToo') loc.add loc.commit('master commit 2') - second_commit = loc.log.first.sha + second_commit_sha = loc.log.first.sha # Make sure fetch message only has the first commit when we fetch the first commit - assert(loc.fetch('origin', {:ref => first_commit}).include?(first_commit)) - assert(!loc.fetch('origin', {:ref => first_commit}).include?(second_commit)) + assert(loc.fetch('origin', {:ref => first_commit_sha}).include?(first_commit_sha)) + assert(!loc.fetch('origin', {:ref => first_commit_sha}).include?(second_commit_sha)) # Make sure fetch message only has the second commit when we fetch the second commit - assert(loc.fetch('origin', {:ref => second_commit}).include?(second_commit)) - assert(!loc.fetch('origin', {:ref => second_commit}).include?(first_commit)) + assert(loc.fetch('origin', {:ref => second_commit_sha}).include?(second_commit_sha)) + assert(!loc.fetch('origin', {:ref => second_commit_sha}).include?(first_commit_sha)) end end end From 33c9f1db5502a76774719df5e35829af13200e95 Mon Sep 17 00:00:00 2001 From: Joshua Liebowitz Date: Thu, 21 Jun 2018 08:33:34 -0700 Subject: [PATCH 6/6] Update for style --- lib/git/lib.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/git/lib.rb b/lib/git/lib.rb index c075f57f..0b3307fc 100644 --- a/lib/git/lib.rb +++ b/lib/git/lib.rb @@ -738,7 +738,7 @@ def tag(name, *opts) def fetch(remote, opts) arr_opts = [remote] - arr_opts << opts[:ref] unless opts[:ref].nil? + arr_opts << opts[:ref] if opts[:ref] arr_opts << '--tags' if opts[:t] || opts[:tags] arr_opts << '--prune' if opts[:p] || opts[:prune]