git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Jeff King <peff@peff.net>
To: git@vger.kernel.org
Cc: "Junio C Hamano" <gitster@pobox.com>, "René Scharfe" <l.s.r@web.de>
Subject: [PATCH v2 04/10] checkout-index: delay automatic setting of to_tempfile
Date: Thu, 31 Aug 2023 17:20:51 -0400	[thread overview]
Message-ID: <20230831212051.GD949469@coredump.intra.peff.net> (raw)
In-Reply-To: <20230831211637.GA949188@coredump.intra.peff.net>

Using --stage=all requires writing to tempfiles, since we cannot put
multiple stages into a single file. So --stage=all implies --temp.

But we do this by setting to_tempfile in the options callback for
--stage, rather than after all options have been parsed. This leads to
two bugs:

  1. If you run "checkout-index --stage=all --stage=2", this should not
     imply --temp, but it currently does. The callback cannot just unset
     to_tempfile when it sees the "2" value, because it no longer knows
     if its value was from the earlier --stage call, or if the user
     specified --temp explicitly.

  2. If you run "checkout-index --stage=all --no-temp", the --no-temp
     will overwrite the earlier implied --temp. But this mode of
     operation cannot work, and the command will fail with "<path>
     already exists" when trying to write the higher stages.

We can fix both by lazily setting to_tempfile. We'll make it a tristate,
with -1 as "not yet given", and have --stage=all enable it only after
all options are parsed. Likewise, after all options are parsed we can
detect and reject the bogus "--no-temp" case.

Note that this does technically change the behavior for "--stage=all
--no-temp" for paths which have only one stage present (which
accidentally worked before, but is now forbidden). But this behavior was
never intended, and you'd have to go out of your way to try to trigger
it.

The new tests cover both cases, as well the general "--stage=all implies
--temp", as most of the other tests explicitly say "--temp". Ironically,
the test "checkout --temp within subdir" is the only one that _doesn't_
use "--temp", and so was implicitly covering this case. But it seems
reasonable to have a more explicit test alongside the other related
ones.

Suggested-by: Junio C Hamano <gitster@pobox.com>
Signed-off-by: Jeff King <peff@peff.net>
---
One other bug-let I noticed in this function: it only looks at the first
byte of the stage argument, so "--stage=13" is the same as "--stage=1".
We could tighten this, but I wonder if anybody does something weird like
"--stage=2ours" or something. That seems pretty unlikely, but I'd still
prefer to do it as a separate patch (if at all).

 builtin/checkout-index.c       |  8 ++++++--
 t/t2004-checkout-cache-temp.sh | 20 ++++++++++++++++++++
 2 files changed, 26 insertions(+), 2 deletions(-)

diff --git a/builtin/checkout-index.c b/builtin/checkout-index.c
index f62f13f2b5..6687a495ff 100644
--- a/builtin/checkout-index.c
+++ b/builtin/checkout-index.c
@@ -24,7 +24,7 @@
 static int nul_term_line;
 static int checkout_stage; /* default to checkout stage0 */
 static int ignore_skip_worktree; /* default to 0 */
-static int to_tempfile;
+static int to_tempfile = -1;
 static char topath[4][TEMPORARY_FILENAME_LENGTH + 1];
 
 static struct checkout state = CHECKOUT_INIT;
