Git development
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: Patrick Steinhardt <ps@pks.im>
Cc: git@vger.kernel.org,  Pablo Sabater <pabloosabaterr@gmail.com>
Subject: Re: [PATCH v2 4/9] reset: introduce dry-run mode
Date: Thu, 04 Jun 2026 08:49:34 +0900	[thread overview]
Message-ID: <xmqqv7bzqj5d.fsf@gitster.g> (raw)
In-Reply-To: <20260603-b4-pks-history-drop-v2-4-742cb5b5176d@pks.im> (Patrick Steinhardt's message of "Wed, 03 Jun 2026 18:14:03 +0200")

Patrick Steinhardt <ps@pks.im> writes:

> In a subsequent commit we'll add add another caller to `reset_head()`

add add?

> that wants to perform a dry-run check of whether it would be possible to
> udpate the index and working tree when moving to a new commit. Introduce

udpate?

> a new flag that lets the caller perform this operation.
>
> Signed-off-by: Patrick Steinhardt <ps@pks.im>
> ---
>  reset.c | 44 +++++++++++++++++++++++++++++++++-----------
>  reset.h |  6 ++++++
>  2 files changed, 39 insertions(+), 11 deletions(-)
>
> diff --git a/reset.c b/reset.c
> index 9ff14f5ed1..a8d7eea4d6 100644
> --- a/reset.c
> +++ b/reset.c
> @@ -92,11 +92,14 @@ int reset_head(struct repository *r, const struct reset_head_opts *opts)
>  	unsigned reset_hard = opts->flags & RESET_HEAD_HARD;
>  	unsigned refs_only = opts->flags & RESET_HEAD_REFS_ONLY;
>  	unsigned update_orig_head = opts->flags & RESET_HEAD_ORIG_HEAD;
> +	unsigned dry_run = opts->flags & RESET_HEAD_DRY_RUN;
>  	struct object_id *head = NULL, head_oid;
>  	struct tree_desc desc[2] = { { NULL }, { NULL } };
>  	struct lock_file lock = LOCK_INIT;
>  	struct unpack_trees_options unpack_tree_opts = { 0 };
>  	struct tree *tree;
> +	struct index_state scratch_index = INDEX_STATE_INIT(r);
> +	struct index_state *istate;
>  	const char *action;
>  	int ret = 0, nr = 0;
>  
> @@ -109,7 +112,7 @@ int reset_head(struct repository *r, const struct reset_head_opts *opts)
>  	if (opts->branch_msg && !opts->branch)
>  		BUG("branch reflog message given without a branch");
>  
> -	if (!refs_only && repo_hold_locked_index(r, &lock, LOCK_REPORT_ON_ERROR) < 0) {
> +	if (!refs_only && !dry_run && repo_hold_locked_index(r, &lock, LOCK_REPORT_ON_ERROR) < 0) {
>  		ret = -1;
>  		goto leave_reset_head;
>  	}
> @@ -124,16 +127,36 @@ int reset_head(struct repository *r, const struct reset_head_opts *opts)
>  	if (!oid)
>  		oid = &head_oid;
>  
> -	if (refs_only)
> -		return update_refs(r, opts, oid, head);
> +	if (refs_only) {
> +		if (!dry_run)
> +			return update_refs(r, opts, oid, head);
> +		return 0;
> +	}
> +
> +	if (dry_run) {
> +		if (read_index_from(&scratch_index, r->index_file, r->gitdir) < 0 ||
> +		    index_state_unmerged_to_stage0(&scratch_index) < 0) {
> +			ret = error(_("could not read index"));
> +			goto leave_reset_head;
> +		}
> +
> +		istate = &scratch_index;
> +	} else {
> +		if (repo_read_index_unmerged(r) < 0) {
> +			ret = error(_("could not read index"));
> +			goto leave_reset_head;
> +		}
> +		istate = r->index;
> +	}
>  
>  	action = reset_hard ? "reset" : "checkout";
>  	setup_unpack_trees_porcelain(&unpack_tree_opts, action);
>  	unpack_tree_opts.head_idx = 1;
> -	unpack_tree_opts.src_index = r->index;
> -	unpack_tree_opts.dst_index = r->index;
> +	unpack_tree_opts.src_index = istate;
> +	unpack_tree_opts.dst_index = istate;
>  	unpack_tree_opts.fn = reset_hard ? oneway_merge : twoway_merge;
> -	unpack_tree_opts.update = 1;
> +	unpack_tree_opts.update = !dry_run;
> +	unpack_tree_opts.dry_run = dry_run;
>  	unpack_tree_opts.merge = 1;
>  	unpack_tree_opts.preserve_ignored = 0; /* FIXME: !overwrite_ignore */
>  	unpack_tree_opts.skip_cache_tree_update = 1;
> @@ -141,11 +164,6 @@ int reset_head(struct repository *r, const struct reset_head_opts *opts)
>  	if (reset_hard)
>  		unpack_tree_opts.reset = UNPACK_RESET_PROTECT_UNTRACKED;
>  
> -	if (repo_read_index_unmerged(r) < 0) {
> -		ret = error(_("could not read index"));
> -		goto leave_reset_head;
> -	}
> -
>  	if (!reset_hard && !fill_tree_descriptor(r, &desc[nr++], &head_oid)) {
>  		ret = error(_("failed to find tree of %s"),
>  			    oid_to_hex(&head_oid));
> @@ -162,6 +180,9 @@ int reset_head(struct repository *r, const struct reset_head_opts *opts)
>  		goto leave_reset_head;
>  	}
>  
> +	if (dry_run)
> +		goto leave_reset_head;
> +
>  	tree = repo_parse_tree_indirect(r, oid);
>  	if (!tree) {
>  		ret = error(_("unable to read tree (%s)"), oid_to_hex(oid));
> @@ -181,6 +202,7 @@ int reset_head(struct repository *r, const struct reset_head_opts *opts)
>  leave_reset_head:
>  	rollback_lock_file(&lock);
>  	clear_unpack_trees_porcelain(&unpack_tree_opts);
> +	release_index(&scratch_index);
>  	while (nr)
>  		free((void *)desc[--nr].buffer);
>  	return ret;
> diff --git a/reset.h b/reset.h
> index 97ced2601e..9f696382c1 100644
> --- a/reset.h
> +++ b/reset.h
> @@ -21,6 +21,12 @@ enum reset_head_flags {
>  
>  	/* Update ORIG_HEAD as well as HEAD */
>  	RESET_HEAD_ORIG_HEAD = (1 << 4),
> +
> +	/*
> +	 * Perform a dry-run by performing the operation without updating
> +	 * any user-visible state.
> +	 */
> +	RESET_HEAD_DRY_RUN = (1 << 5),
>  };
>  
>  struct reset_head_opts {

  parent reply	other threads:[~2026-06-03 23:49 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-06-01 15:36 [PATCH 0/2] builtin/history: introduce "drop" subcommand Patrick Steinhardt
2026-06-01 15:36 ` [PATCH 1/2] builtin/history: split handling of ref updates into two phases Patrick Steinhardt
2026-06-01 15:36 ` [PATCH 2/2] builtin/history: implement "drop" subcommand Patrick Steinhardt
2026-06-01 23:43   ` Junio C Hamano
2026-06-03 10:06     ` Patrick Steinhardt
2026-06-02  7:31   ` Pablo Sabater
2026-06-03 10:06     ` Patrick Steinhardt
2026-06-03 16:13 ` [PATCH v2 0/9] builtin/history: introduce " Patrick Steinhardt
2026-06-03 16:14   ` [PATCH v2 1/9] read-cache: split out function to drop unmerged entries to stage 0 Patrick Steinhardt
2026-06-03 16:14   ` [PATCH v2 2/9] reset: drop `USE_THE_REPOSITORY_VARIABLE` Patrick Steinhardt
2026-06-03 16:14   ` [PATCH v2 3/9] reset: modernize flags passed to `reset_head()` Patrick Steinhardt
2026-06-03 18:01     ` Kristoffer Haugsbakk
2026-06-03 16:14   ` [PATCH v2 4/9] reset: introduce dry-run mode Patrick Steinhardt
2026-06-03 18:18     ` Kristoffer Haugsbakk
2026-06-03 23:49     ` Junio C Hamano [this message]
2026-06-03 16:14   ` [PATCH v2 5/9] reset: introduce ability to skip reference updates Patrick Steinhardt
2026-06-03 23:51     ` Junio C Hamano
2026-06-03 16:14   ` [PATCH v2 6/9] reset: allow the caller to specify the current HEAD object Patrick Steinhardt
2026-06-03 16:14   ` [PATCH v2 7/9] reset: stop assuming that the caller passes in a clean index Patrick Steinhardt
2026-06-03 16:14   ` [PATCH v2 8/9] builtin/history: split handling of ref updates into two phases Patrick Steinhardt
2026-06-03 16:14   ` [PATCH v2 9/9] builtin/history: implement "drop" subcommand Patrick Steinhardt
2026-06-03 19:04     ` Kristoffer Haugsbakk
2026-06-03 23:58     ` Junio C Hamano

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=xmqqv7bzqj5d.fsf@gitster.g \
    --to=gitster@pobox.com \
    --cc=git@vger.kernel.org \
    --cc=pabloosabaterr@gmail.com \
    --cc=ps@pks.im \
    /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