Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Outreachy] merge-ours: parse-option #425

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Georgecanfly
Copy link

@Georgecanfly Georgecanfly commented Oct 25, 2019

[Outreachy] merge-ours: include parse-options

This command currently handles its own argv so by teaching it to
use parse-options instead we can standardize the way commands
handle user input across the project. ex. -h.
NO_PARSEOPT flag was also removed to update git.c with the
conversion of the structure in this command.

Helped by: emily shaffer [email protected]
Helped by: johannes schindelin [email protected]

Signed-off-by: george espinoza [email protected]

@Georgecanfly Georgecanfly changed the title merge-ours: include a parse-option [Outreachy] merge-ours: include a parse-option Oct 25, 2019
dscho
dscho previously requested changes Oct 25, 2019
Copy link
Member

@dscho dscho left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks pretty good for starters!

If I were you, I would also try to make sure that every test that contains the word ours passes:

make -j$(nproc)
cd t
make -j$(nproc) $(git grep -nw ours t[0-9]*.sh)

builtin/merge-ours.c Outdated Show resolved Hide resolved
builtin/merge-ours.c Outdated Show resolved Hide resolved
builtin/merge-ours.c Outdated Show resolved Hide resolved
builtin/merge-ours.c Outdated Show resolved Hide resolved
@nasamuffin
Copy link

Yeah, this looks like it's lost the interesting part of your commit. All this PR consists of now is a header add.

@Georgecanfly
Copy link
Author

Georgecanfly commented Oct 28, 2019

Ok, I was able to push again thank you. Here I decided to revert my extreme first edits and start one bit a a time because I think I was breaking more things than fixing right away. I looked over both your comments very closely over the weekend and thought of different ways to implement all of the advice.
I adopted the first string usage completely and threw away the usage I had created.
I wanted to fix and include:

static const char * const merge_ours_usage[] = {
	N_("git merge-ours [<base>...] -- <head> <merge-head>..."),
	NULL,
};

but I thought that the line that was already there was making this redundant and if i deleted the original one I got an error saying "builtin.h" was expecting:

static const char builtin_merge_ours_usage[]

so I left the one that was working. Unless I could just include both, with the first one I submitted being fixed.

I deleted this since there weren't any options being used, but I really thought of leaving it, which is why Im starting one step at a time instead.

	struct option options[] = {
		OPT_END()
	};

I also wanted to include:
argc = parse_options(argc, argv, prefix, options, merge-ours_usage, 0);

but I wasn't sure if that was me doing manual argv parsing (the wrong way, per the project description).
or if that was me giving more than the expected number of arguments after option parsing is 0.

I also wanted to delete since maybe this is what is doing the check for -h:


        if (argc == 2 && !strcmp(argv[1], "-h"))
                usage(builtin_merge_ours_usage);

but I wasn't sure if touching that would break something else. I had initially deleted -h by just doing


        if (argc == 2 && !strcmp(argv[1], " "))
                usage(builtin_merge_ours_usage);

But I got a test error so I decided to leave it in again. I wasn't sure how else to remove the check for -h.

@nasamuffin
Copy link

Ok, I was able to push again thank you. Here I decided to revert my extreme first edits and start one bit a a time because I think I was breaking more things than fixing right away. I looked over both your comments very closely over the weekend and thought of different ways to implement all of the advice.
I adopted the first string usage completely and threw away the usage I had created.
I wanted to fix and include:

static const char * const merge_ours_usage[] = {
	N_("git merge-ours [<base>...] -- <head> <merge-head>..."),
	NULL,
};

but I thought that the line that was already there was making this redundant and if i deleted the original one I got an error saying "builtin.h" was expecting:

static const char builtin_merge_ours_usage[]

It helps if you can share us the exact error so we can give a better hint.

so I left the one that was working. Unless I could just include both, with the first one I submitted being fixed.

I deleted this since there weren't any options being used, but I really thought of leaving it, which is why Im starting one step at a time instead.

	struct option options[] = {
		OPT_END()
	};

Don't delete this, the parse_options call needs it and the point is to convert to using this kind of structure.

I also wanted to include:
argc = parse_options(argc, argv, prefix, options, merge-ours_usage, 0);

but I wasn't sure if that was me doing manual argv parsing (the wrong way, per the project description).
or if that was me giving more than the expected number of arguments after option parsing is 0.

