def issue(repo, command, sudo=False, dry=False, error='raise', return_out=False): """ issues a command on a repo CommandLine: python -m utool.util_git --exec-repocmd Example: >>> # DISABLE_DOCTEST >>> from utool.util_git import * # NOQA >>> import utool as ut >>> repo = dirname(ut.get_modpath(ut, prefer_pkg=True)) >>> command = 'git status' >>> sudo = False >>> result = repocmd(repo, command, sudo) >>> print(result) """ import utool as ut if ut.WIN32: assert not sudo, 'cant sudo on windows' if command == 'short_status': return repo.short_status() command_list = ut.ensure_iterable(command) cmdstr = '\n '.join([cmd_ for cmd_ in command_list]) if not dry: print('+--- *** repocmd(%s) *** ' % (cmdstr,)) print('repo=%s' % ut.color_text(repo.dpath, 'yellow')) verbose = True with repo.chdir_context(): ret = None for count, cmd in enumerate(command_list): if dry: print(cmd) continue if not sudo or ut.WIN32: # ret = os.system(cmd) cmdinfo = ut.cmd2(cmd, verbout=True) out, err, ret = ut.take(cmdinfo, ['out', 'err', 'ret']) else: # cmdinfo = ut.cmd2('sudo ' + cmd, verbose=1) out, err, ret = ut.cmd(cmd, sudo=True) if verbose > 1: print('ret(%d) = %r' % (count, ret,)) if ret != 0: if error == 'raise': raise Exception('Failed command %r' % (cmd,)) elif error == 'return': return out else: raise ValueError('unknown flag error=%r' % (error,)) if return_out: return out if not dry: print('L____')
def short_status(repo): r""" CommandLine: python -m utool.util_git short_status Example: >>> # DISABLE_DOCTEST >>> from utool.util_git import * # NOQA >>> import utool as ut >>> repo = Repo(dpath=ut.truepath('.')) >>> result = repo.short_status() >>> print(result) """ import utool as ut prefix = repo.dpath with ut.ChdirContext(repo.dpath, verbose=False): out, err, ret = ut.cmd('git status', verbose=False, quiet=True) # parse git status is_clean_msg1 = 'Your branch is up-to-date with' is_clean_msgs = [ 'nothing to commit, working directory clean', 'nothing to commit, working tree clean', ] msg2 = 'nothing added to commit but untracked files present' needs_commit_msgs = [ 'Changes to be committed', 'Changes not staged for commit', 'Your branch is ahead of', ] suffix = '' if is_clean_msg1 in out and any(msg in out for msg in is_clean_msgs): suffix += ut.color_text('is clean', 'blue') if msg2 in out: suffix += ut.color_text('has untracked files', 'yellow') if any(msg in out for msg in needs_commit_msgs): suffix += ut.color_text('has changes', 'red') print(prefix + ' ' + suffix)
def gitcmd(repo, command): try: import utool as ut repo_ = ut.Repo(dpath=repo) repo_.issue(command) except Exception: print() print("************") try: print('repo=%s' % ut.color_text(repo.dpath, 'yellow')) except Exception: print('repo = %r ' % (repo,)) os.chdir(repo) if command.find('git') != 0 and command != 'gcwip': command = 'git ' + command os.system(command) print("************")
def gitcmd(repo, command): try: import utool as ut except ImportError: print() print("************") try: print('repo=%s' % ut.color_text(repo.dpath, 'yellow')) except Exception: print('repo = %r ' % (repo, )) os.chdir(repo) if command.find('git') != 0 and command != 'gcwip': command = 'git ' + command os.system(command) print("************") else: repo_ = ut.Repo(dpath=repo) repo_.issue(command)
def issue(repo, command, sudo=False): """ issues a command on a repo CommandLine: python -m utool.util_git --exec-repocmd Example: >>> # DISABLE_DOCTEST >>> from utool.util_git import * # NOQA >>> import utool as ut >>> repo = dirname(ut.get_modpath(ut, prefer_pkg=True)) >>> command = 'git status' >>> sudo = False >>> result = repocmd(repo, command, sudo) >>> print(result) """ import utool as ut if ut.WIN32: assert not sudo, "cant sudo on windows" command_list = ut.ensure_iterable(command) cmdstr = "\n ".join([cmd_ for cmd_ in command_list]) print("+--- *** repocmd(%s) *** " % (cmdstr,)) print("repo=%s" % ut.color_text(repo.dpath, "yellow")) with ut.ChdirContext(repo.dpath, verbose=False): ret = None for count, cmd in enumerate(command_list): if not sudo or ut.WIN32: ret = os.system(cmd) else: out, err, ret = ut.cmd(cmd, sudo=True) verbose = True if verbose > 1: print("ret(%d) = %r" % (count, ret)) if ret != 0: raise Exception("Failed command %r" % (cmd,)) print("L____")
def issue(repo, command, sudo=False): """ issues a command on a repo CommandLine: python -m utool.util_git --exec-repocmd Example: >>> # DISABLE_DOCTEST >>> from utool.util_git import * # NOQA >>> import utool as ut >>> repo = dirname(ut.get_modpath(ut, prefer_pkg=True)) >>> command = 'git status' >>> sudo = False >>> result = repocmd(repo, command, sudo) >>> print(result) """ import utool as ut if ut.WIN32: assert not sudo, 'cant sudo on windows' command_list = ut.ensure_iterable(command) cmdstr = '\n '.join([cmd_ for cmd_ in command_list]) print('+--- *** repocmd(%s) *** ' % (cmdstr,)) print('repo=%s' % ut.color_text(repo.dpath, 'yellow')) with ut.ChdirContext(repo.dpath, verbose=False): ret = None for count, cmd in enumerate(command_list): if not sudo or ut.WIN32: ret = os.system(cmd) else: out, err, ret = ut.cmd(cmd, sudo=True) verbose = True if verbose > 1: print('ret(%d) = %r' % (count, ret,)) if ret != 0: raise Exception('Failed command %r' % (cmd,)) print('L____')
def color_func(val, level): if level: return ut.color_text(val, 'red') else: return val