git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Christian Couder <christian.couder@gmail.com>
To: git@vger.kernel.org
Cc: Junio C Hamano <gitster@pobox.com>,
	John Cai <johncai86@gmail.com>, Patrick Steinhardt <ps@pks.im>,
	Christian Couder <christian.couder@gmail.com>,
	Christian Couder <chriscool@tuxfamily.org>
Subject: [PATCH v3 2/3] pack-objects: use the missing action API
Date: Fri, 24 May 2024 18:39:25 +0200	[thread overview]
Message-ID: <20240524163926.2019648-3-christian.couder@gmail.com> (raw)
In-Reply-To: <20240524163926.2019648-1-christian.couder@gmail.com>

Both `git rev-list` and `git pack-objects` support a
`--missing=<missing-action>` option. Previous commits created an API
in "missing.{c,h}" to help supporting that option, but only
`git rev-list` has been using that API so far.

Let's make `git pack-objects` use it too.

This involves creating a new parse_missing_action_value_for_packing()
function in the missing action API as `git pack-objects` doesn't
support the MA_PRINT missing action and other commands using the API
in the future might not support it either.

Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
---
 builtin/pack-objects.c | 37 ++++++++++++++-----------------------
 missing.c              | 17 +++++++++++++++++
 missing.h              |  8 ++++++++
 3 files changed, 39 insertions(+), 23 deletions(-)

diff --git a/builtin/pack-objects.c b/builtin/pack-objects.c
index cd2396896d..fad16e0ce2 100644
--- a/builtin/pack-objects.c
+++ b/builtin/pack-objects.c
@@ -39,6 +39,7 @@
 #include "promisor-remote.h"
 #include "pack-mtimes.h"
 #include "parse-options.h"
+#include "missing.h"
 
 /*
  * Objects we are going to pack are collected in the `to_pack` structure.
@@ -250,11 +251,6 @@ static unsigned long window_memory_limit = 0;
 
 static struct string_list uri_protocols = STRING_LIST_INIT_NODUP;
 
-enum missing_action {
-	MA_ERROR = 0,      /* fail if any missing objects are encountered */
-	MA_ALLOW_ANY,      /* silently allow ALL missing objects */
-	MA_ALLOW_PROMISOR, /* silently allow all missing PROMISOR objects */
-};
 static enum missing_action arg_missing_action;
 static show_object_fn fn_show_object;
 
@@ -3830,30 +3826,25 @@ static void show_object__ma_allow_promisor(struct object *obj, const char *name,
 static int option_parse_missing_action(const struct option *opt UNUSED,
 				       const char *arg, int unset)
 {
+	int res;
+	static show_object_fn const fn[] = {
+		[MA_ERROR] = show_object,
+		[MA_ALLOW_ANY] = show_object__ma_allow_any,
+		[MA_ALLOW_PROMISOR] = show_object__ma_allow_promisor,
+	};
+
 	assert(arg);
 	assert(!unset);
 
-	if (!strcmp(arg, "error")) {
-		arg_missing_action = MA_ERROR;
-		fn_show_object = show_object;
-		return 0;
-	}
-
-	if (!strcmp(arg, "allow-any")) {
-		arg_missing_action = MA_ALLOW_ANY;
-		fetch_if_missing = 0;
-		fn_show_object = show_object__ma_allow_any;
-		return 0;
-	}
+	res = parse_missing_action_value_for_packing(arg);
+	if (res < 0 || ARRAY_SIZE(fn) <= res)
+		die(_("invalid value for '%s': '%s'"), "--missing", arg);
 
-	if (!strcmp(arg, "allow-promisor")) {
-		arg_missing_action = MA_ALLOW_PROMISOR;
+	fn_show_object = fn[res];
+	arg_missing_action = res;
+	if (res != MA_ERROR)
 		fetch_if_missing = 0;
-		fn_show_object = show_object__ma_allow_promisor;
-		return 0;
-	}
 
-	die(_("invalid value for '%s': '%s'"), "--missing", arg);
 	return 0;
 }
 
diff --git a/missing.c b/missing.c
index ce3cf734a8..a0c2800d30 100644
--- a/missing.c
+++ b/missing.c
@@ -18,3 +18,20 @@ int parse_missing_action_value(const char *value)
 
 	return -1;
 }
+
+int parse_missing_action_value_for_packing(const char *value)
+{
+	int res = parse_missing_action_value(value);
+
+	if (res < 0)
+		return -1;
+
+	switch (res) {
+	case MA_ERROR:
+	case MA_ALLOW_ANY:
+	case MA_ALLOW_PROMISOR:
+		return res;
+	default:
+		return -2 - res;
+	}
+}
diff --git a/missing.h b/missing.h
index 1e378d6215..cdfd522852 100644
--- a/missing.h
+++ b/missing.h
@@ -14,4 +14,12 @@ enum missing_action {
 */
 int parse_missing_action_value(const char *value);
 
+/*
+  Return a 'res' with the following meaning:
+    0 <= res : an MA_FOO value that is OK for packing
+    -1 = res : parse_missing_action_value() failed
+    -1 > res : (2 - res) is an MA_FOO value which is unsuitable for packing
+ */
+int parse_missing_action_value_for_packing(const char *value);
+
 #endif /* MISSING_H */
-- 
2.45.1.219.gbac909a070


  parent reply	other threads:[~2024-05-24 16:39 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-18 18:40 [PATCH 0/4] upload-pack: support a missing-action Christian Couder
2024-04-18 18:40 ` [PATCH 1/4] rev-list: refactor --missing=<missing-action> Christian Couder
2024-04-18 21:39   ` Junio C Hamano
2024-04-18 18:40 ` [PATCH 2/4] missing: support rejecting --missing=print Christian Couder
2024-04-18 21:47   ` Junio C Hamano
2024-04-18 18:40 ` [PATCH 3/4] pack-objects: use the missing action API Christian Couder
2024-04-18 18:40 ` [PATCH 4/4] upload-pack: allow configuring a missing-action Christian Couder
2024-04-18 19:21 ` [PATCH 0/4] upload-pack: support " Junio C Hamano
2024-05-24 16:39 ` [PATCH v3 0/3] " Christian Couder
2024-05-24 16:39   ` [PATCH v3 1/3] rev-list: refactor --missing=<missing-action> Christian Couder
2024-05-24 16:39   ` Christian Couder [this message]
2024-05-24 16:39   ` [PATCH v3 3/3] upload-pack: allow configuring a missing-action Christian Couder
2024-05-24 18:25   ` [PATCH v3 0/3] upload-pack: support " 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=20240524163926.2019648-3-christian.couder@gmail.com \
    --to=christian.couder@gmail.com \
    --cc=chriscool@tuxfamily.org \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=johncai86@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;
as well as URLs for NNTP newsgroup(s).