diff --git a/src/gitsweep/base.py b/src/gitsweep/base.py index 500f010..623adcc 100644 --- a/src/gitsweep/base.py +++ b/src/gitsweep/base.py @@ -22,10 +22,11 @@ class BaseOperation(object): Base class for all Git-related operations. """ - def __init__(self, repo, remote_name='origin', master_branch='master'): + def __init__(self, repo, remote_name='origin', master_branch='master', verbose=False): self.repo = repo self.remote_name = remote_name self.master_branch = master_branch + self.verbose = verbose def _filtered_remotes(self, origin, skip=[]): """ diff --git a/src/gitsweep/cli.py b/src/gitsweep/cli.py index 6b995b5..c998149 100644 --- a/src/gitsweep/cli.py +++ b/src/gitsweep/cli.py @@ -44,8 +44,14 @@ class CommandLine(object): 'action': 'store_false', 'default': True} + _verbose_kwargs = { + 'help': 'Verbose output', + 'dest': 'verbose', + 'action': 'store_true', + 'default': False} + _preview_usage = dedent(''' - git-sweep preview [-h] [--nofetch] [--skip SKIPS] + git-sweep preview [-h] [--verbose] [--nofetch] [--skip SKIPS] [--master MASTER] [--origin ORIGIN] '''.strip()) @@ -56,10 +62,11 @@ class CommandLine(object): _preview.add_argument('--master', **_master_kwargs) _preview.add_argument('--nofetch', **_no_fetch_kwargs) _preview.add_argument('--skip', **_skip_kwargs) + _preview.add_argument('--verbose', **_verbose_kwargs) _preview.set_defaults(action='preview') _cleanup_usage = dedent(''' - git-sweep cleanup [-h] [--nofetch] [--skip SKIPS] [--force] + git-sweep cleanup [-h] [--verbose] [--nofetch] [--skip SKIPS] [--force] [--master MASTER] [--origin ORIGIN] '''.strip()) @@ -72,6 +79,7 @@ class CommandLine(object): _cleanup.add_argument('--master', **_master_kwargs) _cleanup.add_argument('--nofetch', **_no_fetch_kwargs) _cleanup.add_argument('--skip', **_skip_kwargs) + _cleanup.add_argument('--verbose', **_verbose_kwargs) _cleanup.set_defaults(action='cleanup') def __init__(self, args): @@ -122,7 +130,7 @@ def _sweep(self): # Find branches that could be merged inspector = Inspector(repo, remote_name=remote_name, - master_branch=master_branch) + master_branch=master_branch, verbose=args.verbose) ok_to_delete = inspector.merged_refs(skip=skips) if ok_to_delete: diff --git a/src/gitsweep/inspector.py b/src/gitsweep/inspector.py index 51b111a..8ebbcb3 100644 --- a/src/gitsweep/inspector.py +++ b/src/gitsweep/inspector.py @@ -1,3 +1,4 @@ +import sys from git import Git from .base import BaseOperation @@ -24,11 +25,17 @@ def merged_refs(self, skip=[]): origin, skip=['HEAD', self.master_branch] + skip) merged = [] - for ref in refs: + num_refs = len(refs) + if self.verbose: + sys.stdout.write("Found %d remote refs\n" % num_refs) + + for idx, ref in enumerate(refs): upstream = '{origin}/{master}'.format( origin=origin.name, master=master.remote_head) head = '{origin}/{branch}'.format( origin=origin.name, branch=ref.remote_head) + if self.verbose: + sys.stdout.write("Checking %s (%d/%d)...\n" % (head, idx + 1, num_refs)) cmd = Git(self.repo.working_dir) # Drop to the git binary to do this, it's just easier to work with # at this level. @@ -36,8 +43,13 @@ def merged_refs(self, skip=[]): ['git', 'cherry', upstream, head], with_extended_output=True, with_exceptions=False) if retcode == 0 and not stdout: + if self.verbose: + sys.stdout.write("...merged (ok to delete)\n") # This means there are no commits in the branch that are not # also in the master branch. This is ready to be deleted. merged.append(ref) + else: + if self.verbose: + sys.stdout.write("...not merged\n") return merged diff --git a/src/gitsweep/tests/test_cli.py b/src/gitsweep/tests/test_cli.py index 9ddd6ce..a3238dc 100644 --- a/src/gitsweep/tests/test_cli.py +++ b/src/gitsweep/tests/test_cli.py @@ -79,6 +79,43 @@ def test_will_preview(self): To delete them, run again with `git-sweep cleanup` ''', stdout) + def test_verbose_preview(self): + """ + Will show verbose output when previewing the proposed deletes. + """ + for i in range(1, 6): + self.command('git checkout -b branch{0}'.format(i)) + self.make_commit() + self.command('git checkout master') + self.make_commit() + self.command('git merge branch{0}'.format(i)) + + (retcode, stdout, stderr) = self.gscommand('git-sweep preview --verbose') + + self.assertResults(''' + Fetching from the remote + Found 5 remote refs + Checking origin/branch1 (1/5)... + ...merged (ok to delete) + Checking origin/branch2 (2/5)... + ...merged (ok to delete) + Checking origin/branch3 (3/5)... + ...merged (ok to delete) + Checking origin/branch4 (4/5)... + ...merged (ok to delete) + Checking origin/branch5 (5/5)... + ...merged (ok to delete) + These branches have been merged into master: + + branch1 + branch2 + branch3 + branch4 + branch5 + + To delete them, run again with `git-sweep cleanup --verbose` + ''', stdout) + def test_will_preserve_arguments(self): """ The recommended cleanup command contains the same arguments given.