Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_ungroup(mock_write, _, __, input, expected):
utils.get_groups.cache_clear()
args = ['ungroup'] + shlex.split(input)
__main__.main(args)
mock_write.assert_called_once_with(expected, 'w')
help="execute arbitrary git command/alias for specified or all repos "
"Example: gita super myrepo1 diff --name-only --staged "
"Another: gita super checkout master ")
p_super.set_defaults(func=f_super)
# sub-commands that fit boilerplate
cmds = utils.get_cmds_from_files()
for name, data in cmds.items():
help = data.get('help')
cmd = data.get('cmd') or name
if data.get('allow_all'):
choices = utils.get_choices()
nargs = '*'
help += ' for all repos or'
else:
choices = utils.get_repos().keys() | utils.get_groups().keys()
nargs = '+'
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())
args = p.parse_args(argv)
args.async_blacklist = {
name
for name, data in cmds.items() if data.get('disable_async')
}
if 'func' in args:
args.func(args)
else:
def f_ungroup(args: argparse.Namespace):
groups = utils.get_groups()
to_ungroup = set(args.to_ungroup)
to_del = []
for name, repos in groups.items():
remaining = set(repos) - to_ungroup
if remaining:
groups[name] = list(sorted(remaining))
else:
to_del.append(name)
for name in to_del:
del groups[name]
utils.write_to_groups_file(groups, 'w')
*: unstaged changes
_: untracked files/folders
branch colors:
{info.Color.white}white{info.Color.end}: local has no remote
{info.Color.green}green{info.Color.end}: local is the same as remote
{info.Color.red}red{info.Color.end}: local has diverged from remote
{info.Color.purple}purple{info.Color.end}: local is ahead of remote (good for push)
{info.Color.yellow}yellow{info.Color.end}: local is behind remote (good for merge)'''
p_ll = subparsers.add_parser('ll',
help='display summary of all repos',
formatter_class=argparse.RawTextHelpFormatter,
description=ll_doc)
p_ll.add_argument('group',
nargs='?',
choices=utils.get_groups(),
help="show repos in the chosen group")
p_ll.set_defaults(func=f_ll)
p_ls = subparsers.add_parser(
'ls', help='display names of all repos, or path of a chosen repo')
p_ls.add_argument('repo',
nargs='?',
choices=utils.get_repos(),
help="show path of the chosen repo")
p_ls.set_defaults(func=f_ls)
p_group = subparsers.add_parser(
'group', help='group repos or display names of all groups if no repo is provided')
p_group.add_argument('to_group',
nargs='*',
choices=utils.get_choices(),
def f_git_cmd(args: argparse.Namespace):
"""
Delegate git command/alias defined in `args.cmd`. Asynchronous execution is
disabled for commands in the `args.async_blacklist`.
"""
repos = utils.get_repos()
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]:
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():
print(path)
subprocess.run(cmds, cwd=path)
else: # run concurrent subprocesses
# Async execution cannot deal with multiple repos' user name/password.
def f_ll(args: argparse.Namespace):
"""
Display details of all repos
"""
repos = utils.get_repos()
if args.group: # only display repos in this group
group_repos = utils.get_groups()[args.group]
repos = {k: repos[k] for k in group_repos if k in repos}
for line in utils.describe(repos):
print(line)
def f_group(args: argparse.Namespace):
groups = utils.get_groups()
if args.to_group:
gname = input('group name? ')
if gname in groups:
gname_repos = set(groups[gname])
gname_repos.update(args.to_group)
groups[gname] = sorted(gname_repos)
utils.write_to_groups_file(groups, 'w')
else:
utils.write_to_groups_file({gname: sorted(args.to_group)}, 'a+')
else:
for group, repos in groups.items():
print(f"{group}: {', '.join(repos)}")