[PATCH] convert shortlog to use parse_options

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

 



The behavior should be identical, except that we now support
option bundling (e.g., "git shortlog -nse").

Signed-off-by: Jeff King <peff@xxxxxxxx>
---
Another of my pre-1.5.4 cleanups (I think we may enter 1.5.5 freeze
before I get through my backlog!). This was originally rejected because
it changed the semantics of "-w" to allow "-w X,Y,Z". However, Pierre's
c43a24834 forces short options to stick.

This is also, of course, respun on top of Daniel's shortlog changes.

 builtin-shortlog.c |   62 ++++++++++++++++++++++++++++++---------------------
 1 files changed, 36 insertions(+), 26 deletions(-)

diff --git a/builtin-shortlog.c b/builtin-shortlog.c
index af31aba..bf6802e 100644
--- a/builtin-shortlog.c
+++ b/builtin-shortlog.c
@@ -7,9 +7,12 @@
 #include "utf8.h"
 #include "mailmap.h"
 #include "shortlog.h"
+#include "parse-options.h"
 
-static const char shortlog_usage[] =
-"git-shortlog [-n] [-s] [-e] [<commit-id>... ]";
+static const char * const shortlog_usage[] = {
+	"git-shortlog [-n] [-s] [-e] [<commit-id>... ]",
+	NULL
+};
 
 static int compare_by_number(const void *a1, const void *a2)
 {
@@ -188,8 +191,6 @@ static const char wrap_arg_usage[] = "-w[<width>[,<indent1>[,<indent2>]]]";
 
 static void parse_wrap_args(const char *arg, int *in1, int *in2, int *wrap)
 {
-	arg += 2; /* skip -w */
-
 	*wrap = parse_uint(&arg, ',');
 	if (*wrap < 0)
 		die(wrap_arg_usage);
@@ -212,6 +213,16 @@ static void parse_wrap_args(const char *arg, int *in1, int *in2, int *wrap)
 		die(wrap_arg_usage);
 }
 
+static int parse_wrap_callback(const struct option *opt, const char *arg,
+		int unset)
+{
+	struct shortlog *log = opt->value;
+	log->wrap_lines = 1;
+	if (arg)
+		parse_wrap_args(arg, &log->wrap, &log->in1, &log->in2);
+	return 0;
+}
+
 void shortlog_init(struct shortlog *log)
 {
 	memset(log, 0, sizeof(*log));
@@ -231,29 +242,28 @@ int cmd_shortlog(int argc, const char **argv, const char *prefix)
 
 	shortlog_init(&log);
 
-	/* since -n is a shadowed rev argument, parse our args first */
-	while (argc > 1) {
-		if (!strcmp(argv[1], "-n") || !strcmp(argv[1], "--numbered"))
-			log.sort_by_number = 1;
-		else if (!strcmp(argv[1], "-s") ||
-				!strcmp(argv[1], "--summary"))
-			log.summary = 1;
-		else if (!strcmp(argv[1], "-e") ||
-			 !strcmp(argv[1], "--email"))
-			log.email = 1;
-		else if (!prefixcmp(argv[1], "-w")) {
-			log.wrap_lines = 1;
-			parse_wrap_args(argv[1], &log.in1, &log.in2, &log.wrap);
-		}
-		else if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help"))
-			usage(shortlog_usage);
-		else
-			break;
-		argv++;
-		argc--;
-	}
+	struct option options[] = {
+		OPT_BOOLEAN('n', "numbered", &log.sort_by_number,
+				"sort by number"),
+		OPT_BOOLEAN('s', "summary", &log.summary,
+				"show commit count summary"),
+		OPT_BOOLEAN('e', "email", &log.email, "show email addresses"),
+		{
+			OPTION_CALLBACK, 'w', NULL, &log, "wrap",
+			"line-wrap commit message", PARSE_OPT_OPTARG,
+			parse_wrap_callback
+		},
+		OPT_END()
+	};
+
+	argc = parse_options(argc, argv, options, shortlog_usage, 0);
+
 	init_revisions(&rev, prefix);
-	argc = setup_revisions(argc, argv, &rev, NULL);
+	/* setup_revisions expects to have the program name in argv[0],
+	 * but parse_options has removed it. We have to do parse_options
+	 * before setup_revisions, though, to claim "-n". */
+	argc = setup_revisions(argc+1, argv-1, &rev, NULL);
+
 	if (argc > 1)
 		die ("unrecognized argument: %s", argv[1]);
 
-- 
1.5.4.3.341.g4e44
--
To unsubscribe from this list: 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]

  Powered by Linux