lib/git_local/repository.rb in git_local-0.0.3 vs lib/git_local/repository.rb in git_local-0.0.4

- old
+ new

@@ -1,11 +1,16 @@ module GitLocal class Repository class NotFound < StandardError + end + class InvalidArgument < StandardError + end + def initialize(org:, repo:, branch:, local_directory:) + check_for_special_characters(org, repo, branch, local_directory) @org = org @repo = repo @branch = branch @local_directory = local_directory end @@ -46,32 +51,41 @@ def new_commit_on_remote? popened_io = IO.popen("(cd #{path} && git rev-parse HEAD)") head = popened_io.read.chomp.split("\n").last Process.wait(popened_io.pid) - popened_io = IO.popen("(cd #{path} && git remote update && git rev-parse origin/#{branch})") + popened_io = IO.popen("(cd #{path} && git remote update && git rev-parse origin/#{branch}) 2>&1") + out = popened_io.map { |line| line.chomp } || [] remote = popened_io.read.chomp Process.wait(popened_io.pid) - raise NotFound unless $?.to_i == 0 + raise NotFound.new.exception(out.join(' ')) unless $?.to_i == 0 remote != head end def path @path ||= "#{local_directory}/#{org_repo_branch}" end + def check_for_special_characters(*args) + regexp = Regexp.new(/([A-Za-z0-9\-\_\/#]+)/) + args.each do |arg| + raise InvalidArgument if arg.gsub(regexp, '').length > 0 + end + end + private attr_reader :org, :repo, :branch, :local_directory def clone_and_checkout FileUtils.makedirs(repo_path) unless Dir.exist?(repo_path) - popened_io = IO.popen("(cd #{repo_path} && git clone git@github.com:#{org_repo}.git --branch #{branch} --single-branch #{branch} && cd #{path})") + popened_io = IO.popen("(cd #{repo_path} && git clone git@github.com:#{org_repo}.git --branch #{branch} --single-branch #{branch} && cd #{path}) 2>&1") + out = popened_io.map { |line| line.chomp } || [] Process.wait(popened_io.pid) - raise NotFound unless $?.to_i == 0 + raise NotFound.new.exception(out.join(' ')) unless $?.to_i == 0 end def org_repo @org_repo ||= "#{org}/#{repo}" end