@@ -196,7 +196,6 @@ static int option_parse_stage(const struct option *opt,
 	BUG_ON_OPT_NEG(unset);
 
 	if (!strcmp(arg, "all")) {
-		to_tempfile = 1;
 		checkout_stage = CHECKOUT_ALL;
 	} else {
 		int ch = arg[0];
@@ -269,6 +268,11 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
 		state.base_dir = "";
 	state.base_dir_len = strlen(state.base_dir);
 
+	if (to_tempfile < 0)
+		to_tempfile = (checkout_stage == CHECKOUT_ALL);
+	if (!to_tempfile && checkout_stage == CHECKOUT_ALL)
+		die("--stage=all and --no-temp are incompatible");
+
 	/*
 	 * when --prefix is specified we do not want to update cache.
 	 */
diff --git a/t/t2004-checkout-cache-temp.sh b/t/t2004-checkout-cache-temp.sh
index b16d69ca4a..8eb2ef44eb 100755
--- a/t/t2004-checkout-cache-temp.sh
+++ b/t/t2004-checkout-cache-temp.sh
@@ -117,6 +117,26 @@ test_expect_success 'checkout all stages/one file to temporary files' '
 	test $(cat $s3) = tree3path1)
 '
 
+test_expect_success '--stage=all implies --temp' '
+	rm -f path* .merge_* actual &&
+	git checkout-index --stage=all -- path1 &&
+	test_path_is_missing path1
+'
+
+test_expect_success 'overriding --stage=all resets implied --temp' '
+	rm -f path* .merge_* actual &&
+	git checkout-index --stage=all --stage=2 -- path1 &&
+	echo tree2path1 >expect &&
+	test_cmp expect path1
+'
+
+test_expect_success '--stage=all --no-temp is rejected' '
+	rm -f path* .merge_* actual &&
+	test_must_fail git checkout-index --stage=all --no-temp -- path1 2>err &&
+	grep -v "already exists" err &&
+	grep "stage=all and --no-temp are incompatible" err
+'
+
 test_expect_success 'checkout some stages/one file to temporary files' '
 	rm -f path* .merge_* actual &&
 	git checkout-index --stage=all --temp -- path2 >actual &&
-- 
2.42.0.561.gaa987ecc69


  parent reply	other threads:[~2023-08-31 21:20 UTC|newest]

Thread overview: 47+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-31  7:09 [PATCH 0/8] more unused parameters in parseopt callbacks Jeff King
2023-08-31  7:12 ` [PATCH 1/8] merge: make xopts a strvec Jeff King
2023-08-31  7:22   ` Jeff King
2023-08-31 11:18     ` Phillip Wood
2023-08-31 15:46   ` Junio C Hamano
2023-08-31 20:55     ` Taylor Blau
2023-08-31  7:14 ` [PATCH 2/8] merge: simplify parsing of "-n" option Jeff King
2023-08-31 15:56   ` Junio C Hamano
2023-08-31  7:17 ` [PATCH 3/8] parse-options: prefer opt->value to globals in callbacks Jeff King
2023-08-31 16:14   ` Junio C Hamano
2023-08-31  7:18 ` [PATCH 4/8] parse-options: mark unused "opt" parameter " Jeff King
2023-08-31 16:33   ` Junio C Hamano
2023-08-31 17:50     ` Jeff King
2023-08-31 20:48       ` Jeff King
2023-08-31  7:18 ` [PATCH 5/8] merge: do not pass unused opt->value parameter Jeff King
2023-08-31 16:53   ` Junio C Hamano
2023-08-31  7:19 ` [PATCH 6/8] parse-options: add more BUG_ON() annotations Jeff King
2023-08-31 16:58   ` Junio C Hamano
2023-08-31  7:19 ` [PATCH 7/8] interpret-trailers: mark unused "unset" parameters in option callbacks Jeff King
2023-08-31 17:04   ` Junio C Hamano
2023-08-31 17:56     ` Jeff King
2023-08-31  7:20 ` [PATCH 8/8] parse-options: mark unused parameters in noop callback Jeff King
2023-08-31 17:05   ` Junio C Hamano
2023-08-31 21:16 ` [PATCH v2 0/10] more unused parameters in parseopt callbacks Jeff King
2023-08-31 21:17   ` [PATCH v2 01/10] merge: make xopts a strvec Jeff King
2023-08-31 21:17   ` [PATCH v2 02/10] merge: simplify parsing of "-n" option Jeff King
2023-09-02  6:20     ` René Scharfe
2023-09-05  6:43       ` Jeff King
2023-08-31 21:17   ` [PATCH v2 03/10] format-patch: use OPT_STRING_LIST for to/cc options Jeff King
2023-08-31 21:20   ` Jeff King [this message]
2023-08-31 22:12     ` [PATCH v2 04/10] checkout-index: delay automatic setting of to_tempfile Junio C Hamano
2023-09-02  6:20     ` René Scharfe
2023-09-05  7:12       ` [PATCH v3 " Jeff King
2023-08-31 21:21   ` [PATCH v2 05/10] parse-options: prefer opt->value to globals in callbacks Jeff King
2023-09-02  7:34     ` René Scharfe
2023-09-05  6:52       ` Jeff King
2023-08-31 21:21   ` [PATCH v2 06/10] parse-options: mark unused "opt" parameter " Jeff King
2023-09-02 10:12     ` René Scharfe
2023-09-05  7:05       ` Jeff King
2023-09-19  7:42         ` René Scharfe
2023-08-31 21:21   ` [PATCH v2 07/10] merge: do not pass unused opt->value parameter Jeff King
2023-08-31 21:21   ` [PATCH v2 08/10] parse-options: add more BUG_ON() annotations Jeff King
2023-08-31 21:22   ` [PATCH v2 09/10] interpret-trailers: mark unused "unset" parameters in option callbacks Jeff King
2023-08-31 21:22   ` [PATCH v2 10/10] parse-options: mark unused parameters in noop callback Jeff King
2023-09-02 11:37     ` René Scharfe
2023-09-05  7:09       ` Jeff King
2023-09-07 20:20         ` René Scharfe

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

  List information: http://vger.kernel.org/majordomo-info.html

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

  git send-email \
    --in-reply-to=20230831212051.GD949469@coredump.intra.peff.net \
    --to=peff@peff.net \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=l.s.r@web.de \
    /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.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).