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

[PATCH 4/9] builtin-ls-tree.c: use parse_options()

From
Michele Ballabio <barra_cuda@katamail.com>
Date
Jul 23, 2008, 21:42 UTC
Message-ID
<1216849332-26813-5-git-send-email-barra_cuda@katamail.com>
In-Reply-To
<1216849332-26813-1-git-send-email-barra_cuda@katamail.com>
Signed-off-by: Michele Ballabio <barra_cuda@katamail.com>
---
 builtin-ls-tree.c |   92 +++++++++++++++++++++--------------------------------
 1 files changed, 36 insertions(+), 56 deletions(-)
diff --git a/builtin-ls-tree.c b/builtin-ls-tree.c
index d25767a..a0b17aa 100644
--- a/builtin-ls-tree.c
+++ b/builtin-ls-tree.c
@@ -9,6 +9,7 @@
 #include "commit.h"
 #include "quote.h"
 #include "builtin.h"
+#include "parse-options.h"
 
 static int line_termination = '\n';
 #define LS_RECURSIVE 1
@@ -22,8 +23,10 @@ static const char **pathspec;
 static int chomp_prefix;
 static const char *ls_tree_prefix;
 
-static const char ls_tree_usage[] =
-	"git ls-tree [-d] [-r] [-t] [-l] [-z] [--name-only] [--name-status] [--full-name] [--abbrev[=<n>]] <tree-ish> [path...]";
+static const char * const ls_tree_usage[] = {
+	"git ls-tree [options] <tree-ish> [path...]",
+	NULL
+};
 
 static int show_recursive(const char *base, int baselen, const char *pathname)
 {
@@ -122,70 +125,47 @@ int cmd_ls_tree(int argc, const char **argv, const char *prefix)
 	unsigned char sha1[20];
 	struct tree *tree;
 
+	const struct option options[] = {
+		OPT_SET_INT('z', NULL, &line_termination,
+			    "\\0 line termination on output", 0),
+		OPT_BIT('r', NULL, &ls_options,
+			"recurse into sub-trees", LS_RECURSIVE),
+		OPT_BIT('d', NULL, &ls_options,
+			"show only the tree, not its children",
+			LS_TREE_ONLY),
+		OPT_BIT('t', NULL, &ls_options,
+			"show tree entries", LS_SHOW_TREES),
+		OPT_BIT('l', "long", &ls_options,
+			"show object size of blob (file) entries",
+			LS_SHOW_SIZE),
+		OPT_BIT(0, "name-only", &ls_options,
+			"list only filenames", LS_NAME_ONLY),
+		OPT_BIT(0, "name-status", &ls_options,
+			"same as --name-only", LS_NAME_ONLY),
+		OPT_SET_INT(0, "full-name", &chomp_prefix,
+			    "show the full path name", 0),
+		OPT__ABBREV(&abbrev),
+		OPT_END()
+	};
+
 	git_config(git_default_config, NULL);
 	ls_tree_prefix = prefix;
 	if (prefix && *prefix)
 		chomp_prefix = strlen(prefix);
-	while (1 < argc && argv[1][0] == '-') {
-		switch (argv[1][1]) {
-		case 'z':
-			line_termination = 0;
-			break;
-		case 'r':
-			ls_options |= LS_RECURSIVE;
-			break;
-		case 'd':
-			ls_options |= LS_TREE_ONLY;
-			break;
-		case 't':
-			ls_options |= LS_SHOW_TREES;
-			break;
-		case 'l':
-			ls_options |= LS_SHOW_SIZE;
-			break;
-		case '-':
-			if (!strcmp(argv[1]+2, "name-only") ||
-			    !strcmp(argv[1]+2, "name-status")) {
-				ls_options |= LS_NAME_ONLY;
-				break;
-			}
-			if (!strcmp(argv[1]+2, "long")) {
-				ls_options |= LS_SHOW_SIZE;
-				break;
-			}
-			if (!strcmp(argv[1]+2, "full-name")) {
-				chomp_prefix = 0;
-				break;
-			}
-			if (!prefixcmp(argv[1]+2, "abbrev=")) {
-				abbrev = strtoul(argv[1]+9, NULL, 10);
-				if (abbrev && abbrev < MINIMUM_ABBREV)
-					abbrev = MINIMUM_ABBREV;
-				else if (abbrev > 40)
-					abbrev = 40;
-				break;
-			}
-			if (!strcmp(argv[1]+2, "abbrev")) {
-				abbrev = DEFAULT_ABBREV;
-				break;
-			}
-			/* otherwise fallthru */
-		default:
-			usage(ls_tree_usage);
-		}
-		argc--; argv++;
-	}
+
+	argc = parse_options(argc, argv, options, ls_tree_usage, 0);
+
 	/* -d -r should imply -t, but -d by itself should not have to. */
 	if ( (LS_TREE_ONLY|LS_RECURSIVE) ==
 	    ((LS_TREE_ONLY|LS_RECURSIVE) & ls_options))
 		ls_options |= LS_SHOW_TREES;
 
-	if (argc < 2)
-		usage(ls_tree_usage);
-	if (get_sha1(argv[1], sha1))
-		die("Not a valid object name %s", argv[1]);
+	if (argc < 1)
+		usage_with_options(ls_tree_usage, options);
+	if (get_sha1(argv[0], sha1))
+		die("Not a valid object name %s", argv[0]);
 
-	pathspec = get_pathspec(prefix, argv + 2);
+	pathspec = get_pathspec(prefix, argv + 1);
 	tree = parse_tree_indirect(sha1);
 	if (!tree)
 		die("not a tree object");
-- 
1.5.6.3
Previous: Michele BallabioNext: Michele Ballabio
Message 13 of 31 in “Extend use of parse_options()”
  1. 0/9 Extend use of parse_options()Michele Ballabio, Jul 23, 2008
  2. 1/9 builtin-verify-tag.c: use parse_options()Michele Ballabio, Jul 23, 2008
  3. Olivier MarinJul 24, 2008
  4. Johannes SchindelinJul 24, 2008
  5. Olivier MarinJul 25, 2008
  6. Johannes SchindelinJul 26, 2008
  7. builtin-verify-tag: fix -v option parsingOlivier Marin, Jul 28, 2008
  8. Johannes SchindelinJul 28, 2008
  9. Olivier MarinJul 28, 2008
  10. Johannes SchindelinJul 28, 2008
  11. 2/9 builtin-write-tree.c: use parse_options()Michele Ballabio, Jul 23, 2008
  12. 3/9 builtin-prune-packed.c: use parse_options()Michele Ballabio, Jul 23, 2008
  13. 4/9 builtin-ls-tree.c: use parse_options()Michele Ballabio, Jul 23, 2008
  14. 5/9 builtin-rev-list.c: use parse_options()Michele Ballabio, Jul 23, 2008
  15. 6/9 builtin-init-db.c: use parse_options()Michele Ballabio, Jul 23, 2008
  16. Olivier MarinJul 24, 2008
  17. Johannes SchindelinJul 24, 2008
  18. Olivier MarinJul 25, 2008
  19. Johannes SchindelinJul 26, 2008
  20. Michele BallabioJul 24, 2008
  21. 6/9 builtin-init-db.c: use parse_options()Michele Ballabio, Jul 25, 2008
  22. Olivier MarinJul 25, 2008
  23. 6/9 builtin-init-db.c: use parse_options()Michele Ballabio, Jul 25, 2008
  24. 7/9 builtin-checkout-index.c: use parse_options()Michele Ballabio, Jul 23, 2008
  25. Johannes SchindelinJul 24, 2008
  26. Michele BallabioJul 24, 2008
  27. Sverre RabbelierJul 24, 2008
  28. René ScharfeJul 25, 2008
  29. 7/9 builtin-checkout-index.c: use parse_options()Michele Ballabio, Jul 25, 2008
  30. 8/9 builtin-fetch-pack.c: use parse_options()Michele Ballabio, Jul 23, 2008
  31. 9/9 builtin-mailinfo.c: use parse_options()Michele Ballabio, Jul 23, 2008

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.