Class: Utils::Analytics::PackageFeeds Abstract Private
- Extended by:
- T::Helpers
- Defined in:
- utils/analytics/package_feeds.rb
Overview
This class is part of a private API. This class may only be used in the Homebrew/brew repository. Third parties should avoid using this class if possible, as it may be removed or changed without warning.
It cannot be directly instantiated. Subclasses must implement the abstract methods below.
Collects and writes bounded package feeds from first-parent Git history.
Direct Known Subclasses
Homebrew::DevCmd::GenerateCaskFeeds::Feeds, Homebrew::DevCmd::GenerateFormulaFeeds::Feeds
Defined Under Namespace
Classes: Change
Constant Summary collapse
- Entry =
This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.
T.type_alias { T::Hash[String, T.any(String, T::Array[String])] }
- Builds =
This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.
T.type_alias { T::Hash[String, FormulaHistory::Build] }
- LIMITS =
This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.
T.let({ "new" => 50, "updated" => 500 }.freeze, T::Hash[String, Integer])
- BATCH_SIZE =
This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.
100
Class Method Summary collapse
-
.write(repository:, output:, kind:) ⇒ void
private
Atomically writes the generated feeds.
Instance Method Summary collapse
- #builds(path, blob, sources) ⇒ Builds private
- #changes(offset) ⇒ Array<Change>? private
- #generate ⇒ Hash{String => String, Array<Entry>} private
- #initialize(repository) ⇒ void constructor private
-
#package_path?(path) ⇒ Boolean
abstract
private
Identifies package definitions in the repository's historical layout.
-
#read_builds(path, blob, source) ⇒ Builds
abstract
private
Reads platform-specific versions from a package definition.
Constructor Details
#initialize(repository) ⇒ void
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
39 40 41 42 43 |
# File 'utils/analytics/package_feeds.rb', line 39 def initialize(repository) @history = T.let(FormulaHistory.new(repository), FormulaHistory) @revision = T.let(@history.git("rev-parse", "--verify", "refs/remotes/origin/HEAD^{commit}"), String) @builds = T.let({}, T::Hash[[String, String], Builds]) end |
Class Method Details
.write(repository:, output:, kind:) ⇒ void
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
This method returns an undefined value.
Atomically writes the generated feeds.
31 32 33 34 35 36 |
# File 'utils/analytics/package_feeds.rb', line 31 def self.write(repository:, output:, kind:) feeds = new(repository).generate output.dirname.mkpath output.atomic_write("#{JSON.pretty_generate(feeds)}\n") puts "Generated #{feeds.fetch("new").length} new and #{feeds.fetch("updated").length} updated #{kind} entries" end |
Instance Method Details
#builds(path, blob, sources) ⇒ Builds
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
93 94 95 |
# File 'utils/analytics/package_feeds.rb', line 93 def builds(path, blob, sources) @builds[[path, blob]] ||= read_builds(path, blob, sources.fetch(blob)) end |
#changes(offset) ⇒ Array<Change>?
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'utils/analytics/package_feeds.rb', line 106 def changes(offset) output = @history.git("log", @revision, "--first-parent", "--raw", "--root", "--no-abbrev", "--diff-merges=first-parent", "--find-renames=50%", "--max-count=#{BATCH_SIZE}", "--skip=#{offset}", "--format=%x00commit%x00%H%x00%ct", "-z") return if output.empty? tokens = output.split("\0") result = T.let([], T::Array[Change]) deleted = T.let({}, T::Hash[[String, String], [String, String]]) commit = T.let("", String) = 0 while (token = tokens.shift) if token == "commit" commit = tokens.shift || raise("Missing commit") = Integer(tokens.shift || raise("Missing commit date"), 10) elsif token.lstrip.start_with?(":") = token.lstrip.delete_prefix(":").split old_mode = .fetch(0) mode = .fetch(1) before = .fetch(2) after = .fetch(3) status = .fetch(4) old_path = tokens.shift || raise("Missing package path") path = status.start_with?("R", "C") ? (tokens.shift || raise("Missing renamed package path")) : old_path if status == "D" && %w[100644 100755].include?(old_mode) && package_path?(old_path) deleted[[commit, File.basename(old_path)]] = [old_path, before] end next if %w[100644 100755].exclude?(mode) || !package_path?(path) added = %w[100644 100755].exclude?(old_mode) || !package_path?(old_path) result << Change.new(commit:, timestamp:, path:, old_path:, before: added ? nil : before, after:, added:) end end result.each do |change| next unless change.added next unless (previous = deleted[[change.commit, File.basename(change.path)]]) change.old_path, change.before = previous change.added = false end result end |
#generate ⇒ Hash{String => String, Array<Entry>}
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'utils/analytics/package_feeds.rb', line 46 def generate if @history.git("rev-parse", "--is-shallow-repository") == "true" raise "Package feeds require complete Git history; unshallow the tap checkout first" end feeds = LIMITS.to_h { |name, _limit| [name, T.let([], T::Array[Entry])] } offset = 0 until feeds.all? { |name, entries| entries.length >= LIMITS.fetch(name) } changes = changes(offset) break if changes.nil? selected = changes.select do |change| feed = change.added ? "new" : "updated" feeds.fetch(feed).length < LIMITS.fetch(feed) end blobs = selected.flat_map { |change| [change.before, change.after] }.compact.uniq sources = blobs.empty? ? {} : @history.sources(blobs) selected.each do |change| feed = change.added ? "new" : "updated" next if feeds.fetch(feed).length >= LIMITS.fetch(feed) after = builds(change.path, change.after, sources) before_blob = change.before before = before_blob ? builds(change.old_path, before_blob, sources) : nil next if before && after.none? do |platform, build| old = before.fetch(platform) build.version && (old.version != build.version || build.revision > old.revision) end feeds.fetch(feed) << { "id" => "#{change.commit}:#{change.path}", "name" => File.basename(change.path, ".rb"), "date" => Time.at(change.).utc.iso8601, "commit" => change.commit, "versions" => after.values.filter_map do |build| next unless (version = build.version) build.revision.positive? ? "#{version}_#{build.revision}" : version end.uniq, } end offset += BATCH_SIZE end { "revision" => @revision, **feeds } end |
#package_path?(path) ⇒ Boolean
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
Identifies package definitions in the repository's historical layout.
103 |
# File 'utils/analytics/package_feeds.rb', line 103 def package_path?(path); end |
#read_builds(path, blob, source) ⇒ Builds
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
Reads platform-specific versions from a package definition.
99 |
# File 'utils/analytics/package_feeds.rb', line 99 def read_builds(path, blob, source); end |