Skip to content

Commit 7e972a6

Browse files
committed
Use Patches.make_name() in various commands
The new Patches.make_name() method replaces various uses of the utils.make_patch_name() and utils.find_patch_name() functions, which are removed. make_name() is expected to be more robust at ensuring valid and unique patch names. Signed-off-by: Peter Grayson <[email protected]>
1 parent 0e569fd commit 7e972a6

File tree

11 files changed

+30
-72
lines changed

11 files changed

+30
-72
lines changed

stgit/commands/new.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,7 @@ def func(parser, options, args):
9595
cd = run_commit_msg_hook(stack.repository, cd)
9696

9797
if name is None:
98-
name = utils.make_patch_name(cd.message_str, stack.patches.exists)
99-
assert stack.patches.is_name_valid(name)
98+
name = stack.patches.make_name(cd.message_str)
10099

101100
# Write the new patch.
102101
stack.repository.default_iw

stgit/commands/pick.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from stgit.lib.transaction import StackTransaction, TransactionHalted
1616
from stgit.out import out
1717
from stgit.run import Run
18-
from stgit.utils import STGIT_CONFLICT, STGIT_SUCCESS, find_patch_name, make_patch_name
18+
from stgit.utils import STGIT_CONFLICT, STGIT_SUCCESS
1919

2020
__copyright__ = """
2121
Copyright (C) 2005, Catalin Marinas <[email protected]>
@@ -120,9 +120,9 @@ def __pick_commit(stack, ref_stack, iw, commit, patchname, options):
120120
patchname = 'revert-' + patchname
121121

122122
if patchname:
123-
patchname = find_patch_name(patchname, stack.patches.exists)
123+
patchname = stack.patches.make_name(patchname, lower=False)
124124
else:
125-
patchname = make_patch_name(commit.data.message_str, stack.patches.exists)
125+
patchname = stack.patches.make_name(commit.data.message_str)
126126

127127
if options.parent:
128128
parent = git_commit(options.parent, repository, ref_stack.name)

stgit/commands/refresh.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ def make_temp_patch(stack, patch_name, tree):
213213
message='Refresh of %s' % patch_name,
214214
)
215215
)
216-
temp_name = utils.make_patch_name('refresh-temp', stack.patches.exists)
216+
temp_name = stack.patches.make_name('refresh-temp')
217217
trans = StackTransaction(stack, 'refresh (create temporary patch)')
218218
trans.patches[temp_name] = commit
219219
trans.applied.append(temp_name)

stgit/commands/repair.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from stgit.commands.common import CmdException, DirectoryGotoTopLevel
22
from stgit.lib.transaction import StackTransaction, TransactionHalted
33
from stgit.out import out
4-
from stgit.utils import make_patch_name
54

65
__copyright__ = """
76
Copyright (C) 2006, Karl Hasselström <[email protected]>
@@ -129,10 +128,7 @@ def func(parser, options, args):
129128
)
130129

131130
for c in patchify:
132-
pn = make_patch_name(
133-
c.data.message_str,
134-
unacceptable=lambda name: any(p.name == name for p in patches),
135-
)
131+
pn = stack.patches.make_name(c.data.message_str)
136132
out.info('Creating patch %s from commit %s' % (pn, c.sha1))
137133
applied.append(stack.patches.new(pn, c, 'repair'))
138134
out.done()

stgit/commands/squash.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,14 +117,11 @@ def _squash_patches(trans, patches, msg, save_template, no_verify=False):
117117

118118
def _squash(stack, iw, name, msg, save_template, patches, no_verify=False):
119119
# If a name was supplied on the command line, make sure it's OK.
120-
def bad_name(pn):
121-
return pn not in patches and stack.patches.exists(pn)
120+
if name and name not in patches and stack.patches.exists(name):
121+
raise CmdException('Patch name "%s" already taken' % name)
122122

123123
def get_name(cd):
124-
return name or utils.make_patch_name(cd.message_str, bad_name)
125-
126-
if name and bad_name(name):
127-
raise CmdException('Patch name "%s" already taken' % name)
124+
return name or stack.patches.make_name(cd.message_str, allow=patches)
128125

