Skip to content

Commit

Permalink
Prevent git commands if project is not a git repo
Browse files Browse the repository at this point in the history
Prior to this commit, git commands (such as the command to get
`remote.origin.url`) would always be run if git was installed.  This
caused "fatal: not a git repository" to be printed to `stderr` when the
project was not a git repository.

This commit modifies `SDoc::Helpers::Git#git?` such that git commands
will only be run if git is installed _and_ the project is a git
repository.
  • Loading branch information
jonathanhefner committed Sep 9, 2023
1 parent f2d2369 commit e7b7e50
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
6 changes: 3 additions & 3 deletions lib/sdoc/helpers/git.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ def github?
@options.github && git? && github_repository
end

attr_writer :git_bin_path
attr_writer :git_repo_path

def git?
@git_bin_path ||= `sh -c 'command -v git'`.chomp
!@git_bin_path.empty?
@git_repo_path ||= Dir.chdir(@options.root) { `git rev-parse --show-toplevel 2> /dev/null`.chomp }
!@git_repo_path.empty?
end

def git_command(command)
Expand Down
34 changes: 25 additions & 9 deletions spec/helpers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,29 @@
@helpers.options = RDoc::Options.new
end

describe "#git?" do
it "returns false when git is not installed" do
with_env("PATH" => "") do
_(@helpers.git?).must_equal false
end
end

it "returns false when project is not a git repository" do
Dir.mktmpdir do |dir|
@helpers.options.root = dir

_(@helpers.git?).must_equal false
end
end
end

describe "#github_url" do
before :each do
@helpers.options.github = true
end

it "returns the URL for a given path in the project's GitHub repository at the current SHA1" do
@helpers.git_bin_path = "path/to/git"
@helpers.git_repo_path = "path/to/repo"
@helpers.git_origin_url = "[email protected]:user/repo.git"
@helpers.git_head_sha1 = "1337c0d3"

Expand Down Expand Up @@ -51,8 +67,8 @@
_(@helpers.github_url("foo/bar/qux.rb")).must_be_nil
end

it "returns nil when git is not installed" do
@helpers.git_bin_path = ""
it "returns nil when git is not installed or project is not a git repository" do
@helpers.git_repo_path = ""

_(@helpers.github_url("foo/bar/qux.rb")).must_be_nil
end
Expand Down Expand Up @@ -306,7 +322,7 @@ module Foo; module Bar; module Qux; end; end; end

describe "#project_git_head" do
it "returns the branch name and abbreviated SHA1 of the most recent commit in HEAD" do
@helpers.git_bin_path = "path/to/git"
@helpers.git_repo_path = "path/to/repo"
@helpers.git_head_branch = "1-0-stable"
@helpers.git_head_sha1 = "1337c0d3d00d" * 3

Expand All @@ -317,8 +333,8 @@ module Foo; module Bar; module Qux; end; end; end
_(@helpers.project_git_head).must_match %r"\A.+@[[:xdigit:]]{12}\z"
end

it "returns nil when git is not installed" do
@helpers.git_bin_path = ""
it "returns nil when git is not installed or project is not a git repository" do
@helpers.git_repo_path = ""

_(@helpers.project_git_head).must_be_nil
end
Expand Down Expand Up @@ -367,7 +383,7 @@ module Foo; module Bar; module Qux; end; end; end

describe "#og_modified_time" do
it "returns the commit time of the most recent commit in HEAD" do
@helpers.git_bin_path = "path/to/git"
@helpers.git_repo_path = "path/to/repo"
@helpers.git_head_timestamp = "1999-12-31T12:34:56Z"

_(@helpers.og_modified_time).must_equal "1999-12-31T12:34:56Z"
Expand All @@ -378,8 +394,8 @@ module Foo; module Bar; module Qux; end; end; end
must_match %r"\A\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}[-+]\d{2}:\d{2}\z"
end

it "returns nil when git is not installed" do
@helpers.git_bin_path = ""
it "returns nil when git is not installed or project is not a git repository" do
@helpers.git_repo_path = ""

_(@helpers.og_modified_time).must_be_nil
end
Expand Down

0 comments on commit e7b7e50

Please sign in to comment.