Skip to content

Commit

Permalink
implement shell subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
dzhou-schrodinger committed Jan 13, 2021
1 parent b88fe6e commit a96d4a7
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 3 deletions.
51 changes: 51 additions & 0 deletions gita/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,42 @@ def f_git_cmd(args: argparse.Namespace):
subprocess.run(cmds, cwd=path)


def f_shell(args):
"""
Delegate shell command defined in `args.man`, which may or may not
contain repo names.
"""
names = []
repos = utils.get_repos()
groups = utils.get_groups()
ctx = utils.get_context()
for i, word in enumerate(args.man):
if word in repos or word in groups:
names.append(word)
else:
break
args.repo = names
# TODO: redundant with f_git_cmd
if not args.repo and ctx:
args.repo = [ctx.stem]
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]:
chosen[r] = repos[r]
repos = chosen
cmds = args.man[i:]
for name, path in repos.items():
# TODO: pull this out as a function
got = subprocess.run(cmds, cwd=path, check=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
print(utils.format_output(got.stdout.decode(), name))


def f_super(args):
"""
Delegate git command/alias defined in `args.man`, which may or may not
Expand Down Expand Up @@ -415,6 +451,21 @@ def main(argv=None):
"Another: gita super checkout master ")
p_super.set_defaults(func=f_super)

# shell mode
p_shell = subparsers.add_parser(
'shell',
description='shell mode: delegate any shell command in specified or '
'all repo(s).\n'
'Examples:\n \t gita shell pwd\n'
'\t gita shell repo1 repo2 repo3 touch xx')
p_shell.add_argument(
'man',
nargs=argparse.REMAINDER,
help="execute arbitrary shell command for specified or all repos "
"Example: gita shell myrepo1 ls"
"Another: gita shell git checkout master ")
p_shell.set_defaults(func=f_shell)

# sub-commands that fit boilerplate
cmds = utils.get_cmds_from_files()
for name, data in cmds.items():
Expand Down
4 changes: 2 additions & 2 deletions gita/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ async def run_async(repo_name: str, path: str, cmds: List[str]) -> Union[None, s
stdout, stderr = await process.communicate()
for pipe in (stdout, stderr):
if pipe:
print(format_output(pipe.decode(), f'{repo_name}: '))
print(format_output(pipe.decode(), repo_name))
# The existence of stderr is not good indicator since git sometimes write
# to stderr even if the execution is successful, e.g. git fetch
if process.returncode != 0:
Expand All @@ -178,7 +178,7 @@ def format_output(s: str, prefix: str):
"""
Prepends every line in given string with the given prefix.
"""
return ''.join([f'{prefix}{line}' for line in s.splitlines(keepends=True)])
return ''.join([f'{prefix}: {line}' for line in s.splitlines(keepends=True)])


def exec_async_tasks(tasks: List[Coroutine]) -> List[Union[None, str]]:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
setup(
name='gita',
packages=['gita'],
version='0.12.6',
version='0.12.7',
license='MIT',
description='Manage multiple git repos with sanity',
long_description=long_description,
Expand Down

0 comments on commit a96d4a7

Please sign in to comment.