diff mbox series

[RFC,WIP,3/3] tag: add full support for --edit and --no-edit

Message ID 20191008184727.14337-4-lucasseikioshiro@gmail.com (mailing list archive)
State New, archived
Headers show
Series tag: fix --edit and --no-edit flags | expand

Commit Message

Lucas Oshiro Oct. 8, 2019, 6:47 p.m. UTC
git tag --edit and --no-edit flags are not currently fully supported.
This patch fixes the functionality that allows the editor to be opened
on demand.

Co-authored-by: Bárbara Fernandes <barbara.dcf@gmail.com>
Signed-off-by: Lucas Oshiro <lucasseikioshiro@gmail.com>
Signed-off-by: Bárbara Fernandes <barbara.dcf@gmail.com>
---
 builtin/tag.c  | 16 +++++++++++++---
 t/t7004-tag.sh |  4 ++--
 2 files changed, 15 insertions(+), 5 deletions(-)

Comments

Matheus Tavares Oct. 9, 2019, 9:19 a.m. UTC | #1
On Tue, Oct 8, 2019 at 3:47 PM Lucas Oshiro <lucasseikioshiro@gmail.com> wrote:
>
> git tag --edit and --no-edit flags are not currently fully supported.
> This patch fixes the functionality that allows the editor to be opened
> on demand.
>
> Co-authored-by: Bárbara Fernandes <barbara.dcf@gmail.com>
> Signed-off-by: Lucas Oshiro <lucasseikioshiro@gmail.com>
> Signed-off-by: Bárbara Fernandes <barbara.dcf@gmail.com>
> ---
>  builtin/tag.c  | 16 +++++++++++++---
>  t/t7004-tag.sh |  4 ++--
>  2 files changed, 15 insertions(+), 5 deletions(-)
>
> diff --git a/builtin/tag.c b/builtin/tag.c
> index 0322bdbdfb..7dff61d45a 100644
> --- a/builtin/tag.c
> +++ b/builtin/tag.c
> @@ -230,6 +230,7 @@ static int build_tag_object(struct strbuf *buf, int sign, struct object_id *resu
>  struct create_tag_options {
>         unsigned int message_given:1;
>         unsigned int use_editor:1;
> +       unsigned int force_editor:1;

What if we turn 'use_editor' into a tri-state variable (--edit,
--no-edit and "nothing given") instead of adding this field? Maybe it
would simplify some condition checks at create_tag().

>         unsigned int sign;
>         enum {
>                 CLEANUP_NONE,
> @@ -307,13 +308,21 @@ static void create_tag(const struct object_id *object, const char *object_ref,
>                     tag,
>                     git_committer_info(IDENT_STRICT));
>
> -       if (!opt->message_given || opt->use_editor) {
> +       if (opt->force_editor && !opt->message_given && is_null_oid(prev) &&
> +           !opt->use_editor) {
> +               die(_("no tag message?"));
> +       } else if ((!opt->force_editor && !opt->message_given && is_null_oid(prev))
> +                 || (opt->force_editor && opt->use_editor)) {
> +               /* Editor must be opened */
>                 prepare_tag_template(buf, opt, prev, path, tag);
>                 if (launch_editor(path, buf, NULL)) {
>                         fprintf(stderr,
>                         _("Please supply the message using either -m or -F option.\n"));
>                         exit(1);
>                 }
> +       } else if (!opt->message_given) {
> +               /* Tag already exists and user doesn't want to change it */
> +               strbuf_addstr(buf, get_tag_body(prev, NULL));
>         }
>
>         if (opt->cleanup_mode != CLEANUP_NONE)
> @@ -436,7 +445,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
>         static struct ref_sorting *sorting = NULL, **sorting_tail = &sorting;
>         struct ref_format format = REF_FORMAT_INIT;
>         int icase = 0;
> -       int edit_flag = 0;
> +       int edit_flag = -1;
>         struct option options[] = {
>                 OPT_CMDMODE('l', "list", &cmdmode, N_("list tag names"), 'l'),
>                 { OPTION_INTEGER, 'n', NULL, &filter.lines, N_("n"),
> @@ -592,7 +601,8 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
>                 die(_("tag '%s' already exists"), tag);
>
>         opt.message_given = msg.given || msgfile;
> -       opt.use_editor = edit_flag;
> +       opt.force_editor = edit_flag >= 0;
> +       opt.use_editor = opt.force_editor ? edit_flag : 0;
>
>         if (!cleanup_arg || !strcmp(cleanup_arg, "strip"))
>                 opt.cleanup_mode = CLEANUP_ALL;
> diff --git a/t/t7004-tag.sh b/t/t7004-tag.sh
> index 80eb13d94e..bf43d2c750 100755
> --- a/t/t7004-tag.sh
> +++ b/t/t7004-tag.sh
> @@ -1313,7 +1313,7 @@ test_expect_success GPG,RFC1991 \
>         'reediting a signed tag body omits signature' '
>         echo "rfc1991" >gpghome/gpg.conf &&
>         echo "RFC1991 signed tag" >expect &&
> -       GIT_EDITOR=./fakeeditor git tag -f -s rfc1991-signed-tag $commit &&
> +       GIT_EDITOR=./fakeeditor git tag -f --edit -s rfc1991-signed-tag $commit &&
>         test_cmp expect actual
>  '
>
> @@ -1356,7 +1356,7 @@ test_expect_success GPG,RFC1991 \
>  test_expect_success GPG,RFC1991 \
>         'reediting a signed tag body omits signature' '
>         echo "RFC1991 signed tag" >expect &&
> -       GIT_EDITOR=./fakeeditor git tag -f -s rfc1991-signed-tag $commit &&
> +       GIT_EDITOR=./fakeeditor git tag -f --edit -s rfc1991-signed-tag $commit &&
>         test_cmp expect actual
>  '
>
> --
> 2.23.0
>
Junio C Hamano Oct. 10, 2019, 3:34 a.m. UTC | #2
Lucas Oshiro <lucasseikioshiro@gmail.com> writes:

>  struct create_tag_options {
>  	unsigned int message_given:1;
>  	unsigned int use_editor:1;
> +	unsigned int force_editor:1;
>  	unsigned int sign;

> -	if (!opt->message_given || opt->use_editor) {
> +	if (opt->force_editor && !opt->message_given && is_null_oid(prev) &&
> +	    !opt->use_editor) {
> +		die(_("no tag message?"));

If we didn't get a message from command line, and there is no
previous tag object to read the message from, there is nowhere but
editor to grab the message to be used, so !use_editor would trigger
an error if force_editor (i.e. the command line explicitly said
either --edit or --no-edit).  Makes sense, but I needed to cheat and
look at cmd_tag() to see how "force" is set to understand what is
going on.  I have to say "force" is not a good name for this field;
is this use similar to what we typically use an additional _given
field?

> +	} else if ((!opt->force_editor && !opt->message_given && is_null_oid(prev))
> +		  || (opt->force_editor && opt->use_editor)) {
> +		/* Editor must be opened */

If there is no --[no-]edit and there is no preexisting message, we
need to use the editor.  If the command line explicitly said --edit,
we also would use the editor.  OK.

But it starts to make me wonder if you rather want to replace the
single bit use_editor field with an enum with three possible values
(enum { EDITOR_UNSPECIFIED, EDITOR_YES, EDITOR_NO } use_editor).

>  		prepare_tag_template(buf, opt, prev, path, tag);
>  		if (launch_editor(path, buf, NULL)) {
>  			fprintf(stderr,
>  			_("Please supply the message using either -m or -F option.\n"));
>  			exit(1);
>  		}
> +	} else if (!opt->message_given) {
> +		/* Tag already exists and user doesn't want to change it */

Are we certain at this point in if/else cascade that prev is a valid
tag?  How?

> +		strbuf_addstr(buf, get_tag_body(prev, NULL));

This NULL tells us something about what I mentioned in my review on 1/3.


> diff --git a/t/t7004-tag.sh b/t/t7004-tag.sh
> index 80eb13d94e..bf43d2c750 100755
> --- a/t/t7004-tag.sh
> +++ b/t/t7004-tag.sh
> @@ -1313,7 +1313,7 @@ test_expect_success GPG,RFC1991 \
>  	'reediting a signed tag body omits signature' '
>  	echo "rfc1991" >gpghome/gpg.conf &&
>  	echo "RFC1991 signed tag" >expect &&
> -	GIT_EDITOR=./fakeeditor git tag -f -s rfc1991-signed-tag $commit &&
> +	GIT_EDITOR=./fakeeditor git tag -f --edit -s rfc1991-signed-tag $commit &&
>  	test_cmp expect actual
>  '
>  
> @@ -1356,7 +1356,7 @@ test_expect_success GPG,RFC1991 \
>  test_expect_success GPG,RFC1991 \
>  	'reediting a signed tag body omits signature' '
>  	echo "RFC1991 signed tag" >expect &&
> -	GIT_EDITOR=./fakeeditor git tag -f -s rfc1991-signed-tag $commit &&
> +	GIT_EDITOR=./fakeeditor git tag -f --edit -s rfc1991-signed-tag $commit &&
>  	test_cmp expect actual
>  '

Why do these two need explicit --edit option to invoke the editor?
That smells like an unnecessary backward incompatible change.

Thanks.
diff mbox series

Patch

diff --git a/builtin/tag.c b/builtin/tag.c
index 0322bdbdfb..7dff61d45a 100644
--- a/builtin/tag.c
+++ b/builtin/tag.c
@@ -230,6 +230,7 @@  static int build_tag_object(struct strbuf *buf, int sign, struct object_id *resu
 struct create_tag_options {
 	unsigned int message_given:1;
 	unsigned int use_editor:1;
+	unsigned int force_editor:1;
 	unsigned int sign;
 	enum {
 		CLEANUP_NONE,
@@ -307,13 +308,21 @@  static void create_tag(const struct object_id *object, const char *object_ref,
 		    tag,
 		    git_committer_info(IDENT_STRICT));
 
-	if (!opt->message_given || opt->use_editor) {
+	if (opt->force_editor && !opt->message_given && is_null_oid(prev) &&
+	    !opt->use_editor) {
+		die(_("no tag message?"));
+	} else if ((!opt->force_editor && !opt->message_given && is_null_oid(prev))
+		  || (opt->force_editor && opt->use_editor)) {
+		/* Editor must be opened */
 		prepare_tag_template(buf, opt, prev, path, tag);
 		if (launch_editor(path, buf, NULL)) {
 			fprintf(stderr,
 			_("Please supply the message using either -m or -F option.\n"));
 			exit(1);
 		}
+	} else if (!opt->message_given) {
+		/* Tag already exists and user doesn't want to change it */
+		strbuf_addstr(buf, get_tag_body(prev, NULL));
 	}
 
 	if (opt->cleanup_mode != CLEANUP_NONE)
@@ -436,7 +445,7 @@  int cmd_tag(int argc, const char **argv, const char *prefix)
 	static struct ref_sorting *sorting = NULL, **sorting_tail = &sorting;
 	struct ref_format format = REF_FORMAT_INIT;
 	int icase = 0;
-	int edit_flag = 0;
+	int edit_flag = -1;
 	struct option options[] = {
 		OPT_CMDMODE('l', "list", &cmdmode, N_("list tag names"), 'l'),
 		{ OPTION_INTEGER, 'n', NULL, &filter.lines, N_("n"),
@@ -592,7 +601,8 @@  int cmd_tag(int argc, const char **argv, const char *prefix)
 		die(_("tag '%s' already exists"), tag);
 
 	opt.message_given = msg.given || msgfile;
-	opt.use_editor = edit_flag;
+	opt.force_editor = edit_flag >= 0;
+	opt.use_editor = opt.force_editor ? edit_flag : 0;
 
 	if (!cleanup_arg || !strcmp(cleanup_arg, "strip"))
 		opt.cleanup_mode = CLEANUP_ALL;
diff --git a/t/t7004-tag.sh b/t/t7004-tag.sh
index 80eb13d94e..bf43d2c750 100755
--- a/t/t7004-tag.sh
+++ b/t/t7004-tag.sh
@@ -1313,7 +1313,7 @@  test_expect_success GPG,RFC1991 \
 	'reediting a signed tag body omits signature' '
 	echo "rfc1991" >gpghome/gpg.conf &&
 	echo "RFC1991 signed tag" >expect &&
-	GIT_EDITOR=./fakeeditor git tag -f -s rfc1991-signed-tag $commit &&
+	GIT_EDITOR=./fakeeditor git tag -f --edit -s rfc1991-signed-tag $commit &&
 	test_cmp expect actual
 '
 
@@ -1356,7 +1356,7 @@  test_expect_success GPG,RFC1991 \
 test_expect_success GPG,RFC1991 \
 	'reediting a signed tag body omits signature' '
 	echo "RFC1991 signed tag" >expect &&
-	GIT_EDITOR=./fakeeditor git tag -f -s rfc1991-signed-tag $commit &&
+	GIT_EDITOR=./fakeeditor git tag -f --edit -s rfc1991-signed-tag $commit &&
 	test_cmp expect actual
 '