Skip to content

Warn when the upstream remote isn't origin - #24299

Merged
MikeMcQuaid merged 2 commits into
Homebrew:mainfrom
HaraldNordgren:changed-files-upstream-remote
Oct 20, 2026
Merged

MikeMcQuaid merged 2 commits into
Homebrew:mainfrom
HaraldNordgren:changed-files-upstream-remote

Conversation

@HaraldNordgren

@HaraldNordgren HaraldNordgren commented Oct 19, 2026 •

Copy link
Copy Markdown
Member

Homebrew assumes origin is Homebrew/brew: brew update fetches from it, and the --changed options (and so brew lgtm) compare against the merge-base with origin/HEAD. gh repo fork sets things up the other way round, with the fork as origin and Homebrew/brew as upstream, both with --clone and when run inside an existing clone. If the fork's default branch is behind, brew lgtm then checks every file changed upstream since then, without saying why.

This makes brew doctor and brew lgtm detect that layout, where origin isn't the expected repository but another remote is, and suggest renaming the remotes:

Warning: The current git origin is:
  git@github.com:HaraldNordgren/brew.git
but the `upstream` remote is:
  git@github.com:Homebrew/brew.git

With a non-standard origin, Homebrew won't update properly
and `--changed` options compare against the wrong branch.

You can solve this by renaming the remotes:
  git -C "/path/to/brew" remote rename origin fork
  git -C "/path/to/brew" remote rename upstream origin

brew lgtm shows it before running any checks, for the brew repository or an official tap. brew doctor already flagged a non-standard origin, but it suggested git remote set-url origin https://github.com/Homebrew/brew, which would replace the fork's URL, and it also reported SSH origins like git@github.com:Homebrew/brew.git as non-standard. Both are fixed here.

This replaces the earlier version of this PR, which made --changed look for the Homebrew/brew remote by URL.

How I got here

brew lgtm had been failing for me while installing formulae, before it got to any checks. My checkout had the layout gh repo fork creates: origin was my fork, whose origin/HEAD pointed at a master from August, about 1,450 commits behind, and upstream was Homebrew/brew. So --changed saw about 1,700 changed files, 54 of them shell or workflow files. brew style then needed shfmt, and because mine was outdated, ./bin/brew tried to build go from source on my macOS 12 machine and failed.

Once I realised why, I renamed my remotes so that origin is Homebrew/brew:

$ git remote -v
HaraldNordgren	git@github.com:HaraldNordgren/brew.git (fetch)
HaraldNordgren	git@github.com:HaraldNordgren/brew.git (push)
origin	git@github.com:Homebrew/brew.git (fetch)
origin	git@github.com:Homebrew/brew.git (push)

After that, brew lgtm ran cleanly without installing anything. With this change, brew lgtm would have told me what to rename straight away.

Steps to reproduce

  1. In a clone of Homebrew/brew, run gh repo fork --remote (or use gh repo fork Homebrew/brew --clone), with a fork whose default branch is behind Homebrew/brew.
  2. git switch -c some-change upstream/main and change one Ruby file, e.g. Library/Homebrew/utils/git.rb.
  3. ./bin/brew lgtm checks every file changed upstream since the fork's default branch, and ./bin/brew doctor suggests git remote set-url origin https://github.com/Homebrew/brew, which would replace the fork's URL.

Changes

  • brew doctor: when origin isn't the expected repository but another remote is, suggest renaming the remotes rather than git remote set-url. Accept GitHub SSH URLs for origin.
  • brew lgtm: show the same warning for the brew repository or an official tap before running any checks.
  • Add specs for both.

  • Have you followed our Contributing guidelines?
  • Have you checked for other open Pull Requests for the same change?
  • Have you explained what your changes do? Performance claims (e.g. "this is faster") must include brew benchmark results.
  • Have you explained why you'd like these changes included, not just what they do?
  • For bug fixes, have you given step-by-step brew commands to reproduce the bug?
  • Have you written new tests (excluding integration tests)? Here's an example.
  • Have you successfully run brew lgtm (style, typechecking and tests) locally?

  • I did not use AI/LLM to create this PR, or I disclosed the tool/model below and reviewed its output; I did not attribute commits to AI and will answer maintainer questions and review comments myself without AI/LLM.

Claude Code (Claude Opus 5.5) helped trace why brew lgtm was installing formulae and write the change and specs. The new specs fail without the change and pass with it, and brew lgtm passes locally.