129126
def make_squashed_patch(trans, new_commit_data):
130127
name = get_name(new_commit_data)
@@ -172,6 +169,8 @@ def func(parser, options, args):
172169
patches = parse_patches(args, list(stack.patchorder.all))
173170
if len(patches) < 2:
174171
raise CmdException('Need at least two patches')
172+
if options.name and not stack.patches.is_name_valid(options.name):
173+
raise CmdException('Patch name "%s" is invalid' % options.name)
175174
return _squash(
176175
stack,
177176
stack.repository.default_iw,

stgit/commands/uncommit.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from stgit import utils
21
from stgit.argparse import opt
32
from stgit.commands.common import CmdException, DirectoryHasRepository
43
from stgit.lib import transaction
@@ -143,11 +142,13 @@ def check_and_append(c, n):
143142
for pn in patchnames:
144143
if pn in taken_names:
145144
raise CmdException('Patch name "%s" already taken' % pn)
145+
elif not stack.patches.is_name_valid(pn):
146+
raise CmdException('Patch name "%s" is invalid' % pn)
146147
taken_names.add(pn)
147148
else:
148149
patchnames = []
149150
for c in reversed(commits):
150-
pn = utils.make_patch_name(c.data.message_str, lambda pn: pn in taken_names)
151+
pn = stack.patches.make_name(c.data.message_str, disallow=taken_names)
151152
patchnames.append(pn)
152153
taken_names.add(pn)
153154
patchnames.reverse()

stgit/lib/stack.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ def new(self, name, commit, msg):
168168
self._patches[name] = p
169169
return p
170170

171-
def make_name(self, raw, unique=True, lower=True):
171+
def make_name(self, raw, unique=True, lower=True, allow=(), disallow=()):
172172
"""Make a unique and valid patch name from provided raw name.
173173
174174
The raw name may come from a filename, commit message, or email subject line.
@@ -228,7 +228,9 @@ def make_name(self, raw, unique=True, lower=True):
228228
return short_name
229229

230230
unique_name = short_name
231-
while self.exists(unique_name):
231+
while unique_name not in allow and (
232+
self.exists(unique_name) or unique_name in disallow
233+
):
232234
m = re.match(r'(.*?)(-)?(\d+)$', unique_name)
233235
if m:
234236
base, sep, n_str = m.groups()

stgit/utils.py

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -124,54 +124,6 @@ def edit_bytes(s, filename):
124124
return s
125125

126126

127-
def find_patch_name(patchname, unacceptable):
128-
"""Find a patch name which is acceptable."""
129-
if unacceptable(patchname):
130-
suffix = 0
131-
while unacceptable('%s-%d' % (patchname, suffix)):
132-
suffix += 1
133-
patchname = '%s-%d' % (patchname, suffix)
134-
return patchname
135-
136-
137-
def patch_name_from_msg(msg):
138-
"""Return a string to be used as a patch name.
139-
140-
This is generated from the first line of the specified message string.
141-
142-
"""
143-
if not msg:
144-
return None
145-
146-
name_len = config.getint('stgit.namelength')
147-
148-
subject_line = msg.split('\n', 1)[0].lstrip().lower()
149-
words = re.sub(r'(?u)[\W]+', ' ', subject_line).split()
150-
151-
# use loop to avoid truncating the last name
152-
name = words and words[0] or 'unknown'
153-
for word in words[1:]:
154-
new = name + '-' + word
155-
if len(new) > name_len:
156-
break
157-
name = new
158-
159-
return name
160-
161-
162-
def make_patch_name(msg, unacceptable, default_name='patch'):
163-
"""Generate a patch name from the given commit message.
164-
165-
The generated name is guaranteed to make unacceptable(name) be false. If the commit
166-
message is empty, base the name on default_name instead.
167-
168-
"""
169-
patchname = patch_name_from_msg(msg)
170-
if not patchname:
171-
patchname = default_name
172-
return find_patch_name(patchname, unacceptable)
173-
174-
175127
def add_trailer(message, trailer, name, email):
176128
trailer_line = '%s: %s <%s>' % (trailer, name, email)
177129
return (

t/t1300-uncommit.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,10 @@ test_expect_success 'Attempt to reuse patch name' '
122122
stg commit --all
123123
'
124124

125+
test_expect_success 'Attempt to use invalid patch name' '
126+
command_error stg uncommit bad..patchname
127+
'
128+
125129
test_expect_success 'Uncommit a commit with not precisely one parent' '
126130
command_error stg uncommit -n 5 &&
127131
[ "$(echo $(stg series))" = "" ]

t/t2600-squash.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ test_expect_success 'Attempt duplicate patch name' '
2525
grep -e "Patch name \"p3\" already taken"
2626
'
2727

28+
test_expect_success 'Attempt invalid patch name' '
29+
command_error stg squash -n invalid..name -- p0 p1 2>&1 |
30+
grep -e "Patch name \"invalid..name\" is invalid"
31+
'
32+
2833
test_expect_success 'Save template' '
2934
stg squash --save-template mytemplate p0 p1 &&
3035
test_path_is_file mytemplate &&

0 commit comments

Comments
 (0)