diff options
| author | Daniel Andersson <daniel.r.andersson@volvocars.com> | 2022-04-01 12:55:38 +0200 |
|---|---|---|
| committer | Mike Frysinger <vapier@google.com> | 2022-04-08 21:06:37 +0000 |
| commit | d52ca421d52c75837d1614ec54549569f354b7ec (patch) | |
| tree | c8f18a193433680c03572d55b46d9c322c70427b | |
| parent | a2ff20dd209fe3eb091cdf1bddd4672f86b76bd8 (diff) | |
| download | git-repo-d52ca421d52c75837d1614ec54549569f354b7ec.tar.gz | |
sync: respect `sync-c` manifest option
The documentation states that a `sync-c` attribute in the manifest file
can set a default for whether only the current branch should be fetched
or all branches. This seems to have been broken for some time.
Commit 7356114 introduced the `--no-current-branch` CLI option and
relied on getting `None` via `optparse` if neither `--current-branch`
nor `--no-current-branch` was set to distinguish it from a boolean
value. If `None` was received, it would read the value from the manifest
option `sync-c`. The parsing went through the utility function
`_GetCurrentBranchOnly` which returned `True` if `--current-branch` had
been given on the command-line, or fell back on the "superproject"
setting, which would either return `True` or `None`. This would
incorrectly make `repo` fall back to the manifest setting even if the
user had given `--no-current-branch` if no superproject was requested --
the manifest became "too powerful":
Command-line Using superproject → `current_branch_only`
------------ ------------------ -----------------------
No From manifest
Yes True
--current-branch No True
--current-branch Yes True
--no-current-branch No From manifest ← wrong
--no-current-branch Yes True
In commit 0cb6e92 the superproject configuration value reading changed
from something that could return `None` to something that always
returned a boolean. If it returned `False`, this would then incorrectly
make `repo` ignore the manifest option even if neither
`--current-branch` nor `--no-current-branch` had been given. The
manifest default became useless:
Command-line Using superproject → `current_branch_only`
------------ ------------------ -----------------------
No False ← wrong
Yes True
--current-branch No True
--current-branch Yes True
--no-current-branch No False
--no-current-branch Yes True
By swapping the order in which the command-line option target and the
superproject setting is evaluated, things should work as documented:
Command-line Using superproject → `current_branch_only`
------------ ------------------ -----------------------
No From manifest
Yes True
--current-branch No True
--current-branch Yes True
--no-current-branch No False
--no-current-branch Yes True
Change-Id: I933c232d2fbecc6b9bdc364ebac181798bce9175
Tested-by: Daniel Andersson <daniel.r.andersson@volvocars.com>
Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/334270
Reviewed-by: Mike Frysinger <vapier@google.com>
| -rw-r--r-- | subcmds/sync.py | 9 | ||||
| -rw-r--r-- | tests/test_subcmds_sync.py | 45 |
2 files changed, 52 insertions, 2 deletions
diff --git a/subcmds/sync.py b/subcmds/sync.py index baee6b23..9e783205 100644 --- a/subcmds/sync.py +++ b/subcmds/sync.py | |||
| @@ -280,8 +280,13 @@ later is required to fix a server side protocol bug. | |||
| 280 | return branch | 280 | return branch |
| 281 | 281 | ||
| 282 | def _GetCurrentBranchOnly(self, opt): | 282 | def _GetCurrentBranchOnly(self, opt): |
| 283 | """Returns True if current-branch or use-superproject options are enabled.""" | 283 | """Returns whether current-branch or use-superproject options are enabled. |
| 284 | return opt.current_branch_only or git_superproject.UseSuperproject(opt, self.manifest) | 284 | |
| 285 | Returns: | ||
| 286 | True if a superproject is requested, otherwise the value of the | ||
| 287 | current_branch option (True, False or None). | ||
| 288 | """ | ||
| 289 | return git_superproject.UseSuperproject(opt, self.manifest) or opt.current_branch_only | ||
| 285 | 290 | ||
| 286 | def _UpdateProjectsRevisionId(self, opt, args, load_local_manifests, superproject_logging_data): | 291 | def _UpdateProjectsRevisionId(self, opt, args, load_local_manifests, superproject_logging_data): |
| 287 | """Update revisionId of every project with the SHA from superproject. | 292 | """Update revisionId of every project with the SHA from superproject. |
diff --git a/tests/test_subcmds_sync.py b/tests/test_subcmds_sync.py new file mode 100644 index 00000000..c1d1758e --- /dev/null +++ b/tests/test_subcmds_sync.py | |||
| @@ -0,0 +1,45 @@ | |||
| 1 | # Copyright (C) 2022 The Android Open Source Project | ||
| 2 | # | ||
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 4 | # you may not use this file except in compliance with the License. | ||
| 5 | # You may obtain a copy of the License at | ||
| 6 | # | ||
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 | ||
| 8 | # | ||
| 9 | # Unless required by applicable law or agreed to in writing, software | ||
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, | ||
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 12 | # See the License for the specific language governing permissions and | ||
| 13 | # limitations under the License. | ||
| 14 | |||
| 15 | """Unittests for the subcmds/sync.py module.""" | ||
| 16 | |||
| 17 | from unittest import mock | ||
| 18 | |||
| 19 | import pytest | ||
| 20 | |||
| 21 | from subcmds import sync | ||
| 22 | |||
| 23 | |||
| 24 | @pytest.mark.parametrize( | ||
| 25 | 'use_superproject, cli_args, result', | ||
| 26 | [ | ||
| 27 | (True, ['--current-branch'], True), | ||
| 28 | (True, ['--no-current-branch'], True), | ||
| 29 | (True, [], True), | ||
| 30 | (False, ['--current-branch'], True), | ||
| 31 | (False, ['--no-current-branch'], False), | ||
| 32 | (False, [], None), | ||
| 33 | ] | ||
| 34 | ) | ||
| 35 | def test_get_current_branch_only(use_superproject, cli_args, result): | ||
| 36 | """Test Sync._GetCurrentBranchOnly logic. | ||
| 37 | |||
| 38 | Sync._GetCurrentBranchOnly should return True if a superproject is requested, | ||
| 39 | and otherwise the value of the current_branch_only option. | ||
| 40 | """ | ||
| 41 | cmd = sync.Sync() | ||
| 42 | opts, _ = cmd.OptionParser.parse_args(cli_args) | ||
| 43 | |||
| 44 | with mock.patch('git_superproject.UseSuperproject', return_value=use_superproject): | ||
| 45 | assert cmd._GetCurrentBranchOnly(opts) == result | ||
