Skip to content

Commit 1bc0dd5

Browse files
committed
Fix issue #1 and #2
This makes the git-sweep help menus more useful and also addresses an issue where the command that git-sweep recommended you use to cleanup was missing the arguments that you may have used to preview. This could have some nasty side effects.
1 parent fb642ce commit 1bc0dd5

File tree

3 files changed

+145
-19
lines changed

3 files changed

+145
-19
lines changed

README.rst

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ Tell it to skip the ``git fetch`` that it does by default.
103103

104104
branch1
105105

106-
To delete them, run again with `git-sweep cleanup`
106+
To delete them, run again with `git-sweep cleanup --nofetch`
107107

108108
Make it skip certain branches.
109109

@@ -117,7 +117,36 @@ Make it skip certain branches.
117117
upgrade-libs
118118
derp-removal
119119

120-
To delete them, run again with `git-sweep cleanup`
120+
To delete them, run again with `git-sweep cleanup --skip=develop`
121+
122+
Once git-sweep finds the branches, you'll be asked to confirm that you wish to
123+
delete them.
124+
125+
::
126+
127+
Delete these branches? (y/n)
128+
129+
You can use the ``--force`` option to bypass this and start deleting
130+
immediately.
131+
132+
::
133+
134+
$ git-sweep cleanup --skip=develop --force
135+
Fetching from the remote
136+
These branches have been merged into master:
137+
138+
important-upgrade
139+
upgrade-libs
140+
derp-removal
141+
142+
deleting important-upgrade (done)
143+
deleting upgrade-libs (done)
144+
deleting derp-removal (done)
145+
146+
All done!
147+
148+
Tell everyone to run `git fetch --prune` to sync with this remote.
149+
(you don't have to, yours is synced)
121150

122151
Development
123152
-----------
@@ -138,16 +167,23 @@ To run the tests, bootstrap Buildout and run this command:
138167
...
139168
$ ./bin/test
140169

170+
We also use Tox_. It will run the tests for Python 2.6 and 2.7.
171+
172+
::
173+
174+
$ ./bin/tox
175+
141176
Requirements
142177
------------
143178

144179
* Git >= 1.7
145180
* Python >= 2.6
146181

147-
.. _GitHub Flow: http://scottchacon.com/2011/08/31/github-flow.html
148-
.. _git-flow: http://nvie.com/posts/a-successful-git-branching-model/
149-
150182
License
151183
-------
152184

153185
Friendly neighborhood MIT license.
186+
187+
.. _GitHub Flow: http://scottchacon.com/2011/08/31/github-flow.html
188+
.. _git-flow: http://nvie.com/posts/a-successful-git-branching-model/
189+
.. _Tox: http://pypi.python.org/pypi/tox

src/gitsweep/cli.py

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import sys
22
from os import getcwd
33
from argparse import ArgumentParser
4+
from textwrap import dedent
45

56
from git import Repo, InvalidGitRepositoryError
67

@@ -15,8 +16,9 @@ class CommandLine(object):
1516
1617
"""
1718
parser = ArgumentParser(
18-
description='Clean up your Git repository remote branches',
19-
prog='git-sweep')
19+
description='Clean up your Git remote branches.',
20+
usage='git-sweep <action> [-h]',
21+
)
2022

2123
_sub_parsers = parser.add_subparsers(title='action',
2224
description='Preview changes or perform clean up')
@@ -42,16 +44,30 @@ class CommandLine(object):
4244
'action': 'store_false',
4345
'default': True}
4446

47+
_preview_usage = dedent('''
48+
git-sweep preview [-h] [--nofetch] [--skip SKIPS]
49+
[--master MASTER] [--origin ORIGIN]
50+
'''.strip())
51+
4552
_preview = _sub_parsers.add_parser('preview',
46-
help='Preview the branches that will be deleted')
53+
help='Preview the branches that will be deleted',
54+
usage=_preview_usage)
4755
_preview.add_argument('--origin', **_origin_kwargs)
4856
_preview.add_argument('--master', **_master_kwargs)
4957
_preview.add_argument('--nofetch', **_no_fetch_kwargs)
5058
_preview.add_argument('--skip', **_skip_kwargs)
5159
_preview.set_defaults(action='preview')
5260

61+
_cleanup_usage = dedent('''
62+
git-sweep cleanup [-h] [--nofetch] [--skip SKIPS] [--force]
63+
[--master MASTER] [--origin ORIGIN]
64+
'''.strip())
65+
5366
_cleanup = _sub_parsers.add_parser('cleanup',
54-
help='Delete merged branches from the remote')
67+
help='Delete merged branches from the remote',
68+
usage=_cleanup_usage)
69+
_cleanup.add_argument('--force', action='store_true', default=False,
70+
dest='force', help='Do not ask, cleanup immediately')
5571
_cleanup.add_argument('--origin', **_origin_kwargs)
5672
_cleanup.add_argument('--master', **_master_kwargs)
5773
_cleanup.add_argument('--nofetch', **_no_fetch_kwargs)
@@ -66,6 +82,10 @@ def run(self):
6682
Runs git-sweep.
6783
"""
6884
try:
85+
if not self.args:
86+
self.parser.print_help()
87+
sys.exit(1)
88+
6989
self._sweep()
7090

7191
sys.exit(0)
@@ -120,9 +140,10 @@ def _sweep(self):
120140
deleter = Deleter(repo, remote_name=remote_name,
121141
master_branch=master_branch)
122142

123-
sys.stdout.write('\nDelete these branches? (y/n) ')
124-
answer = raw_input()
125-
if answer.lower().startswith('y'):
143+
if not args.force:
144+
sys.stdout.write('\nDelete these branches? (y/n) ')
145+
answer = raw_input()
146+
if args.force or answer.lower().startswith('y'):
126147
sys.stdout.write('\n')
127148
for ref in ok_to_delete:
128149
sys.stdout.write(' deleting {0}'.format(ref.remote_head))
@@ -136,5 +157,10 @@ def _sweep(self):
136157
else:
137158
sys.stdout.write('\nOK, aborting.\n')
138159
elif ok_to_delete:
160+
# Replace the first argument with cleanup
161+
sysv_copy = self.args[:]
162+
sysv_copy[0] = 'cleanup'
163+
command = 'git-sweep {0}'.format(' '.join(sysv_copy))
164+
139165
sys.stdout.write(
140-
'\nTo delete them, run again with `git-sweep cleanup`\n')
166+
'\nTo delete them, run again with `{0}`\n'.format(command))

