tag: add --edit option
Add a --edit option whichs allows modifying the messages provided by -m or -F, the same way git commit --edit does. Signed-off-by: Nicolas Morey-Chaisemartin <NMoreyChaisemartin@suse.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Nicolas Morey-Chaisemartin committed
Feb 6, 2018 at 09:36 UTC
9eed6e40c0ec9cbe89efd7303c3facd16fa1364f
3 files changed
+46
-3
Documentation/git-tag.txt
+7
-1
@@ -9,7 +9,7 @@ git-tag - Create, list, delete or verify a tag object signed with GPG
9
SYNOPSIS
10
--------
11
[verse]
12
-'git tag' [-a | -s | -u <keyid>] [-f] [-m <msg> | -F <file>]
12
+'git tag' [-a | -s | -u <keyid>] [-f] [-m <msg> | -F <file>] [-e]
13
<tagname> [<commit> | <object>]
14
'git tag' -d <tagname>...
15
'git tag' [-n[<num>]] -l [--contains <commit>] [--no-contains <commit>]
@@ -167,6 +167,12 @@ This option is only applicable when listing tags without annotation lines.
167
Implies `-a` if none of `-a`, `-s`, or `-u <keyid>`
168
is given.
169
170
+-e::
171
+--edit::
172
+ The message taken from file with `-F` and command line with
173
+ `-m` are usually used as the tag message unmodified.
174
+ This option lets you further edit the message taken from these sources.
175
+
176
--cleanup=<mode>::
177
This option sets how the tag message is cleaned up.
178
The '<mode>' can be one of 'verbatim', 'whitespace' and 'strip'. The
builtin/tag.c
+9
-2
@@ -194,6 +194,7 @@ static int build_tag_object(struct strbuf *buf, int sign, struct object_id *resu
194
195
struct create_tag_options {
196
unsigned int message_given:1;
197
+ unsigned int use_editor:1;
198
unsigned int sign;
199
enum {
200
CLEANUP_NONE,
@@ -224,7 +225,7 @@ static void create_tag(const struct object_id *object, const char *tag,
225
tag,
226
git_committer_info(IDENT_STRICT));
227
227
- if (!opt->message_given) {
228
+ if (!opt->message_given || opt->use_editor) {
229
int fd;
230
231
/* write the template message before editing: */
@@ -233,7 +234,10 @@ static void create_tag(const struct object_id *object, const char *tag,
234
if (fd < 0)
235
die_errno(_("could not create file '%s'"), path);
236
236
- if (!is_null_oid(prev)) {
237
+ if (opt->message_given) {
238
+ write_or_die(fd, buf->buf, buf->len);
239
+ strbuf_reset(buf);
240
+ } else if (!is_null_oid(prev)) {
241
write_tag_body(fd, prev);
242
} else {
243
struct strbuf buf = STRBUF_INIT;
@@ -372,6 +376,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
376
static struct ref_sorting *sorting = NULL, **sorting_tail = &sorting;
377
struct ref_format format = REF_FORMAT_INIT;
378
int icase = 0;
379
+ int edit_flag = 0;
380
struct option options[] = {
381
OPT_CMDMODE('l', "list", &cmdmode, N_("list tag names"), 'l'),
382
{ OPTION_INTEGER, 'n', NULL, &filter.lines, N_("n"),
@@ -386,6 +391,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
391
OPT_CALLBACK('m', "message", &msg, N_("message"),
392
N_("tag message"), parse_msg_arg),
393
OPT_FILENAME('F', "file", &msgfile, N_("read message from file")),
394
+ OPT_BOOL('e', "edit", &edit_flag, N_("force edit of tag message")),
395
OPT_BOOL('s', "sign", &opt.sign, N_("annotated and GPG-signed tag")),
396
OPT_STRING(0, "cleanup", &cleanup_arg, N_("mode"),
397
N_("how to strip spaces and #comments from message")),
@@ -524,6 +530,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
530
die(_("tag '%s' already exists"), tag);
531
532
opt.message_given = msg.given || msgfile;
533
+ opt.use_editor = edit_flag;
534
535
if (!cleanup_arg || !strcmp(cleanup_arg, "strip"))
536
opt.cleanup_mode = CLEANUP_ALL;
t/t7004-tag.sh
+30
@@ -452,6 +452,21 @@ test_expect_success \
452
test_cmp expect actual
453
'
454
455
+get_tag_header annotated-tag-edit $commit commit $time >expect
456
+echo "An edited message" >>expect
457
+test_expect_success 'set up editor' '
458
+ write_script fakeeditor <<-\EOF
459
+ sed -e "s/A message/An edited message/g" <"$1" >"$1-"
460
+ mv "$1-" "$1"
461
+ EOF
462
+'
463
+test_expect_success \
464
+ 'creating an annotated tag with -m message --edit should succeed' '
465
+ GIT_EDITOR=./fakeeditor git tag -m "A message" --edit annotated-tag-edit &&
466
+ get_tag_msg annotated-tag-edit >actual &&
467
+ test_cmp expect actual
468
+'
469
+
470
cat >msgfile <<EOF
471
Another message
472
in a file.
@@ -465,6 +480,21 @@ test_expect_success \
480
test_cmp expect actual
481
'
482
483
+get_tag_header file-annotated-tag-edit $commit commit $time >expect
484
+sed -e "s/Another message/Another edited message/g" msgfile >>expect
485
+test_expect_success 'set up editor' '
486
+ write_script fakeeditor <<-\EOF
487
+ sed -e "s/Another message/Another edited message/g" <"$1" >"$1-"
488
+ mv "$1-" "$1"
489
+ EOF
490
+'
491
+test_expect_success \
492
+ 'creating an annotated tag with -F messagefile --edit should succeed' '
493
+ GIT_EDITOR=./fakeeditor git tag -F msgfile --edit file-annotated-tag-edit &&
494
+ get_tag_msg file-annotated-tag-edit >actual &&
495
+ test_cmp expect actual
496
+'
497
+
498
cat >inputmsg <<EOF
499
A message from the
500
standard input