Speed up MissingFormula.deleted_reason

The current way we find out where a formula was deleted is
using `git log` to query the last months worth of formula.
For core formula that is 11,000+ commits which makes this slow.

This quits early by diffing the current branch with a commit
from a month ago and checks if that file has been deleted in the
meantime. Then, it procedes with the above `git log` command.

Note: This will miss formula that were introduced less than a month
ago and removed in the meantime though that is seemingly very rare.
This commit is contained in:
apainintheneck 2022-11-11 14:39:50 -08:00
parent e1b2f68a6d
commit 5241439f43

View File

@ -154,17 +154,25 @@ module Homebrew
end
end
log_command = "git log --since='1 month ago' --diff-filter=D " \
"--name-only --max-count=1 " \
"--format=%H\\\\n%h\\\\n%B -- #{relative_path}"
hash, short_hash, *commit_message, relative_path =
Utils.popen_read(log_command).gsub("\\n", "\n").lines.map(&:chomp)
commit_command = "git log --before='1 month ago' --max-count=1 --format=%H"
month_old_commit = Utils.popen_read(commit_command).chomp
if hash.blank? || short_hash.blank? || relative_path.blank?
# Check if the formula has been deleted in the last month
# by comparing the diff between now and a month ago.
diff_command = "git diff --diff-filter=D --name-only " \
"#{month_old_commit} HEAD -- #{relative_path}"
if Utils.popen_read(diff_command).blank?
ofail "No previously deleted formula found." unless silent
return
end
log_command = "git log --since='1 month ago' --diff-filter=D " \
"--name-only --max-count=1 " \
"--format=%h\\\\n%B -- #{relative_path}"
short_hash, *commit_message, relative_path =
Utils.popen_read(log_command).gsub("\\n", "\n").lines.map(&:chomp)
commit_message = commit_message.reject(&:empty?).join("\n ")
commit_message.sub!(/ \(#(\d+)\)$/, " (#{tap.issues_url}/\\1)")