Skip to contentSkip to conversation

gitopt - command-line parsing enhancements (take #2)

PATCH/RFC In review Eric Wong 3 participants 12 messages Jun 15, 2016 Kbuild

lore
Share & tools
lore

Eric Wong

author
#0 lore raw

This should be a less scary series of patches for gitopt.

[PATCH 1/5] gitopt: a new command-line option parser for git

[PATCH 2/5] gitopt: convert ls-files, ls-tree, update-index
Simple conversions.

[PATCH 3/5] gitopt: convert setup_revisions() and friends
This one is pretty big, some extra testing + review would be nice.

[PATCH 4/5] commit: allow --pretty= args to be abbreviated
Not strictly related to gitopt, but finger-friendly nevertheless.

[PATCH 5/5] diff: parse U/u/unified options with an optional integer arg
Originally, this was bundled into:
<[email protected]>,
Then Linus did a more correct one that didn't forget combine-diff:
<[email protected]>
This one combines both of them.

author
#1 lore raw

PATCH 1/5 gitopt: a new command-line option parser for git

It was initially conceived as an addition to the git.c wrapper, and not affect other programs. But it turns out existing C programs can use it pretty easily, too.

Features include:

* getopt-style permuting (can easily be disabled for things like update-index)

* command-line compatibile with existing usage:
-S=pickaxe-arg-with-leading-equals is unchanged

* printf-style rewriting (for front-ending shell scripts)

* unbundling of short options: -un20z => -u -n20 -z

* automatically understands unambiguous abbreviations

* optional argument handling (-C<num>, -M<num>) -C <num> (with a space between them) has not changed, however, <num> can be a sha1, or a path

Changes from the initial patch:

* automatically handle rewrites when not in pass-through mode (pass-through mode is used for git wrapper only, usually for shell scripts).

* Fixed an off-by-one error in parse_bundled that could cause a segfault

* Supports being called as an iterator mode, as suggested by Junio, meaning:

* no additional global variables required for converting existing C programs

* no more scary macros :)

---

 .gitignore           |    1 
 Makefile             |   10 +
 git.c                |   11 +
 gitopt.c             |  662 ++++++++++++++++++++++++++++++++++++++++++++++++++
 gitopt.h             |  120 +++++++++
 gitopt/git_wrapper.h |   45 +++
 gitopt/sh.h          |   45 +++
 t/t0200-gitopt.sh    |  295 ++++++++++++++++++++++
 test-gitopt.c        |  112 ++++++++
 9 files changed, 1296 insertions(+), 5 deletions(-)
 create mode 100644 gitopt.c
 create mode 100644 gitopt.h
 create mode 100644 gitopt/git_wrapper.h
 create mode 100644 gitopt/sh.h
 create mode 100755 t/t0200-gitopt.sh
 create mode 100644 test-gitopt.c

eea531f2f17355161d58b61c3371f496f12c5364

.gitignore +1 -0
index b5959d6..b2d8b06 100644
@@ -123,6 +123,7 @@ git-write-tree
 git-core-*/?*
 test-date
 test-delta
+test-gitopt
 common-cmds.h
 *.tar.gz
 *.dsc
Makefile +8 -2
index 37fbe78..8fd3e13 100644
@@ -197,7 +197,7 @@ LIB_H = \
 	blob.h cache.h commit.h csum-file.h delta.h \
 	diff.h object.h pack.h pkt-line.h quote.h refs.h \
 	run-command.h strbuf.h tag.h tree.h git-compat-util.h revision.h \
-	tree-walk.h log-tree.h
+	tree-walk.h log-tree.h gitopt.h
 
 DIFF_OBJS = \
 	diff.o diff-lib.o diffcore-break.o diffcore-order.o \
@@ -212,6 +212,7 @@ LIB_OBJS = \
 	server-info.o setup.o sha1_file.o sha1_name.o strbuf.o \
 	tag.o tree.o usage.o config.o environment.o ctype.o copy.o \
 	fetch-clone.o revision.o pager.o tree-walk.o xdiff-interface.o \
+	gitopt.o \
 	$(DIFF_OBJS)
 
 BUILTIN_OBJS = \
@@ -470,6 +471,8 @@ all:
 strip: $(PROGRAMS) git$X
 	$(STRIP) $(STRIP_OPTS) $(PROGRAMS) git$X
 
+gitopt.o: gitopt.c gitopt.h gitopt/*.h
+
 git$X: git.c common-cmds.h $(BUILTIN_OBJS) $(GITLIBS)
 	$(CC) -DGIT_VERSION='"$(GIT_VERSION)"' \
 		$(ALL_CFLAGS) -o $@ $(filter %.c,$^) \
@@ -600,7 +603,7 @@ # with that.
 
 export NO_PYTHON
 
-test: all
+test: all test-gitopt$X
 	$(MAKE) -C t/ all
 
 test-date$X: test-date.c date.o ctype.o
@@ -609,6 +612,9 @@ test-date$X: test-date.c date.o ctype.o
 test-delta$X: test-delta.c diff-delta.o patch-delta.o
 	$(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) $^
 
+test-gitopt$X: test-gitopt.c gitopt.o ctype.o usage.o
+	$(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) $^
+
 check:
 	for i in *.c; do sparse $(ALL_CFLAGS) $(SPARSE_FLAGS) $$i || exit; done
 
git.c +8 -3
index 49ba518..785d97f 100644
@@ -11,6 +11,7 @@ #include <stdarg.h>
 #include "git-compat-util.h"
 #include "exec_cmd.h"
 
+#include "gitopt/git_wrapper.h"
 #include "builtin.h"
 
 static void prepend_to_path(const char *dir, int len)
@@ -72,6 +73,7 @@ int main(int argc, const char **argv, ch
 	char *slash = strrchr(cmd, '/');
 	char git_command[PATH_MAX + 1];
 	const char *exec_path = NULL;
+	struct exec_args *ea;
 
 	/*
 	 * Take the basename of argv[0] as the command
@@ -99,7 +101,8 @@ int main(int argc, const char **argv, ch
 	if (!strncmp(cmd, "git-", 4)) {
 		cmd += 4;
 		argv[0] = cmd;
-		handle_internal_command(argc, argv, envp);
+		ea = gitopt_parse_git(argc, argv);
+		handle_internal_command(ea->argc, ea->argv, envp);
 		die("cannot handle %s internally", cmd);
 	}
 
@@ -144,6 +147,8 @@ int main(int argc, const char **argv, ch
 	}
 	argv[0] = cmd;
 
+	ea = gitopt_parse_git(argc, argv);
+
 	/*
 	 * We search for git commands in the following order:
 	 *  - git_exec_path()
@@ -157,10 +162,10 @@ int main(int argc, const char **argv, ch
 	prepend_to_path(exec_path, strlen(exec_path));
 
 	/* See if it's an internal command */