Using this line is the ask for this microproject. You should be using this line.

I also wanted to delete since maybe this is what is doing the check for -h:


        if (argc == 2 && !strcmp(argv[1], "-h"))
                usage(builtin_merge_ours_usage);

This part is the manual argv parsing. You can see we are doing a string compare on a member of argv dereferenced with a magic number, which is precisely what we mean by "manual parse".

but I wasn't sure if touching that would break something else. I had initially deleted -h by just doing


        if (argc == 2 && !strcmp(argv[1], " "))
                usage(builtin_merge_ours_usage);

But I got a test error so I decided to leave it in again. I wasn't sure how else to remove the check for -h.

-h should be handled by the call to parse-options. You may wish to remove in git.c where NO_PARSEOPT is passed as a flag to the merge-ours registration - I imagine that has to do with the failure you're seeing.

@dscho
Copy link
Member

dscho commented Oct 28, 2019

Oh, I also remembered another thing: parse_options() will "eat" a double-dash if it sees one. This can happen when trying to merge unrelated histories, in which case there is no merge base.

So you definitely want to pass the flag to parse_options() where it keeps the double-dash.

@Georgecanfly
Copy link
Author

Ok, I think this might be better :)
After the weekend's trials and errors and today's extra comments gave me more of a revelation on how to go about this. I went ahead and did a make and did a test after the changes.

@Georgecanfly
Copy link
Author

As I typed this I do see some CI failures. So maybe I'll rebuild and re-test.

@Georgecanfly
Copy link
Author

I have to admit that I did make and prove before changing git.c.

Now that I'm running make Im seeing this error:

builtin/merge-ours.c: In function ‘cmd_merge_ours’:
builtin/merge-ours.c:22:16: error: array type has incomplete element type ‘struct option’
22 | struct option options[] = {
| ^~~~~~~
builtin/merge-ours.c:23:3: error: implicit declaration of function ‘OPT_END’; did you mean ‘EXT_END’? [-Werror=implicit-function-declaration]
23 | OPT_END()
| ^~~~~~~
| EXT_END
builtin/merge-ours.c:26:9: error: implicit declaration of function ‘parse_options’; did you mean ‘SSL_set_options’? [-Werror=implicit-function-declaration]
26 | argc = parse_options(argc, argv, prefix, options, merge_ours_usage, 0);
| ^~~~~~~~~~~~~
| SSL_set_options
builtin/merge-ours.c:22:16: error: unused variable ‘options’ [-Werror=unused-variable]
22 | struct option options[] = {
| ^~~~~~~
cc1: all warnings being treated as errors
make: *** [Makefile:2378: builtin/merge-ours.o] Error 1

Is is because I removed:
| NO_PARSEOPT }

from

{ "merge-ours", cmd_merge_ours, RUN_SETUP | NO_PARSEOPT },

@Georgecanfly
Copy link
Author

Oh, that's really weird. It deleted the #include "parse-options.h" and my comment.
While I was building I did see something that said rewind.
Ok I'll add that again.

@Georgecanfly
Copy link
Author

I think i fixed it finally :) let me know what you both think. @nasamuffin @dscho Thank you so much for your help so far! I'm starting to feel more confident in adding parse-options to commands.

@nasamuffin
Copy link

My opinion is that you should:

  1. try and make the commit message conform better to Git's style, which is to focus on the "why" rather than the what, and then
  2. send it to the list, because the code looks ready, assuming the tests all pass nicely.

For 1), I suggest you read through some of the other commits (besides Junio's merge commits) to get a good idea of what they're trying to convey. A good rule is that you shouldn't just summarize your diff - you probably want to explain why you made the diff you did. So it's not enough to say "I made merge-ours use parse-options" - you want to say something like "...because parse-options helps make sure we handle user input like -h in a standardized way across the project". And rather than saying "I removed NO_PARSEOPT from git.c too" you can say "Since merge-ours now uses parse-options, update git.c accordingly" (note that now it says why you removed NO_PARSEOPT).

@Georgecanfly
Copy link
Author

ok @nasamuffin! Thank you :)

@Georgecanfly
Copy link
Author

/submit

@gitgitgadget
Copy link

gitgitgadget bot commented Oct 28, 2019

Submitted as [email protected]

@@ -11,14 +11,20 @@
#include "git-compat-util.h"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On the Git mailing list, Emily Shaffer wrote (reply to this):

