lib/git/multi.rb in git-multi-1.0.10 vs lib/git/multi.rb in git-multi-1.1.0
- old
+ new
@@ -42,10 +42,18 @@
HTML_PAGE = File.expand_path('../../man/git-multi.html', __dir__)
module_function
#
+ # multi-repo support
+ #
+
+ def valid?(multi_repo)
+ (USERS + ORGANIZATIONS).include? multi_repo
+ end
+
+ #
# local repositories (in WORKAREA)
#
@local_user_repositories = Hash.new { |repos, user|
repos[user] = Dir.new(WORKAREA).git_repos(user)
@@ -54,27 +62,65 @@
@local_org_repositories = Hash.new { |repos, org|
repos[org] = Dir.new(WORKAREA).git_repos(org)
}
def local_repositories
- (
+ @local_repositories ||= (
USERS.map { |user| @local_user_repositories[user] } +
ORGANIZATIONS.map { |org| @local_org_repositories[org] }
).flatten
end
+ def local_repositories_for(multi_repo = nil)
+ case (owner = multi_repo)
+ when nil
+ local_repositories
+ when *USERS
+ @local_user_repositories[owner]
+ when *ORGANIZATIONS
+ @local_org_repositories[owner]
+ else
+ raise "Unknown multi repo: #{multi_repo}"
+ end
+ end
+
#
# remote repositories (on GitHub)
#
+ @github_user_repositories = Hash.new { |repos, user|
+ repos[user] = Git::Hub.user_repositories(user)
+ }
+
+ @github_org_repositories = Hash.new { |repos, org|
+ repos[org] = Git::Hub.org_repositories(org)
+ }
+
def github_repositories
@github_repositories ||= (
- USERS.map { |user| Git::Hub.user_repositories(user) } +
- ORGANIZATIONS.map { |org| Git::Hub.org_repositories(org) }
+ USERS.map { |user| @github_user_repositories[user] } +
+ ORGANIZATIONS.map { |org| @github_org_repositories[org] }
).flatten
end
+ def github_repositories_for(multi_repo = nil)
+ case (owner = multi_repo)
+ when nil
+ github_repositories
+ when *USERS
+ @github_user_repositories[owner]
+ when *ORGANIZATIONS
+ @github_org_repositories[owner]
+ else
+ raise "Unknown multi repo: #{multi_repo}"
+ end
+ end
+
+ #
+ # manage the local repository cache
+ #
+
def refresh_repositories
File.directory?(CACHE) || FileUtils.mkdir_p(CACHE)
File.open(REPOSITORIES, 'wb') do |file|
Marshal.dump(github_repositories, file)
@@ -131,45 +177,59 @@
repositories # retry
end
end
#
+ # lists of repos for a given multi-repo
+ #
+
+ def repositories_for(multi_repo = nil)
+ if multi_repo.nil?
+ repositories
+ else
+ repositories.find_all { |repository|
+ repository.owner.login == multi_repo
+ }
+ end
+ end
+
+ #
# lists of repositories with a given state
#
- def archived_repositories
- repositories.find_all(&:archived)
+ def archived_repositories_for(multi_repo = nil)
+ repositories_for(multi_repo).find_all(&:archived)
end
- def forked_repositories
- repositories.find_all(&:fork)
+ def forked_repositories_for(multi_repo = nil)
+ repositories_for(multi_repo).find_all(&:fork)
end
- def private_repositories
- repositories.find_all(&:private)
+ def private_repositories_for(multi_repo = nil)
+ repositories_for(multi_repo).find_all(&:private)
end
#
# derived lists of repositories
#
- def excess_repositories
- repository_full_names = repositories.map(&:full_name)
- local_repositories.reject { |project|
+ def excess_repositories_for(multi_repo = nil)
+ repository_full_names = repositories_for(multi_repo).map(&:full_name)
+ local_repositories_for(multi_repo).reject { |project|
repository_full_names.include? project.full_name
}
end
- def stale_repositories
- repository_full_names = github_repositories.map(&:full_name)
- repositories.reject { |project|
+ def stale_repositories_for(multi_repo = nil)
+ repository_full_names = github_repositories_for(multi_repo).map(&:full_name)
+ repositories_for(multi_repo).reject { |project|
repository_full_names.include? project.full_name
}
end
- def spurious_repositories
- cloned_repositories.find_all { |project|
+ def spurious_repositories_for(multi_repo = nil)
+ cloned_repositories_for(multi_repo).find_all { |project|
origin_url = `git -C #{project.local_path} config --get remote.origin.url`.chomp
![
project.clone_url,
project.ssh_url,
project.compliant_ssh_url,
@@ -177,17 +237,17 @@
project.git_url,
].include? origin_url
}
end
- def missing_repositories
- repositories.find_all { |project|
+ def missing_repositories_for(multi_repo = nil)
+ repositories_for(multi_repo).find_all { |project|
!File.directory? project.local_path
}
end
- def cloned_repositories
- repositories.find_all { |project|
+ def cloned_repositories_for(multi_repo = nil)
+ repositories_for(multi_repo).find_all { |project|
File.directory? project.local_path
}
end
end