Re: [PATCH] commit: allow --pretty= args to be abbreviated

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Junio C Hamano <junkio@xxxxxxx> wrote:
> Eric Wong <normalperson@xxxxxxxx> writes:
> 
> > Unlike the original one, this one only does prefix matches, so
> > you can't do --pretty=er anymore :)
> 
> Sounds good.  But then you know how long the unique prefix
> are for each candidate, so wouldn't this rather be redundant, I
> wonder?

I just copied the idea for the 2nd for loop from gitopt, since it makes
maintenance easier when there are lots of possibilities.  We only have 6
(soon 7) to worry about for --pretty= here, so hard coding lengths
probably makes more sense.

> > +
> > +	/* look for abbreviations */
> > +	len = strlen(arg);
> > +	found = -1;
> > +	for (i = 0; i < ARRAY_SIZE(cmt_fmts); i++) {
> > +		if (!strncmp(cmt_fmts[i].n, arg, len)) {
> > +			if (found >= 0)
> > +				die("invalid --pretty format: %s", arg);
> > +			found = i;
> > +		}
> > +	}
> > +	if (found >= 0)
> > +		return cmt_fmts[found].v;
> > +	die("invalid --pretty format: %s", arg);
> >  }
> 
> It would probably be better to say "ambiguous" not "invalid" in
> the die() message.

Yes, but only one die() message left now :)

-- 
Eric Wong

>From nobody Mon Sep 17 00:00:00 2001
From: Eric Wong <normalperson@xxxxxxxx>
Date: Sun, 14 May 2006 17:20:46 -0700
Subject: [PATCH] commit: allow --pretty= args to be abbreviated

Unlike the original one, this one only does prefix matches, so
you can't do --pretty=er anymore :)

This one really works with and without the gitopt changes.

Signed-off-by: Eric Wong <normalperson@xxxxxxxx>

---

 commit.c |   38 ++++++++++++++++++++++++--------------
 1 files changed, 24 insertions(+), 14 deletions(-)

044dff6523c25e173eb7fb1c5d5c8a8e6ada8fdc
diff --git a/commit.c b/commit.c
index 2717dd8..4a26070 100644
--- a/commit.c
+++ b/commit.c
@@ -22,23 +22,33 @@ struct sort_node
 
 const char *commit_type = "commit";
 
+struct cmt_fmt_map {
+	const char *n;
+	size_t cmp_len;
+	enum cmit_fmt v;
+} cmt_fmts[] = {
+	{ "raw",	1,	CMIT_FMT_RAW },
+	{ "medium",	1,	CMIT_FMT_MEDIUM },
+	{ "short",	1,	CMIT_FMT_SHORT },
+	{ "full",	5,	CMIT_FMT_FULL },
+	{ "fuller",	5,	CMIT_FMT_FULLER },
+	{ "oneline",	1,	CMIT_FMT_ONELINE },
+};
+
 enum cmit_fmt get_commit_format(const char *arg)
 {
-	if (!*arg)
+	int i;
+
+	if (!arg || !*arg)
 		return CMIT_FMT_DEFAULT;
-	if (!strcmp(arg, "=raw"))
-		return CMIT_FMT_RAW;
-	if (!strcmp(arg, "=medium"))
-		return CMIT_FMT_MEDIUM;
-	if (!strcmp(arg, "=short"))
-		return CMIT_FMT_SHORT;
-	if (!strcmp(arg, "=full"))
-		return CMIT_FMT_FULL;
-	if (!strcmp(arg, "=fuller"))
-		return CMIT_FMT_FULLER;
-	if (!strcmp(arg, "=oneline"))
-		return CMIT_FMT_ONELINE;
-	die("invalid --pretty format");
+	if (*arg == '=')
+		arg++;
+	for (i = 0; i < ARRAY_SIZE(cmt_fmts); i++) {
+		if (!strncmp(arg, cmt_fmts[i].n, cmt_fmts[i].cmp_len))
+			return cmt_fmts[i].v;
+	}
+
+	die("invalid --pretty format: %s", arg);
 }
 
 static struct commit *check_commit(struct object *obj,
-- 
1.3.2.g7d11

-
: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]