On Mon, Oct 28, 2019 at 11:42:29PM +0000, george espinoza via GitGitGadget wrote:
> From: george espinoza <[email protected]>
> 
> Teach this command which currently handles its own argv to use
> parse-options instead because parse-options helps make sure we handle
> user input like -h in a standardized way across the project.
> Also deleted the NO_PARSEOPT flag from git.c to coincide with
> the conversion of the structure in this command since merge-ours
> now uses parse-options and needed to update git.c accordingly.

Hmm, I could still wish for some rephrasing on this commit message. Now
that you took my example suggestions and pasted them directly into your
previous sentences, it doesn't flow very nicely. The point comes across
but it's a little redundant (for example, "also <verb> from git.c ....
and needed to update git.c" is redundant).

When significant assistance and advice is received it's often good form
to include a "Helped-by:" line which looks similar to the signoff line,
to provide credit. Maybe you will consider it as myself and dscho spent
quite some time helping you in the GitGitGadget PR as well as via
IRC/mail? :)

Otherwise, the code looks OK to me.

 - Emily

> 
> Signed-off-by: george espinoza <[email protected]>
> ---
>  builtin/merge-ours.c | 14 ++++++++++----
>  git.c                |  2 +-
>  2 files changed, 11 insertions(+), 5 deletions(-)
> 
> diff --git a/builtin/merge-ours.c b/builtin/merge-ours.c
> index 4594507420..fb3674a384 100644
> --- a/builtin/merge-ours.c
> +++ b/builtin/merge-ours.c
> @@ -11,14 +11,20 @@
>  #include "git-compat-util.h"
>  #include "builtin.h"
>  #include "diff.h"
> +#include "parse-options.h"
>  
> -static const char builtin_merge_ours_usage[] =
> -	"git merge-ours <base>... -- HEAD <remote>...";
> +static const char * const merge_ours_usage[] = {
> +	N_("git merge-ours [<base>...] -- <head> <merge-head>..."),
> +	NULL,
> +};
>  
>  int cmd_merge_ours(int argc, const char **argv, const char *prefix)
>  {
> -	if (argc == 2 && !strcmp(argv[1], "-h"))
> -		usage(builtin_merge_ours_usage);
> +	struct option options[] = {
> +		OPT_END()
> +	};
> +
> +	argc = parse_options(argc, argv, prefix, options, merge_ours_usage, 0);
>  
>  	/*
>  	 * The contents of the current index becomes the tree we
> diff --git a/git.c b/git.c
> index ce6ab0ece2..6aee0e9477 100644
> --- a/git.c
> +++ b/git.c
> @@ -527,7 +527,7 @@ static struct cmd_struct commands[] = {
>  	{ "merge-base", cmd_merge_base, RUN_SETUP },
>  	{ "merge-file", cmd_merge_file, RUN_SETUP_GENTLY },
>  	{ "merge-index", cmd_merge_index, RUN_SETUP | NO_PARSEOPT },
> -	{ "merge-ours", cmd_merge_ours, RUN_SETUP | NO_PARSEOPT },
> +	{ "merge-ours", cmd_merge_ours, RUN_SETUP },
>  	{ "merge-recursive", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE | NO_PARSEOPT },
>  	{ "merge-recursive-ours", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE | NO_PARSEOPT },
>  	{ "merge-recursive-theirs", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE | NO_PARSEOPT },
> -- 
> gitgitgadget

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, my sincerest apologies. I should have been more thorough in evaluating the importance of a clean original commit message before submitting it. I will certainly keep all of that in mind for this project and any future projects and contributions. I will edit it accordingly to your advice and include credit for the substantial and significant assistance you and dscho provided. :) ty.

-George

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On the Git mailing list, George Espinoza wrote (reply to this):

On Tue, Oct 29, 2019 at 1:42 PM Emily Shaffer <[email protected]> wrote:
>
> On Mon, Oct 28, 2019 at 11:42:29PM +0000, george espinoza via GitGitGadget wrote:
> > From: george espinoza <[email protected]>
> >
> > Teach this command which currently handles its own argv to use
> > parse-options instead because parse-options helps make sure we handle
> > user input like -h in a standardized way across the project.
> > Also deleted the NO_PARSEOPT flag from git.c to coincide with
> > the conversion of the structure in this command since merge-ours
> > now uses parse-options and needed to update git.c accordingly.
>
> Hmm, I could still wish for some rephrasing on this commit message. Now
> that you took my example suggestions and pasted them directly into your
> previous sentences, it doesn't flow very nicely. The point comes across
> but it's a little redundant (for example, "also <verb> from git.c ....
> and needed to update git.c" is redundant).
>
> When significant assistance and advice is received it's often good form
> to include a "Helped-by:" line which looks similar to the signoff line,
> to provide credit. Maybe you will consider it as myself and dscho spent
> quite some time helping you in the GitGitGadget PR as well as via
> IRC/mail? :)
>
> Otherwise, the code looks OK to me.
>
>  - Emily
>
> >
> > Signed-off-by: george espinoza <[email protected]>
> > ---
> >  builtin/merge-ours.c | 14 ++++++++++----
> >  git.c                |  2 +-
> >  2 files changed, 11 insertions(+), 5 deletions(-)
> >
> > diff --git a/builtin/merge-ours.c b/builtin/merge-ours.c
> > index 4594507420..fb3674a384 100644
> > --- a/builtin/merge-ours.c
> > +++ b/builtin/merge-ours.c
> > @@ -11,14 +11,20 @@
> >  #include "git-compat-util.h"
> >  #include "builtin.h"
> >  #include "diff.h"
> > +#include "parse-options.h"
> >
> > -static const char builtin_merge_ours_usage[] =
> > -     "git merge-ours <base>... -- HEAD <remote>...";
> > +static const char * const merge_ours_usage[] = {
> > +     N_("git merge-ours [<base>...] -- <head> <merge-head>..."),
> > +     NULL,
> > +};
> >
> >  int cmd_merge_ours(int argc, const char **argv, const char *prefix)
> >  {
> > -     if (argc == 2 && !strcmp(argv[1], "-h"))
> > -             usage(builtin_merge_ours_usage);
> > +     struct option options[] = {
> > +             OPT_END()
> > +     };
> > +
> > +     argc = parse_options(argc, argv, prefix, options, merge_ours_usage, 0);
> >
> >       /*
> >        * The contents of the current index becomes the tree we
> > diff --git a/git.c b/git.c
> > index ce6ab0ece2..6aee0e9477 100644
> > --- a/git.c
> > +++ b/git.c
> > @@ -527,7 +527,7 @@ static struct cmd_struct commands[] = {
> >       { "merge-base", cmd_merge_base, RUN_SETUP },
> >       { "merge-file", cmd_merge_file, RUN_SETUP_GENTLY },
> >       { "merge-index", cmd_merge_index, RUN_SETUP | NO_PARSEOPT },
> > -     { "merge-ours", cmd_merge_ours, RUN_SETUP | NO_PARSEOPT },
> > +     { "merge-ours", cmd_merge_ours, RUN_SETUP },
> >       { "merge-recursive", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE | NO_PARSEOPT },
> >       { "merge-recursive-ours", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE | NO_PARSEOPT },
> >       { "merge-recursive-theirs", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE | NO_PARSEOPT },
> > --
> > gitgitgadget

Ah, my sincerest apologies. I should have been more thorough in
evaluating the importance of a clean original commit message before
submitting it. I will certainly keep all of that in mind for this
project and any future projects and contributions. I will edit it
accordingly to your advice and include credit for the substantial and
significant assistance you and dscho provided. :) ty.

-George

This command currently handles its own argv so by teaching it to
use parse-options instead we can standardize the way commands
handle user input across the project. ex. -h.
NO_PARSEOPT flag was also removed to update git.c with the
conversion of the structure in this command.

Helped by: emily shaffer <[email protected]>
Helped by: johannes schindelin <[email protected]>

Signed-off-by: george espinoza <[email protected]>
@Georgecanfly
Copy link
Author

Georgecanfly commented Oct 29, 2019 via email

@dscho dscho dismissed their stale review October 30, 2019 14:16

force-pushed version; review no longer applies.

@Georgecanfly Georgecanfly changed the title [Outreachy] merge-ours: include a parse-option [Outreachy] merge-ours: iparse-option Nov 5, 2019
@Georgecanfly Georgecanfly changed the title [Outreachy] merge-ours: iparse-option [Outreachy] merge-ours: parse-option Nov 5, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants