Skip to content

Commit

Permalink
FEATURE: Optional explicit directory names (#3)
Browse files Browse the repository at this point in the history
for handling repo name collisions (e.g org1/repo-name vs org2/repo-name)

See also: discourse/all-the-themes#45
  • Loading branch information
CvX authored Jan 31, 2025
1 parent 803898c commit 9092b7d
Showing 1 changed file with 23 additions and 17 deletions.
40 changes: 23 additions & 17 deletions lib/mass_git_clone.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ def prefixed_puts(str)
# Clone or update a repo with the given URL
# On failure, return the repo url
# On success, return nil
def update_repo(repo_url)
dir_name = File.basename(repo_url, ".git")
def update_repo(repo_url, dir_name)
if Dir.exist?(dir_name)
prefixed_puts "Updating #{dir_name}..."

Expand Down Expand Up @@ -70,35 +69,42 @@ def self.mass_clone(repo_base_dir:, repo_list:)
false
end

all_repos = repo_list.map(&:strip).filter { |l| l.length > 0 }
if all_repos.size === 0
prefixed_puts "No plugin URLs supplied"
all_entries = repo_list.map(&:strip).filter { |l| l.length > 0 }
if all_entries.size === 0
prefixed_puts "No git repository URLs supplied"
exit 1
end

all_repos =
all_repos.map do |repo|
if !repo.match?(%r{\A[\w-]+/[\w-]+\z})
repo # Full URL, leave untouched
elsif use_ssh
"[email protected]:#{repo}"
else
"https://github.com/#{repo}"
end
all_entries =
all_entries.map do |entry|
repo, dir_name = entry.split(" ")

repo_url =
if !repo.match?(%r{\A[\w-]+/[\w-]+\z})
repo # Full URL, leave untouched
elsif use_ssh
"[email protected]:#{repo}"
else
"https://github.com/#{repo}"
end

dir_name ||= File.basename(repo_url, ".git")

[repo_url, dir_name]
end

Dir.mkdir(repo_base_dir) if !Dir.exist?(repo_base_dir)

Dir.chdir(repo_base_dir) do
failures =
Parallel
.map(all_repos, in_threads: ENV["PARALLEL"]&.to_i || 10) do |repo_url|
update_repo(repo_url)
.map(all_entries, in_threads: ENV["PARALLEL"]&.to_i || 10) do |repo_url, dir_name|
update_repo(repo_url, dir_name)
end
.reject(&:nil?)

final_dirs = Dir.glob("*")
expected_dirs = Set.new(all_repos.map { |r| File.basename(r, ".git") })
expected_dirs = Set.new(all_entries.map { |_, dir_name| dir_name })
to_delete = final_dirs.reject { |dir| expected_dirs.include?(dir) }

if to_delete.any?
Expand Down

0 comments on commit 9092b7d

Please sign in to comment.