Skip to content

Conversation

@Pagten
Copy link
Contributor

@Pagten Pagten commented Nov 9, 2025

The git_repository and new_git_repository rules currently require not only the remote repository URI to be specified but also either the branch, tag or revision (commit) to check out. This PR removes the latter requirement. If no branch, tag or revision are specified, the repo's default branch will be checked out.

The main reason for introducing this proposed change, is because I noticed certain Bazel rules already generate git_repository targets from git URIs or other git repo specs that might not have a branch, tag or revision specified. These rules then typically default to a branch name of master, which is not correct in general. This change will allow those rules to simply omit the branch, tag and revision from the generated git_repository targets, to check out the repo's default branch.

An alternative, more explicit, interface for specifying that the default branch is to be checked out, would have been to add an optional boolean default_branch attribute to the git_repository and new_git_repository rules. The presence of that attribute would be mutually exclusive with the presence of branch, tag and commit. Let me know if that interface is preferable.

Addresses issue #27610.

@github-actions github-actions bot added the awaiting-review PR is awaiting review from an assigned reviewer label Nov 9, 2025
Copy link
Collaborator

@fmeum fmeum left a comment

Choose a reason for hiding this comment

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

I would use this!

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds support for checking out a Git repository's default branch when using git_repository or new_git_repository rules without specifying a branch, tag, or commit. Previously, at least one of these attributes was required; now they are all optional, allowing the repository's default branch to be checked out automatically.

  • Updated validation logic to allow zero or one of branch, tag, or commit (previously required exactly one)
  • Added default branch handling in git_worker.bzl using origin/HEAD
  • Updated documentation to reflect the new optional behavior
  • Added test coverage for the default branch scenario in both shell and Java tests

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
tools/build_defs/repo/git_worker.bzl Added default branch case handling using origin/HEAD and updated documentation
tools/build_defs/repo/git.bzl Changed validation from "exactly one" to "at most one" and updated rule documentation
src/test/shell/bazel/starlark_git_repository_test.sh Added tests for default branch checkout and removed obsolete error test
src/test/java/com/google/devtools/build/lib/blackbox/tests/workspace/GitRepositoryBlackBoxTest.java Added testCloneAtDefaultBranch test and updated class documentation

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@Pagten Pagten force-pushed the pieagt/support-checking-out-default-branch-in-git-repository-rule branch from 0111db3 to e99ce2a Compare November 17, 2025 14:09
Copy link
Member

@meteorcloudy meteorcloudy left a comment

Choose a reason for hiding this comment

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

Thanks!

@meteorcloudy meteorcloudy added the awaiting-PR-merge PR has been approved by a reviewer and is ready to be merge internally label Nov 17, 2025
@meteorcloudy
Copy link
Member

meteorcloudy commented Nov 17, 2025

@bazel-io fork 9.0.0

@iancha1992 iancha1992 added the team-ExternalDeps External dependency handling, remote repositiories, WORKSPACE file. label Nov 17, 2025
@Wyverald
Copy link
Member

Thank you for this well-thought out, well-explained and well-written PR!

@github-actions github-actions bot removed awaiting-PR-merge PR has been approved by a reviewer and is ready to be merge internally awaiting-review PR is awaiting review from an assigned reviewer labels Nov 18, 2025
bazel-io pushed a commit to bazel-io/bazel that referenced this pull request Nov 18, 2025
…ew_git_repository` rule

The `git_repository` and `new_git_repository` rules currently require not only the remote repository URI to be specified but also either the branch, tag or revision (commit) to check out. This PR removes the latter requirement. If no branch, tag or revision are specified, the repo's default branch will be checked out.

The main reason for introducing this proposed change, is because I noticed certain Bazel rules already generate `git_repository` targets from git URIs or other git repo specs that might not have a branch, tag or revision specified. These rules then typically default to a branch name of `master`, which is not correct in general. This change will allow those rules to simply omit the branch, tag and revision from the generated `git_repository` targets, to check out the repo's default branch.

An alternative, more explicit, interface for specifying that the default branch is to be checked out, would have been to add an optional boolean `default_branch` attribute to the `git_repository` and `new_git_repository` rules. The presence of that attribute would be mutually exclusive with the presence of `branch`, `tag` and `commit`. Let me know if that interface is preferable.

Addresses issue bazelbuild#27610.

Closes bazelbuild#27611.

PiperOrigin-RevId: 833710273
Change-Id: I7bc18efcbcfcaba61bf27052fecceb50336d2eb3
@Pagten
Copy link
Contributor Author

Pagten commented Nov 18, 2025

Thank you for this well-thought out, well-explained and well-written PR!

Thank you, glad to be contributing back!

I have a question though: I see the PR has been forked off into the 9.0.0 release branch but not into the 8.5.0 branch. I'm not aware of the policy around this, but at least in terms of backwards compatibility it should be okay to include these changes in 8.5.0 as well.

@meteorcloudy
Copy link
Member

Looks like we need another rc for 8.5.0, I'm fine with backporting to 8.5.0

@meteorcloudy
Copy link
Member

@bazel-io fork 8.5.0

bazel-io pushed a commit to bazel-io/bazel that referenced this pull request Nov 18, 2025
…ew_git_repository` rule

The `git_repository` and `new_git_repository` rules currently require not only the remote repository URI to be specified but also either the branch, tag or revision (commit) to check out. This PR removes the latter requirement. If no branch, tag or revision are specified, the repo's default branch will be checked out.

The main reason for introducing this proposed change, is because I noticed certain Bazel rules already generate `git_repository` targets from git URIs or other git repo specs that might not have a branch, tag or revision specified. These rules then typically default to a branch name of `master`, which is not correct in general. This change will allow those rules to simply omit the branch, tag and revision from the generated `git_repository` targets, to check out the repo's default branch.

An alternative, more explicit, interface for specifying that the default branch is to be checked out, would have been to add an optional boolean `default_branch` attribute to the `git_repository` and `new_git_repository` rules. The presence of that attribute would be mutually exclusive with the presence of `branch`, `tag` and `commit`. Let me know if that interface is preferable.

Addresses issue bazelbuild#27610.

Closes bazelbuild#27611.

PiperOrigin-RevId: 833710273
Change-Id: I7bc18efcbcfcaba61bf27052fecceb50336d2eb3
github-merge-queue bot pushed a commit that referenced this pull request Nov 18, 2025
…tory`/`new_git_repository` rule (#27701)

The `git_repository` and `new_git_repository` rules currently require
not only the remote repository URI to be specified but also either the
branch, tag or revision (commit) to check out. This PR removes the
latter requirement. If no branch, tag or revision are specified, the
repo's default branch will be checked out.

The main reason for introducing this proposed change, is because I
noticed certain Bazel rules already generate `git_repository` targets
from git URIs or other git repo specs that might not have a branch, tag
or revision specified. These rules then typically default to a branch
name of `master`, which is not correct in general. This change will
allow those rules to simply omit the branch, tag and revision from the
generated `git_repository` targets, to check out the repo's default
branch.

An alternative, more explicit, interface for specifying that the default
branch is to be checked out, would have been to add an optional boolean
`default_branch` attribute to the `git_repository` and
`new_git_repository` rules. The presence of that attribute would be
mutually exclusive with the presence of `branch`, `tag` and `commit`.
Let me know if that interface is preferable.

Addresses issue #27610.

Closes #27611.

PiperOrigin-RevId: 833710273
Change-Id: I7bc18efcbcfcaba61bf27052fecceb50336d2eb3

Commit
ee3af97

Co-authored-by: Pieter Agten <pieter.agten@gmail.com>
github-merge-queue bot pushed a commit that referenced this pull request Nov 19, 2025
…tory`/`new_git_repository` rule (#27705)

The `git_repository` and `new_git_repository` rules currently require
not only the remote repository URI to be specified but also either the
branch, tag or revision (commit) to check out. This PR removes the
latter requirement. If no branch, tag or revision are specified, the
repo's default branch will be checked out.

The main reason for introducing this proposed change, is because I
noticed certain Bazel rules already generate `git_repository` targets
from git URIs or other git repo specs that might not have a branch, tag
or revision specified. These rules then typically default to a branch
name of `master`, which is not correct in general. This change will
allow those rules to simply omit the branch, tag and revision from the
generated `git_repository` targets, to check out the repo's default
branch.

An alternative, more explicit, interface for specifying that the default
branch is to be checked out, would have been to add an optional boolean
`default_branch` attribute to the `git_repository` and
`new_git_repository` rules. The presence of that attribute would be
mutually exclusive with the presence of `branch`, `tag` and `commit`.
Let me know if that interface is preferable.

Addresses issue #27610.

Closes #27611.

PiperOrigin-RevId: 833710273
Change-Id: I7bc18efcbcfcaba61bf27052fecceb50336d2eb3

Commit
ee3af97

---------

Co-authored-by: Pieter Agten <pieter.agten@gmail.com>
Co-authored-by: Yun Peng <pcloudy@google.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

team-ExternalDeps External dependency handling, remote repositiories, WORKSPACE file.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants