Skip to content

Commit 976fc10

Browse files
committed
'stg edit' aborts on empty patch description; add instructions.
This resolves #138 by... 1. Adding instruction hints on the *power* of 'stg edit'. 2. Stripping these new hint lines from the description. 3. Aborting 'stg edit' if all description lines are cleared. From conversation on #138 it became clear that aborting the 'edit' on an empty editor is correct. This behavior mirrors 'git commit' and 'stg squash'. Previously, if a user cleared the 'stg edit' editor window, the patch description would be cleared. This is unintuative! Users likely expect that this would abort the edit instead.
1 parent 965e578 commit 976fc10

File tree

5 files changed

+46
-20
lines changed

5 files changed

+46
-20
lines changed

stgit/commands/common.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -369,11 +369,15 @@ def __split_descr_diff(string):
369369
return desc, diff
370370

371371

372-
def __parse_description(descr):
372+
def __parse_description(descr, fail_on_empty_description):
373373
"""Parse the patch description for patch and author information."""
374374
subject = ''
375375
patch_name = authname = authemail = authdate = None
376376

377+
descr = '\n'.join(strip_comments(descr)).strip()
378+
if fail_on_empty_description and not descr:
379+
raise CmdException('Aborting edit due to empty patch description')
380+
377381
descr_lines = [line.rstrip() for line in descr.splitlines()]
378382

379383
lasthdr = 0
@@ -415,7 +419,7 @@ def __parse_description(descr):
415419
return (message, patch_name, authname, authemail, authdate)
416420

417421

418-
def parse_patch(patch_data, contains_diff):
422+
def parse_patch(patch_data, contains_diff, fail_on_empty_description=True):
419423
"""Parse patch data.
420424
421425
Returns (description, patch_name, authname, authemail, authdate, diff)
@@ -428,12 +432,18 @@ def parse_patch(patch_data, contains_diff):
428432
descr = patch_data
429433
diff = None
430434
(descr, patch_name, authname, authemail, authdate) = __parse_description(
431-
decode_utf8_with_latin1(descr)
435+
decode_utf8_with_latin1(descr), fail_on_empty_description
432436
)
433437

434438
return (descr, patch_name, authname, authemail, authdate, diff)
435439

436440

441+
def strip_comments(message):
442+
for line in message.splitlines():
443+
if not line.startswith('#'):
444+
yield line
445+
446+
437447
def run_commit_msg_hook(repo, cd, editor_is_used=True):
438448
"""Run the commit-msg hook (if any) on a commit.
439449

stgit/commands/imprt.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ def __import_file(filename, options):
286286
f.close()
287287

288288
message, patch_name, author_name, author_email, author_date, diff = parse_patch(
289-
patch_data, contains_diff=True
289+
patch_data, contains_diff=True, fail_on_empty_description=False
290290
)
291291

292292
__create_patch(

stgit/commands/squash.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
DirectoryHasRepository,
66
parse_patches,
77
run_commit_msg_hook,
8+
strip_comments,
89
)
910
from stgit.lib.transaction import StackTransaction, TransactionHalted
1011

@@ -61,12 +62,6 @@ class SaveTemplateDone(Exception):
6162
pass
6263

6364

64-
def _strip_comments(message):
65-
for line in message.splitlines():
66-
if not line.startswith('#'):
67-
yield line
68-
69-
7065
def _squash_patches(trans, patches, name, msg, save_template, no_verify=False):
7166
cd = trans.patches[patches[0]].data
7267
for pn in patches[1:]:
@@ -101,7 +96,7 @@ def _squash_patches(trans, patches, name, msg, save_template, no_verify=False):
10196
else:
10297
msg = utils.edit_string(msg, '.stgit-squash.txt')
10398

104-
msg = '\n'.join(_strip_comments(msg)).strip()
99+
msg = '\n'.join(strip_comments(msg)).strip()
105100
if not msg:
106101
raise CmdException('Aborting squash due to empty commit message')
107102

stgit/lib/edit.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@
1111
from stgit.lib.log import log_stack_state
1212
from stgit.out import out
1313

14+
EDIT_MESSAGE_INSTRUCTIONS = """# Everything here is editable! You can modify the patch name, author,
15+
# date, commit message, and the diff (if --diff was given).
16+
# Lines starting with '#' will be ignored, and an empty message
17+
# aborts the edit.
18+
"""
19+
1420

1521
def update_patch_description(repo, cd, text, contains_diff):
1622
"""Create commit with updated description.
@@ -68,10 +74,11 @@ def patch_desc(repo, cd, patch_name, append_diff, diff_flags, replacement_diff):
6874
'Date: %s' % cd.author.date.isoformat(),
6975
'',
7076
cd.message_str,
77+
EDIT_MESSAGE_INSTRUCTIONS,
7178
]
7279
).encode(commit_encoding)
7380
if append_diff:
74-
parts = [desc.rstrip(), b'', b'---', b'']
81+
parts = [desc.rstrip(), b'---', b'']
7582
if replacement_diff:
7683
parts.append(replacement_diff)
7784
else:

t/t3300-edit.sh

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,15 @@ msg () { git cat-file -p $1 | sed '1,/^$/d' | tr '\n' / | sed 's,/*$,,' ; }
3131
auth () { git log -n 1 --pretty=format:"%an, %ae" $1 ; }
3232
adate () { git log -n 1 --pretty=format:%ai $1 ; }
3333

34+
write_script diffedit <<EOF
35+
echo "" > "\$1"
36+
EOF
37+
test_expect_success 'Empty editor aborts edit' '
38+
EDITOR=./diffedit command_error stg edit 2>&1 |
39+
grep -e "Aborting edit due to empty patch description"
40+
'
41+
rm -f diffedit
42+
3443
write_script diffedit <<EOF
3544
sed 's/Empty patch/Empty Patch/' "\$1" > "\$1".tmp && mv "\$1".tmp "\$1"
3645
EOF
@@ -84,7 +93,12 @@ test_expect_success 'Set patch message with --file -' '
8493
'
8594

8695
( printf 'Patch: p2\nFrom: A Ú Thor <[email protected]>\nDate: <omitted>'
87-
printf '\n\nPride and Prejudice\n' ) > expected-tmpl
96+
printf '\n\nPride and Prejudice\n'
97+
printf '\n# Everything here is editable! You can modify the patch name, author,'
98+
printf '\n# date, commit message, and the diff (if --diff was given).'
99+
printf "\n# Lines starting with '#' will be ignored, and an empty message"
100+
printf '\n# aborts the edit.\n'
101+
) > expected-tmpl
88102
omit_date () { sed "s/^Date:.*$/Date: <omitted>/" ; }
89103

90104
test_expect_success 'Save template to file' '
@@ -121,45 +135,45 @@ test_expect_success 'Edit commit message interactively (vi)' '
121135
unset EDITOR
122136
m=$(msg HEAD) &&
123137
PATH=.:$PATH stg edit p2 &&
124-
test "$(msg HEAD)" = "$m/vi"
138+
test "$(msg HEAD)" = "$m//vi"
125139
'
126140

127141
mkeditor e1
128142
test_expect_success 'Edit commit message interactively (EDITOR)' '
129143
m=$(msg HEAD) &&
130144
EDITOR=./e1 PATH=.:$PATH stg edit p2 &&
131145
echo $m && echo $(msg HEAD) &&
132-
test "$(msg HEAD)" = "$m/e1"
146+
test "$(msg HEAD)" = "$m//e1"
133147
'
134148

135149
mkeditor e2
136150
test_expect_success 'Edit commit message interactively (VISUAL)' '
137151
m=$(msg HEAD) &&
138152
VISUAL=./e2 EDITOR=./e1 PATH=.:$PATH stg edit p2 &&
139-
test "$(msg HEAD)" = "$m/e2"
153+
test "$(msg HEAD)" = "$m//e2"
140154
'
141155

142156
mkeditor e3
143157
test_expect_success 'Edit commit message interactively (core.editor)' '
144158
m=$(msg HEAD) &&
145159
git config core.editor e3 &&
146160
VISUAL=./e2 EDITOR=./e1 PATH=.:$PATH stg edit p2 &&
147-
test "$(msg HEAD)" = "$m/e3"
161+
test "$(msg HEAD)" = "$m//e3"
148162
'
149163

150164
mkeditor e4
151165
test_expect_success 'Edit commit message interactively (stgit.editor)' '
152166
m=$(msg HEAD) &&
153167
git config stgit.editor e4 &&
154168
VISUAL=./e2 EDITOR=./e1 PATH=.:$PATH stg edit p2 &&
155-
test "$(msg HEAD)" = "$m/e4"
169+
test "$(msg HEAD)" = "$m//e4"
156170
'
157171

158172
mkeditor e5
159173
test_expect_success 'Edit commit message interactively (GIT_EDITOR)' '
160174
m=$(msg HEAD) &&
161175
GIT_EDITOR=./e5 VISUAL=./e2 EDITOR=./e1 PATH=.:$PATH stg edit p2 &&
162-
test "$(msg HEAD)" = "$m/e5"
176+
test "$(msg HEAD)" = "$m//e5"
163177
'
164178

165179
rm -f vi e1 e2 e3 e4 e5
@@ -169,7 +183,7 @@ git config --unset stgit.editor
169183
mkeditor twoliner
170184
test_expect_success 'Both noninterative and interactive editing' '
171185
EDITOR=./twoliner stg edit -e -m "oneliner" p2 &&
172-
test "$(msg HEAD)" = "oneliner/twoliner"
186+
test "$(msg HEAD)" = "oneliner//twoliner"
173187
'
174188
rm -f twoliner
175189

0 commit comments

Comments
 (0)