summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormo khan <mo@mokhan.ca>2021-12-24 14:43:24 -0700
committermo khan <mo@mokhan.ca>2021-12-24 14:43:24 -0700
commit0aaa2daf488884ea1f7216fc4be221e81ceb8956 (patch)
treeecd901ba68b83e628c914478c8ea4f1267a07670
parent382d21a014f28c4b53c3eb1133dfd5898da577a5 (diff)
fix: checkout existing branch
-rw-r--r--lib/dependabot/git.rb2
-rw-r--r--spec/integration/dependabot/git_spec.rb47
-rw-r--r--spec/spec_helper.rb1
3 files changed, 31 insertions, 19 deletions
diff --git a/lib/dependabot/git.rb b/lib/dependabot/git.rb
index 26869ed..7b6598c 100644
--- a/lib/dependabot/git.rb
+++ b/lib/dependabot/git.rb
@@ -13,7 +13,7 @@ module Dependabot
end
def checkout(branch:)
- repo.create_branch(branch, repo.head.name)
+ repo.create_branch(branch, repo.head.name) unless repo.branches[branch]
repo.checkout(branch)
end
diff --git a/spec/integration/dependabot/git_spec.rb b/spec/integration/dependabot/git_spec.rb
index 9dfabcb..8dea207 100644
--- a/spec/integration/dependabot/git_spec.rb
+++ b/spec/integration/dependabot/git_spec.rb
@@ -1,24 +1,25 @@
# frozen_string_literal: true
RSpec.describe ::Dependabot::Git do
- def setup_git_repo(path)
- system "git init --quiet #{path}"
- system "git config user.email you@example.com"
- system "git config user.name example"
- system "echo 'hello' > README.md"
- system "git add README.md"
- system "git commit --quiet --no-gpg-sign -m 'initial commit'"
- system "echo 'change' > README.md"
+ def within_git_repo
+ within_tmp_dir do |path|
+ system "git init --quiet #{path}"
+ system "git config user.email you@example.com"
+ system "git config user.name example"
+ system "echo 'hello' > README.md"
+ system "git add README.md"
+ system "git commit --quiet --no-gpg-sign -m 'initial commit'"
+ system "echo 'change' > README.md"
+ yield described_class.new(path)
+ end
end
describe "#checkout" do
context "when the branch does not exist" do
- let(:branch_name) { "example" }
+ let(:branch_name) { SecureRandom.uuid }
it "creates a new branch" do
- within_tmp_dir do |path|
- setup_git_repo(path)
- subject = described_class.new(path)
+ within_git_repo do |subject|
subject.checkout(branch: branch_name)
expect(`git branch`).to include(branch_name)
@@ -26,9 +27,21 @@ RSpec.describe ::Dependabot::Git do
end
it "switches to the new branch" do
- within_tmp_dir do |path|
- setup_git_repo(path)
- subject = described_class.new(path)
+ within_git_repo do |subject|
+ subject.checkout(branch: branch_name)
+
+ expect(File.read(".git/HEAD").chomp).to eql("ref: refs/heads/#{branch_name}")
+ end
+ end
+ end
+
+ context "when the branch already exists" do
+ let(:branch_name) { SecureRandom.uuid }
+
+ it "switches to that branch" do
+ within_git_repo do |subject|
+ system "git branch #{branch_name}"
+
subject.checkout(branch: branch_name)
expect(File.read(".git/HEAD").chomp).to eql("ref: refs/heads/#{branch_name}")
@@ -40,9 +53,7 @@ RSpec.describe ::Dependabot::Git do
describe "#commit" do
context "when a tracked file is changed" do
def within_dir
- within_tmp_dir do |path|
- setup_git_repo(path)
- subject = described_class.new(path)
+ within_git_repo do |subject|
subject.checkout(branch: "example")
subject.commit(all: true, message: "The message")
yield
diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb
index 6524bfc..1cb59bd 100644
--- a/spec/spec_helper.rb
+++ b/spec/spec_helper.rb
@@ -3,6 +3,7 @@
require "bundler/setup"
require "dependabot/cli"
require "securerandom"
+require "debug"
require "tmpdir"
require "webmock/rspec"