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

Extended CSV Output #7

Open
wants to merge 1 commit into
base: master
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
61 changes: 44 additions & 17 deletions src/gitsweep/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
from os import getcwd
from argparse import ArgumentParser
from textwrap import dedent
from datetime import date

import csv

from git import Repo, InvalidGitRepositoryError

Expand Down Expand Up @@ -37,6 +40,12 @@ class CommandLine(object):
'help': 'Comma-separated list of branches to skip',
'dest': 'skips',
'default': ''}

_output_csv = {
'help': 'Output branch list as CSV',
'dest': 'csv',
'action': 'store_true',
'default': False}

_no_fetch_kwargs = {
'help': 'Do not fetch from the remote',
Expand All @@ -45,7 +54,7 @@ class CommandLine(object):
'default': True}

_preview_usage = dedent('''
git-sweep preview [-h] [--nofetch] [--skip SKIPS]
git-sweep preview [-h] [--nofetch] [--csv] [--skip SKIPS]
[--master MASTER] [--origin ORIGIN]
'''.strip())

Expand All @@ -56,6 +65,7 @@ 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('--csv', **_output_csv)
_preview.set_defaults(action='preview')

_cleanup_usage = dedent('''
Expand Down Expand Up @@ -105,6 +115,11 @@ def _sweep(self):
dry_run = True if args.action == 'preview' else False
fetch = args.fetch
skips = [i.strip() for i in args.skips.split(',')]

try:
use_csv = args.csv
except AttributeError:
use_csv = False

# Is this a Git repository?
repo = Repo(getcwd())
Expand All @@ -115,7 +130,8 @@ def _sweep(self):
if fetch:
for remote in repo.remotes:
if remote.name == remote_name:
sys.stdout.write('Fetching from the remote\n')
if use_csv == False:
sys.stdout.write('Fetching from the remote\n')
remote.fetch()

master_branch = args.master
Expand All @@ -125,16 +141,26 @@ def _sweep(self):
master_branch=master_branch)
ok_to_delete = inspector.merged_refs(skip=skips)

if ok_to_delete:
sys.stdout.write(
'These branches have been merged into {0}:\n\n'.format(
master_branch))
if use_csv == False:
if ok_to_delete:
sys.stdout.write(
'These branches have been merged into {0}:\n\n'.format(
master_branch))
else:
sys.stdout.write('No remote branches are available for '
'cleaning up\n')
else:
sys.stdout.write('No remote branches are available for '
'cleaning up\n')

writer = csv.writer(sys.stdout)

for ref in ok_to_delete:
sys.stdout.write(' {0}\n'.format(ref.remote_head))
if use_csv == True:
commit_data = repo.commit(ref)
writer.writerow([
ref.remote_head,
date.fromtimestamp(commit_data.committed_date).strftime('%Y-%m-%d'),
commit_data.author.name])
else:
sys.stdout.write(' {0}\n'.format(ref.remote_head))

if not dry_run:
deleter = Deleter(repo, remote_name=remote_name,
Expand All @@ -157,10 +183,11 @@ def _sweep(self):
else:
sys.stdout.write('\nOK, aborting.\n')
elif ok_to_delete:
# Replace the first argument with cleanup
sysv_copy = self.args[:]
sysv_copy[0] = 'cleanup'
command = 'git-sweep {0}'.format(' '.join(sysv_copy))

sys.stdout.write(
'\nTo delete them, run again with `{0}`\n'.format(command))
if use_csv == False:
# Replace the first argument with cleanup
sysv_copy = self.args[:]
sysv_copy[0] = 'cleanup'
command = 'git-sweep {0}'.format(' '.join(sysv_copy))

sys.stdout.write(
'\nTo delete them, run again with `{0}`\n'.format(command))
21 changes: 21 additions & 0 deletions src/gitsweep/tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,27 @@ def test_will_preview(self):

To delete them, run again with `git-sweep cleanup`
''', stdout)

def test_will_preview_csv(self):
"""
Will preview the proposed deletes in CSV format
"""
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 --csv')

valid_csv = True
data = stdout.split('\r\n')
for row in data:
if row.count(',') != 2 and row.strip() != '':
valid_csv = False

self.assertTrue(valid_csv)

def test_will_preserve_arguments(self):
"""
Expand Down