Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tsrc sync -r support #362

Closed
wants to merge 14 commits into from
1 change: 1 addition & 0 deletions THANKS
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Jakob Heuser
Tronje Krabbe
Matthew Lovell
Atte Pellikka
Albert De La Fuente Vigliotti


If you make a contribution, feel free to make a pull request to add your name
Expand Down
6 changes: 3 additions & 3 deletions docs/guide/groups.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,16 @@ tsrc init [email protected]:acme/manifest --group g1

## Filtering repositories in groups with regular expressions

You can utilize inclusive regular expression with the `-r`-flag and
exclusive regular expression with the `-i`-flag. This allows you to filter
You can utilize inclusive regular expression with the `-i`-flag and
exclusive regular expression with the `-e`-flag. This allows you to filter
repositories within a group or a set of groups for the given action.


To include all repositories in the group g1 matching "config" and excluding "template",
you can do the following:

```
tsrc init [email protected]:acme/manifest --group g1 -r config -i template
tsrc init [email protected]:acme/manifest --group g1 -i config -e template
```


Expand Down
6 changes: 3 additions & 3 deletions docs/guide/remotes.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ In that case, you can use an alternative syntax:
repos:
# foo is open source and thus needs two remotes:
- dest: foo
- remotes:
remotes:
- name: origin
url: [email protected]/your-team/foo
- name: github
Expand Down Expand Up @@ -57,14 +57,14 @@ In that case, you can create a manifest looking like this:
```yaml
repos:
- dest: foo
- remotes:
remotes:
- name: origin
url: [email protected]/your-team/foo
- name: vpn
url: [email protected]/gitlab/your-team/foo

- dest: bar
- remotes:
remotes:
- name: origin
url: [email protected]/your-team/bar
- name: vpn
Expand Down
4 changes: 2 additions & 2 deletions docs/ref/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ tsrc init MANIFEST_URL [--group GROUP1, GROUP2] [--singular-remote SINGULAR_REMO
The `-g,--groups` option can be used to specify a list of groups
to use when cloning repositories.

The `-r` "inclusive regular expression" and `-i` "exclusive regular expression" options
can be combined with the group option to filter for repositories within a group. `-r` takes
The `-i` "inclusive regular expression" and `-e` "exclusive regular expression" options
can be combined with the group option to filter for repositories within a group. `-i` takes
precedence if both options are present.

The `-s,--shallow` option can be used to make shallow clone of all repositories.
Expand Down
2 changes: 1 addition & 1 deletion docs/ref/manifest-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ repos:
dest: foo
branch: next

- remotes:
remotes:
- name: origin
url: [email protected]:proj1/bar
- name: upstream
Expand Down
6 changes: 5 additions & 1 deletion docs/ref/workspace-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,8 @@ singular_remote:
* `shallow_clones`: whether to use only shallow clones when cloning missing repositories
* `repo_groups`: the list of groups to use - every mentioned group must be present in the `manifest.yml` file (see above)
* `clone_all_repos`: whether to ignore groups entirely and clone every repository from the manifest instead
* `singular_remote`: if set to `<remote-name>`, behaves as if `tsrc sync` and `tsrc init` were called with `--singular-remote <remote-name>` option. See the [Using remotes guide](../guide/remotes.md) for details.
* `singular_remote`: if set to `<remote-name>`, behaves as if `tsrc sync` and
`tsrc init` were called with `--singular-remote <remote-name>` option. See the
[Using remotes guide](../guide/remotes.md) for details. If `tsrc sync -r
<remote-name>` is used, it will take precedence over the file configuration
parameter.
17 changes: 15 additions & 2 deletions tsrc/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,12 @@ def add_repos_selection_args(parser: argparse.ArgumentParser) -> None:
help="run on all cloned repos",
)
parser.add_argument(
"-r",
"-i",
dest="regex",
help="Include only repositories matching the regex",
)
parser.add_argument(
"-i", dest="iregex", help="Exclude repositories matching the regex"
"-e", dest="iregex", help="Exclude repositories matching the regex"
)


Expand Down Expand Up @@ -116,6 +116,7 @@ def get_workspace_with_repos(namespace: argparse.Namespace) -> Workspace:
def resolve_repos(
workspace: Workspace,
*,
singular_remote: str = "",
groups: Optional[List[str]],
all_cloned: bool,
regex: str = "",
Expand All @@ -138,6 +139,18 @@ def resolve_repos(
else:
repos = repos_from_config(manifest, workspace.config)

if singular_remote:
filtered_repos = []
for repo in repos:
remotes = [
remote
for remote in repo.remotes
if re.search(singular_remote, remote.name)
dmerejkowsky marked this conversation as resolved.
Show resolved Hide resolved
]
if remotes:
filtered_repos.append(repo)
repos = filtered_repos

if regex:
repos = [repo for repo in repos if re.search(regex, repo.dest)]

Expand Down
15 changes: 13 additions & 2 deletions tsrc/cli/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ def configure_parser(subparser: argparse._SubParsersAction) -> None:
dest="update_manifest",
help="skip updating the manifest before syncing repositories",
)
parser.add_argument(
"-r",
"--singular-remote",
help="only use this remote when cloning repositories",
)
add_num_jobs_arg(parser)
parser.set_defaults(run=run)

Expand All @@ -37,6 +42,7 @@ def run(args: argparse.Namespace) -> None:
update_manifest = args.update_manifest
groups = args.groups
all_cloned = args.all_cloned
singular_remote = args.singular_remote
regex = args.regex
iregex = args.iregex
workspace = get_workspace(args)
Expand All @@ -49,11 +55,16 @@ def run(args: argparse.Namespace) -> None:
ui.info_2("Not updating manifest")

workspace.repos = resolve_repos(
workspace, groups=groups, all_cloned=all_cloned, regex=regex, iregex=iregex
workspace,
singular_remote=singular_remote,
groups=groups,
all_cloned=all_cloned,
regex=regex,
iregex=iregex,
)

workspace.clone_missing(num_jobs=num_jobs)
workspace.set_remotes(num_jobs=num_jobs)
workspace.sync(force=force, num_jobs=num_jobs)
workspace.sync(force=force, singular_remote=singular_remote, num_jobs=num_jobs)
workspace.perform_filesystem_operations()
ui.info_1("Workspace synchronized")
13 changes: 9 additions & 4 deletions tsrc/workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,15 @@ def perform_filesystem_operations(
collection.print_errors()
raise FileSystemOperatorError

def sync(self, *, force: bool = False, num_jobs: int = 1) -> None:
syncer = Syncer(
self.root_path, force=force, remote_name=self.config.singular_remote
)
def sync(
self, *, singular_remote: str = "", force: bool = False, num_jobs: int = 1
) -> None:
remote_name = ""
if singular_remote:
remote_name = singular_remote
elif self.config.singular_remote:
remote_name = self.config.singular_remote
syncer = Syncer(self.root_path, force=force, remote_name=remote_name)
repos = self.repos
ui.info_2("Synchronizing repos")
collection = process_items(repos, syncer, num_jobs=num_jobs)
Expand Down