summaryrefslogtreecommitdiffstats
path: root/gita/__main__.py
diff options
context:
space:
mode:
Diffstat (limited to 'gita/__main__.py')
-rw-r--r--gita/__main__.py193
1 files changed, 151 insertions, 42 deletions
diff --git a/gita/__main__.py b/gita/__main__.py
index 6e47f8e..9a24bb9 100644
--- a/gita/__main__.py
+++ b/gita/__main__.py
@@ -15,16 +15,23 @@ https://github.com/nosarthur/gita/blob/master/.gita-completion.bash
'''
import os
+import sys
+import yaml
import argparse
import subprocess
import pkg_resources
+from itertools import chain
+from pathlib import Path
-from . import utils, info
+from . import utils, info, common
def f_add(args: argparse.Namespace):
repos = utils.get_repos()
- utils.add_repos(repos, args.paths)
+ paths = args.paths
+ if args.recursive:
+ paths = chain.from_iterable(Path(p).glob('**') for p in args.paths)
+ utils.add_repos(repos, paths)
def f_rename(args: argparse.Namespace):
@@ -32,12 +39,33 @@ def f_rename(args: argparse.Namespace):
utils.rename_repo(repos, args.repo[0], args.new_name)
-def f_info(_):
- all_items, to_display = info.get_info_items()
- print('In use:', ','.join(to_display))
- unused = set(all_items) - set(to_display)
- if unused:
- print('Unused:', ' '.join(unused))
+def f_color(args: argparse.Namespace):
+ cmd = args.color_cmd or 'll'
+ if cmd == 'll': # pragma: no cover
+ info.show_colors()
+ elif cmd == 'set':
+ print('not implemented')
+
+
+def f_info(args: argparse.Namespace):
+ to_display = info.get_info_items()
+ cmd = args.info_cmd or 'll'
+ if cmd == 'll':
+ print('In use:', ','.join(to_display))
+ unused = set(info.ALL_INFO_ITEMS) - set(to_display)
+ if unused:
+ print('Unused:', ' '.join(unused))
+ return
+ if cmd == 'add' and args.info_item not in to_display:
+ to_display.append(args.info_item)
+ yml_config = common.get_config_fname('info.yml')
+ with open(yml_config, 'w') as f:
+ yaml.dump(to_display, f, default_flow_style=None)
+ elif cmd == 'rm' and args.info_item in to_display:
+ to_display.remove(args.info_item)
+ yml_config = common.get_config_fname('info.yml')
+ with open(yml_config, 'w') as f:
+ yaml.dump(to_display, f, default_flow_style=None)
def f_ll(args: argparse.Namespace):
@@ -45,10 +73,13 @@ def f_ll(args: argparse.Namespace):
Display details of all repos
"""
repos = utils.get_repos()
+ ctx = utils.get_context()
+ if args.group is None and ctx:
+ args.group = ctx.stem
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):
+ for line in utils.describe(repos, no_colors=args.no_colors):
print(line)
@@ -62,8 +93,26 @@ def f_ls(args: argparse.Namespace):
def f_group(args: argparse.Namespace):
groups = utils.get_groups()
- if args.to_group:
- gname = input('group name? ')
+ cmd = args.group_cmd or 'll'
+ if cmd == 'll':
+ for group, repos in groups.items():
+ print(f"{group}: {' '.join(repos)}")
+ elif cmd == 'ls':
+ print(' '.join(groups))
+ elif cmd == 'rename':
+ new_name = args.new_name
+ if new_name in groups:
+ sys.exit(f'{new_name} already exists.')
+ gname = args.gname
+ groups[new_name] = groups[gname]
+ del groups[gname]
+ utils.write_to_groups_file(groups, 'w')
+ elif cmd == 'rm':
+ for name in args.to_ungroup:
+ del groups[name]
+ utils.write_to_groups_file(groups, 'w')
+ elif cmd == 'add':
+ gname = args.gname
if gname in groups:
gname_repos = set(groups[gname])
gname_repos.update(args.to_group)
@@ -71,31 +120,32 @@ def f_group(args: argparse.Namespace):
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)}")
-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))
+def f_context(args: argparse.Namespace):
+ choice = args.choice
+ ctx = utils.get_context()
+ if choice is None:
+ if ctx:
+ group = ctx.stem
+ print(f"{group}: {' '.join(utils.get_groups()[group])}")
+ else:
+ print('Context is not set')
+ elif choice == 'none': # remove context
+ ctx and ctx.unlink()
+ else: # set context
+ fname = Path(common.get_config_dir()) / (choice + '.context')
+ if ctx:
+ ctx.rename(fname)
else:
- to_del.append(name)
- for name in to_del:
- del groups[name]
- utils.write_to_groups_file(groups, 'w')
+ open(fname, 'w').close()
def f_rm(args: argparse.Namespace):
"""
Unregister repo(s) from gita
"""
- path_file = utils.get_config_fname('repo_path')
+ path_file = common.get_config_fname('repo_path')
if os.path.isfile(path_file):
repos = utils.get_repos()
for repo in args.repo:
@@ -110,6 +160,9 @@ def f_git_cmd(args: argparse.Namespace):
"""
repos = utils.get_repos()
groups = utils.get_groups()
+ ctx = utils.get_context()
+ 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:
@@ -170,6 +223,8 @@ def main(argv=None):
# bookkeeping sub-commands
p_add = subparsers.add_parser('add', help='add repo(s)')
p_add.add_argument('paths', nargs='+', help="add repo(s)")
+ p_add.add_argument('-r', dest='recursive', action='store_true',
+ help="recursively add repo(s) in the given path.")
p_add.set_defaults(func=f_add)
p_rm = subparsers.add_parser('rm', help='remove repo(s)')
@@ -190,8 +245,38 @@ def main(argv=None):
help="new name")
p_rename.set_defaults(func=f_rename)
- p_info = subparsers.add_parser('info', help='show information items of the ll sub-command')
+ p_color = subparsers.add_parser('color',
+ help='display and modify branch coloring of the ll sub-command.')
+ p_color.set_defaults(func=f_color)
+ color_cmds = p_color.add_subparsers(dest='color_cmd',
+ help='additional help with sub-command -h')
+ color_cmds.add_parser('ll',
+ description='display available colors and the current branch coloring in the ll sub-command')
+ pc_set = color_cmds.add_parser('set',
+ description='Set color for local/remote situation.')
+ pc_set.add_argument('situation',
+ choices=info.get_color_encoding(),
+ help="5 possible local/remote situations")
+ pc_set.add_argument('color',
+ choices=[c.name for c in info.Color],
+ help="available colors")
+
+ p_info = subparsers.add_parser('info',
+ help='list, add, or remove information items of the ll sub-command.')
p_info.set_defaults(func=f_info)
+ info_cmds = p_info.add_subparsers(dest='info_cmd',
+ help='additional help with sub-command -h')
+ info_cmds.add_parser('ll',
+ description='show used and unused information items of the ll sub-command')
+ info_cmds.add_parser('add', description='Enable information item.'
+ ).add_argument('info_item',
+ choices=('branch', 'commit_msg', 'path'),
+ help="information item to add")
+ info_cmds.add_parser('rm', description='Disable information item.'
+ ).add_argument('info_item',
+ choices=('branch', 'commit_msg', 'path'),
+ help="information item to delete")
+
ll_doc = f''' status symbols:
+: staged changes
@@ -212,8 +297,19 @@ def main(argv=None):
nargs='?',
choices=utils.get_groups(),
help="show repos in the chosen group")
+ p_ll.add_argument('-n', '--no-colors', action='store_true',
+ help='Disable coloring on the branch names.')
p_ll.set_defaults(func=f_ll)
+ p_context = subparsers.add_parser('context',
+ help='Set and remove context. A context is a group.'
+ ' When set, all operations apply only to repos in that group.')
+ p_context.add_argument('choice',
+ nargs='?',
+ choices=set().union(utils.get_groups(), {'none'}),
+ help="Without argument, show current context. Otherwise choose a group as context. To remove context, use 'none'. ")
+ p_context.set_defaults(func=f_context)
+
p_ls = subparsers.add_parser(
'ls', help='display names of all repos, or path of a chosen repo')
p_ls.add_argument('repo',
@@ -223,21 +319,34 @@ def main(argv=None):
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(),
- help="repo(s) to be grouped")
+ 'group', help='list, add, or remove repo group(s)')
p_group.set_defaults(func=f_group)
-
- p_ungroup = subparsers.add_parser(
- 'ungroup', help='remove group information for repos',
- description="Remove group information on repos")
- p_ungroup.add_argument('to_ungroup',
- nargs='+',
- choices=utils.get_repos(),
- help="repo(s) to be ungrouped")
- p_ungroup.set_defaults(func=f_ungroup)
+ group_cmds = p_group.add_subparsers(dest='group_cmd',
+ help='additional help with sub-command -h')
+ group_cmds.add_parser('ll', description='List all groups with repos.')
+ group_cmds.add_parser('ls', description='List all group names.')
+ pg_add = group_cmds.add_parser('add', description='Add repo(s) to a group.')
+ pg_add.add_argument('to_group',
+ nargs='+',
+ metavar='repo',
+ choices=utils.get_repos(),
+ help="repo(s) to be grouped")
+ pg_add.add_argument('-n', '--name',
+ dest='gname',
+ metavar='group-name',
+ required=True,
+ help="group name")
+ pg_rename = group_cmds.add_parser('rename', description='Change group name.')
+ pg_rename.add_argument('gname', metavar='group-name',
+ choices=utils.get_groups(),
+ help="existing group to rename")
+ pg_rename.add_argument('new_name', metavar='new-name',
+ help="new group name")
+ group_cmds.add_parser('rm',
+ description='Remove group(s).').add_argument('to_ungroup',
+ nargs='+',
+ choices=utils.get_groups(),
+ help="group(s) to delete")
# superman mode
p_super = subparsers.add_parser(