Skip to content

Commit

Permalink
refactor Git module to reduce duplication (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
mockdeep authored Sep 4, 2024
1 parent 1c1c0aa commit 016606d
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 57 deletions.
58 changes: 28 additions & 30 deletions lib/baes/git.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
67 changes: 40 additions & 27 deletions spec/baes/git_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 016606d

Please sign in to comment.