git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [RFC] git-fsck-cache argument processing

From
Sean <seanlkml@sympatico.ca>
Date
May 21, 2005, 04:36 UTC
Message-ID
<4966.10.10.10.24.1116650175.squirrel@linux1>
In-Reply-To
<428EB444.7010200@pobox.com>
On Sat, May 21, 2005 12:08 am, Jeff Garzik said:
Show 11 quoted lines
> Pretty good.  You'll probably want some additional changes:
>
> 1) eliminate
> +       case ARGP_KEY_ARG:      state->next = state->argc; break;
>
> This will cause option processing to stop at the first unknown argument.
>
> 2) Pass-by-reference a variable to argp_parse(), which will store the
> index of the argument where processing stopped.  This is the first
> hash/file/etc. non-option argument.
>
Thanks Jeff, that's pretty cool.  Here's an updated patch.

With this updated patch, options following or even intermingled with the SHA1 list also are picked up, for example:

$ git-fsck-cache 804c64ea864d0a8ee13f3de0b74158a3e9c3166d -crudt

fsck-cache.c | 66 +++++++++++++++++++++++++++++++++++------------------------ 1 files changed, 40 insertions(+), 26 deletions(-)

Sean
fsck-cache.c: needs update
Index: fsck-cache.c
===================================================================
--- 58741c69570705801db4b785681790d636475695/fsck-cache.c  (mode:100644)
+++ uncommitted/fsck-cache.c  (mode:100644)
@@ -1,5 +1,7 @@
 #include <sys/types.h>
 #include <dirent.h>
+#include <argp.h>
+const char *argp_program_version = "git 1.0";
 
 #include "cache.h"
 #include "commit.h"
@@ -407,36 +409,48 @@
 	find_file_objects(git_dir, "refs");
 }
 
+#define O_UNREACH 'u'
+#define O_TAGS 't'
+#define O_ROOT 'r'
+#define O_DELTA 'd'
+#define O_CACHE 'c'
+
+static const char doc[] = "Perform repository consistency check";
+
+static struct argp_option options[] = {
+ {"unreachable", O_UNREACH, 0, 0, "Show missing objects or deltas"},
+ {"tags", O_TAGS, 0, 0, "Show revision tags"},
+ {"root", O_ROOT, 0, 0, "Show root objects, ie. those without parents"},
+ {"delta-depth", O_DELTA, 0, 0, "Show the maximum length of delta chains"},
+ {"cache", O_CACHE, 0, 0, "Mark all objects referenced by cache as reachable"},
+ { }
+};
+
+static error_t parse_opt (int key, char *arg, struct argp_state *state)
+{
+        switch (key) {
+        case O_UNREACH:		show_unreachable = 1; break;
+        case O_TAGS:		show_tags = 1; break;
+        case O_ROOT:		show_root = 1; break;
+        case O_DELTA:		show_max_delta_depth = 1; break;
+        case O_CACHE:		keep_cache_objects = 1; break;
+        default:		return ARGP_ERR_UNKNOWN;
+        }
+        return 0;
+}
+
+static const struct argp argp = { options, parse_opt, "[HEAD-SHA1...]", doc };
+
 int main(int argc, char **argv)
 {
 	int i, heads;
 	char *sha1_dir;
+	int idx;
 
-	for (i = 1; i < argc; i++) {
-		const char *arg = argv[i];
-
-		if (!strcmp(arg, "--unreachable")) {
-			show_unreachable = 1;
-			continue;
-		}
-		if (!strcmp(arg, "--tags")) {
-			show_tags = 1;
-			continue;
-		}
-		if (!strcmp(arg, "--root")) {
-			show_root = 1;
-			continue;
-		}
-		if (!strcmp(arg, "--delta-depth")) {
-			show_max_delta_depth = 1;
-			continue;
-		}
-		if (!strcmp(arg, "--cache")) {
-			keep_cache_objects = 1;
-			continue;
-		}
-		if (*arg == '-')
-			usage("git-fsck-cache [--tags] [[--unreachable] [--cache] <head-sha1>*]");
+	error_t rc = argp_parse(&argp, argc, argv, 0, &idx, NULL);
+	if (rc) {
+		fprintf(stderr, "argument failed: %s\n", strerror(rc));
+		return 1;
 	}
 
 	sha1_dir = get_object_directory();
@@ -450,7 +464,7 @@
 	expand_deltas();
 
 	heads = 0;
-	for (i = 1; i < argc; i++) {
+	for (i = idx; i < argc; i++) {
 		const char *arg = argv[i]; 
 
 		if (*arg == '-')
Previous: Jeff GarzikNext: Jeff Garzik
Message 3 of 18 in “[RFC] git-fsck-cache argument processing”
  1. SeanMay 21, 2005
  2. Jeff GarzikMay 21, 2005
  3. SeanMay 21, 2005
  4. Jeff GarzikMay 21, 2005
  5. Junio C HamanoMay 21, 2005
  6. Jeff GarzikMay 21, 2005
  7. Junio C HamanoMay 21, 2005
  8. Olivier GalibertMay 21, 2005
  9. Jeff GarzikMay 21, 2005
  10. SeanMay 21, 2005
  11. Olivier GalibertMay 21, 2005
  12. SeanMay 21, 2005
  13. Jeff GarzikMay 21, 2005
  14. Joel BeckerMay 21, 2005
  15. Linus TorvaldsMay 21, 2005
  16. SeanMay 21, 2005
  17. SeanMay 21, 2005
  18. Linus TorvaldsMay 21, 2005

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.