def test_colors(*args): # Colors are inactive by default when stdout is not a tty assert colored.is_tty() is False assert colored.is_color_active() is False assert colored.problem("foo") == "foo" assert colored.tty_colored(None) == "None" # activate_colors(None) means "use default setting" colored.activate_colors(None) assert colored.is_color_active() is False assert colored.highlight("foo") == "foo" # Turn off coloring explicitly colored.activate_colors(False) assert colored.is_color_active() is False assert colored.pop("foo") == "foo" assert colored.warn("foo") == "foo" assert colored.progress("foo") == "foo" assert colored.note("foo") == "foo" # Turn on coloring explicitly colored.activate_colors(True) assert colored.is_color_active() is True assert colored.problem("foo") == '[31mfoo[0m' # Go back to default colored.activate_colors(None) assert colored.is_color_active() is False assert colored.highlight("foo") == "foo"
def clean_show(target): """ :param GitCheckout target: Target to show """ print(target.header()) if not target.git.local_cleanable_branches: print(" No local branches can be cleaned") else: for branch in target.git.local_cleanable_branches: print(" %s branch %s can be cleaned" % (colored.highlight("local"), colored.highlight(branch))) if not target.git.remote_cleanable_branches: print(" No remote branches can be cleaned") else: for branch in target.git.remote_cleanable_branches: print(" %s can be cleaned" % (colored.highlight(branch)))
def header(self, report=None, freshness=True): """ :param GitRunReport|None report: Optional report to show (defaults to self.git.report) :return str: Textual representation """ report = GitRunReport( report or self.git.report(inspect_remotes=self.prefs.inspect_remotes)) result = "%s:" % self.aligned_name if self.git.is_git_checkout: branch = colored.highlight( self.git.branches.shortened_current_branch) n = len(self.git.branches.local) if n > 1: branch += " +%s" % (n - 1) result += " [%s]" % branch if freshness: freshness = self.git.status.freshness if freshness: result += " %s" % freshness if not report.has_problems and self.parent and self.prefs and self.prefs.all and self.parent.predominant: if self.origin_project != self.parent.predominant: report.add(note="not part of %s" % self.parent.predominant) if report: rep = report.representation() if rep: result += " %s" % rep return result
def run_raw_git_command(self, *args): """ :param *args: Execute git command with provided args, don't capture its output, but let it show through stdout/stderr :return GitRunReport: Report """ cmd, pretty_args = self._git_command(args) pretty_args = "git %s" % " ".join(args) print("Running: %s" % colored.highlight(pretty_args)) proc = subprocess.Popen(cmd) # nosec proc.communicate() if proc.returncode: return GitRunReport(problem="git exited with code %s" % proc.returncode) return GitRunReport()
def header(self): result = "%s:" % colored.note(pretty_path(self.path)) if not self.projects: return "%s %s" % (result, colored.warn("no git folders")) if self.predominant: result += colored.highlight( " %s %s" % (len(self.projects[self.predominant]), self.predominant)) else: result += colored.warn(" no predominant project") if self.additional: result += " (%s)" % colored.note(", ".join( "+%s %s" % (len(self.projects[project]), project) for project in self.additional)) return result
def handle_single_clean(target, what): """ :param GitCheckout target: Single checkout to clean :param str what: Operation """ report = target.git.fetch() if report.has_problems: if what != "reset": what = "clean" print( target.header( GitRunReport(report).add(problem="<can't %s" % what))) abort() if what == "reset": return clean_reset(target) if what == "show": return clean_show(target) total_cleaned = 0 print(target.header()) if what in "remote all": if not target.git.remote_cleanable_branches: print(" No remote branches can be cleaned") else: total = len(target.git.remote_cleanable_branches) cleaned = 0 for branch in target.git.remote_cleanable_branches: remote, _, name = branch.partition("/") if not remote and name: raise Exception("Unknown branch spec '%s'" % branch) if run_git(target, False, "branch", "--delete", "--remotes", branch): cleaned += run_git(target, False, "push", "--delete", remote, name) total_cleaned += cleaned if cleaned == total: print("%s cleaned" % colored.plural(cleaned, "remote branch")) else: print("%s/%s remote branches cleaned" % (cleaned, total)) target.git.reset_cached_properties() if what == "all": # Fetch to update remote branches (and correctly detect new dangling local) target.git.fetch() if what in "local all": if not target.git.local_cleanable_branches: print(" No local branches can be cleaned") else: total = len(target.git.local_cleanable_branches) cleaned = 0 for branch in target.git.local_cleanable_branches: if branch == target.git.branches.current: fallback = target.git.fallback_branch() if not fallback: print( "Skipping branch '%s', can't determine fallback branch" % target.git.branches.current) continue run_git(target, True, "checkout", fallback) run_git(target, True, "pull") cleaned += run_git(target, False, "branch", "--delete", branch) total_cleaned += cleaned if cleaned == total: print( colored.highlight("%s cleaned" % colored.plural(cleaned, "local branch"))) else: print( colored.warn("%s/%s local branches cleaned" % (cleaned, total))) target.git.reset_cached_properties() if total_cleaned: print(target.header())