1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
|
# Copyright (C) 2026 The Android Open Source Project
#
# 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.
"""E2E tests for `repo sync` modes (`-n`, `-l`) & sync flags."""
from typing import List
from harness import GIT_DEFAULTS_PARAMS
from harness import GitHost
from harness import Gitlink
from harness import manifest_xml
from harness import RepoClient
import pytest
# Interleaved (the default) & phased syncs implement `-n` & `-l` separately.
SYNC_MODES = [[], ["--no-interleaved"]]
@pytest.mark.parametrize("mode_args", SYNC_MODES, ids=["interleaved", "phased"])
def test_network_only_then_local_only(
host: GitHost, client: RepoClient, mode_args: List[str]
) -> None:
"""`sync -n` on an existing checkout updates objects & remote refs only.
The worktree, HEAD & local branches don't change until `sync -l`, which
checks out what `-n` fetched without touching the network.
"""
foo = host.create_repo("platform/foo", {"f": "1"})
bar = host.create_repo("platform/bar", {"f": "1"})
host.create_manifest(
manifest_xml(
'<project name="platform/foo" path="foo" />',
'<project name="platform/bar" path="bar" />',
)
)
client.init(host.manifest_url)
client.sync(*mode_args)
old_foo = foo.rev_parse("main")
# Local work on a topic branch in bar.
client.run("start", "topic", "bar")
(client.top / "bar/local").write_text("local")
client.git("bar", "add", "local")
client.git("bar", "commit", "-q", "-m", "local change")
local_commit = client.head("bar")
fetched = {"foo": foo.commit({"f": "2"}), "bar": bar.commit({"f": "2"})}
assert client.sync("-n", *mode_args).stderr == ""
assert client.head("foo") == old_foo
assert (client.top / "foo/f").read_text() == "1"
assert client.head("bar") == local_commit
assert client.git("bar", "symbolic-ref", "--short", "HEAD") == "topic"
for path, commit in fetched.items():
assert client.has_object(path, commit)
assert client.git(path, "rev-parse", "refs/remotes/origin/main") == (
commit
)
assert client.git(path, "status", "--porcelain") == ""
# The server moves on, but `-l` must only use what `-n` fetched.
foo.commit({"f": "3"})
with host.watch_requests() as requests, host.offline():
client.sync("-l", *mode_args)
assert requests == []
assert client.head("foo") == fetched["foo"]
assert (client.top / "foo/f").read_text() == "2"
# The local commit on `bar` was rebased onto what `-n` fetched.
assert client.git("bar", "symbolic-ref", "--short", "HEAD") == "topic"
assert client.git("bar", "rev-parse", "HEAD~1") == fetched["bar"]
assert (client.top / "bar/local").read_text() == "local"
def test_local_only_unfetched_project(
host: GitHost, client: RepoClient
) -> None:
"""`sync -l` fails clearly for a project that was never fetched.
The other projects are still checked out, no network requests are made,
and the error says how to fix it.
"""
foo = host.create_repo("platform/foo", {"f": "1"})
host.create_repo("platform/bar", {"f": "bar"})
host.create_manifest(
manifest_xml('<project name="platform/foo" path="foo" />')
)
client.init(host.manifest_url)
client.sync()
v2 = foo.commit({"f": "2"})
host.update_manifest(
manifest_xml(
'<project name="platform/foo" path="foo" />',
'<project name="platform/bar" path="bar" />',
)
)
# Update the manifest & fetch foo, but not the new project.
client.sync("-n", "platform/foo")
assert not (client.repodir / "projects/bar.git").exists()
with host.watch_requests() as requests, host.offline():
result = client.sync("-l", check=False)
assert requests == []
assert result.returncode != 0
assert (
"Cannot checkout platform/bar due to missing network sync; Run "
"`repo sync -n platform/bar` first." in result.stderr
)
assert not (client.top / "bar").exists()
# The already-fetched project still got checked out.
assert client.head("foo") == v2
assert (client.top / "foo/f").read_text() == "2"
# Fetching the missing project fixes it.
client.sync("-n", "platform/bar")
with host.watch_requests() as requests, host.offline():
client.sync("-l")
assert requests == []
assert (client.top / "bar/f").read_text() == "bar"
assert client.list_projects() == {
"bar": "platform/bar",
"foo": "platform/foo",
}
@pytest.mark.parametrize("git_defaults", GIT_DEFAULTS_PARAMS)
def test_two_phase_sync_superproject(host: GitHost, client: RepoClient) -> None:
"""`sync -n` pins projects via the superproject; `sync -l` reuses the pins.
The local phase must not hit the network, even to re-read the
superproject after the server's superproject moved on.
"""
foo = host.create_repo("platform/foo", {"f": "v1"})
pinned = foo.rev_parse("main")
superproject = host.create_repo("superproject", {"foo": Gitlink(pinned)})
foo.commit({"f": "v2"})
host.create_manifest(
manifest_xml(
'<superproject name="superproject" />',
'<project name="platform/foo" path="foo" />',
)
)
client.init(host.manifest_url, "--use-superproject")
assert client.sync("-n").stderr == ""
assert not (client.top / "foo").exists()
assert (
client.repodir / "exp-superproject/superproject_override.xml"
).exists()
# Advance the superproject on the server before running `sync -l`.
new_pin = foo.commit({"f": "v3"})
superproject.commit({"foo": Gitlink(new_pin)})
with host.watch_requests() as requests, host.offline():
client.sync("-l")
assert requests == []
assert client.head("foo") == pinned
assert (client.top / "foo/f").read_text() == "v1"
# The next two-phase sync picks up the new superproject pin.
client.sync("-n")
assert client.head("foo") == pinned
with host.watch_requests() as requests, host.offline():
client.sync("-l")
assert requests == []
assert client.head("foo") == new_pin
assert (client.top / "foo/f").read_text() == "v3"
@pytest.mark.parametrize(
"sync_args,project_attrs,expect_tags",
[
# Despite `repo sync --help` calling `--no-tags` the default.
([], "", True),
# `-c` only limits branches: tags on other branches are still fetched
# unless `--no-tags` is passed.
(["-c"], "", True),
(["--tags"], "", True),
(["-c", "--no-tags"], "", False),
(["-c"], 'sync-tags="false"', False),
(["-c", "--tags"], 'sync-tags="false"', True),
],
)
def test_tags(
host: GitHost,
client: RepoClient,
sync_args: List[str],
project_attrs: str,
expect_tags: bool,
) -> None:
"""Tags are fetched unless `--no-tags` or the manifest says `sync-tags`."""
foo = host.create_repo("platform/foo", {"f": "1"})
foo.tag("v1", "main")
# A tag that isn't on the synced branch, like release tags usually are.
foo.commit({"f": "release"}, branch="release")
foo.tag("v2", "release")
host.create_manifest(
manifest_xml(f'<project name="platform/foo" {project_attrs} />')
)
client.init(host.manifest_url)
client.sync(*sync_args)
assert client.tags("platform/foo") == (
{"v1", "v2"} if expect_tags else set()
)
expected_branches = {"main"} if "-c" in sync_args else {"main", "release"}
assert client.remote_branches("platform/foo") == expected_branches
def test_prune(host: GitHost, client: RepoClient) -> None:
"""Remote-tracking branches deleted on the server are pruned by default.
`--no-prune` keeps them.
"""
foo = host.create_repo("platform/foo", {"f": "1"})
foo.commit({"f": "stale"}, branch="stale")
host.create_manifest(manifest_xml('<project name="platform/foo" />'))
client.init(host.manifest_url)
client.sync()
path = "platform/foo"
assert client.remote_branches(path) == {"main", "stale"}
foo.git("update-ref", "-d", "refs/heads/stale")
client.sync("--no-prune")
assert client.remote_branches(path) == {"main", "stale"}
client.sync()
assert client.remote_branches(path) == {"main"}
def test_force_sync(host: GitHost, client: RepoClient) -> None:
"""`--force-sync` re-creates a path whose project changed in the manifest.
The old gitdir points at another project's object dir, which sync refuses
to overwrite unless forced.
"""
host.create_repo("platform/foo", {"f": "foo"})
foo_v2 = host.create_repo("platform/foo-v2", {"f": "foo-v2"})
host.create_manifest(
manifest_xml('<project name="platform/foo" path="foo" />')
)
client.init(host.manifest_url)
client.sync()
host.update_manifest(
manifest_xml('<project name="platform/foo-v2" path="foo" />')
)
result = client.sync(check=False)
assert result.returncode != 0
assert "--force-sync not enabled" in result.stderr
assert "`repo sync --force-sync foo`" in result.stderr
assert (client.top / "foo/f").read_text() == "foo"
client.sync("--force-sync")
assert client.head("foo") == foo_v2.rev_parse("main")
assert (client.top / "foo/f").read_text() == "foo-v2"
assert client.list_projects() == {"foo": "platform/foo-v2"}
assert client.git("foo", "status", "--porcelain") == ""
@pytest.mark.parametrize("mode_args", SYNC_MODES, ids=["interleaved", "phased"])
def test_network_only_keeps_removed_project(
host: GitHost, client: RepoClient, mode_args: List[str]
) -> None:
"""`sync -n` leaves projects dropped from the manifest to `sync -l`.
Removing their checkouts and copyfiles changes the worktree, which `-n`
must not do.
"""
host.create_repo("platform/foo", {"f": "foo"})
host.create_repo("platform/bar", {"f": "bar"})
host.create_manifest(
manifest_xml(
'<project name="platform/foo" path="foo" />',
'<project name="platform/bar" path="bar">'
'<copyfile src="f" dest="bar-copy" />'
"</project>",
)
)
client.init(host.manifest_url)
client.sync(*mode_args)
host.update_manifest(
manifest_xml('<project name="platform/foo" path="foo" />')
)
assert client.sync("-n", *mode_args).stderr == ""
assert (client.top / "bar/f").read_text() == "bar"
assert (client.top / "bar-copy").read_text() == "bar"
with host.watch_requests() as requests, host.offline():
client.sync("-l", *mode_args)
assert requests == []
assert not (client.top / "bar").exists()
assert not (client.top / "bar-copy").exists()
assert client.list_projects() == {"foo": "platform/foo"}
@pytest.mark.parametrize("mode_args", SYNC_MODES, ids=["interleaved", "phased"])
def test_force_remove_dirty(
host: GitHost, client: RepoClient, mode_args: List[str]
) -> None:
"""Removed projects with uncommitted changes need `--force-remove-dirty`."""
host.create_repo("platform/foo", {"f": "foo"})
host.create_repo("platform/bar", {"f": "bar"})
host.create_manifest(
manifest_xml(
'<project name="platform/foo" path="foo" />',
'<project name="platform/bar" path="bar" />',
)
)
client.init(host.manifest_url)
client.sync(*mode_args)
(client.top / "bar/f").write_text("dirty")
host.update_manifest(
manifest_xml('<project name="platform/foo" path="foo" />')
)
# Repeat to check that the project stays tracked in project.list (rather
# than forgotten & left behind) after the first failure.
for _ in range(2):
result = client.sync(*mode_args, check=False)
assert result.returncode != 0
# Logged once when removal fails and once in the final error summary:
# removal is not attempted (and reported) a second time in the same
# sync.
assert (
result.stderr.count(
"bar: Cannot remove project: uncommitted changes are present"
)
== 2
)
assert result.stderr.count("Updating local project lists failed") == 1
assert (client.top / "bar/f").read_text() == "dirty"
result = client.sync("--force-remove-dirty", *mode_args)
assert "bar: Removing dirty project" in result.stderr
assert not (client.top / "bar").exists()
assert client.list_projects() == {"foo": "platform/foo"}
def test_detach(host: GitHost, client: RepoClient) -> None:
"""`sync -d` detaches projects on a topic branch at the manifest revision.
The topic branch & its local commit are left alone.
"""
foo = host.create_repo("platform/foo", {"f": "1"})
host.create_manifest(
manifest_xml('<project name="platform/foo" path="foo" />')
)
client.init(host.manifest_url)
client.sync()
client.run("start", "topic", "--all")
(client.top / "foo/local").write_text("local")
client.git("foo", "add", "local")
client.git("foo", "commit", "-q", "-m", "local change")
local = client.head("foo")
latest = foo.commit({"f": "2"})
client.sync("-d")
assert client.is_detached("foo")
assert client.head("foo") == latest
assert (client.top / "foo/f").read_text() == "2"
assert not (client.top / "foo/local").exists()
assert client.git("foo", "rev-parse", "topic") == local
# A regular sync afterwards stays detached.
client.sync()
assert client.is_detached("foo")
@pytest.mark.parametrize("mode", ["detached", "commit", "tag"])
def test_smart_sync_without_tracked_branch(
host: GitHost, client: RepoClient, mode: str
) -> None:
"""Smart sync requires `.repo/manifests` to track a remote branch.
When `sync-smartsync="true"` is set in the manifest, plain `repo sync`
falls back to a regular sync without contacting the manifest server, while
an explicit `repo sync -s` fails early with an actionable error
(b/580004259).
"""
foo = host.create_repo("platform/foo", {"f": "foo"})
manifest = host.create_manifest(
manifest_xml(
f'<manifest-server url="{host.url}/manifestserver" />',
'<project name="platform/foo" path="foo" />',
default='remote="origin" revision="main" sync-smartsync="true"',
)
)
pinned = manifest.rev_parse("main")
manifest.tag("v1.0", pinned)
# Detached `.repo/manifests` falls back to `init.defaultBranch` (`master`
# under Git 2 defaults) when updating the manifest repo during `repo sync`.
manifest.set_ref("refs/heads/master", pinned)
if mode == "detached":
client.init(host.manifest_url)
client.git(".repo/manifests", "checkout", "-q", "--detach")
assert client.is_detached(".repo/manifests")
elif mode == "commit":
client.init(host.manifest_url, "-b", pinned)
assert (
client.config(".repo/manifests", "branch.default.merge") == pinned
)
else:
client.init(host.manifest_url, "-b", "refs/tags/v1.0")
assert (
client.config(".repo/manifests", "branch.default.merge")
== "refs/tags/v1.0"
)
with host.watch_requests() as requests:
explicit = client.sync("-s", check=False)
assert explicit.returncode != 0
assert "not tracking a remote branch" in explicit.stderr
assert "Using manifest server" not in explicit.output
assert requests == []
with host.watch_requests() as requests:
implicit = client.sync()
assert implicit.stderr == ""
assert "Using manifest server" not in implicit.output
assert not [r for r in requests if "manifestserver" in r.path]
assert client.head("foo") == foo.rev_parse("main")
assert (client.top / "foo/f").read_text() == "foo"
|