Class: GitCloner

Inherits:
Object
  • Object
show all
Defined in:
lib/git_cloner.rb

Overview

Clones git repositories locally

Instance Method Summary collapse

Constructor Details

#initialize(base_dir = '.') ⇒ GitCloner

Returns a new instance of GitCloner.

Parameters:

  • base_dir (defaults to: '.')

    The directory to which the repositories will be copied locally



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

def initialize(base_dir = '.')
  @base_dir = base_dir
end

Instance Method Details

#clone_repo(url) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/git_cloner.rb', line 24

def clone_repo(url)
  repo_path = extract_repo_path(url)
  local_path = File.join(@base_dir, repo_path)

  if Dir.exist?(local_path)
    Log.info "Repository already exists at #{local_path}. Skipping..."
  else
    Log.info "Cloning #{url} into #{local_path}..."
    FileUtils.mkdir_p(local_path)
    Git.clone(url, local_path)
  end
end

#clone_repos(repo_urls) ⇒ Object

Accepts an array of GitHub repository URLs (either HTTPS or SSH links) and clones them locally if they have not already been cloned.

Parameters:

  • repo_urls

    An array of URLs pointing to GitHub repositories



18
19
20
21
22
# File 'lib/git_cloner.rb', line 18

def clone_repos(repo_urls)
  repo_urls.each do |url|
    clone_repo(url)
  end
end

#extract_repo_path(url) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/git_cloner.rb', line 37

def extract_repo_path(url)
  # Extract the username and repository name from the GitHub URL
  # Don't ask me what's in this monstruosity
  match = url.match(%r{github\.com[:/]([^/]+)/([^/]+?)(?:\.git)?$})
  raise "Invalid GitHub URL: #{url}" unless match

  username = match[1]
  repo_name = match[2]
  "#{username}/#{repo_name}"
end