-
Notifications
You must be signed in to change notification settings - Fork 145
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
Two fixes for the built-in git add -i
#528
Two fixes for the built-in git add -i
#528
Conversation
When the user does not select any files to `patch` or `diff`, there is no need to call `run_add_p()` on them. Even worse: we _have_ to avoid calling `parse_pathspec()` with an empty list because that would trigger this error: BUG: pathspec.c:557: PATHSPEC_PREFER_CWD requires arguments So let's avoid doing any work on a list of files that is empty anyway. This fixes git-for-windows#2466. Signed-off-by: Johannes Schindelin <[email protected]>
The interactive `add` command allows selecting multiple files for some of its sub-commands, via unique prefixes, indices or index ranges. When re-implementing `git add -i` in C, we even added a code comment talking about ranges with a missing end index, such as `2-`, but the code did not actually accept those, as pointed out in git-for-windows#2466 (comment). Let's fix this, and add a test case to verify that this stays fixed forever. Signed-off-by: Johannes Schindelin <[email protected]>
/submit |
Submitted as [email protected] |
@@ -915,7 +915,7 @@ static int run_patch(struct add_i_state *s, const struct pathspec *ps, | |||
|
There was a problem hiding this comment.
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, Junio C Hamano wrote (reply to this):
"Johannes Schindelin via GitGitGadget" <[email protected]>
writes:
> From: Johannes Schindelin <[email protected]>
>
> When the user does not select any files to `patch` or `diff`, there is
> no need to call `run_add_p()` on them.
>
> Even worse: we _have_ to avoid calling `parse_pathspec()` with an empty
> list because that would trigger this error:
>
> BUG: pathspec.c:557: PATHSPEC_PREFER_CWD requires arguments
>
> So let's avoid doing any work on a list of files that is empty anyway.
>
> This fixes https://github.com/git-for-windows/git/issues/2466.
>
> Signed-off-by: Johannes Schindelin <[email protected]>
> ---
> add-interactive.c | 4 ++--
> 1 file changed, 2 insertions(+), 2 deletions(-)
Makes sense. No tests?
>
> diff --git a/add-interactive.c b/add-interactive.c
> index f395d54c08..14d4688c26 100644
> --- a/add-interactive.c
> +++ b/add-interactive.c
> @@ -915,7 +915,7 @@ static int run_patch(struct add_i_state *s, const struct pathspec *ps,
>
> opts->prompt = N_("Patch update");
> count = list_and_choose(s, files, opts);
> - if (count >= 0) {
> + if (count > 0) {
> struct argv_array args = ARGV_ARRAY_INIT;
>
> argv_array_pushl(&args, "git", "add--interactive", "--patch",
> @@ -953,7 +953,7 @@ static int run_diff(struct add_i_state *s, const struct pathspec *ps,
> opts->flags = IMMEDIATE;
> count = list_and_choose(s, files, opts);
> opts->flags = 0;
> - if (count >= 0) {
> + if (count > 0) {
> struct argv_array args = ARGV_ARRAY_INIT;
>
> argv_array_pushl(&args, "git", "diff", "-p", "--cached",
There was a problem hiding this comment.
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, Johannes Schindelin wrote (reply to this):
Hi Junio,
On Thu, 16 Jan 2020, Junio C Hamano wrote:
> "Johannes Schindelin via GitGitGadget" <[email protected]>
> writes:
>
> > From: Johannes Schindelin <[email protected]>
> >
> > When the user does not select any files to `patch` or `diff`, there is
> > no need to call `run_add_p()` on them.
> >
> > Even worse: we _have_ to avoid calling `parse_pathspec()` with an empty
> > list because that would trigger this error:
> >
> > BUG: pathspec.c:557: PATHSPEC_PREFER_CWD requires arguments
> >
> > So let's avoid doing any work on a list of files that is empty anyway.
> >
> > This fixes https://github.com/git-for-windows/git/issues/2466.
> >
> > Signed-off-by: Johannes Schindelin <[email protected]>
> > ---
> > add-interactive.c | 4 ++--
> > 1 file changed, 2 insertions(+), 2 deletions(-)
>
> Makes sense. No tests?
Do we really need tests here? ;-)
Honestly, I would deem the likelihood of this to get broken again very
low, and the effort for writing a regression test relatively high...
If you insist, I will add some, of course, but not today.
Ciao,
Dscho
>
> >
> > diff --git a/add-interactive.c b/add-interactive.c
> > index f395d54c08..14d4688c26 100644
> > --- a/add-interactive.c
> > +++ b/add-interactive.c
> > @@ -915,7 +915,7 @@ static int run_patch(struct add_i_state *s, const struct pathspec *ps,
> >
> > opts->prompt = N_("Patch update");
> > count = list_and_choose(s, files, opts);
> > - if (count >= 0) {
> > + if (count > 0) {
> > struct argv_array args = ARGV_ARRAY_INIT;
> >
> > argv_array_pushl(&args, "git", "add--interactive", "--patch",
> > @@ -953,7 +953,7 @@ static int run_diff(struct add_i_state *s, const struct pathspec *ps,
> > opts->flags = IMMEDIATE;
> > count = list_and_choose(s, files, opts);
> > opts->flags = 0;
> > - if (count >= 0) {
> > + if (count > 0) {
> > struct argv_array args = ARGV_ARRAY_INIT;
> >
> > argv_array_pushl(&args, "git", "diff", "-p", "--cached",
>
@@ -328,7 +328,10 @@ static ssize_t list_and_choose(struct add_i_state *s, | |||
if (endp == p + sep) |
There was a problem hiding this comment.
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, Junio C Hamano wrote (reply to this):
"Johannes Schindelin via GitGitGadget" <[email protected]>
writes:
> From: Johannes Schindelin <[email protected]>
>
> The interactive `add` command allows selecting multiple files for some
> of its sub-commands, via unique prefixes, indices or index ranges.
>
> When re-implementing `git add -i` in C, we even added a code comment
> talking about ranges with a missing end index, such as `2-`, but the
> code did not actually accept those, as pointed out in
> https://github.com/git-for-windows/git/issues/2466#issuecomment-574142760.
>
> Let's fix this, and add a test case to verify that this stays fixed
> forever.
>
> Signed-off-by: Johannes Schindelin <[email protected]>
> ---
> add-interactive.c | 5 ++++-
> t/t3701-add-interactive.sh | 9 +++++++++
> 2 files changed, 13 insertions(+), 1 deletion(-)
>
> diff --git a/add-interactive.c b/add-interactive.c
> index 14d4688c26..396066e724 100644
> --- a/add-interactive.c
> +++ b/add-interactive.c
> @@ -328,7 +328,10 @@ static ssize_t list_and_choose(struct add_i_state *s,
> if (endp == p + sep)
> to = from + 1;
> else if (*endp == '-') {
> - to = strtoul(++endp, &endp, 10);
> + if (isdigit(*(++endp)))
> + to = strtoul(endp, &endp, 10);
> + else
> + to = items->items.nr;
Good. We do not allow "everything up to N" with "-N", so covering
"N and everything after" with "N-" is sufficient.
> diff --git a/t/t3701-add-interactive.sh b/t/t3701-add-interactive.sh
> index d4f9386621..b02fe73631 100755
> --- a/t/t3701-add-interactive.sh
> +++ b/t/t3701-add-interactive.sh
> @@ -57,6 +57,15 @@ test_expect_success 'revert works (initial)' '
> ! grep . output
> '
>
> +test_expect_success 'add untracked (multiple)' '
> + test_when_finished "git reset && rm [1-9]" &&
> + touch $(test_seq 9) &&
> + test_write_lines a "2-5 8-" | git add -i -- [1-9] &&
> + test_write_lines 2 3 4 5 8 9 >expected &&
> + git ls-files [1-9] >output &&
> + test_cmp expected output
> +'
> +
> test_expect_success 'setup (commit)' '
> echo baseline >file &&
> git add file &&
There was a problem hiding this comment.
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, Johannes Schindelin wrote (reply to this):
Hi Junio,
On Thu, 16 Jan 2020, Junio C Hamano wrote:
> "Johannes Schindelin via GitGitGadget" <[email protected]>
> writes:
>
> > From: Johannes Schindelin <[email protected]>
> >
> > The interactive `add` command allows selecting multiple files for some
> > of its sub-commands, via unique prefixes, indices or index ranges.
> >
> > When re-implementing `git add -i` in C, we even added a code comment
> > talking about ranges with a missing end index, such as `2-`, but the
> > code did not actually accept those, as pointed out in
> > https://github.com/git-for-windows/git/issues/2466#issuecomment-574142760.
> >
> > Let's fix this, and add a test case to verify that this stays fixed
> > forever.
> >
> > Signed-off-by: Johannes Schindelin <[email protected]>
> > ---
> > add-interactive.c | 5 ++++-
> > t/t3701-add-interactive.sh | 9 +++++++++
> > 2 files changed, 13 insertions(+), 1 deletion(-)
> >
> > diff --git a/add-interactive.c b/add-interactive.c
> > index 14d4688c26..396066e724 100644
> > --- a/add-interactive.c
> > +++ b/add-interactive.c
> > @@ -328,7 +328,10 @@ static ssize_t list_and_choose(struct add_i_state *s,
> > if (endp == p + sep)
> > to = from + 1;
> > else if (*endp == '-') {
> > - to = strtoul(++endp, &endp, 10);
> > + if (isdigit(*(++endp)))
> > + to = strtoul(endp, &endp, 10);
> > + else
> > + to = items->items.nr;
>
> Good. We do not allow "everything up to N" with "-N", so covering
> "N and everything after" with "N-" is sufficient.
Even worse, `-N` means "toggle N off". But that can't be fixed easily as
it has been part of the UI for ages.
Thanks,
Dscho
>
> > diff --git a/t/t3701-add-interactive.sh b/t/t3701-add-interactive.sh
> > index d4f9386621..b02fe73631 100755
> > --- a/t/t3701-add-interactive.sh
> > +++ b/t/t3701-add-interactive.sh
> > @@ -57,6 +57,15 @@ test_expect_success 'revert works (initial)' '
> > ! grep . output
> > '
> >
> > +test_expect_success 'add untracked (multiple)' '
> > + test_when_finished "git reset && rm [1-9]" &&
> > + touch $(test_seq 9) &&
> > + test_write_lines a "2-5 8-" | git add -i -- [1-9] &&
> > + test_write_lines 2 3 4 5 8 9 >expected &&
> > + git ls-files [1-9] >output &&
> > + test_cmp expected output
> > +'
> > +
> > test_expect_success 'setup (commit)' '
> > echo baseline >file &&
> > git add file &&
>
There was a problem hiding this comment.
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, Junio C Hamano wrote (reply to this):
Johannes Schindelin <[email protected]> writes:
>> Good. We do not allow "everything up to N" with "-N", so covering
>> "N and everything after" with "N-" is sufficient.
>
> Even worse, `-N` means "toggle N off". But that can't be fixed easily as
> it has been part of the UI for ages.
There is no loss. "N-" is useful because you do not necessarily
know what the maximum is, but "everything up to N" will always be
"1-N" and you do not know what the minimum is.
So, there is not even worse there at all ;-)
This patch series was integrated into pu via git@22153c3. |
This branch is now known as |
This patch series was integrated into pu via git@441c155. |
This patch series was integrated into next via git@3cf944a. |
This patch series was integrated into pu via git@2af274c. |
This patch series was integrated into pu via git@f094074. |
This patch series was integrated into next via git@f094074. |
This patch series was integrated into master via git@f094074. |
Closed via f094074. |
These issues were noticed in git-for-windows#2466, fulfilling my hope that Git for Windows' exposure of the built-in
git add -i
/git add -p
as an experimental option would attract testers.