Skip to content

Commit

Permalink
[7.1.0] bzlmod: support git repos in source.json (#21036)
Browse files Browse the repository at this point in the history
bzlmod: support git repos in source.json

Closes #20912.

RELNOTES: Added `init_submodules` attribute to `git_override`.
Registries now support the `git_repository` type in `source.json`.

Change-Id: Ia267131e6d081572a642888ba34e285805bbbe9f
PiperOrigin-RevId: 601268823

---------

Co-authored-by: Nachum Goldstein <[email protected]>
Co-authored-by: Keith Smiley <[email protected]>
  • Loading branch information
3 people authored Jan 26, 2024
1 parent c6c7693 commit d0b4c39
Show file tree
Hide file tree
Showing 9 changed files with 346 additions and 60 deletions.
10 changes: 10 additions & 0 deletions site/en/external/registry.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,16 @@ An index registry must follow the format below:
By default, the archive type is determined from the file extension of the URL. If the file has
no extension, you can explicitly specify one of the following: `"zip"`, `"jar"`, `"war"`, `"aar"`,
`"tar"`, `"tar.gz"`, `"tgz"`, `"tar.xz"`, `"txz"`, `"tar.zst"`, `"tzst"`, `tar.bz2`, `"ar"`, or `"deb"`.
* The type can be changed to use a git repository, with these fields:
* `type`: `git_repository`
* The following fields as described at https://bazel.build/rules/lib/repo/git:
* `remote`
* `commit`
* `shallow_since`
* `tag`
* `init_submodules`
* `verbose`
* `strip_prefix`
* The type can be changed to use a local path, representing a
`local_repository` repo, with these fields:
* `type`: `local_path`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ java_library(
srcs = [
"ArchiveRepoSpecBuilder.java",
"AttributeValues.java",
"GitRepoSpecBuilder.java",
"ModuleFile.java",
"ModuleKey.java",
"RepoSpec.java",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,21 @@

import com.google.auto.value.AutoValue;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.devtools.build.lib.bazel.bzlmod.BazelModuleInspectorValue.AugmentedModule.ResolutionReason;
import com.google.devtools.build.lib.cmdline.RepositoryName;

/** Specifies that a module should be retrieved from a Git repository. */
@AutoValue
public abstract class GitOverride implements NonRegistryOverride {
public static final String GIT_REPOSITORY_PATH = "@bazel_tools//tools/build_defs/repo:git.bzl";

public static GitOverride create(
String remote,
String commit,
ImmutableList<String> patches,
ImmutableList<String> patchCmds,
int patchStrip) {
return new AutoValue_GitOverride(remote, commit, patches, patchCmds, patchStrip);
int patchStrip,
boolean initSubmodules) {
return new AutoValue_GitOverride(
remote, commit, patches, patchCmds, patchStrip, initSubmodules);
}

/** The URL pointing to the git repository. */
Expand All @@ -50,22 +49,22 @@ public static GitOverride create(
/** The number of path segments to strip from the paths in the supplied patches. */
public abstract int getPatchStrip();

/** Whether submodules in the fetched repo should be recursively initialized. */
public abstract boolean getInitSubmodules();

/** Returns the {@link RepoSpec} that defines this repository. */
@Override
public RepoSpec getRepoSpec(RepositoryName repoName) {
ImmutableMap.Builder<String, Object> attrBuilder = ImmutableMap.builder();
attrBuilder
.put("name", repoName.getName())
.put("remote", getRemote())
.put("commit", getCommit())
.put("patches", getPatches())
.put("patch_cmds", getPatchCmds())
.put("patch_args", ImmutableList.of("-p" + getPatchStrip()));
return RepoSpec.builder()
.setBzlFile(GIT_REPOSITORY_PATH)
.setRuleClassName("git_repository")
.setAttributes(AttributeValues.create(attrBuilder.buildOrThrow()))
.build();
GitRepoSpecBuilder builder = new GitRepoSpecBuilder();
builder
.setRepoName(repoName.getName())
.setRemote(getRemote())
.setCommit(getCommit())
.setPatches(getPatches())
.setPatchCmds(getPatchCmds())
.setPatchArgs(ImmutableList.of("-p" + getPatchStrip()))
.setInitSubmodules(getInitSubmodules());
return builder.build();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
// Copyright 2021 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

package com.google.devtools.build.lib.bazel.bzlmod;

import com.google.common.collect.ImmutableMap;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.util.List;

/**
* Builder for a {@link RepoSpec} object that indicates how to materialize a repo corresponding to a
* {@code git_repository} repo rule call.
*/
public class GitRepoSpecBuilder {

public static final String GIT_REPO_PATH = "@bazel_tools//tools/build_defs/repo:git.bzl";

private final ImmutableMap.Builder<String, Object> attrBuilder;

public GitRepoSpecBuilder() {
attrBuilder = new ImmutableMap.Builder<>();
}

@CanIgnoreReturnValue
public GitRepoSpecBuilder setRepoName(String repoName) {
return setAttr("name", repoName);
}

@CanIgnoreReturnValue
public GitRepoSpecBuilder setRemote(String remoteRepoUrl) {
return setAttr("remote", remoteRepoUrl);
}

@CanIgnoreReturnValue
public GitRepoSpecBuilder setCommit(String gitCommitHash) {
return setAttr("commit", gitCommitHash);
}

@CanIgnoreReturnValue
public GitRepoSpecBuilder setShallowSince(String shallowSince) {
return setAttr("shallow_since", shallowSince);
}

@CanIgnoreReturnValue
public GitRepoSpecBuilder setTag(String tag) {
return setAttr("tag", tag);
}

@CanIgnoreReturnValue
public GitRepoSpecBuilder setInitSubmodules(boolean initSubmodules) {
setAttr("init_submodules", initSubmodules);
setAttr("recursive_init_submodules", initSubmodules);
return this;
}

@CanIgnoreReturnValue
public GitRepoSpecBuilder setVerbose(boolean verbose) {
return setAttr("verbose", verbose);
}

@CanIgnoreReturnValue
public GitRepoSpecBuilder setStripPrefix(String stripPrefix) {
return setAttr("strip_prefix", stripPrefix);
}

@CanIgnoreReturnValue
public GitRepoSpecBuilder setPatches(List<String> patches) {
return setAttr("patches", patches);
}

@CanIgnoreReturnValue
public GitRepoSpecBuilder setPatchTool(String patchTool) {
return setAttr("patch_tool", patchTool);
}

@CanIgnoreReturnValue
public GitRepoSpecBuilder setPatchArgs(List<String> patchArgs) {
return setAttr("patch_args", patchArgs);
}

@CanIgnoreReturnValue
public GitRepoSpecBuilder setPatchCmds(List<String> patchCmds) {
return setAttr("patch_cmds", patchCmds);
}

@CanIgnoreReturnValue
public GitRepoSpecBuilder setPatchCmdsWin(List<String> patchCmdsWin) {
return setAttr("patch_cmds_win", patchCmdsWin);
}

public RepoSpec build() {
return RepoSpec.builder()
.setBzlFile(GIT_REPO_PATH)
.setRuleClassName("git_repository")
.setAttributes(AttributeValues.create(attrBuilder.buildOrThrow()))
.build();
}

@CanIgnoreReturnValue
private GitRepoSpecBuilder setAttr(String name, String value) {
if (value != null && !value.isEmpty()) {
attrBuilder.put(name, value);
}
return this;
}

@CanIgnoreReturnValue
private GitRepoSpecBuilder setAttr(String name, boolean value) {
attrBuilder.put(name, value);
return this;
}

@CanIgnoreReturnValue
private GitRepoSpecBuilder setAttr(String name, List<String> value) {
if (value != null && !value.isEmpty()) {
attrBuilder.put(name, value);
}
return this;
}
}
Loading

0 comments on commit d0b4c39

Please sign in to comment.