-	handle_internal_command(argc, argv, envp);
+	handle_internal_command(ea->argc, ea->argv, envp);
 
 	/* .. then try the external ones */
-	execv_git_cmd(argv);
+	execv_git_cmd(ea->argv);
 
 	if (errno == ENOENT)
 		cmd_usage(0, exec_path, "'%s' is not a git-command", cmd);
gitopt.c new file +662 -0

662 lines not on this page · raw message

gitopt.h new file +120 -0

120 lines not on this page · raw message

gitopt/git_wrapper.h new file +45 -0

45 lines not on this page · raw message

gitopt/sh.h new file +45 -0

45 lines not on this page · raw message

t/t0200-gitopt.sh new file +295 -0

295 lines not on this page · raw message

test-gitopt.c new file +112 -0

112 lines not on this page · raw message

Signature · 1 line
1.3.2.g102e322
author
#2 lore raw

PATCH 2/5 gitopt: convert ls-files, ls-tree, update-index

In reply to Eric Wong

All of these are pretty straighforward conversions

---

 ls-files.c     |  184 +++++++++++++++++++++-------------------
 ls-tree.c      |   82 ++++++++++++------
 update-index.c |  257 ++++++++++++++++++++++++++++++--------------------------
 3 files changed, 287 insertions(+), 236 deletions(-)

962175b2be70eab14997a10ebb9c7cd719d72c1b

ls-files.c +96 -88
index 4a4af1c..9f4d328 100644
@@ -10,6 +10,7 @@ #include <fnmatch.h>
 
 #include "cache.h"
 #include "quote.h"
+#include "gitopt.h"
 
 static int abbrev = 0;
 static int show_deleted = 0;
@@ -648,133 +649,140 @@ static const char ls_files_usage[] =
 	"[ --exclude-per-directory=<filename> ] [--full-name] [--abbrev] "
 	"[--] [<file>]*";
 
+enum ls_files_ids {
+	opt_z = 1, opt_v, opt_t, opt_c, opt_d, opt_m, opt_o, opt_i, opt_s,
+	opt_k, opt_directory, opt_no_empty_directory,
+	opt_u, opt_x, opt_X, opt_exclude_per_dir,
+	opt_abbrev, opt_full_name, opt_error_unmatch
+};
+
+static const struct opt_spec ls_files_ost[] = {
+	{ 0,			'z',	0,	0,	opt_z },
+	{ 0,			'v',	0,	0,	opt_v },
+	{ 0,			't',	0,	0,	opt_t },
+	{ "cached",		'c',	0,	0,	opt_c },
+	{ "deleted",		'd',	0,	0,	opt_d },
+	{ "modified",		'm',	0,	0,	opt_m },
+	{ "others",		'o',	0,	0,	opt_o },
+	{ "ignored",		'i',	0,	0,	opt_i },
+	{ "stage",		's',	0,	0,	opt_s },
+	{ "killed",		'k',	0,	0,	opt_k },
+	{ "directory",		0,	0,	0,	opt_directory },
+	{ "no-empty-directory",	0,	0,	0,	opt_no_empty_directory},
+	{ "unmerged",		'u',	0,	0,	opt_u },
+	{ "exclude",		'x',	0,	ARG_ONE,	opt_x },
+	{ "exclude-from",	'X',	0,	ARG_ONE,	opt_X },
+	{ "exclude-per-directory",0,	0,	ARG_ONE, opt_exclude_per_dir },
+	{ "full-name",		0,	0,	0,	opt_full_name },
+	{ "error-unmatch",	0,	0,	0,	opt_error_unmatch },
+	{ "abbrev",		0,	0,	ARG_OPTINT, opt_abbrev },
+	{ 0, 0 }
+};
+
 int main(int argc, const char **argv)
 {
 	int i;
 	int exc_given = 0;
+	struct gitopt_iterator gi;
+	struct exec_args *ea, *b;
 
 	prefix = setup_git_directory();
 	if (prefix)
 		prefix_offset = strlen(prefix);
 	git_config(git_default_config);
 
-	for (i = 1; i < argc; i++) {
-		const char *arg = argv[i];
-
-		if (!strcmp(arg, "--")) {
-			i++;
-			break;
-		}
-		if (!strcmp(arg, "-z")) {
+	gitopt_iter_setup(&gi, argc, argv);
+	ea = gi.ea;
+	b = gi.b;
+	for (; (i = gitopt_iter_parse(&gi, ls_files_ost));
+				gitopt_iter_next(&gi)) {
+		switch (i) {
+		case opt_z:
 			line_terminator = 0;
-			continue;
-		}
-		if (!strcmp(arg, "-t") || !strcmp(arg, "-v")) {
+			break;
+		case opt_v:
+			show_valid_bit = 1; /* fall through */
+		case opt_t:
 			tag_cached = "H ";
 			tag_unmerged = "M ";
 			tag_removed = "R ";
 			tag_modified = "C ";
 			tag_other = "? ";
 			tag_killed = "K ";
-			if (arg[1] == 'v')
-				show_valid_bit = 1;
-			continue;
-		}
-		if (!strcmp(arg, "-c") || !strcmp(arg, "--cached")) {
+			break;
+		case opt_c:
 			show_cached = 1;
-			continue;
-		}
-		if (!strcmp(arg, "-d") || !strcmp(arg, "--deleted")) {
+			break;
+		case opt_d:
 			show_deleted = 1;
-			continue;
-		}
-		if (!strcmp(arg, "-m") || !strcmp(arg, "--modified")) {
+			break;
+		case opt_m:
 			show_modified = 1;
-			continue;
-		}
-		if (!strcmp(arg, "-o") || !strcmp(arg, "--others")) {
+			break;
+		case opt_o:
 			show_others = 1;
-			continue;
-		}
-		if (!strcmp(arg, "-i") || !strcmp(arg, "--ignored")) {
+			break;
+		case opt_i:
 			show_ignored = 1;
-			continue;
-		}
-		if (!strcmp(arg, "-s") || !strcmp(arg, "--stage")) {
+			break;
+		case opt_s:
 			show_stage = 1;
-			continue;
-		}
-		if (!strcmp(arg, "-k") || !strcmp(arg, "--killed")) {
+			break;
+		case opt_k:
 			show_killed = 1;
-			continue;
-		}
-		if (!strcmp(arg, "--directory")) {
+			break;
+		case opt_directory:
 			show_other_directories = 1;
-			continue;
-		}
-		if (!strcmp(arg, "--no-empty-directory")) {
+			break;
+		case opt_no_empty_directory:
 			hide_empty_directories = 1;
-			continue;
-		}
-		if (!strcmp(arg, "-u") || !strcmp(arg, "--unmerged")) {
+			break;
+		case opt_u:
 			/* There's no point in showing unmerged unless
 			 * you also show the stage information.
 			 */
 			show_stage = 1;
 			show_unmerged = 1;
-			continue;
-		}
-		if (!strcmp(arg, "-x") && i+1 < argc) {
-			exc_given = 1;
-			add_exclude(argv[++i], "", 0, &exclude_list[EXC_CMDL]);
-			continue;
-		}
-		if (!strncmp(arg, "--exclude=", 10)) {
-			exc_given = 1;
-			add_exclude(arg+10, "", 0, &exclude_list[EXC_CMDL]);
-			continue;
-		}
-		if (!strcmp(arg, "-X") && i+1 < argc) {
+			break;
+		case opt_x:
 			exc_given = 1;
-			add_excludes_from_file(argv[++i]);
-			continue;
-		}
-		if (!strncmp(arg, "--exclude-from=", 15)) {
+			add_exclude(ea->argv[0], "", 0, &exclude_list[EXC_CMDL]);
+			break;
+		case opt_X:
 			exc_given = 1;
-			add_excludes_from_file(arg+15);
-			continue;
-		}
-		if (!strncmp(arg, "--exclude-per-directory=", 24)) {
+			add_excludes_from_file(ea->argv[0]);
+			break;
+		case opt_exclude_per_dir:
 			exc_given = 1;
-			exclude_per_dir = arg + 24;
-			continue;
-		}
-		if (!strcmp(arg, "--full-name")) {
+			exclude_per_dir = ea->argv[0];
+			break;
+		case opt_full_name:
 			prefix_offset = 0;
-			continue;
-		}
-		if (!strcmp(arg, "--error-unmatch")) {
+			break;
+		case opt_error_unmatch:
 			error_unmatch = 1;
-			continue;
-		}
-		if (!strncmp(arg, "--abbrev=", 9)) {
-			abbrev = strtoul(arg+9, NULL, 10);
-			if (abbrev && abbrev < MINIMUM_ABBREV)
-				abbrev = MINIMUM_ABBREV;
-			else if (abbrev > 40)
-				abbrev = 40;
-			continue;
-		}
-		if (!strcmp(arg, "--abbrev")) {
-			abbrev = DEFAULT_ABBREV;
-			continue;
-		}
-		if (*arg == '-')
+			break;
+		case opt_abbrev:
+			if (ea->argc == 1)
+				abbrev = DEFAULT_ABBREV;
+			else {
+				if (abbrev && abbrev < MINIMUM_ABBREV)
+					abbrev = MINIMUM_ABBREV;
+				else if (abbrev > 40)
+					abbrev = 40;
+			}
+			break;
+		case GITOPT_DD:
+			break;
+		case GITOPT_NON_OPTION:
+			b->argv[b->argc++] = argv[gi.pos];
+			break;
+		case GITOPT_ERROR:
 			usage(ls_files_usage);
-		break;
+		}
 	}
 
-	pathspec = get_pathspec(prefix, argv + i);
+	pathspec = get_pathspec(prefix, b->argv);
 
 	/* Verify that the pathspec matches the prefix */
 	if (pathspec)
ls-tree.c +53 -29

125 lines not on this page · raw message

update-index.c +138 -119

282 lines not on this page · raw message

Signature · 1 line
1.3.2.g102e322
author
#3 lore raw

PATCH 3/5 gitopt: convert setup_revisions() and friends

In reply to Eric Wong

diff_opt_parse => diff_opt_handle

I've added --raw to diff_opt_handle() for consistency's sake

Lightly tested, some bugs in the original series of submitted patches were fixed wrt argument parsing for -C,-M,-B).

Passes all tests so that's a good sign.

---

 builtin-diff.c      |  152 +++++++-------
 builtin-log.c       |    7 -
 commit.c            |   14 +
 diff-files.c        |   25 +-
 diff-index.c        |   18 +-
 diff-stages.c       |   56 +++--
 diff-tree.c         |   15 +
 diff.c              |  183 ++++++++++++-----
 diff.h              |    7 -
 gitopt/diff-files.h |   45 ++++
 gitopt/diff-index.h |   22 ++
 gitopt/diff-tree.h  |   22 ++
 gitopt/diff.h       |   19 ++
 http-push.c         |    2 
 rev-list.c          |   51 +++--
 revision.c          |  542 +++++++++++++++++++++++++++------------------------
 revision.h          |    4 
 17 files changed, 704 insertions(+), 480 deletions(-)
 create mode 100644 gitopt/diff-files.h
 create mode 100644 gitopt/diff-index.h
 create mode 100644 gitopt/diff-tree.h
 create mode 100644 gitopt/diff.h

946397f743f363198d154efaa74d5cc5bd9d6aae

builtin-diff.c +76 -76
index d3ac581..3aefd3c 100644
@@ -12,6 +12,9 @@ #include "diffcore.h"
 #include "revision.h"
 #include "log-tree.h"
 #include "builtin.h"
+#include "gitopt/diff-index.h"
+#include "gitopt/diff-files.h"
+#include "gitopt/diff-tree.h"
 
 /* NEEDSWORK: struct object has place for name but we _do_
  * know mode when we extracted the blob out of a tree, which
@@ -25,26 +28,25 @@ struct blobinfo {
 static const char builtin_diff_usage[] =
 "diff <options> <rev>{0,2} -- <path>*";
 
+static int extra_diff_flags[LAST_DIFF_EXTRA_ID] = { 0 };
+
 static int builtin_diff_files(struct rev_info *revs,
 			      int argc, const char **argv)
 {
-	int silent = 0;
-	while (1 < argc) {
-		const char *arg = argv[1];
-		if (!strcmp(arg, "--base"))
-			revs->max_count = 1;
-		else if (!strcmp(arg, "--ours"))
-			revs->max_count = 2;
-		else if (!strcmp(arg, "--theirs"))
-			revs->max_count = 3;
-		else if (!strcmp(arg, "-q"))
-			silent = 1;
-		else if (!strcmp(arg, "--raw"))
-			revs->diffopt.output_format = DIFF_FORMAT_RAW;
-		else
+	int i;
+	struct df_extra_opts dfeo = { revs, 0 };
+
+	if (argc)
+		usage(builtin_diff_usage);
+
+	for (i = 1; i < ARRAY_SIZE(extra_diff_flags); i++) {
+		if (extra_diff_flags[i]) {
+			if (diff_files_opt_handler(NULL, i, &dfeo) > 0)
+				continue;
 			usage(builtin_diff_usage);
-		argv++; argc--;
+		}
 	}
+
 	/*
 	 * Make sure there are NO revision (i.e. pending object) parameter,
 	 * specified rev.max_count is reasonable (0 <= n <= 3), and
@@ -65,7 +67,7 @@ static int builtin_diff_files(struct rev
 	 */
 	if (revs->diffopt.output_format == DIFF_FORMAT_NO_OUTPUT)
 		revs->diffopt.output_format = DIFF_FORMAT_RAW;
-	return run_diff_files(revs, silent);
+	return run_diff_files(revs, dfeo.silent);
 }
 
 static void stuff_change(struct diff_options *opt,
@@ -107,14 +109,9 @@ static int builtin_diff_b_f(struct rev_i
 	/* Blob vs file in the working tree*/
 	struct stat st;
 
-	while (1 < argc) {
-		const char *arg = argv[1];
-		if (!strcmp(arg, "--raw"))
-			revs->diffopt.output_format = DIFF_FORMAT_RAW;
-		else
-			usage(builtin_diff_usage);
-		argv++; argc--;
-	}
+	if (argc)
+		usage(builtin_diff_usage);
+
 	if (lstat(path, &st))
 		die("'%s': %s", path, strerror(errno));
 	if (!(S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)))
@@ -135,14 +132,9 @@ static int builtin_diff_blobs(struct rev
 	/* Blobs */
 	unsigned mode = canon_mode(S_IFREG | 0644);
 
-	while (1 < argc) {
-		const char *arg = argv[1];
-		if (!strcmp(arg, "--raw"))
-			revs->diffopt.output_format = DIFF_FORMAT_RAW;
-		else
-			usage(builtin_diff_usage);
-		argv++; argc--;
-	}
+	if (argc)
+		usage(builtin_diff_usage);
+
 	stuff_change(&revs->diffopt,
 		     mode, mode,
 		     blob[0].sha1, blob[1].sha1,
@@ -155,17 +147,19 @@ static int builtin_diff_blobs(struct rev
 static int builtin_diff_index(struct rev_info *revs,
 			      int argc, const char **argv)
 {
-	int cached = 0;
-	while (1 < argc) {
-		const char *arg = argv[1];
-		if (!strcmp(arg, "--cached"))
-			cached = 1;
-		else if (!strcmp(arg, "--raw"))
-			revs->diffopt.output_format = DIFF_FORMAT_RAW;
-		else
+	int i, cached = 0;
+
+	if (argc)
+		usage(builtin_diff_usage);
+
+	for (i = 1; i < ARRAY_SIZE(extra_diff_flags); i++) {
+		if (extra_diff_flags[i]) {
+			if (diff_index_opt_handler(NULL, i, &cached) > 0)
+				continue;
 			usage(builtin_diff_usage);
-		argv++; argc--;
+		}
 	}
+
 	/*
 	 * Make sure there is one revision (i.e. pending object),
 	 * and there is no revision filtering parameters.
@@ -183,14 +177,9 @@ static int builtin_diff_tree(struct rev_
 {
 	const unsigned char *(sha1[2]);
 	int swap = 1;
-	while (1 < argc) {
-		const char *arg = argv[1];
-		if (!strcmp(arg, "--raw"))
-			revs->diffopt.output_format = DIFF_FORMAT_RAW;
-		else
-			usage(builtin_diff_usage);
-		argv++; argc--;
-	}
+
+	if (argc)
+		usage(builtin_diff_usage);
 
 	/* We saw two trees, ent[0] and ent[1].
 	 * unless ent[0] is unintesting, they are swapped
@@ -212,14 +201,9 @@ static int builtin_diff_combined(struct 
 	const unsigned char (*parent)[20];
 	int i;
 
-	while (1 < argc) {
-		const char *arg = argv[1];
-		if (!strcmp(arg, "--raw"))
-			revs->diffopt.output_format = DIFF_FORMAT_RAW;
-		else
-			usage(builtin_diff_usage);
-		argv++; argc--;
-	}
+	if (argc)
+		usage(builtin_diff_usage);
+
 	if (!revs->dense_combined_merges && !revs->combine_merges)
 		revs->dense_combined_merges = revs->combine_merges = 1;
 	parent = xmalloc(ents * sizeof(*parent));
@@ -243,6 +227,27 @@ static void add_head(struct rev_info *re
 	add_object(obj, &revs->pending_objects, NULL, "HEAD");
 }
 
+static struct opt_spec * diff_combined_ost()
+{
+	struct opt_spec *rv, *tmp;
+
+	tmp = combine_opt_spec(diff_files_ost, diff_tree_ost);
+	rv = combine_opt_spec(tmp, diff_index_ost);
+	free(tmp);
+	return rv;
+}
+
+/* just set flags in here for use by the builtin_diff_* functions  */
+static int diff_combined_handler(struct gitopt_iterator *gi,
+				const int id, void *args)
+{
+	if (id > 0 && id < LAST_DIFF_EXTRA_ID) {
+		extra_diff_flags[id] = 1;
+		return 1;
+	}
+	return 0;
+}
+
 int cmd_diff(int argc, const char **argv, char **envp)
 {
 	struct rev_info rev;
@@ -250,6 +255,9 @@ int cmd_diff(int argc, const char **argv
 	int ents = 0, blobs = 0, paths = 0;
 	const char *path = NULL;
 	struct blobinfo blob[2];
+	struct exec_args *b;
+	struct opt_spec *ost = diff_combined_ost();
+	struct gitopt_extra ge = { ost, diff_combined_handler, NULL };
 
 	/*
 	 * We could get N tree-ish in the rev.pending_objects list.
@@ -275,22 +283,14 @@ int cmd_diff(int argc, const char **argv
 	init_revisions(&rev);
 	rev.diffopt.output_format = DIFF_FORMAT_PATCH;
 
-	argc = setup_revisions(argc, argv, &rev, NULL);
+	b = setup_revisions(argc, argv, &rev, NULL, &ge);
+	free(ost);
+
 	/* Do we have --cached and not have a pending object, then
 	 * default to HEAD by hand.  Eek.
 	 */
-	if (!rev.pending_objects) {
-		int i;
-		for (i = 1; i < argc; i++) {
-			const char *arg = argv[i];
-			if (!strcmp(arg, "--"))
-				break;
-			else if (!strcmp(arg, "--cached")) {
-				add_head(&rev);
-				break;
-			}
-		}
-	}
+	if (!rev.pending_objects && extra_diff_flags[opt_cached])
+		add_head(&rev);
 
 	for (list = rev.pending_objects; list; list = list->next) {
 		struct object *obj = list->item;
@@ -340,17 +340,17 @@ int cmd_diff(int argc, const char **argv
 	if (!ents) {
 		switch (blobs) {
 		case 0:
-			return builtin_diff_files(&rev, argc, argv);
+			return builtin_diff_files(&rev, b->argc, b->argv);
 			break;
 		case 1:
 			if (paths != 1)
 				usage(builtin_diff_usage);
-			return builtin_diff_b_f(&rev, argc, argv, blob, path);
+			return builtin_diff_b_f(&rev, b->argc, b->argv, blob, path);
 			break;
 		case 2:
 			if (paths)
 				usage(builtin_diff_usage);
-			return builtin_diff_blobs(&rev, argc, argv, blob);
+			return builtin_diff_blobs(&rev, b->argc, b->argv, blob);
 			break;
 		default:
 			usage(builtin_diff_usage);
@@ -359,10 +359,10 @@ int cmd_diff(int argc, const char **argv
 	else if (blobs)
 		usage(builtin_diff_usage);
 	else if (ents == 1)
-		return builtin_diff_index(&rev, argc, argv);
+		return builtin_diff_index(&rev, b->argc, b->argv);
 	else if (ents == 2)
-		return builtin_diff_tree(&rev, argc, argv, ent);
+		return builtin_diff_tree(&rev, b->argc, b->argv, ent);
 	else
-		return builtin_diff_combined(&rev, argc, argv, ent, ents);
+		return builtin_diff_combined(&rev, b->argc, b->argv, ent, ents);
 	usage(builtin_diff_usage);
 }
builtin-log.c +4 -3
index 69f2911..3cfc8ac 100644
@@ -14,14 +14,15 @@ static int cmd_log_wc(int argc, const ch
 		      struct rev_info *rev)
 {
 	struct commit *commit;
+	struct exec_args *b;
 
 	rev->abbrev = DEFAULT_ABBREV;
 	rev->commit_format = CMIT_FMT_DEFAULT;
 	rev->verbose_header = 1;
-	argc = setup_revisions(argc, argv, rev, "HEAD");
+	b = setup_revisions(argc, argv, rev, "HEAD", NULL);
 
-	if (argc > 1)
-		die("unrecognized argument: %s", argv[1]);
+	if (b->argc > 1)
+		die("unrecognized argument: %s", b->argv[0]);
 
 	prepare_revision_walk(rev);
 	setup_pager();
commit.c +7 -7
index 2717dd8..2343729 100644
@@ -24,19 +24,19 @@ const char *commit_type = "commit";
 
 enum cmit_fmt get_commit_format(const char *arg)
 {
-	if (!*arg)
+	if (!arg)
 		return CMIT_FMT_DEFAULT;
-	if (!strcmp(arg, "=raw"))
+	if (!strcmp(arg, "raw"))
 		return CMIT_FMT_RAW;
-	if (!strcmp(arg, "=medium"))
+	if (!strcmp(arg, "medium"))
 		return CMIT_FMT_MEDIUM;
-	if (!strcmp(arg, "=short"))
+	if (!strcmp(arg, "short"))
 		return CMIT_FMT_SHORT;
-	if (!strcmp(arg, "=full"))
+	if (!strcmp(arg, "full"))
 		return CMIT_FMT_FULL;
-	if (!strcmp(arg, "=fuller"))
+	if (!strcmp(arg, "fuller"))
 		return CMIT_FMT_FULLER;
-	if (!strcmp(arg, "=oneline"))
+	if (!strcmp(arg, "oneline"))
 		return CMIT_FMT_ONELINE;
 	die("invalid --pretty format");
 }
diff-files.c +9 -16
index b9d193d..431487b 100644
@@ -7,6 +7,7 @@ #include "cache.h"
 #include "diff.h"
 #include "commit.h"
 #include "revision.h"
+#include "gitopt/diff-files.h"
 
 static const char diff_files_usage[] =
 "git-diff-files [-q] [-0/-1/2/3 |-c|--cc] [<common diff options>] [<path>...]"
@@ -15,26 +16,18 @@ COMMON_DIFF_OPTIONS_HELP;
 int main(int argc, const char **argv)
 {
 	struct rev_info rev;
-	int silent = 0;
+	struct exec_args *b;
+	struct df_extra_opts dfeo = { &rev, 0 };
+	struct gitopt_extra ge = { diff_files_ost, diff_files_opt_handler,
+				   &dfeo };
 
 	git_config(git_diff_config);
 	init_revisions(&rev);
 	rev.abbrev = 0;
 
-	argc = setup_revisions(argc, argv, &rev, NULL);
-	while (1 < argc && argv[1][0] == '-') {
-		if (!strcmp(argv[1], "--base"))
-			rev.max_count = 1;
-		else if (!strcmp(argv[1], "--ours"))
-			rev.max_count = 2;
-		else if (!strcmp(argv[1], "--theirs"))
-			rev.max_count = 3;
-		else if (!strcmp(argv[1], "-q"))
-			silent = 1;
-		else
-			usage(diff_files_usage);
-		argv++; argc--;
-	}
+	b = setup_revisions(argc, argv, &rev, NULL, &ge);
+	if (b->argc)
+		usage(diff_files_usage);
 	/*
 	 * Make sure there are NO revision (i.e. pending object) parameter,
 	 * rev.max_count is reasonable (0 <= n <= 3),
@@ -50,5 +43,5 @@ int main(int argc, const char **argv)
 	 */
 	if (rev.diffopt.output_format == DIFF_FORMAT_NO_OUTPUT)
 		rev.diffopt.output_format = DIFF_FORMAT_RAW;
-	return run_diff_files(&rev, silent);
+	return run_diff_files(&rev, dfeo.silent);
 }
diff-index.c +8 -10
index 8c9f601..987a00a 100644
@@ -2,6 +2,7 @@ #include "cache.h"
 #include "diff.h"
 #include "commit.h"
 #include "revision.h"
+#include "gitopt/diff-index.h"
 
 static const char diff_cache_usage[] =
 "git-diff-index [-m] [--cached] "
@@ -12,21 +13,18 @@ int main(int argc, const char **argv)
 {
 	struct rev_info rev;
 	int cached = 0;
-	int i;
+	struct exec_args *b;
+	struct gitopt_extra ge = { diff_index_ost,
+				diff_index_opt_handler, &cached };
 
 	git_config(git_diff_config);
 	init_revisions(&rev);
 	rev.abbrev = 0;
 
-	argc = setup_revisions(argc, argv, &rev, NULL);
-	for (i = 1; i < argc; i++) {
-		const char *arg = argv[i];
-			
-		if (!strcmp(arg, "--cached"))
-			cached = 1;
-		else
-			usage(diff_cache_usage);
-	}
+	b = setup_revisions(argc, argv, &rev, NULL, &ge);
+	if (b->argc)
+		usage(diff_cache_usage);
+
 	/*
 	 * Make sure there is one revision (i.e. pending object),
 	 * and there is no revision filtering parameters.
diff-stages.c +35 -21

81 lines not on this page · raw message

diff-tree.c +6 -9
index 7207867..b5c6072 100644
@@ -2,6 +2,7 @@ #include "cache.h"
 #include "diff.h"
 #include "commit.h"
 #include "log-tree.h"
+#include "gitopt/diff-tree.h"
 
 static struct rev_info log_tree_opt;
 
@@ -66,23 +67,19 @@ int main(int argc, const char **argv)
 	static struct rev_info *opt = &log_tree_opt;
 	struct object_list *list;
 	int read_stdin = 0;
+	struct exec_args *b;
+	struct gitopt_extra ge = { diff_tree_ost, diff_tree_opt_handler,
+				&read_stdin };
 
 	git_config(git_diff_config);
 	nr_sha1 = 0;
 	init_revisions(opt);
 	opt->abbrev = 0;
 	opt->diff = 1;
-	argc = setup_revisions(argc, argv, opt, NULL);
+	b = setup_revisions(argc, argv, opt, NULL, &ge);
 
-	while (--argc > 0) {
-		const char *arg = *++argv;
-
-		if (!strcmp(arg, "--stdin")) {
-			read_stdin = 1;
-			continue;
-		}
+	if (b->argc)
 		usage(diff_tree_usage);
-	}
 
 	/*
 	 * NOTE! "setup_revisions()" will have inserted the revisions
diff.c +123 -60

245 lines not on this page · raw message

diff.h +4 -3
index d052608..fa44d1b 100644
@@ -5,6 +5,7 @@ #ifndef DIFF_H
 #define DIFF_H
 
 #include "tree-walk.h"
+#include "gitopt.h"
 
 struct rev_info;
 struct diff_options;
@@ -96,15 +97,13 @@ extern void diff_change(struct diff_opti
 extern void diff_unmerge(struct diff_options *,
 			 const char *path);
 
-extern int diff_scoreopt_parse(const char *opt);
-
 #define DIFF_SETUP_REVERSE      	1
 #define DIFF_SETUP_USE_CACHE		2
 #define DIFF_SETUP_USE_SIZE_CACHE	4
 
 extern int git_diff_config(const char *var, const char *value);
 extern void diff_setup(struct diff_options *);
-extern int diff_opt_parse(struct diff_options *, const char **, int);
+extern int diff_opt_handler(struct gitopt_iterator *, const int, void *);
 extern int diff_setup_done(struct diff_options *);
 
 #define DIFF_DETECT_RENAME	1
@@ -117,6 +116,8 @@ extern void diffcore_std(struct diff_opt
 
 extern void diffcore_std_no_resolve(struct diff_options *);
 
+extern const struct opt_spec diff_ost[];
+
 #define COMMON_DIFF_OPTIONS_HELP \
 "\ncommon diff options:\n" \
 "  -z            output diff-raw with lines terminated with NUL.\n" \
gitopt/diff-files.h new file +45 -0

45 lines not on this page · raw message

gitopt/diff-index.h new file +22 -0
new file mode 100644
index 0000000..aff0944
@@ -0,0 +1,22 @@
+#ifndef GITOPT_DIFF_INDEX_H
+#define GITOPT_DIFF_INDEX_H
+
+#include "../gitopt/diff.h"
+
+static const struct opt_spec diff_index_ost[] = {
+	{ "cached",	0,	0,	0,	opt_cached },
+	{ 0, 0 }
+};
+
+static int diff_index_opt_handler(struct gitopt_iterator *gi,
+				const int id, void *args)
+{
+	if (id == opt_cached) {
+		*(int *)args = 1;
+		return 1;
+	}
+	return 0;
+}
+
+#endif
+
gitopt/diff-tree.h new file +22 -0
new file mode 100644
index 0000000..cfee9b5
@@ -0,0 +1,22 @@
+#ifndef GITOPT_DIFF_TREE_H
+#define GITOPT_DIFF_TREE_H
+
+#include "../gitopt/diff.h"
+
+static const struct opt_spec diff_tree_ost[] = {
+	{ "stdin",	0,	0,	0,	opt_stdin },
+	{ 0, 0 }
+};
+
+/* inline to suppress a warning as it's not used in builtin_diff_tree */
+static inline int diff_tree_opt_handler(struct gitopt_iterator *gi,
+				const int id, void *args)
+{
+	if (id == opt_stdin) {
+		*(int *)args = 1;
+		return 1;
+	}
+	return 0;
+}
+
+#endif
gitopt/diff.h new file +19 -0
new file mode 100644
index 0000000..abc2c7a
@@ -0,0 +1,19 @@
+#ifndef GITOPT_DIFF_H
+#define GITOPT_DIFF_H
+
+#include "../gitopt.h"
+
+enum diff_extra_ids {
+	/* diff-files */
+	opt_base = 1, opt_ours, opt_theirs, opt_q,
+
+	/* diff-tree */
+	opt_stdin,
+
+	/* diff-index */
+	opt_cached,
+
+	LAST_DIFF_EXTRA_ID,
+};
+
+#endif /* GITOPT_DIFF_H */
http-push.c +1 -1
index b4327d9..9c16f3b 100644
@@ -2499,7 +2499,7 @@ int main(int argc, char **argv)
 			commit_argc++;
 		}
 		init_revisions(&revs);
-		setup_revisions(commit_argc, commit_argv, &revs, NULL);
+		setup_revisions(commit_argc, commit_argv, &revs, NULL, NULL);
 		free(new_sha1_hex);
 		if (old_sha1_hex) {
 			free(old_sha1_hex);
rev-list.c +33 -18

67 lines not on this page · raw message

revision.c +287 -255

578 lines not on this page · raw message

revision.h +3 -1
index 48d7b4c..28211b6 100644
@@ -81,7 +81,9 @@ extern int rev_same_tree_as_empty(struct
 extern int rev_compare_tree(struct rev_info *, struct tree *t1, struct tree *t2);
 
 extern void init_revisions(struct rev_info *revs);
-extern int setup_revisions(int argc, const char **argv, struct rev_info *revs, const char *def);
+extern struct exec_args *setup_revisions(int, const char **argv,
+					struct rev_info *revs, const char *def,
+					const struct gitopt_extra *);
 extern void prepare_revision_walk(struct rev_info *revs);
 extern struct commit *get_revision(struct rev_info *revs);
 
Signature · 1 line
1.3.2.g102e322
author
#4 lore raw

PATCH 4/5 commit: allow --pretty= args to be abbreviated

In reply to Eric Wong

---

 commit.c |   42 +++++++++++++++++++++++++++++-------------
 1 files changed, 29 insertions(+), 13 deletions(-)

b6190a2b46acc250277fd55e33e0a7216b7fad75

commit.c +29 -13
index 2343729..a786371 100644
@@ -22,23 +22,39 @@ struct sort_node
 
 const char *commit_type = "commit";
 
+struct cmt_fmt_map {
+	const char *n;
+	enum cmit_fmt v;
+} cmt_fmts[] = {
+	{ "raw",	CMIT_FMT_RAW },
+	{ "medium",	CMIT_FMT_MEDIUM },
+	{ "short",	CMIT_FMT_SHORT },
+	{ "full",	CMIT_FMT_FULL },
+	{ "fuller",	CMIT_FMT_FULLER },
+	{ "oneline",	CMIT_FMT_ONELINE },
+};
+
 enum cmit_fmt get_commit_format(const char *arg)
 {
+	int i, found = -1;
 	if (!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");
+	for (i = 0; i < ARRAY_SIZE(cmt_fmts); i++) {
+		if (!strcmp(arg, cmt_fmts[i].n))
+			return cmt_fmts[i].v;
+	}
+
+	/* look for abbreviations */
+	for (i = 0; i < ARRAY_SIZE(cmt_fmts); i++) {
+		if (strstr(cmt_fmts[i].n, arg)) {
+			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);
 }
 
 static struct commit *check_commit(struct object *obj,
Signature · 1 line
1.3.2.g102e322
#5 lore raw

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

Quoted 2 lines · Eric Wong
 commit.c |   42 +++++++++++++++++++++++++++++-------------
 1 files changed, 29 insertions(+), 13 deletions(-)

This is applicable without the gitopt changes, but I have a feeling that when we think about abbreviations the users would expect the leading substring abbreviation, not strstr().

While "git log --pretty=lle" or "git log --pretty=or" might be unambiguous, I think that is trying to be too cute and confusing, especially if somebody picks up that habit by watching others type such a cute abbreviations.

That comment probably incidentally applies to your bigger patches.

author
#6 lore raw

PATCH 1/1 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.

---

Quoted 9 lines · Junio C Hamano
Quoted 2 lines · Eric Wong
 commit.c |   42 +++++++++++++++++++++++++++++-------------
 1 files changed, 29 insertions(+), 13 deletions(-)

This is applicable without the gitopt changes, but I have a feeling that when we think about abbreviations the users would expect the leading substring abbreviation, not strstr().

Quoted 7 lines

While "git log --pretty=lle" or "git log --pretty=or" might be unambiguous, I think that is trying to be too cute and confusing, especially if somebody picks up that habit by watching others type such a cute abbreviations.

That comment probably incidentally applies to your bigger patches.

Ok. The current gitopt patch only uses leading substring abbreviations by default. GIT_ABBREV_HARDER needs to be set if you want all the crazyness :)

 commit.c |   50 ++++++++++++++++++++++++++++++++++++--------------
 1 files changed, 36 insertions(+), 14 deletions(-)

d4473559924b1a5ba655cd5d5b18d39f53e25184

commit.c +36 -14
index 2717dd8..2753677 100644
@@ -22,23 +22,45 @@ struct sort_node
 
 const char *commit_type = "commit";
 
+struct cmt_fmt_map {
+	const char *n;
+	enum cmit_fmt v;
+} cmt_fmts[] = {
+	{ "raw",	CMIT_FMT_RAW },
+	{ "medium",	CMIT_FMT_MEDIUM },
+	{ "short",	CMIT_FMT_SHORT },
+	{ "full",	CMIT_FMT_FULL },
+	{ "fuller",	CMIT_FMT_FULLER },
+	{ "oneline",	CMIT_FMT_ONELINE },
+};
+
 enum cmit_fmt get_commit_format(const char *arg)
 {
-	if (!*arg)
+	int i, found;
+	size_t len;
+
+	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 (!strcmp(arg, cmt_fmts[i].n))
+			return cmt_fmts[i].v;
+	}
+
+	/* 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);
 }
 
 static struct commit *check_commit(struct object *obj,
Signature · 1 line
1.3.2.g58c0
#7 lore raw

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

Quoted 2 lines · Eric Wong

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?

Quoted 5 lines
commit.c +5 -0
+
+	/* look for abbreviations */
+	len = strlen(arg);
+	found = -1;
+	for (i = 0; i < ARRAY_SIZE(cmt_fmts); i++) {

commit.c struct sort_node · line 64

Quoted 10 lines
commit.c +9 -0
+		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.

author
#8 lore raw

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

Quoted 8 lines · Junio C Hamano
Quoted 2 lines · Eric Wong

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.

Quoted 10 lines
Quoted 10 lines
(quoted diff) +10 -0
+
+	/* 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;
+		}
Quoted 8 lines

Review

Quoted 5 lines
(quoted diff) +4 -0
+	}
+	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 :)

Signature · 1 line
Eric Wong
Quoted 1 line

From nobody Mon Sep 17 00:00:00 2001

From: Eric Wong <[email protected]>
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.

---

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

044dff6523c25e173eb7fb1c5d5c8a8e6ada8fdc

commit.c +24 -14
index 2717dd8..4a26070 100644
@@ -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,
Signature · 1 line
1.3.2.g7d11
author
#9 lore raw

PATCH 5/5 diff: parse U/u/unified options with an optional integer arg

In reply to Eric Wong

Looks like Linus and I both think GIT_DIFF_OPTS="--unified=5" is silly, but it still continues to work.

This was originally bundled into my first series gitopt patches, and now Linus has a more correct/complete one that affects combine-diff and works with uppercase -U This patch combines the Linus one with my gitopt version.

-u (lowercase) now accepts an optional arg, like -U (GNU diff -u also has this behavior).

This uses the built-in gitopt parsers to do optional integer argument handling.

---

 combine-diff.c |    1 +
 diff.c         |   11 ++++++++---
 diff.h         |    1 +
 3 files changed, 10 insertions(+), 3 deletions(-)

102e3227de7c7f08a69096d5094ee31128bf9819

combine-diff.c +1 -0
index 8a8fe38..64b20cc 100644
@@ -608,6 +608,7 @@ static int show_patch_diff(struct combin
 	int abbrev = opt->full_index ? 40 : DEFAULT_ABBREV;
 	mmfile_t result_file;
 
+	context = opt->context;
 	/* Read the result of merge first */
 	if (!working_tree_file)
 		result = grab_blob(elem->sha1, &result_size);
diff.c +8 -3
index 7d88dc5..6ee612f 100644
@@ -15,7 +15,7 @@ #include "gitopt.h"
 static int diff_scoreopt_parse(const int id, const char *opt);
 
 enum diff_ost_ids {
-	opt_p = GITOPT_DIFF_BASE, opt_raw, opt_patch_with_raw,
+	opt_u = GITOPT_DIFF_BASE, opt_p, opt_raw, opt_patch_with_raw,
 	opt_stat, opt_patch_with_stat,
 	opt_z, opt_l, opt_full_index, opt_name_only, opt_name_status,
 	opt_R, opt_S, opt_s, opt_O, opt_diff_filter,
@@ -26,7 +26,8 @@ enum diff_ost_ids {
 
 const struct opt_spec diff_ost[] = {
 	{ 0,			'p',	0,	0,	opt_p },
-	{ "unified",		'u',	0,	0,	opt_p },
+	{ "unified",		'u',	0,	ARG_OPTINT,	opt_u },
+	{ 0,			'U',	0,	ARG_OPTINT,	opt_u },
 	{ "raw",		0,	0,	0,	opt_raw },
 	{ "patch-with-raw",	0,	0,	0,	opt_patch_with_raw },
 	{ "stat",		0,	0,	0,	opt_stat },
@@ -600,7 +601,7 @@ static void builtin_diff(const char *nam
 
 		ecbdata.label_path = lbl;
 		xpp.flags = XDF_NEED_MINIMAL;
-		xecfg.ctxlen = 3;
+		xecfg.ctxlen = o->context;
 		xecfg.flags = XDL_EMIT_FUNCNAMES;
 		if (!diffopts)
 			;
@@ -1224,6 +1225,7 @@ void diff_setup(struct diff_options *opt
 	options->line_termination = '\n';
 	options->break_opt = -1;
 	options->rename_limit = -1;
+	options->context = 3;
 
 	options->change = diff_change;
 	options->add_remove = diff_addremove;
@@ -1269,6 +1271,9 @@ int diff_opt_handler(struct gitopt_itera
 	struct diff_options *options = (struct diff_options *)args;
 
 	switch (id) {
+	case opt_u:
+		if (gi->ea->argc > 1)
+			options->context = strtol(gi->ea->argv[1], NULL, 10);
 	case opt_p:
 		options->output_format = DIFF_FORMAT_PATCH;
 		break;
diff.h +1 -0
index fa44d1b..b9d7573 100644
@@ -33,6 +33,7 @@ struct diff_options {
 		 full_index:1,
 		 silent_on_remove:1,
 		 find_copies_harder:1;
+	int context;
 	int break_opt;
 	int detect_rename;
 	int line_termination;
Signature · 1 line
1.3.2.g102e322
#10 lore raw

Re: [PATCH 5/5] diff: parse U/u/unified options with an optional integer arg

Review

Quoted 3 lines · Eric Wong
(quoted diff) +0 -2
-u (lowercase) now accepts an optional arg, like -U (GNU diff
-u also has this behavior).

Actually, modern GNU "diff -u5" will say

diff: -5' option is obsolete; use -U 5'
diff: Try `diff --help' for more information.

and exit.

I'm not entirely sure why, but I think it's because "u" can be mixed (ie with something like "-urN"), while "U" cannot. The GNU diff rule seems to be that simple arguments can be mixed together, but arguments with parameters cannot. Which sounds sane.

Linus

author
#11 lore raw

Re: [PATCH 5/5] diff: parse U/u/unified options with an optional integer arg

Quoted 12 lines · Linus Torvalds

Review

Quoted 3 lines · Eric Wong
(quoted diff) +0 -2
-u (lowercase) now accepts an optional arg, like -U (GNU diff
-u also has this behavior).

Actually, modern GNU "diff -u5" will say

diff: -5' option is obsolete; use -U 5'
diff: Try `diff --help' for more information.

Quoted 6 lines

and exit.

I'm not entirely sure why, but I think it's because "u" can be mixed (ie with something like "-urN"), while "U" cannot. The GNU diff rule seems to be that simple arguments can be mixed together, but arguments with parameters cannot. Which sounds sane.

Interesting. With _POSIX2_VERSION < 200112, diff is happy to accept -u5. Setting _POSIX2_VERSION >= 200112 causes it to burp, however.

It turns out -u5 is actually short for -u -5, but I (and apparently some other people) have always assumed -u5 worked like -U5. POSIX doesn't like -<num> switches, however, but we support it regardless in rev-list.

I prefer that it works the -u5 way, of course :)

Also, I find integer arguments being bundled with other parameters to be pretty nice feature, but it's easy to disable if users don't like it:

gitopt.c +3 -11
index 9e85247..139cd2d 100644
@@ -270,17 +270,9 @@ static const char * parse_bundled(struct
        } else if (s->arg_fl) {
                if (*cur) {
                        /* no space between the arg and opt switch: */
-                       if (s->arg_fl & ARG_IS_INT) {
-                               /* we know to handle stuff like:
-                                * -h24w80 => -h=24 -w=80 */
-                               char *endptr = (char *)cur;
-                               strtol(cur, &endptr, 10);
-
-                               while (cur < endptr)
-                                       *c++ = *cur++;
-                       } else if (s->arg_fl & ARG_ONE) {
-                               /* unfortunately, other args are less
-                                * clear-cut */
+                       if (s->arg_fl & ARG_ONE) {
+                               /* don't attempt further unbundling, extra
+                                * chars are arguments */
                                while (*cur)
                                        *c++ = *cur++;
                        }
Signature · 1 line
Eric Wong