Summary by CodeRabbit

  • Bug Fixes
    • brew lgtm now checks Git origins for repositories outside taps and for core and official taps, warning when an origin does not match the expected repository.
    • Recognised equivalent GitHub SSH and HTTPS URLs are accepted as matching origins.
    • If another fetch remote matches the expected repository, the diagnostic identifies the remote and suggests making it origin; otherwise, it retains the existing guidance for correcting the origin URL.

@MikeMcQuaid MikeMcQuaid left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This seems potentially a bit error-prone than the existing status quo. This assumption is baked all over the place in the app. I'd suggest just using origin for Homebrew/brew instead?

@HaraldNordgren

Copy link
Copy Markdown
Member Author

This seems potentially a bit error-prone than the existing status quo. This assumption is baked all over the place in the app. I'd suggest just using origin for Homebrew/brew instead?

Maybe, or we change all the assumed origin uses instead? Well, obviously that's a lot of work!

My acute problem can be solved easily now that I actually identified it: rename my branches.

But as always: I'm interested in general solutions. And I didn't arrive at this situation by any arcane magic, I just cloned the repo and forked using the official gh tool, so I can't be the only one with this setup.

@MikeMcQuaid

Copy link
Copy Markdown
Member

But as always: I'm interested in general solutions. And I didn't arrive at this situation by any arcane magic, I just cloned the repo and forked using the official gh tool, so I can't be the only one with this setup.

Is there any way to configure the gh tool to handle this repo differently? I don't want us to optimise too hard for a specific tool; others will work differently.

@HaraldNordgren

Copy link
Copy Markdown
Member Author

Is there any way to configure the gh tool to handle this repo differently? I don't want us to optimise too hard for a specific tool; others will work differently.

Probably yes.

@MikeMcQuaid

Copy link
Copy Markdown
Member

@HaraldNordgren Happy to rework this PR accordingly to that otherwise can close out.

@HaraldNordgren

HaraldNordgren commented Oct 19, 2026 •

Copy link
Copy Markdown
Member Author

@HaraldNordgren Happy to rework this PR accordingly to that otherwise can close out.

Aha, you meant to instruct gh to handle us differently? Well, I'll take a look and otherwise close it.

@MikeMcQuaid

Copy link
Copy Markdown
Member

Aha, you meant to instruct gh to handle us differently? Well, I'll take a look and otherwise close it.

Indeed or maybe even a e.g. brew doctor check to point how to make these the right way around; less concerned about a warning like that?

@HaraldNordgren
HaraldNordgren force-pushed the changed-files-upstream-remote branch from 413647b to a79b3a2 Compare October 19, 2026 18:41
@HaraldNordgren HaraldNordgren changed the title Prefer the upstream remote over a fork's origin Warn when the upstream remote isn't origin Oct 19, 2026
@HaraldNordgren
HaraldNordgren force-pushed the changed-files-upstream-remote branch from a79b3a2 to 114ff3a Compare October 19, 2026 18:42
- `gh repo fork` sets up the fork as `origin` and `Homebrew/brew` as
  `upstream`, so `--changed` compared against the fork's possibly stale
  default branch and `brew lgtm` checked every file changed upstream
  since then.
- Have `brew doctor` and `brew lgtm` suggest renaming the remotes in
  that case, rather than `git remote set-url` replacing the fork.
- Accept GitHub SSH origins, which `brew doctor` reported as
  non-standard.
@HaraldNordgren
HaraldNordgren force-pushed the changed-files-upstream-remote branch from 114ff3a to a7cb8c7 Compare October 19, 2026 18:54
@HaraldNordgren
HaraldNordgren marked this pull request as ready for review October 19, 2026 18:55
@HaraldNordgren

Copy link
Copy Markdown
Member Author

@MikeMcQuaid I turned it into a warning instead.

@coderabbitai

coderabbitai Bot commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

📝 Walkthrough

brew lgtm now checks Git origins for Homebrew and core or official taps. Origin diagnostics recognise supported GitHub SSH URL forms and can suggest remote renames when another fetch remote matches the desired repository.

Changes: Git origin checks

Layer / File(s) Summary
Match and diagnose Git origins
Library/Homebrew/diagnostic.rb, Library/Homebrew/test/diagnostic_checks_spec.rb
examine_git_origin recognises supported GitHub SSH URLs. If another fetch remote matches the desired repository, the diagnostic suggests renaming the remotes. Specs cover matching origins, remote rename suggestions, rewritten URLs, and URL pattern matching.
Run origin checks from lgtm
Library/Homebrew/dev-cmd/lgtm.rb, Library/Homebrew/test/dev-cmd/lgtm_spec.rb
brew lgtm checks the Homebrew origin outside a tap, the configured core remote for core taps, or the default remote for official taps. It warns when the check returns a diagnostic. Specs cover warning behaviour and the remote passed to the check.

