-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
125ee11
commit a5f7aae
Showing
7 changed files
with
155 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
class GitConfigParser | ||
GIT_CONFIG_PATH = ".git/config" | ||
|
||
def initialize | ||
@config = {} | ||
parse(GIT_CONFIG_PATH) if File.exist?(GIT_CONFIG_PATH) | ||
end | ||
|
||
def parse(file_path) | ||
current_section = nil | ||
File.readlines(file_path).each do |line| | ||
line = line.strip | ||
|
||
if line.start_with?("[") && line.end_with?("]") | ||
current_section = line[1...-1] | ||
@config[current_section] = {} | ||
elsif current_section && line.include?("=") | ||
key, value = line.split("=", 2).map(&:strip) | ||
@config[current_section][key] = value | ||
end | ||
end | ||
end | ||
|
||
def read_value(section, key) | ||
return @config[section][key] if @config.key?(section) && @config[section].key?(key) | ||
|
||
nil | ||
end | ||
|
||
# Github defined as following in repository config | ||
# git@git.domain.com:organization/repo.git | ||
# TODO Gitlab and Bitbucket urls are slightly different so it wont work for them as of yet | ||
def github_repo_url | ||
"https://" + self.read_value('remote "origin"', 'url').split('@').last.gsub(':', '/').gsub(/\.git$/, "") + "/blob/HEAD/" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# frozen_string_literal: true | ||
|
||
require "test_helper" | ||
|
||
class GitConfigParserTest < Minitest::Test | ||
def setup | ||
@test_config_content = <<~HEREDOC | ||
[remote "origin"] | ||
url = git@github.com:org_name/repo_name.git | ||
[user] | ||
name = John Doe | ||
email = john@example.com | ||
HEREDOC | ||
end | ||
|
||
def test_initialize_parses_git_config_file | ||
File.stub(:exist?, true) do | ||
File.stub(:readlines, @test_config_content.lines) do | ||
assert_equal({ | ||
'remote "origin"' => { 'url' => 'git@github.com:org_name/repo_name.git' }, | ||
'user' => { 'name' => 'John Doe', 'email' => 'john@example.com' } | ||
}, GitConfigParser.new.instance_variable_get(:@config)) | ||
end | ||
end | ||
end | ||
|
||
def test_read_value_returns_value_for_given_section_and_key | ||
File.stub(:exist?, true) do | ||
File.stub(:readlines, @test_config_content.lines) do | ||
assert_equal('git@github.com:org_name/repo_name.git', GitConfigParser.new.read_value('remote "origin"', 'url')) | ||
end | ||
end | ||
|
||
end | ||
|
||
def test_read_value_returns_nil_for_nonexistent_section_or_key | ||
File.stub(:exist?, true) do | ||
File.stub(:readlines, @test_config_content.lines) do | ||
assert_nil(GitConfigParser.new.read_value('nonexistent_section', 'nonexistent_key')) | ||
end | ||
end | ||
end | ||
|
||
def test_github_repo_url_returns_correct_github_repository_url | ||
File.stub(:exist?, true) do | ||
File.stub(:readlines, @test_config_content.lines) do | ||
assert_equal('https://github.com/org_name/repo_name/blob/HEAD/', GitConfigParser.new.github_repo_url) | ||
end | ||
end | ||
end | ||
end |