lib/git_local/repository.rb in git_local-0.0.8 vs lib/git_local/repository.rb in git_local-0.0.9

- old
+ new

@@ -4,19 +4,28 @@ end class InvalidArgument < StandardError end + class InvalidProtocol < StandardError + end + GITHUB_HOST = "github.com".freeze + VALID_PROTOCOLS = { + ssh: "SSH", + https: "HTTPS" + }.freeze - def initialize(org:, repo:, branch:, local_directory:, host: GITHUB_HOST) + def initialize(org:, repo:, branch:, local_directory:, host: GITHUB_HOST, protocol: VALID_PROTOCOLS[:ssh]) check_for_special_characters(org, repo, branch, local_directory) @branch = branch @host = host @local_directory = local_directory @org = org @repo = repo + @protocol = protocol + validate_protocol end def get Dir.exist?(path) && new_commit_on_remote? ? reset_to_latest_from_origin : clone_and_checkout end @@ -80,11 +89,11 @@ attr_reader :branch, :host, :local_directory, :org, :repo def clone_and_checkout FileUtils.makedirs(repo_path) unless Dir.exist?(repo_path) - popened_io = IO.popen("(cd #{repo_path} && git clone git@#{host}:#{org_repo}.git --branch #{branch} --single-branch #{branch} && cd #{path}) 2>&1") + popened_io = IO.popen("(cd #{repo_path} && git clone #{repo_endpoint} --branch #{branch} --single-branch #{branch} && cd #{path}) 2>&1") out = popened_io.map(&:chomp) || [] Process.wait(popened_io.pid) raise NotFound.new.exception(out.join(" ")) unless $?.to_i == 0 end @@ -101,8 +110,16 @@ Process.wait(IO.popen("(cd #{path} && git fetch && git reset origin/#{branch} --hard)").pid) end def repo_path @repo_path ||= "#{local_directory}/#{org_repo}" + end + + def validate_protocol + raise InvalidProtocol, "Protocol must be either HTTPS or SSH." unless VALID_PROTOCOLS.values.include?(@protocol) + end + + def repo_endpoint + @protocol == VALID_PROTOCOLS[:ssh] ? "git@#{host}:#{org_repo}.git" : "https://#{host}/#{org_repo}.git" end end end