diff --git a/lib/baes/git.rb b/lib/baes/git.rb index 56d29b1..03626f5 100644 --- a/lib/baes/git.rb +++ b/lib/baes/git.rb @@ -12,56 +12,31 @@ class GitError < StandardError; end class << self # checkout git branch and raise error on failure def checkout(branch_name) - stdout, stderr, status = Open3.capture3("git checkout #{branch_name}") + stdout = run_or_raise("git checkout #{branch_name}") output.puts(stdout) - output.puts(stderr) unless stderr.empty? - - return if status.success? - - raise GitError, "failed to rebase on '#{branch_name}'" end # rebase current branch on given branch name and return status def rebase(branch_name) - stdout, stderr, status = Open3.capture3("git rebase #{branch_name}") - - output.puts(stdout) - output.puts(stderr) unless stderr.empty? - - status + run_returning_status("git rebase #{branch_name}") end # get current branch name and raise on error def current_branch_name - stdout, stderr, status = Open3.capture3("git rev-parse --abbrev-ref HEAD") - - output.puts(stderr) unless stderr.empty? - - raise GitError, "failed to get current branch" unless status.success? - - stdout.strip + run_or_raise("git rev-parse --abbrev-ref HEAD") end # list branch names and raise on failure def branch_names - stdout, stderr, status = Open3.capture3("git branch") - - output.puts(stderr) unless stderr.empty? - - raise GitError, "failed to get branches" unless status.success? + stdout = run_or_raise("git branch") stdout.lines.map { |line| line.sub(/^\*/, "").strip } end # skip current commit during rebase and return status def rebase_skip - stdout, stderr, status = Open3.capture3("git rebase --skip") - - output.puts(stdout) - output.puts(stderr) unless stderr.empty? - - status + run_returning_status("git rebase --skip") end # return the commit number the rebase is currently halted on @@ -81,5 +56,28 @@ def last_rebase_step Integer(File.read("./.git/rebase-merge/end")) end end + + private + + def run_or_raise(command) + stdout, stderr, status = Open3.capture3(command) + + unless status.success? + output.puts(stderr) + + raise GitError, "failed to run '#{command}'" + end + + stdout.strip + end + + def run_returning_status(command) + stdout, stderr, status = Open3.capture3(command) + + output.puts(stdout) + output.puts(stderr) unless stderr.empty? + + status + end end end diff --git a/spec/baes/git_spec.rb b/spec/baes/git_spec.rb index 1ac7bff..3267217 100644 --- a/spec/baes/git_spec.rb +++ b/spec/baes/git_spec.rb @@ -9,6 +9,12 @@ def stub3(command, stdout: "", stderr: "", success: true) expect(Open3).to receive(:capture3).with(command).and_return(result) end + def run_and_rescue + yield + rescue StandardError + nil + end + describe "#checkout" do it "prints stdout" do stub3("git checkout my_branch", stdout: "out") @@ -18,19 +24,21 @@ def stub3(command, stdout: "", stderr: "", success: true) expect(output.string).to eq("out\n") end - it "prints stderr when present" do - stub3("git checkout my_branch", stdout: "out", stderr: "error") + context "when command is not successful" do + it "prints stdout and stderr" do + stub3("git checkout my_branch", stderr: "error", success: false) - described_class.checkout("my_branch") + run_and_rescue { described_class.checkout("my_branch") } - expect(output.string).to eq("out\nerror\n") - end + expect(output.string).to eq("error\n") + end - it "raises an error when command is not successful" do - stub3("git checkout my_branch", success: false) + it "raises an error" do + stub3("git checkout my_branch", success: false) - expect { described_class.checkout("my_branch") } - .to raise_error("failed to rebase on 'my_branch'") + expect { described_class.checkout("my_branch") } + .to raise_error("failed to run 'git checkout my_branch'") + end end end @@ -61,19 +69,22 @@ def stub3(command, stdout: "", stderr: "", success: true) end describe "#current_branch_name" do - it "prints stderr when present" do - stub3("git rev-parse --abbrev-ref HEAD", stderr: "error") + context "when command is not successful" do + it "prints stderr" do + command = "git rev-parse --abbrev-ref HEAD" + stub3(command, stderr: "error", success: false) - described_class.current_branch_name + run_and_rescue { described_class.current_branch_name } - expect(output.string).to eq("error\n") - end + expect(output.string).to eq("error\n") + end - it "raises an error when status is not success" do - stub3("git rev-parse --abbrev-ref HEAD", success: false) + it "raises an error" do + stub3("git rev-parse --abbrev-ref HEAD", success: false) - expect { described_class.current_branch_name } - .to raise_error("failed to get current branch") + expect { described_class.current_branch_name } + .to raise_error("failed to run 'git rev-parse --abbrev-ref HEAD'") + end end it "returns the current branch name from stdout" do @@ -86,19 +97,21 @@ def stub3(command, stdout: "", stderr: "", success: true) end describe "#branch_names" do - it "prints stderr when present" do - stub3("git branch", stdout: "out", stderr: "error") + context "when command is not successful" do + it "prints stderr" do + stub3("git branch", stderr: "error", success: false) - described_class.branch_names + run_and_rescue { described_class.branch_names } - expect(output.string).to eq("error\n") - end + expect(output.string).to eq("error\n") + end - it "raises an error when status is not success" do - stub3("git branch", success: false) + it "raises an error when status is not success" do + stub3("git branch", success: false) - expect { described_class.branch_names } - .to raise_error("failed to get branches") + expect { described_class.branch_names } + .to raise_error("failed to run 'git branch'") + end end it "returns the list of branches from stdout" do