public inbox for git@vger.kernel.org
 help / color / mirror / Atom feed
From: Johannes Schindelin <Johannes.Schindelin@gmx.de>
To: Luiz Campos <luizedc1@gmail.com>
Cc: git@vger.kernel.org, peff@peff.net, sagotsky@gmail.com
Subject: Re: [RFC PATCH 1/1] add -p: support discarding hunks with 'x'
Date: Wed, 25 Mar 2026 17:49:40 +0100 (CET)	[thread overview]
Message-ID: <a4305ef7-50ff-4a68-ab42-fe2fa73e8f37@gmx.de> (raw)
In-Reply-To: <20260325075055.354709-2-luizedc1@gmail.com>

Hi Luiz,

On Wed, 25 Mar 2026, Luiz Campos wrote:

> When using `git add -p`, users can stage or skip hunks,
> but cannot discard unwanted changes from the working tree.
> 
> Introduce a new 'x' action to discard the current hunk by
> reverse-applying it.
> 
> This idea was suggested in a previous mailing list discussion:
> https://lore.kernel.org/git/X%2FiFCo0bXLR%2BLZXs@coredump.intra.peff.net/t/#m0576e6f3c6375e11cc4693b9dca3c1fc57baadd0

Sounds good!

Just two minor comments (not really actionable, I think):

> @@ -1026,25 +1046,26 @@ static void reassemble_patch(struct add_p_state *s,
>  		struct hunk merged = { 0 };
>  
>  		hunk = file_diff->hunk + i;
> -		if (!use_all && hunk->use != USE_HUNK)
> +		if (!should_merge_hunk(file_diff, i, use_all, merge_for_discard)) {
>  			delta += hunk->header.old_count
>  				- hunk->header.new_count;
> -		else {
> -			/* merge overlapping hunks into a temporary hunk */
> -			if (merge_hunks(s, file_diff, &i, use_all, &merged))
> -				hunk = &merged;
> +			continue;
> +		}
>  
> -			render_hunk(s, hunk, delta, 0, out);
> +		if (merge_hunks(s, file_diff, &i, use_all, &merged,
> +				merge_for_discard))
> +			hunk = &merged;
>  
> -			/*
> -			 * In case `merge_hunks()` used `plain` as a scratch
> -			 * pad (this happens when an edited hunk had to be
> -			 * coalesced with another hunk).
> -			 */
> -			strbuf_setlen(&s->plain, save_len);
> +		render_hunk(s, hunk, delta, 0, out);
>  
> -			delta += hunk->delta;
> -		}
> +		/*
> +		 * In case `merge_hunks()` used `plain` as a scratch
> +		 * pad (this happens when an edited hunk had to be
> +		 * coalesced with another hunk).
> +		 */
> +		strbuf_setlen(&s->plain, save_len);
> +
> +		delta += hunk->delta;

This hunk is quite hard to read because of the `if ... else ...` -> `if {
... continue; } ...` change that de-indents a large chunk of code.

After pouring over the diff for a bit, I was able to convince myself that
the diff is correct.

> @@ -1547,21 +1570,57 @@ N_("j - go to the next undecided hunk, roll over at the bottom\n"
>     "? - print help\n"
>     "HUNKS SUMMARY - Hunks: %d, USE: %d, SKIP: %d\n");
>  
> +static int apply_discard_hunks(struct add_p_state *s,
> +			       struct file_diff *file_diff)
> +{
> +	struct child_process check_cp = CHILD_PROCESS_INIT;
> +	struct child_process apply_cp = CHILD_PROCESS_INIT;
> +
> +	strbuf_reset(&s->buf);
> +	reassemble_patch(s, file_diff, 0, REASSEMBLE_DISCARD, &s->buf);

If you detect an empty patch here and indicate this via an early return
value, then...

> +
> +	discard_index(s->index);
> +
> +	setup_child_process(s, &check_cp, "apply", "-R", "--check", NULL);
> +	if (pipe_command(&check_cp, s->buf.buf, s->buf.len, NULL, 0, NULL, 0)) {
> +		error(_("'git apply -R --check' failed"));
> +		return -1;
> +	}
> +
> +	setup_child_process(s, &apply_cp, "apply", "-R", NULL);
> +	if (pipe_command(&apply_cp, s->buf.buf, s->buf.len, NULL, 0, NULL, 0)) {
> +		error(_("'git apply -R' failed"));
> +		return -1;
> +	}
> +
> +	return 0;
> +}
> +
>  static void apply_patch(struct add_p_state *s, struct file_diff *file_diff)
>  {
>  	struct child_process cp = CHILD_PROCESS_INIT;
>  	size_t j;
> +	int needs_refresh = 0;
> +
> +	if (s->mode == &patch_mode_add) {
> +		for (j = 0; j < file_diff->hunk_nr; j++) {
> +			if (file_diff->hunk[j].use == DISCARD_HUNK)
> +				break;
> +		}
> +		if (j < file_diff->hunk_nr && apply_discard_hunks(s, file_diff))
> +			return;
> +		if (j < file_diff->hunk_nr)
> +			needs_refresh = 1;
> +	}

... then this loop is no longer necessary.

Other than that, looks good to me!

Ciao,
Johannes

  parent reply	other threads:[~2026-03-25 16:49 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-03-25  7:50 [RFC PATCH 0/1] add -p: support discarding hunks Luiz Campos
2026-03-25  7:50 ` [RFC PATCH 1/1] add -p: support discarding hunks with 'x' Luiz Campos
2026-03-25 15:44   ` D. Ben Knoble
2026-03-25 17:04     ` Luiz Eduardo Campos
2026-03-25 16:24   ` Phillip Wood
2026-03-25 18:38     ` Luiz Eduardo Campos
2026-03-25 16:49   ` Johannes Schindelin [this message]
2026-03-25 18:58     ` Luiz Eduardo Campos
2026-03-25 18:03 ` [RFC PATCH 0/1] add -p: support discarding hunks Junio C Hamano
2026-03-25 19:22   ` Luiz Eduardo Campos

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=a4305ef7-50ff-4a68-ab42-fe2fa73e8f37@gmx.de \
    --to=johannes.schindelin@gmx.de \
    --cc=git@vger.kernel.org \
    --cc=luizedc1@gmail.com \
    --cc=peff@peff.net \
    --cc=sagotsky@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox