git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* [PATCH 1/3] ls-tree: make <tree-ish> optional
@ 2018-07-03  3:58 Joshua Nelson
  2018-07-03  3:58 ` [PATCH 2/3] ls-tree: update usage info Joshua Nelson
                   ` (3 more replies)
  0 siblings, 4 replies; 21+ messages in thread
From: Joshua Nelson @ 2018-07-03  3:58 UTC (permalink / raw)
  To: git; +Cc: Joshua Nelson

use syntax similar to `git-checkout` to make <tree-ish> optional for
`ls-tree`. if <tree-ish> is omitted, default to HEAD. infer arguments as
follows:

1. if args start with --
	assume <tree-ish> to be HEAD
2. if exactly one arg precedes --, treat the argument as <tree-ish>
3. if more than one arg precedes --, exit with an error
4. if -- is not in args
	a) if args[0] is a valid <tree-ish> object, treat is as such
	b) else, assume <tree-ish> to be HEAD

in all cases, every argument besides <tree-ish> is treated as a <path>
---
 builtin/ls-tree.c | 39 ++++++++++++++++++++++++++++++++++-----
 1 file changed, 34 insertions(+), 5 deletions(-)

diff --git builtin/ls-tree.c builtin/ls-tree.c
index 409da4e83..14102b052 100644
--- builtin/ls-tree.c
+++ builtin/ls-tree.c
@@ -153,7 +153,7 @@ int cmd_ls_tree(int argc, const char **argv, const char *prefix)
 		chomp_prefix = strlen(prefix);
 
 	argc = parse_options(argc, argv, prefix, ls_tree_options,
-			     ls_tree_usage, 0);
+			     ls_tree_usage, PARSE_OPT_KEEP_DASHDASH);
 	if (full_tree) {
 		ls_tree_prefix = prefix = NULL;
 		chomp_prefix = 0;
@@ -163,10 +163,39 @@ int cmd_ls_tree(int argc, const char **argv, const char *prefix)
 	    ((LS_TREE_ONLY|LS_RECURSIVE) & ls_options))
 		ls_options |= LS_SHOW_TREES;
 
+	const char *object;
+	short initialized = 0;
 	if (argc < 1)
-		usage_with_options(ls_tree_usage, ls_tree_options);
-	if (get_oid(argv[0], &oid))
-		die("Not a valid object name %s", argv[0]);
+		object = "HEAD";
+	else {
+		/* taken from checkout.c;
+		 * we have a simpler case because we never create a branch */
+		short dash_dash_pos = -1, i = 0;
+		for (; i < argc; i++) {
+			if (!strcmp(argv[i], "--")) {
+				dash_dash_pos = i;
+				break;
+			}
+		}
+		if (dash_dash_pos == 0) {
+			object = "HEAD";
+			argv++, argc++;
+		} else if (dash_dash_pos == 1) {
+			object = argv[0];
+			argv += 2, argc += 2;
+		} else if (dash_dash_pos >= 2)
+			die(_("only one reference expected, %d given."), dash_dash_pos);
+		else if (get_oid(argv[0], &oid)) // not a valid object
+			object = "HEAD";
+		else {
+			argv++, argc++;
+			initialized = 1;
+		}
+	}
+
+	if (!initialized) // if we've already run get_oid, don't run it again
+		if (get_oid(object, &oid))
+			die("Not a valid object name %s", object);
 
 	/*
 	 * show_recursive() rolls its own matching code and is
@@ -177,7 +206,7 @@ int cmd_ls_tree(int argc, const char **argv, const char *prefix)
 	parse_pathspec(&pathspec, PATHSPEC_ALL_MAGIC &
 				  ~(PATHSPEC_FROMTOP | PATHSPEC_LITERAL),
 		       PATHSPEC_PREFER_CWD,
-		       prefix, argv + 1);
+		       prefix, argv);
 	for (i = 0; i < pathspec.nr; i++)
 		pathspec.items[i].nowildcard_len = pathspec.items[i].len;
 	pathspec.has_wildcard = 0;
-- 
2.18.GIT


^ permalink raw reply related	[flat|nested] 21+ messages in thread

end of thread, other threads:[~2018-07-06 21:32 UTC | newest]

Thread overview: 21+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-07-03  3:58 [PATCH 1/3] ls-tree: make <tree-ish> optional Joshua Nelson
2018-07-03  3:58 ` [PATCH 2/3] ls-tree: update usage info Joshua Nelson
2018-07-03  7:14   ` Elijah Newren
2018-07-03  7:18   ` Eric Sunshine
2018-07-03  3:58 ` [PATCH 3/3] ls-tree: add unit tests for arguments Joshua Nelson
2018-07-03  7:30   ` Elijah Newren
2018-07-03  7:33   ` Eric Sunshine
2018-07-03  7:12 ` [PATCH 1/3] ls-tree: make <tree-ish> optional Elijah Newren
2018-07-03 22:05   ` Junio C Hamano
2018-07-03 22:55     ` Elijah Newren
2018-07-03 22:58       ` Joshua Nelson
2018-07-06 17:01       ` Junio C Hamano
2018-07-06 21:26         ` Joshua Nelson
2018-07-06 21:32           ` Junio C Hamano
2018-07-03  7:15 ` Eric Sunshine
2018-07-03 23:15   ` Joshua Nelson
2018-07-03 23:53     ` [PATCH] " Joshua Nelson
2018-07-04  0:05       ` Joshua Nelson
2018-07-04  9:38         ` Eric Sunshine
2018-07-04 10:04       ` Eric Sunshine
2018-07-04  9:29     ` [PATCH 1/3] " Eric Sunshine

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).