From bfa22cdaae3d7f4751fa468836d8967623cef9e8 Mon Sep 17 00:00:00 2001 From: Dong Zhou Date: Sat, 23 May 2020 01:12:07 -0400 Subject: [PATCH] support group(s) for git commands --- README.md | 11 +++++++---- gita/__main__.py | 19 ++++++++++++++----- gita/utils.py | 10 ++++++---- setup.py | 2 +- 4 files changed, 28 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 5c7361e..55c5069 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,7 @@ The bookkeeping sub-commands are - `gita rm `: remove repo(s) from `gita` (won't remove files from disk) - `gita group`: show grouping of the repos - `gita group `: group repos +- `gita ungroup `: remove grouping for repos - `gita ll`: display the status of all repos - `gita ll `: display the status of repos in a group - `gita ls`: display the names of all repos @@ -64,8 +65,10 @@ Repo paths are saved in `$XDG_CONFIG_HOME/gita/repo_path` (most likely `~/.confi The delegating sub-commands are of two formats -- `gita [repo-name(s)]`: optional repo input, and no input means all repos. -- `gita `: required repo name(s) input +- `gita [repo-name(s) or group-name(s)]`: + optional repo or group input, and no input means all repos. +- `gita `: + required repo name(s) or group name(s) input By default, only `fetch` and `pull` take optional input. @@ -139,10 +142,10 @@ The superman mode delegates any git command/alias. Usage: ``` -gita super [repo-name(s)] +gita super [repo-name(s) or group-name(s)] ``` -Here `repo-name(s)` is optional, and absence means all repos. +Here `repo-name(s)` or `group-name(s)` are optional, and their absence means all repos. For example, - `gita super checkout master` puts all repos on the master branch diff --git a/gita/__main__.py b/gita/__main__.py index 528f503..de818a1 100644 --- a/gita/__main__.py +++ b/gita/__main__.py @@ -110,8 +110,16 @@ def f_git_cmd(args: argparse.Namespace): disabled for commands in the `args.async_blacklist`. """ repos = utils.get_repos() - if args.repo: # with user specified repo(s) - repos = {k: repos[k] for k in args.repo} + groups = utils.get_groups() + if args.repo: # with user specified repo(s) or group(s) + chosen = {} + for k in args.repo: + if k in repos: + chosen[k] = repos[k] + if k in groups: + for r in groups[k].split(): + chosen[r] = repos[r] + repos = chosen cmds = ['git'] + args.cmd if len(repos) == 1 or cmds[1] in args.async_blacklist: for path in repos.values(): @@ -136,8 +144,9 @@ def f_super(args): """ names = [] repos = utils.get_repos() + groups = utils.get_groups() for i, word in enumerate(args.man): - if word in repos: + if word in repos or word in groups: names.append(word) else: break @@ -255,9 +264,9 @@ def main(argv=None): nargs = '*' help += ' for all repos or' else: - choices = utils.get_repos() + choices = utils.get_repos().keys() | utils.get_groups().keys() nargs = '+' - help += ' for the chosen repo(s)' + help += ' for the chosen repo(s) or group(s)' sp = subparsers.add_parser(name, help=help) sp.add_argument('repo', nargs=nargs, choices=choices, help=help) sp.set_defaults(func=f_git_cmd, cmd=cmd.split()) diff --git a/gita/utils.py b/gita/utils.py index ab9a39e..4fdc267 100644 --- a/gita/utils.py +++ b/gita/utils.py @@ -59,15 +59,17 @@ def get_groups() -> Dict[str, str]: def get_choices() -> List[Union[str, None]]: """ - Return all repo names and an additional empty list. This is a workaround of + Return all repo names, group names, and an additional empty list. The empty + list is added as a workaround of argparse's problem with coexisting nargs='*' and choices. See https://utcc.utoronto.ca/~cks/space/blog/python/ArgparseNargsChoicesLimitation and https://bugs.python.org/issue27227 """ - repos = list(get_repos()) - repos.append([]) - return repos + choices = list(get_repos()) + choices.extend(get_groups()) + choices.append([]) + return choices def is_git(path: str) -> bool: diff --git a/setup.py b/setup.py index 1953ac6..257b038 100644 --- a/setup.py +++ b/setup.py @@ -7,7 +7,7 @@ setup( name='gita', packages=['gita'], - version='0.10.7', + version='0.10.8', license='MIT', description='Manage multiple git repos', long_description=long_description,