dev-cmd/contributions: Use methods to get arguments

- I got these with hash syntax because I couldn't figure out Sorbet, but
  there's `args.rbi` to add the CLI args methods to. Nice!
- In doing this I realised that `--repositories` is required again, we
  no longer infer `--repositories=all` from no `--repositories` passed
  as we did in a previous version of this.
This commit is contained in:
Issy Long 2022-07-30 00:36:31 +01:00
parent 5d312d25d0
commit c02e03a179
No known key found for this signature in database
GPG Key ID: 8247C390DADC67D4
2 changed files with 22 additions and 12 deletions

View File

@ -300,6 +300,15 @@ module Homebrew
sig { returns(T.nilable(String)) }
def screen_saverdir; end
sig { returns(T::Array[String])}
def repositories; end
sig { returns(T.nilable(String)) }
def from; end
sig { returns(T.nilable(String)) }
def to; end
sig { returns(T.nilable(T::Array[String])) }
def groups; end

View File

@ -40,12 +40,13 @@ module Homebrew
sig { returns(NilClass) }
def contributions
args = contributions_args.parse
return ofail "Please specify `--repositories` to search, or `--repositories=all`." unless args[:repositories].empty?
commits = 0
coauthorships = 0
all_repos = args[:repositories].first == "all"
repos = all_repos ? SUPPORTED_REPOS : args[:repositories]
all_repos = args.repositories.first == "all"
repos = all_repos ? SUPPORTED_REPOS : args.repositories
repos.each do |repo|
if SUPPORTED_REPOS.exclude?(repo)
return ofail "Unsupported repository: #{repo}. Try one of #{SUPPORTED_REPOS.join(", ")}."
@ -66,12 +67,12 @@ module Homebrew
sentence = "Person #{args.named.first} directly authored #{commits} commits " \
"and co-authored #{coauthorships} commits " \
"across #{all_repos ? "all Homebrew repos" : repos.to_sentence}"
sentence += if args[:from] && args[:to]
" between #{args[:from]} and #{args[:to]}"
elsif args[:from]
" after #{args[:from]}"
elsif args[:to]
" before #{args[:to]}"
sentence += if args.from && args.to
" between #{args.from} and #{args.to}"
elsif args.from
" after #{args.from}"
elsif args.to
" before #{args.to}"
else
" in all time"
end
@ -90,8 +91,8 @@ module Homebrew
sig { params(repo_path: Pathname, args: Homebrew::CLI::Args).returns(Integer) }
def git_log_author_cmd(repo_path, args)
cmd = ["git", "-C", repo_path, "log", "--oneline", "--author=#{args.named.first}"]
cmd << "--before=#{args[:to]}" if args[:to]
cmd << "--after=#{args[:from]}" if args[:from]
cmd << "--before=#{args.to}" if args.to
cmd << "--after=#{args.from}" if args.from
Utils.safe_popen_read(*cmd).lines.count
end
@ -100,8 +101,8 @@ module Homebrew
def git_log_coauthor_cmd(repo_path, args)
cmd = ["git", "-C", repo_path, "log", "--oneline"]
cmd << "--format='%(trailers:key=Co-authored-by:)'"
cmd << "--before=#{args[:to]}" if args[:to]
cmd << "--after=#{args[:from]}" if args[:from]
cmd << "--before=#{args.to}" if args.to
cmd << "--after=#{args.from}" if args.from
Utils.safe_popen_read(*cmd).lines.count { |l| l.include?(args.named.first) }
end