Skip to content

Commit

Permalink
Merge pull request #363 from jonathanhefner/renderer-fix-git-memoization
Browse files Browse the repository at this point in the history
Fix performance regression due to git calls
  • Loading branch information
jonathanhefner authored Jan 17, 2024
2 parents fd538ad + 3ceb588 commit 07df49f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 33 deletions.
31 changes: 13 additions & 18 deletions lib/sdoc/helpers/git.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
module SDoc::Helpers::Git
def _git
@@_git ||= {}
end

def github_url(relative_path, line: nil)
return unless github?
line = "#L#{line}" if line
Expand All @@ -9,43 +13,34 @@ def github?
@options.github && git? && github_repository
end

attr_writer :git_repo_path

def git?
@git_repo_path ||= Dir.chdir(@options.root) { `git rev-parse --show-toplevel 2> /dev/null`.chomp }
!@git_repo_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)
Dir.chdir(@options.root) { `git #{command}`.chomp } if git?
end

attr_writer :git_head_branch

def git_head_branch
@git_head_branch ||= git_command("rev-parse --abbrev-ref HEAD")
_git[:head_branch] ||= git_command("rev-parse --abbrev-ref HEAD")
end

attr_writer :git_head_sha1

def git_head_sha1
@git_head_sha1 ||= git_command("rev-parse HEAD")
_git[:head_sha1] ||= git_command("rev-parse HEAD")
end

attr_writer :git_head_timestamp

def git_head_timestamp
@git_head_timestamp ||= git_command("show -s --format=%cI HEAD")
_git[:head_timestamp] ||= git_command("show -s --format=%cI HEAD")
end

attr_writer :git_origin_url

def git_origin_url
@git_origin_url ||= git_command("config --get remote.origin.url")
_git[:origin_url] ||= git_command("config --get remote.origin.url")
end

def github_repository
return @github_repository if defined?(@github_repository)
@github_repository = git_origin_url.chomp(".git")[%r"github\.com[/:](.+)", 1]
_git.fetch(:github_repository) do
_git[:github_repository] = git_origin_url.chomp(".git")[%r"github\.com[/:](.+)", 1]
end
end
end
31 changes: 16 additions & 15 deletions spec/helpers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
attr_accessor :options
end.new

@helpers._git.clear
@helpers.options = RDoc::Options.new
end

Expand All @@ -33,9 +34,9 @@
end

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

_(@helpers.github_url("foo/bar/qux.rb")).
must_equal "https://github.com/user/repo/blob/1337c0d3/foo/bar/qux.rb"
Expand All @@ -47,14 +48,14 @@
end

it "supports HTTPS remote URL" do
@helpers.git_origin_url = "https://github.com/user/repo.git"
@helpers._git[:origin_url] = "https://github.com/user/repo.git"

_(@helpers.github_url("foo/bar/qux.rb")).
must_match %r"\Ahttps://github.com/user/repo/blob/[0-9a-f]{40}/foo/bar/qux\.rb\z"
end

it "supports HTTPS remote URL without .git extension" do
@helpers.git_origin_url = "https://github.com/user/repo"
@helpers._git[:origin_url] = "https://github.com/user/repo"

_(@helpers.github_url("foo/bar/qux.rb")).
must_match %r"\Ahttps://github.com/user/repo/blob/[0-9a-f]{40}/foo/bar/qux\.rb\z"
Expand All @@ -67,19 +68,19 @@
end

it "returns nil when git is not installed or project is not a git repository" do
@helpers.git_repo_path = ""
@helpers._git[:repo_path] = ""

_(@helpers.github_url("foo/bar/qux.rb")).must_be_nil
end

it "returns nil when 'origin' remote is not present" do
@helpers.git_origin_url = ""
@helpers._git[:origin_url] = ""

_(@helpers.github_url("foo/bar/qux.rb")).must_be_nil
end

it "returns nil when 'origin' remote is not recognized" do
@helpers.git_origin_url = "[email protected]:user/repo.git"
@helpers._git[:origin_url] = "[email protected]:user/repo.git"

_(@helpers.github_url("foo/bar/qux.rb")).must_be_nil
end
Expand Down Expand Up @@ -383,9 +384,9 @@ 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_repo_path = "path/to/repo"
@helpers.git_head_branch = "1-0-stable"
@helpers.git_head_sha1 = "1337c0d3d00d" * 3
@helpers._git[:repo_path] = "path/to/repo"
@helpers._git[:head_branch] = "1-0-stable"
@helpers._git[:head_sha1] = "1337c0d3d00d" * 3

_(@helpers.project_git_head).must_equal "1-0-stable@1337c0d3d00d"
end
Expand All @@ -395,7 +396,7 @@ module Foo; module Bar; module Qux; end; end; end
end

it "returns nil when git is not installed or project is not a git repository" do
@helpers.git_repo_path = ""
@helpers._git[:repo_path] = ""

_(@helpers.project_git_head).must_be_nil
end
Expand Down Expand Up @@ -444,8 +445,8 @@ 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_repo_path = "path/to/repo"
@helpers.git_head_timestamp = "1999-12-31T12:34:56Z"
@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"
end
Expand All @@ -456,7 +457,7 @@ module Foo; module Bar; module Qux; end; end; end
end

it "returns nil when git is not installed or project is not a git repository" do
@helpers.git_repo_path = ""
@helpers._git[:repo_path] = ""

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

0 comments on commit 07df49f

Please sign in to comment.