2022-07-24 22:06:00 +01:00
|
|
|
# typed: true
|
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require "cli/parser"
|
|
|
|
|
|
|
|
module Homebrew
|
|
|
|
extend T::Sig
|
|
|
|
|
|
|
|
module_function
|
|
|
|
|
|
|
|
SUPPORTED_REPOS = %w[brew core cask bundle].freeze
|
|
|
|
|
|
|
|
sig { returns(CLI::Parser) }
|
|
|
|
def contributions_args
|
|
|
|
Homebrew::CLI::Parser.new do
|
|
|
|
usage_banner <<~EOS
|
2022-07-24 23:32:48 +01:00
|
|
|
`contributions [email]`
|
2022-07-24 22:06:00 +01:00
|
|
|
|
|
|
|
Contributions to Homebrew repos for a user.
|
|
|
|
EOS
|
|
|
|
|
|
|
|
flag "--from=",
|
|
|
|
description: "Date (ISO-8601 format) to start searching contributions."
|
|
|
|
|
|
|
|
flag "--to=",
|
|
|
|
description: "Date (ISO-8601 format) to stop searching contributions."
|
|
|
|
|
|
|
|
comma_array "--repos=",
|
|
|
|
description: "The Homebrew repositories to search for contributions in. " \
|
|
|
|
"Comma separated. Supported repos: #{SUPPORTED_REPOS.join(", ")}."
|
|
|
|
|
2022-07-25 10:28:51 +01:00
|
|
|
named_args :email, number: 1
|
2022-07-24 22:06:00 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
sig { returns(NilClass) }
|
|
|
|
def contributions
|
|
|
|
args = contributions_args.parse
|
|
|
|
|
2022-07-24 23:41:00 +01:00
|
|
|
return ofail "`--repos` is required." if args[:repos].empty?
|
2022-07-24 22:06:00 +01:00
|
|
|
|
|
|
|
commits = 0
|
|
|
|
coauthorships = 0
|
|
|
|
|
|
|
|
args[:repos].each do |repo|
|
2022-07-24 22:44:25 +01:00
|
|
|
if SUPPORTED_REPOS.exclude?(repo)
|
|
|
|
return ofail "Unsupported repo: #{repo}. Try one of #{SUPPORTED_REPOS.join(", ")}."
|
2022-07-24 22:06:00 +01:00
|
|
|
end
|
|
|
|
|
2022-07-24 22:44:25 +01:00
|
|
|
repo_path = find_repo_path_for_repo(repo)
|
2022-07-24 23:18:27 +01:00
|
|
|
return ofail "Couldn't find repo #{repo} locally. Run `brew tap homebrew/#{repo}`." unless repo_path.exist?
|
2022-07-24 22:44:25 +01:00
|
|
|
|
2022-07-24 23:41:00 +01:00
|
|
|
commits += git_log_author_cmd(T.must(repo_path), args)
|
|
|
|
coauthorships += git_log_coauthor_cmd(T.must(repo_path), args)
|
2022-07-24 22:06:00 +01:00
|
|
|
end
|
|
|
|
|
2022-07-25 10:52:32 +01:00
|
|
|
sentence = "Person #{args.named.first} directly authored #{commits} commits " \
|
|
|
|
"and co-authored #{coauthorships} commits " \
|
|
|
|
"to #{args[:repos].join(", ")}"
|
2022-07-24 22:06:00 +01:00
|
|
|
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
|
|
|
|
sentence += "."
|
|
|
|
|
|
|
|
puts sentence
|
|
|
|
end
|
|
|
|
|
2022-07-24 23:41:00 +01:00
|
|
|
sig { params(repo: String).returns(Pathname) }
|
2022-07-24 22:06:00 +01:00
|
|
|
def find_repo_path_for_repo(repo)
|
2022-07-24 23:18:27 +01:00
|
|
|
return HOMEBREW_REPOSITORY if repo == "brew"
|
|
|
|
|
|
|
|
Tap.fetch("homebrew", repo).path
|
2022-07-24 22:06:00 +01:00
|
|
|
end
|
|
|
|
|
2022-07-24 23:41:00 +01:00
|
|
|
sig { params(repo_path: Pathname, args: Homebrew::CLI::Args).returns(Integer) }
|
2022-07-24 23:24:52 +01:00
|
|
|
def git_log_author_cmd(repo_path, args)
|
2022-07-28 11:20:04 +01:00
|
|
|
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]
|
2022-07-24 23:24:52 +01:00
|
|
|
|
2022-07-28 11:20:04 +01:00
|
|
|
Utils.safe_popen_read(*cmd).lines.count
|
2022-07-24 23:24:52 +01:00
|
|
|
end
|
|
|
|
|
2022-07-24 23:41:00 +01:00
|
|
|
sig { params(repo_path: Pathname, args: Homebrew::CLI::Args).returns(Integer) }
|
2022-07-24 23:24:52 +01:00
|
|
|
def git_log_coauthor_cmd(repo_path, args)
|
2022-07-28 11:20:04 +01:00
|
|
|
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]
|
2022-07-24 22:06:00 +01:00
|
|
|
|
2022-07-28 11:20:04 +01:00
|
|
|
Utils.safe_popen_read(*cmd).lines.select { |l| l.include?(args.named.first) }.length
|
2022-07-24 22:06:00 +01:00
|
|
|
end
|
|
|
|
end
|