Skip to content

Commit dde6d20

Browse files
fmeumcopybara-github
authored andcommitted
Do not recommend shallow_since for git_repository
Git's `--shallow-since` feature can lead to broken checkouts and a fix isn't likely to happen: https://public-inbox.org/git/CAPSG9dZV2EPpVKkOMcjv5z+NF7rUu=V-ZkZNx47rCv122HsiKg@mail.gmail.com/T/#u This commit removes `shallow_since` from the canonical reproducible form of `git_repository` unless the attribute is provided explicitly. In cases where Bazel's attempt at fetching with `--depth=1` fail, e.g. if there is no server support for shallow fetches of arbitrary commits, the progress message is now used to inform the user about this potential for fetch time optimizations. Fixes #10292 Fixes #12857 Closes #17356. PiperOrigin-RevId: 508569071 Change-Id: I01e6662e38c236d1800d427f66d48a05df818800
1 parent 79fc488 commit dde6d20

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

tools/build_defs/repo/git.bzl

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@ def _clone_or_update_repo(ctx):
4242
for item in ctx.path(dest_link).readdir():
4343
ctx.symlink(item, root.get_child(item.basename))
4444

45-
return {"commit": git_.commit, "shallow_since": git_.shallow_since}
45+
if ctx.attr.shallow_since:
46+
return {"commit": git_.commit, "shallow_since": git_.shallow_since}
47+
else:
48+
return {"commit": git_.commit}
4649

4750
def _update_git_attrs(orig, keys, override):
4851
result = update_attrs(orig, keys, override)
@@ -68,12 +71,14 @@ _common_attrs = {
6871
"shallow_since": attr.string(
6972
default = "",
7073
doc =
71-
"an optional date, not after the specified commit; the " +
72-
"argument is not allowed if a tag is specified (which allows " +
73-
"cloning with depth 1). Setting such a date close to the " +
74-
"specified commit allows for a more shallow clone of the " +
75-
"repository, saving bandwidth " +
76-
"and wall-clock time.",
74+
"an optional date, not after the specified commit; the argument " +
75+
"is not allowed if a tag or branch is specified (which can " +
76+
"always be cloned with --depth=1). Setting such a date close to " +
77+
"the specified commit may allow for a shallow clone of the " +
78+
"repository even if the server does not support shallow fetches " +
79+
"of arbitary commits. Due to bugs in git's --shallow-since " +
80+
"implementation, using this attribute is not recommended as it " +
81+
"may result in fetch failures.",
7782
),
7883
"tag": attr.string(
7984
default = "",
@@ -183,6 +188,10 @@ makes its targets available for binding. Also determine the id of the
183188
commit actually checked out and its date, and return a dict with parameters
184189
that provide a reproducible version of this rule (which a tag not necessarily
185190
is).
191+
192+
Bazel will first try to perform a shallow fetch of only the specified commit.
193+
If that fails (usually due to missing server support), it will fall back to a
194+
full fetch of the repository.
186195
""",
187196
)
188197

tools/build_defs/repo/git_worker.bzl

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ def git_repo(ctx, directory):
7979
recursive_init_submodules = ctx.attr.recursive_init_submodules,
8080
)
8181

82-
ctx.report_progress("Cloning %s of %s" % (reset_ref, ctx.attr.remote))
83-
if (ctx.attr.verbose):
82+
_report_progress(ctx, git_repo)
83+
if ctx.attr.verbose:
8484
print("git.bzl: Cloning or updating %s repository %s using strip_prefix of [%s]" %
8585
(
8686
" (%s)" % shallow if shallow else "",
@@ -95,6 +95,12 @@ def git_repo(ctx, directory):
9595

9696
return struct(commit = actual_commit, shallow_since = shallow_date)
9797

98+
def _report_progress(ctx, git_repo, *, shallow_failed = False):
99+
warning = ""
100+
if shallow_failed:
101+
warning = " (shallow fetch failed, fetching full history)"
102+
ctx.report_progress("Cloning %s of %s%s" % (git_repo.reset_ref, git_repo.remote, warning))
103+
98104
def _update(ctx, git_repo):
99105
ctx.delete(git_repo.directory)
100106

@@ -133,6 +139,7 @@ def fetch(ctx, git_repo):
133139
# "ignore what is specified and fetch all tags".
134140
# The arguments below work correctly for both before 1.9 and after 1.9,
135141
# as we directly specify the list of references to fetch.
142+
_report_progress(ctx, git_repo, shallow_failed = True)
136143
_git(
137144
ctx,
138145
git_repo,

0 commit comments

Comments
 (0)