Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --verbose flag so we can see progress. #11

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/gitsweep/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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=[]):
"""
Expand Down
14 changes: 11 additions & 3 deletions src/gitsweep/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand All @@ -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())

Expand All @@ -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):
Expand Down Expand Up @@ -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:
Expand Down
14 changes: 13 additions & 1 deletion src/gitsweep/inspector.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import sys
from git import Git

from .base import BaseOperation
Expand All @@ -24,20 +25,31 @@ 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.
(retcode, stdout, stderr) = cmd.execute(
['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
37 changes: 37 additions & 0 deletions src/gitsweep/tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down