diff mbox series

[v3,2/3] pack-objects: use the missing action API

Message ID 20240524163926.2019648-3-christian.couder@gmail.com (mailing list archive)
State New, archived
Headers show
Series upload-pack: support a missing-action | expand

Commit Message

Christian Couder May 24, 2024, 4:39 p.m. UTC
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 mbox series

Patch

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 */