Skip to content

Commit 50514e6

Browse files
authored
Merge pull request #92 from Shopify/checkout-branch
checkout the original branch instead of commit when available
2 parents 745e433 + f728b28 commit 50514e6

File tree

4 files changed

+30
-3
lines changed

4 files changed

+30
-3
lines changed

lib/spoom/cli/coverage.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,9 @@ def timeline
4444
path = exec_path
4545
sorbet = options[:sorbet]
4646

47-
sha_before = Spoom::Git.last_commit(path: path)
48-
unless sha_before
47+
ref_before = Spoom::Git.current_branch
48+
ref_before = Spoom::Git.last_commit(path: path) unless ref_before
49+
unless ref_before
4950
say_error("Not in a git repository")
5051
say_error("\nSpoom needs to checkout into your previous commits to build the timeline.", status: nil)
5152
exit(1)
@@ -107,7 +108,7 @@ def timeline
107108
File.write(file, snapshot.to_json)
108109
say(" Snapshot data saved under `#{file}`\n\n")
109110
end
110-
Spoom::Git.checkout(sha_before, path: path)
111+
Spoom::Git.checkout(ref_before, path: path)
111112
end
112113

113114
desc "report", "Produce a typing coverage report"

lib/spoom/git.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ def self.show(*arg, path: ".")
5050
exec("git show #{arg.join(' ')}", path: path)
5151
end
5252

53+
sig { params(path: String).returns(T.nilable(String)) }
54+
def self.current_branch(path: ".")
55+
out, _, status = exec("git branch --show-current", path: path)
56+
return nil unless status
57+
out.strip
58+
end
59+
5360
# Utils
5461

5562
# Get the commit epoch timestamp for a `sha`

lib/spoom/test_helpers/project.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,16 @@ def destroy
100100
FileUtils.rm_rf(path)
101101
end
102102

103+
sig { params(name: String).void }
104+
def create_and_checkout_branch(name)
105+
Spoom::Git.exec("git checkout -b #{name}", path: path)
106+
end
107+
108+
sig { returns(T.nilable(String)) }
109+
def current_branch
110+
Spoom::Git.current_branch(path: path)
111+
end
112+
103113
private
104114

105115
# Create an absolute path from `self.path` and `rel_path`

test/spoom/cli/coverage_test.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,15 @@ def test_report_generate_html_file
420420
assert(File.exist?("#{@project.path}/spoom_report.html"))
421421
end
422422

423+
def test_finish_on_original_branch
424+
create_git_history
425+
assert_equal("master", @project.current_branch)
426+
@project.create_and_checkout_branch("fake-branch")
427+
assert_equal("fake-branch", @project.current_branch)
428+
@project.bundle_exec("spoom coverage timeline --save")
429+
assert_equal("fake-branch", @project.current_branch)
430+
end
431+
423432
private
424433

425434
def create_git_history

0 commit comments

Comments
 (0)