Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resolve_latest_keg should return the latest HEAD keg when no stable kegs exist #11824

Merged
merged 4 commits into from
Aug 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion Library/Homebrew/cli/named_args.rb
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,11 @@ def resolve_latest_keg(name)
# Return keg if it is the only installed keg
return kegs if kegs.length == 1

kegs.reject { |k| k.version.head? }.max_by(&:version)
stable_kegs = kegs.reject { |k| k.version.head? }

return kegs.max_by { |keg| Tab.for_keg(keg).source_modified_time } if stable_kegs.blank?
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I included the source_modified_time code here directly since it is so brief. If you think this should be encapsulated elsewhere for use in resolve_latest_keg and latest_head_version, I'm happy to make changes. @Rylan12

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cnnrmnn I think this is fine. I'm not sure it really makes much sense to extract what's essentially #source_modified_time to its own method.

Just making sure, the revision secondary sort from Formula#latest_head_version isn't necessary here, right?

Copy link
Contributor Author

@cnnrmnn cnnrmnn Aug 10, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure that the revision secondary sort is needed anywhere we're dealing exclusively with HEAD versions. If you check out the parsing function for PkgVersion, you'll see that it parses the revision using a regex. HEAD paths (e.g., HEAD-6974ce8) matched to that regex will always set revision to nil. Any HEAD version should have a revision equal to 0 since to_i is called on the parsed nil revision.

Am I missing anything?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotcha makes sense. So it could probably be removed from latest_head_version too, right? Probably not worth doing but just curious if there's a difference.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So it could probably be removed from latest_head_version too, right?

Yep, should be fine to remove. I can do that in a quick PR as to not confuse anyone dealing with that code in the future.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You definitely don't need to but it probably wouldn't hurt. Plus it will expose more maintainers to it in case anyone knows something we're missing and it turns out it is necessary

Copy link
Contributor Author

@cnnrmnn cnnrmnn Aug 11, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Rylan12 Did some quick digging, and found a spec that tests HEAD versions with revisions. Do you know if there's any scenario where a HEAD version would actually have a revision?

I would think so given that the above test exists, but I'm still curious. If you don't know, I can do a little more digging and restore the secondary sort here if appropriate.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, good call. If you do brew install --HEAD on a formula that's specified a revision it will show up as HEAD-6974ce8_3 (which means there will be a revision), so I think it does need to be added here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotcha, will push a fix shortly.


stable_kegs.max_by(&:version)
end

def resolve_default_keg(name)
Expand Down
10 changes: 7 additions & 3 deletions Library/Homebrew/test/cli/named_args_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -223,12 +223,16 @@ def setup_unredable_cask(name)
(HOMEBREW_CELLAR/"foo/1.0").mkpath
(HOMEBREW_CELLAR/"foo/2.0").mkpath
(HOMEBREW_CELLAR/"bar/1.0").mkpath
(HOMEBREW_CELLAR/"baz/HEAD-1").mkpath
head2 = HOMEBREW_CELLAR/"baz/HEAD-2"
head2.mkpath
(head2/"INSTALL_RECEIPT.json").write (TEST_FIXTURE_DIR/"receipt.json").read
end

it "resolves the latest kegs with #resolve_latest_keg" do
latest_kegs = described_class.new("foo", "bar").to_latest_kegs
expect(latest_kegs.map(&:name)).to eq ["foo", "bar"]
expect(latest_kegs.map { |k| k.version.version.to_s }).to eq ["2.0", "1.0"]
latest_kegs = described_class.new("foo", "bar", "baz").to_latest_kegs
expect(latest_kegs.map(&:name)).to eq ["foo", "bar", "baz"]
expect(latest_kegs.map { |k| k.version.version.to_s }).to eq ["2.0", "1.0", "HEAD-2"]
end

it "when there are no matching kegs returns an empty array" do
Expand Down
1 change: 1 addition & 0 deletions Library/Homebrew/test/support/fixtures/receipt.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"bin/foo"
],
"time": 1403827774,
"source_modified_time": 1628303333,
"HEAD": "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef",
"alias_path": "/usr/local/Library/Taps/homebrew/homebrew-core/Aliases/test-formula",
"stdlib": "libcxx",
Expand Down