Module: Spill::RepoRemotes

Defined in:
lib/spill/repo_remotes.rb

Constant Summary collapse

SSH_PATTERN =
%r{\Agit@github\.com:(?<slug>.+?)(?:\.git)?/?\z}
SSH_PROTOCOL_PATTERN =
%r{\Assh://git@github\.com/(?<slug>.+?)(?:\.git)?/?\z}
HTTPS_PATTERN =
%r{\Ahttps://github\.com/(?<slug>.+?)(?:\.git)?/?\z}

Class Method Summary collapse

Class Method Details

.github_slugs(repo_paths) ⇒ Object



10
11
12
# File 'lib/spill/repo_remotes.rb', line 10

def self.github_slugs(repo_paths)
  slug_map(repo_paths).keys.to_set
end

.origin_url(path) ⇒ Object



29
30
31
32
33
34
# File 'lib/spill/repo_remotes.rb', line 29

def self.origin_url(path)
  out, _err, status = Open3.capture3("git", "-C", path, "remote", "get-url", "origin")
  status.success? ? out.strip : nil
rescue Errno::ENOENT
  nil
end

.slug_for(path) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/spill/repo_remotes.rb', line 21

def self.slug_for(path)
  url = origin_url(path)
  return nil if url.nil?

  match = SSH_PATTERN.match(url) || SSH_PROTOCOL_PATTERN.match(url) || HTTPS_PATTERN.match(url)
  match && match[:slug].downcase
end

.slug_map(repo_paths) ⇒ Object



14
15
16
17
18
19
# File 'lib/spill/repo_remotes.rb', line 14

def self.slug_map(repo_paths)
  repo_paths.each_with_object({}) do |path, map|
    slug = slug_for(path)
    map[slug] = File.basename(path) if slug
  end
end