src/gitsweep/tests/test_cli.py

Lines changed: 70 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from mock import patch
2-
from nose.plugins.attrib import attr
32

43
from gitsweep.tests.testcases import CommandTestCase
54

@@ -17,9 +16,9 @@ def test_help(self):
1716
(retcode, stdout, stderr) = self.gscommand('git-sweep -h')
1817

1918
self.assertResults('''
20-
usage: git-sweep [-h] {preview,cleanup} ...
19+
usage: git-sweep <action> [-h]
2120
22-
Clean up your Git repository remote branches
21+
Clean up your Git remote branches.
2322
2423
optional arguments:
2524
-h, --help show this help message and exit
@@ -80,6 +79,35 @@ def test_will_preview(self):
8079
To delete them, run again with `git-sweep cleanup`
8180
''', stdout)
8281

82+
def test_will_preserve_arguments(self):
83+
"""
84+
The recommended cleanup command contains the same arguments given.
85+
"""
86+
for i in range(1, 6):
87+
self.command('git checkout -b branch{0}'.format(i))
88+
self.make_commit()
89+
self.command('git checkout master')
90+
self.make_commit()
91+
self.command('git merge branch{0}'.format(i))
92+
93+
preview = 'git-sweep preview --master=master --origin=origin'
94+
cleanup = 'git-sweep cleanup --master=master --origin=origin'
95+
96+
(retcode, stdout, stderr) = self.gscommand(preview)
97+
98+
self.assertResults('''
99+
Fetching from the remote
100+
These branches have been merged into master:
101+
102+
branch1
103+
branch2
104+
branch3
105+
branch4
106+
branch5
107+
108+
To delete them, run again with `{0}`
109+
'''.format(cleanup), stdout)
110+
83111
def test_will_preview_none_found(self):
84112
"""
85113
Will preview the proposed deletes.
@@ -131,7 +159,7 @@ def test_will_cleanup(self):
131159
All done!
132160
133161
Tell everyone to run `git fetch --prune` to sync with this remote.
134-
(you don't have to, your's is synced)
162+
(you don't have to, yours is synced)
135163
''', stdout)
136164

137165
def test_will_abort_cleanup(self):
@@ -163,7 +191,6 @@ def test_will_abort_cleanup(self):
163191
OK, aborting.
164192
''', stdout)
165193

166-
@attr('focus')
167194
def test_will_skip_certain_branches(self):
168195
"""
169196
Can be forced to skip certain branches.
@@ -178,6 +205,8 @@ def test_will_skip_certain_branches(self):
178205
(retcode, stdout, stderr) = self.gscommand(
179206
'git-sweep preview --skip=branch1,branch2')
180207

208+
cleanup = 'git-sweep cleanup --skip=branch1,branch2'
209+
181210
self.assertResults('''
182211
Fetching from the remote
183212
These branches have been merged into master:
@@ -186,5 +215,40 @@ def test_will_skip_certain_branches(self):
186215
branch4
187216
branch5
188217
189-
To delete them, run again with `git-sweep cleanup`
218+
To delete them, run again with `{0}`
219+
'''.format(cleanup), stdout)
220+
221+
def test_will_force_clean(self):
222+
"""
223+
Will cleanup immediately if forced.
224+
"""
225+
for i in range(1, 6):
226+
self.command('git checkout -b branch{0}'.format(i))
227+
self.make_commit()
228+
self.command('git checkout master')
229+
self.make_commit()
230+
self.command('git merge branch{0}'.format(i))
231+
232+
(retcode, stdout, stderr) = self.gscommand('git-sweep cleanup --force')
233+
234+
self.assertResults('''
235+
Fetching from the remote
236+
These branches have been merged into master:
237+
238+
branch1
239+
branch2
240+
branch3
241+
branch4
242+
branch5
243+
244+
deleting branch1 (done)
245+
deleting branch2 (done)
246+
deleting branch3 (done)
247+
deleting branch4 (done)
248+
deleting branch5 (done)
249+
250+
All done!
251+
252+
Tell everyone to run `git fetch --prune` to sync with this remote.
253+
(you don't have to, yours is synced)
190254
''', stdout)

0 commit comments

Comments
 (0)