Priority: ➖ Normal

Merge Risk: 🔵 Low · up to e0ce0

The PR is mergeable with a bounded follow-up: developers whose matching remote is named fork will receive rename instructions that do not work.

Pre-merge checks: Passed 4
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly describes the main change: warning when the upstream remote is not named origin.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 3


ℹ️ Review info
⚙️ Run configuration
  • Configuration used: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 211fee93-08da-429b-a57f-a6dae71d040a
📥 Commits

Reviewing files that changed from the base of the PR and between 41f40de and a7cb8c7.

📒 Files selected for processing: 4
  • Library/Homebrew/dev-cmd/lgtm.rb
  • Library/Homebrew/diagnostic.rb
  • Library/Homebrew/test/dev-cmd/lgtm_spec.rb
  • Library/Homebrew/test/diagnostic_checks_spec.rb

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

Comment thread Library/Homebrew/dev-cmd/lgtm.rb Resolved
Comment thread Library/Homebrew/diagnostic.rb Resolved
Comment thread Library/Homebrew/diagnostic.rb Resolved
- Compare remotes with `Tap.same_remote?`, keeping the `.git` suffix
  and trailing slash allowance for other hosts, so e.g. `githubXcom`
  no longer matches `github.com`.
- Never suggest renaming `origin` itself, e.g. when `url.insteadOf`
  rewrites its URL.
- Check the core tap against `$HOMEBREW_CORE_GIT_REMOTE` in
  `brew lgtm`, as `brew doctor` does.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Caution

Some comments are outside the diff and can’t be posted inline due to GitHub limitations.

⚠️ Outside diff range comments (1)

🟡 Minor · Use a collision-free temporary remote when swapping remote names. · diagnostic.rb:214-234

Library/Homebrew/diagnostic.rb:214-234
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Use a collision-free temporary remote when swapping remote names.

When both origin and the matching fork remote exist, both remediation commands fail because each destination name is already occupied. The matching remote does not become origin.

Suggested fix
         elsif desired_remote
+          remote_names = remotes.map { |line| line.split.first }.compact.uniq
+          temporary_remote = "homebrew-temp"
+          suffix = 0
+          while remote_names.include?(temporary_remote)
+            suffix += 1
+            temporary_remote = "homebrew-temp-#{suffix}"
+          end
+
           Finding.new(
@@
-                  git -C "#{repository_path}" remote rename origin fork
-                  git -C "#{repository_path}" remote rename #{desired_remote} origin
+                  git -C "#{repository_path}" remote rename #{desired_remote} #{temporary_remote}
+                  git -C "#{repository_path}" remote rename origin #{desired_remote}
+                  git -C "#{repository_path}" remote rename #{temporary_remote} origin
@@
-                "git -C \"#{repository_path}\" remote rename origin fork",
-                "git -C \"#{repository_path}\" remote rename #{desired_remote} origin",
+                "git -C \"#{repository_path}\" remote rename #{desired_remote} #{temporary_remote}",
+                "git -C \"#{repository_path}\" remote rename origin #{desired_remote}",
+                "git -C \"#{repository_path}\" remote rename #{temporary_remote} origin",

ℹ️ Review info
⚙️ Run configuration
  • Configuration used: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 51bb17f4-7c5f-4dd2-906f-2608f9680b7c
📥 Commits

Reviewing files that changed from the base of the PR and between a7cb8c7 and e0ce0f3.

📒 Files selected for processing: 4
  • Library/Homebrew/dev-cmd/lgtm.rb
  • Library/Homebrew/diagnostic.rb
  • Library/Homebrew/test/dev-cmd/lgtm_spec.rb
  • Library/Homebrew/test/diagnostic_checks_spec.rb
🚧 Files skipped from review as they are similar to previous changes: 4
  • Library/Homebrew/dev-cmd/lgtm.rb
  • Library/Homebrew/test/diagnostic_checks_spec.rb
  • Library/Homebrew/test/dev-cmd/lgtm_spec.rb
  • Library/Homebrew/diagnostic.rb

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.

@MikeMcQuaid MikeMcQuaid left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks!

@MikeMcQuaid
MikeMcQuaid added this pull request to the merge queue Oct 20, 2026
Merged via the queue into Homebrew:main with commit 14b6b22 Oct 20, 2026
58 checks passed
@HaraldNordgren
HaraldNordgren deleted the changed-files-upstream-remote branch October 20, 2026 09:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

⌂ 换网址