Skip to contentSkip to conversation

Add a simple option parser for use by builtin-commit.c.

PATCH In review Kristian Høgsberg 5 participants 17 messages Jun 15, 2016 Kbuild

lore
Share & tools
lore
#0 lore raw

PATCH 1/4 Add a simple option parser for use by builtin-commit.c.

---
 Makefile        |    2 +-
 parse-options.c |   74 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 parse-options.h |   29 +++++++++++++++++++++
 3 files changed, 104 insertions(+), 1 deletions(-)
 create mode 100644 parse-options.c
 create mode 100644 parse-options.h
Makefile +1 -1
index 62bdac6..d90e959 100644
@@ -310,7 +310,7 @@ LIB_OBJS = \
 	alloc.o merge-file.o path-list.o help.o unpack-trees.o $(DIFF_OBJS) \
 	color.o wt-status.o archive-zip.o archive-tar.o shallow.o utf8.o \
 	convert.o attr.o decorate.o progress.o mailmap.o symlinks.o remote.o \
-	transport.o bundle.o
+	transport.o bundle.o parse-options.o
 
 BUILTIN_OBJS = \
 	builtin-add.o \
parse-options.c new file +74 -0

74 lines not on this page · raw message

parse-options.h new file +29 -0
new file mode 100644
index 0000000..39399c3
@@ -0,0 +1,29 @@
+#ifndef PARSE_OPTIONS_H
+#define PARSE_OPTIONS_H
+
+enum option_type {
+    OPTION_BOOLEAN,
+    OPTION_STRING,
+    OPTION_INTEGER,
+    OPTION_LAST,
+};
+
+struct option {
+    enum option_type type;
+    const char *long_name;
+    char short_name;
+    void *value;
+};
+
+/* Parse the given options against the list of known options.  The
+ * order of the option structs matters, in that ambiguous
+ * abbreviations (eg, --in could be short for --include or
+ * --interactive) are matched by the first option that share the
+ * prefix.
+ */
+
+extern int parse_options(const char ***argv,
+			 struct option *options, int count,
+			 const char *usage_string);
+
+#endif
Signature · 1 line
1.5.2.GIT
#1 lore raw

PATCH 2/4 This exports the update() function from builtin-add.c as

---
 builtin-add.c |    8 ++++----
 commit.h      |    2 ++
 2 files changed, 6 insertions(+), 4 deletions(-)
builtin-add.c +4 -4
index 5e30380..966e145 100644
@@ -107,7 +107,7 @@ static void update_callback(struct diff_queue_struct *q,
 	}
 }
 
-static void update(int verbose, const char *prefix, const char **files)
+void add_files_to_cache(int verbose, const char *prefix, const char **files)
 {
 	struct rev_info rev;
 	init_revisions(&rev, prefix);
@@ -116,8 +116,6 @@ static void update(int verbose, const char *prefix, const char **files)
 	rev.diffopt.output_format = DIFF_FORMAT_CALLBACK;
 	rev.diffopt.format_callback = update_callback;
 	rev.diffopt.format_callback_data = &verbose;
-	if (read_cache() < 0)
-		die("index file corrupt");
 	run_diff_files(&rev, 0);
 }
 
@@ -218,7 +216,9 @@ int cmd_add(int argc, const char **argv, const char *prefix)
 	}
 
 	if (take_worktree_changes) {
-		update(verbose, prefix, argv + i);
+		if (read_cache() < 0)
+			die("index file corrupt");
+		add_files_to_cache(verbose, prefix, argv + i);
 		goto finish;
 	}
 
commit.h +2 -0
index cc8d890..89caa12 100644
@@ -130,6 +130,8 @@ extern struct commit_list *get_shallow_commits(struct object_array *heads,
 int in_merge_bases(struct commit *, struct commit **, int);
 
 extern int interactive_add(void);
+extern void add_files_to_cache(int verbose,
+			       const char *prefix, const char **files);
 extern int rerere(void);
 
 #endif /* COMMIT_H */
Signature · 1 line
1.5.2.GIT
#2 lore raw

PATCH 3/4 Implement git commit as a builtin command.

Move git-commit.sh to contrib/examples.

---
 Makefile                       |    9 +-
 builtin-commit.c               |  611 +++++++++++++++++++++++++++++++++++++++
 builtin-tag.c                  |    3 +-
 builtin.h                      |    3 +-
 contrib/examples/git-commit.sh |  627 ++++++++++++++++++++++++++++++++++++++++
 git-commit.sh                  |  627 ----------------------------------------
 git.c                          |    3 +-
 strbuf.h                       |    1 +
 t/t3501-revert-cherry-pick.sh  |    4 +-
 t/t3901-i18n-patch.sh          |    8 +-
 t/test-lib.sh                  |    4 +-
 11 files changed, 1255 insertions(+), 645 deletions(-)
 create mode 100644 builtin-commit.c
 create mode 100755 contrib/examples/git-commit.sh
 delete mode 100755 git-commit.sh
Makefile +3 -6
index d90e959..6172589 100644
@@ -206,7 +206,7 @@ BASIC_LDFLAGS =
 
 SCRIPT_SH = \
 	git-bisect.sh git-checkout.sh \
-	git-clean.sh git-clone.sh git-commit.sh \
+	git-clean.sh git-clone.sh \
 	git-ls-remote.sh \
 	git-merge-one-file.sh git-mergetool.sh git-parse-remote.sh \
 	git-pull.sh git-rebase.sh git-rebase--interactive.sh \
@@ -254,7 +254,7 @@ EXTRA_PROGRAMS =
 BUILT_INS = \
 	git-format-patch$X git-show$X git-whatchanged$X git-cherry$X \
 	git-get-tar-commit-id$X git-init$X git-repo-config$X \
-	git-fsck-objects$X git-cherry-pick$X \
+	git-fsck-objects$X git-cherry-pick$X git-status$X\
 	$(patsubst builtin-%.o,git-%$X,$(BUILTIN_OBJS))
 
 # what 'all' will build and 'install' will install, in gitexecdir
@@ -324,6 +324,7 @@ BUILTIN_OBJS = \
 	builtin-check-attr.o \
 	builtin-checkout-index.o \
 	builtin-check-ref-format.o \
+	builtin-commit.o \
 	builtin-commit-tree.o \
 	builtin-count-objects.o \
 	builtin-describe.o \
@@ -362,7 +363,6 @@ BUILTIN_OBJS = \
 	builtin-rev-parse.o \
 	builtin-revert.o \
 	builtin-rm.o \
-	builtin-runstatus.o \
 	builtin-shortlog.o \
 	builtin-show-branch.o \
 	builtin-stripspace.o \
@@ -822,9 +822,6 @@ $(patsubst %.perl,%,$(SCRIPT_PERL)): % : %.perl
 	chmod +x $@+ && \
 	mv $@+ $@
 
-git-status: git-commit
-	$(QUIET_GEN)cp $< $@+ && mv $@+ $@
-
 gitweb/gitweb.cgi: gitweb/gitweb.perl
 	$(QUIET_GEN)$(RM) $@ $@+ && \
 	sed -e '1s|#!.*perl|#!$(PERL_PATH_SQ)|' \
builtin-commit.c new file +611 -0

611 lines not on this page · raw message

builtin-tag.c +1 -2
index a1a2cf0..0a36a5d 100644
@@ -17,12 +17,11 @@ static const char builtin_tag_usage[] =
 
 static char signingkey[1000];
 
-static void launch_editor(const char *path, struct strbuf *buffer)
+void launch_editor(const char *path, struct strbuf *buffer)
 {
 	const char *editor, *terminal;
 	struct child_process child;
 	const char *args[3];
-	int fd;
 
 	editor = getenv("GIT_EDITOR");
 	if (!editor && editor_program)
builtin.h +2 -1
index 65cc0fb..470a788 100644
@@ -23,6 +23,7 @@ extern int cmd_check_attr(int argc, const char **argv, const char *prefix);
 extern int cmd_check_ref_format(int argc, const char **argv, const char *prefix);
 extern int cmd_cherry(int argc, const char **argv, const char *prefix);
 extern int cmd_cherry_pick(int argc, const char **argv, const char *prefix);
+extern int cmd_commit(int argc, const char **argv, const char *prefix);
 extern int cmd_commit_tree(int argc, const char **argv, const char *prefix);
 extern int cmd_count_objects(int argc, const char **argv, const char *prefix);
 extern int cmd_describe(int argc, const char **argv, const char *prefix);
@@ -67,10 +68,10 @@ extern int cmd_rev_list(int argc, const char **argv, const char *prefix);
 extern int cmd_rev_parse(int argc, const char **argv, const char *prefix);
 extern int cmd_revert(int argc, const char **argv, const char *prefix);
 extern int cmd_rm(int argc, const char **argv, const char *prefix);
-extern int cmd_runstatus(int argc, const char **argv, const char *prefix);
 extern int cmd_shortlog(int argc, const char **argv, const char *prefix);
 extern int cmd_show(int argc, const char **argv, const char *prefix);
 extern int cmd_show_branch(int argc, const char **argv, const char *prefix);
+extern int cmd_status(int argc, const char **argv, const char *prefix);
 extern int cmd_stripspace(int argc, const char **argv, const char *prefix);
 extern int cmd_symbolic_ref(int argc, const char **argv, const char *prefix);
 extern int cmd_tag(int argc, const char **argv, const char *prefix);
contrib/examples/git-commit.sh new file +627 -0

627 lines not on this page · raw message

git-commit.sh deleted +0 -627

627 lines not on this page · raw message

git.c +2 -1
index d7c6bca..9565555 100644
@@ -320,6 +320,7 @@ static void handle_internal_command(int argc, const char **argv)
 		{ "check-attr", cmd_check_attr, RUN_SETUP | NEED_WORK_TREE },
 		{ "cherry", cmd_cherry, RUN_SETUP },
 		{ "cherry-pick", cmd_cherry_pick, RUN_SETUP | NEED_WORK_TREE },
+		{ "commit", cmd_commit, RUN_SETUP | NEED_WORK_TREE },
 		{ "commit-tree", cmd_commit_tree, RUN_SETUP },
 		{ "config", cmd_config },
 		{ "count-objects", cmd_count_objects, RUN_SETUP },
@@ -368,10 +369,10 @@ static void handle_internal_command(int argc, const char **argv)
 		{ "rev-parse", cmd_rev_parse, RUN_SETUP },
 		{ "revert", cmd_revert, RUN_SETUP | NEED_WORK_TREE },
 		{ "rm", cmd_rm, RUN_SETUP | NEED_WORK_TREE },
-		{ "runstatus", cmd_runstatus, RUN_SETUP | NEED_WORK_TREE },
 		{ "shortlog", cmd_shortlog, RUN_SETUP | USE_PAGER },
 		{ "show-branch", cmd_show_branch, RUN_SETUP },
 		{ "show", cmd_show, RUN_SETUP | USE_PAGER },
+		{ "status", cmd_status, RUN_SETUP | NEED_WORK_TREE },
 		{ "stripspace", cmd_stripspace },
 		{ "symbolic-ref", cmd_symbolic_ref, RUN_SETUP },
 		{ "tag", cmd_tag, RUN_SETUP },
strbuf.h +1 -0
index 5657e3d..eef4e6d 100644
@@ -113,5 +113,6 @@ extern int strbuf_read_file(struct strbuf *sb, const char *path);
 extern int strbuf_getline(struct strbuf *, FILE *, int);
 
 extern void stripspace(struct strbuf *buf, int skip_comments);
+extern void launch_editor(const char *path, struct strbuf *buffer);
 
 #endif /* STRBUF_H */
t/t3501-revert-cherry-pick.sh +2 -2
index 552af1c..2dbe04f 100755
@@ -44,7 +44,7 @@ test_expect_success setup '
 test_expect_success 'cherry-pick after renaming branch' '
 
 	git checkout rename2 &&
-	EDITOR=: VISUAL=: git cherry-pick added &&
+	git cherry-pick added &&
 	test -f opos &&
 	grep "Add extra line at the end" opos
 
@@ -53,7 +53,7 @@ test_expect_success 'cherry-pick after renaming branch' '
 test_expect_success 'revert after renaming branch' '
 
 	git checkout rename1 &&
-	EDITOR=: VISUAL=: git revert added &&
+	git revert added &&
 	test -f spoo &&
 	! grep "Add extra line at the end" spoo
 
t/t3901-i18n-patch.sh +4 -4
index 28e9e37..235f372 100755
@@ -154,7 +154,7 @@ test_expect_success 'cherry-pick(U/U)' '
 	git reset --hard master &&
 	git cherry-pick side^ &&
 	git cherry-pick side &&
-	EDITOR=: VISUAL=: git revert HEAD &&
+	git revert HEAD &&
 
 	check_encoding 3
 '
@@ -169,7 +169,7 @@ test_expect_success 'cherry-pick(L/L)' '
 	git reset --hard master &&
 	git cherry-pick side^ &&
 	git cherry-pick side &&
-	EDITOR=: VISUAL=: git revert HEAD &&
+	git revert HEAD &&
 
 	check_encoding 3 8859
 '
@@ -184,7 +184,7 @@ test_expect_success 'cherry-pick(U/L)' '
 	git reset --hard master &&
 	git cherry-pick side^ &&
 	git cherry-pick side &&
-	EDITOR=: VISUAL=: git revert HEAD &&
+	git revert HEAD &&
 
 	check_encoding 3
 '
@@ -200,7 +200,7 @@ test_expect_success 'cherry-pick(L/U)' '
 	git reset --hard master &&
 	git cherry-pick side^ &&
 	git cherry-pick side &&
-	EDITOR=: VISUAL=: git revert HEAD &&
+	git revert HEAD &&
 
 	check_encoding 3 8859
 '
t/test-lib.sh +2 -2
index cc1253c..a232bd6 100644
@@ -9,8 +9,8 @@ LC_ALL=C
 PAGER=cat
 TZ=UTC
 export LANG LC_ALL PAGER TZ
-EDITOR=:
-VISUAL=:
+EDITOR=/bin/true
+VISUAL=/bin/true
 unset GIT_EDITOR
 unset AUTHOR_DATE
 unset AUTHOR_EMAIL
Signature · 1 line
1.5.2.GIT
#3 lore raw

PATCH 4/4 Move launch_editor() and stripspace() to new file editor.c.

---
 Makefile             |    2 +-
 builtin-commit.c     |    1 +
 builtin-stripspace.c |   68 +------------------------------
 builtin-tag.c        |   40 +------------------
 editor.c             |  109 ++++++++++++++++++++++++++++++++++++++++++++++++++
 editor.h             |    9 ++++
 strbuf.h             |    3 -
 7 files changed, 122 insertions(+), 110 deletions(-)
 create mode 100644 editor.c
 create mode 100644 editor.h
Makefile +1 -1
index 6172589..f6d991e 100644
@@ -310,7 +310,7 @@ LIB_OBJS = \
 	alloc.o merge-file.o path-list.o help.o unpack-trees.o $(DIFF_OBJS) \
 	color.o wt-status.o archive-zip.o archive-tar.o shallow.o utf8.o \
 	convert.o attr.o decorate.o progress.o mailmap.o symlinks.o remote.o \
-	transport.o bundle.o parse-options.o
+	transport.o bundle.o parse-options.o editor.o
 
 BUILTIN_OBJS = \
 	builtin-add.o \
builtin-commit.c +1 -0
index 69e8b19..7d87812 100644
@@ -19,6 +19,7 @@
 #include "strbuf.h"
 #include "utf8.h"
 #include "parse-options.h"
+#include "editor.h"
 
 static const char builtin_commit_usage[] =
 	"[-a | --interactive] [-s] [-v] [--no-verify] [-m <message> | -F <logfile> | (-C|-c) <commit> | --amend] [-u] [-e] [--author <author>] [--template <file>] [[-i | -o] <path>...]";
builtin-stripspace.c +1 -67

73 lines not on this page · raw message

builtin-tag.c +1 -39
index 0a36a5d..c9a0497 100644
@@ -10,6 +10,7 @@
 #include "builtin.h"
 #include "refs.h"
 #include "tag.h"
+#include "editor.h"
 #include "run-command.h"
 
 static const char builtin_tag_usage[] =
@@ -17,45 +18,6 @@ static const char builtin_tag_usage[] =
 
 static char signingkey[1000];
 
-void launch_editor(const char *path, struct strbuf *buffer)
-{
-	const char *editor, *terminal;
-	struct child_process child;
-	const char *args[3];
-
-	editor = getenv("GIT_EDITOR");
-	if (!editor && editor_program)
-		editor = editor_program;
-	if (!editor)
-		editor = getenv("VISUAL");
-	if (!editor)
-		editor = getenv("EDITOR");
-
-	terminal = getenv("TERM");
-	if (!editor && (!terminal || !strcmp(terminal, "dumb"))) {
-		fprintf(stderr,
-		"Terminal is dumb but no VISUAL nor EDITOR defined.\n"
-		"Please supply the message using either -m or -F option.\n");
-		exit(1);
-	}
-
-	if (!editor)
-		editor = "vi";
-
-	memset(&child, 0, sizeof(child));
-	child.argv = args;
-	args[0] = editor;
-	args[1] = path;
-	args[2] = NULL;
-
-	if (run_command(&child))
-		die("There was a problem with the editor %s.", editor);
-
-	if (strbuf_read_file(buffer, path) < 0)
-		die("could not read message file '%s': %s",
-		    path, strerror(errno));
-}
-
 struct tag_filter {
 	const char *pattern;
 	int lines;
editor.c new file +109 -0

109 lines not on this page · raw message

editor.h new file +9 -0
new file mode 100644
index 0000000..9dc39f5
@@ -0,0 +1,9 @@
+#ifndef EDITOR_H
+#define EDITOR_H
+
+#include "cache.h"
+
+extern void launch_editor(const char *path, struct strbuf *buffer);
+extern void stripspace(struct strbuf *sb, int skip_comments);
+
+#endif
strbuf.h +0 -3
index eef4e6d..d4d9e56 100644
@@ -112,7 +112,4 @@ extern int strbuf_read_file(struct strbuf *sb, const char *path);
 
 extern int strbuf_getline(struct strbuf *, FILE *, int);
 
-extern void stripspace(struct strbuf *buf, int skip_comments);
-extern void launch_editor(const char *path, struct strbuf *buffer);
-
 #endif /* STRBUF_H */
Signature · 1 line
1.5.2.GIT
#4 lore raw

Re: [PATCH 2/4] This exports the update() function from builtin-add.c as

In reply to Kristian Høgsberg
Quoted 1 line · Kristian Høgsberg

Huh? -EPARSE_TITLE_STRING

[PATCH 1/4] Add a simple option parser for use by builtin-commit.c. [PATCH 2/4] This exports the update() function from builtin-add.c as [PATCH 3/4] Implement git commit as a builtin command. [PATCH 4/4] Move launch_editor() and stripspace() to new file editor.c.

Let's step back a bit. The whole series organization is very screwy. Especially I do not think 4/4 should be at the end.

Reviewing your old series...

* Enable wt-status output to a given FILE pointer.
* Enable wt-status to run against non-standard index file.

Let's have the above two from the previous series in 'next'.

Now the following five that have been in 'pu' are from the older series:

* Introduce entry point for launching add--interactive.
* Clean up stripspace a bit, use strbuf even more.
* Add strbuf_read_file().
* Export rerere() and launch_editor().
* Implement git commit as a builtin command.

The changes to stripspace and strbuf_read_file() are unrelated to making the commit command a builtin. I have extended the strbuf topic with these two and merged the result to 'next'.

I think the right organization for the "builtin-commit" series should be:

* merge strbuf topic in kh/commit topic, in order to get the stripspace updates and strbuf_read_file();

* add--interactive entry point change (respin the one from the old series);

* rename update() to add_files_to_cache() and export (respin this [2/4] with a better commit message);

* create a separate rerere() function and export (respin part of old series, with proper refactoring);

I am not happy with builtin-foo.c calling into something from builtin-bar.c, though. We probably would want to move rerere() and add_files_to_cache() somewhere else.

* move launch_editor() and stripspace() to create editor.c (new [4/4]);

* add option parser in parse-options.[ch] (new [1/4]);

* finally, create builtin-commit that uses the groundwork laid out above (new [3/4]).

I ended up doing the above up to the rerere() one myself, but haven't done the rest.

I probably would start more aggressively asking the original author to clean up and resubmit from now on. I haven't managed to scrape enough time for myself to code anything meaningful for git recently, and instead spent too much time fixing up other peoples code.

#5 lore raw

Re: [PATCH 2/4] This exports the update() function from builtin-add.c as

On Thu, 2007-09-27 at 02:05 -0700, Junio C Hamano wrote:
...

Quoted 24 lines

I think the right organization for the "builtin-commit" series should be:

* merge strbuf topic in kh/commit topic, in order to get the stripspace updates and strbuf_read_file();

* add--interactive entry point change (respin the one from the old series);

* rename update() to add_files_to_cache() and export (respin this [2/4] with a better commit message);

* create a separate rerere() function and export (respin part of old series, with proper refactoring);

I am not happy with builtin-foo.c calling into something from builtin-bar.c, though. We probably would want to move rerere() and add_files_to_cache() somewhere else.

* move launch_editor() and stripspace() to create editor.c (new [4/4]);

* add option parser in parse-options.[ch] (new [1/4]);

Quoted 7 lines

* finally, create builtin-commit that uses the groundwork laid out above (new [3/4]).

I ended up doing the above up to the rerere() one myself, but haven't done the rest.

From what I see in next today, it looks like we're just missing the

parse-options patch and builtin-commit patches. I resent a better version of parse-options and a patch that ports builtin-add.c to the option parser. To use the option parser in more places, we'll probably have to extend it a bit, but the patch is a good start. Let's get that in shape and into next and then I'll send an updated builtin-commit patch.

thanks,
Kristian

#6 lore raw

Re: [PATCH 2/4] This exports the update() function from builtin-add.c as

In reply to Kristian Høgsberg

Hi,

Quoted 2 lines · Kristian Høgsberg
 builtin-add.c |    8 ++++----
 commit.h      |    2 ++

Maybe move it to read-cache.c, just after "add_file_to_index()"? And expose it via cache.h, not commit.h?

Ciao,
Dscho

#7 lore raw

Re: [PATCH 2/4] This exports the update() function from builtin-add.c as

Quoted 7 lines · Johannes Schindelin
Quoted 2 lines · Kristian Høgsberg
 builtin-add.c |    8 ++++----
 commit.h      |    2 ++

Maybe move it to read-cache.c, just after "add_file_to_index()"? And expose it via cache.h, not commit.h?

Hmmmmmmmm. read-cache.c has been one of the lowest level files that define atomic operations, similar to sha1_file.c, and this function is much more porcelain-ish molecule operation. I'd rather not.

What other useful molecules would we have and/or need to have by splitting existing standalone commands? rerere() is another, and probably we would need to rip the --with-tree bit from ls-files out to make it usable from builtin-commit.

I do not think we would want to go to the "one file per function" extreme, either. Can we have a new file that hold these helper functions for porcelains?

#8 lore raw
In reply to Kristian Høgsberg

Hello Kristian,

I have some comments on your patch. Some of the "improvement" might have to wait until after your builtin-commit changes hits git.git. However, if we could agree on some of the general changes, I could start porting other of the main porcelain commands to use the option parser without depending on the state of the remaining builtin-commit series.

Kristian Høgsberg <[email protected]> wrote Thu, Sep 27, 2007:

Quoted 34 lines
---
 Makefile        |    2 +-
 parse-options.c |   74 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 parse-options.h |   29 +++++++++++++++++++++
 3 files changed, 104 insertions(+), 1 deletions(-)
 create mode 100644 parse-options.c
 create mode 100644 parse-options.h
Makefile +1 -1
index 62bdac6..d90e959 100644
@@ -310,7 +310,7 @@ LIB_OBJS = \
 	alloc.o merge-file.o path-list.o help.o unpack-trees.o $(DIFF_OBJS) \
 	color.o wt-status.o archive-zip.o archive-tar.o shallow.o utf8.o \
 	convert.o attr.o decorate.o progress.o mailmap.o symlinks.o remote.o \
-	transport.o bundle.o
+	transport.o bundle.o parse-options.o
 
 BUILTIN_OBJS = \
 	builtin-add.o \
parse-options.c new file +6 -0
new file mode 100644
index 0000000..2fb30cd
@@ -0,0 +1,74 @@
+#include "git-compat-util.h"
+#include "parse-options.h"
+
+int parse_options(const char ***argv,
+		  struct option *options, int count,
+		  const char *usage_string)

parse-options.c line 16

Quoted 10 lines
parse-options.c new file +10 -0
+{
+	const char *value, *eq;
+	int i;
+
+	if (**argv == NULL)
+		return 0;
+	if ((**argv)[0] != '-')
+		return 0;
+	if (!strcmp(**argv, "--"))
+		return 0;

I don't know if this is a bug, but you do not remove "--" from argv, which is later (in the patch that adds builtin-commit.c) passed to add_files_to_cache and then get_pathspec where it is not removed or detected either.

Quoted 26 lines
parse-options.c new file +26 -0
+
+	value = NULL;
+	for (i = 0; i < count; i++) {
+		if ((**argv)[1] == '-') {
+			if (!prefixcmp(options[i].long_name, **argv + 2)) {
+				if (options[i].type != OPTION_BOOLEAN)
+					value = *++(*argv);
+				goto match;
+			}
+
+			eq = strchr(**argv + 2, '=');
+			if (eq && options[i].type != OPTION_BOOLEAN &&
+			    !strncmp(**argv + 2,
+				     options[i].long_name, eq - **argv - 2)) {
+				value = eq + 1;
+				goto match;
+			}
+		}
+
+		if ((**argv)[1] == options[i].short_name) {
+			if ((**argv)[2] == '\0') {
+				if (options[i].type != OPTION_BOOLEAN)
+					value = *++(*argv);
+				goto match;
+			}
+

parse-options.c line 52

Quoted 10 lines
parse-options.c new file +10 -0
+			if (options[i].type != OPTION_BOOLEAN) {
+				value = **argv + 2;
+				goto match;
+			}
+		}
+	}
+
+	usage(usage_string);
+
+ match:

I think the goto can be avoided by simply breaking out of the above loop when an option has been found and add ...

parse-options.c line 53

Quoted 1 line
parse-options.c new file +1 -0
+	switch (options[i].type) {
	case OPTION_LAST
		usage(usage_string);
		break;

parse-options.c line 56

Quoted 3 lines
parse-options.c new file +3 -0
+	case OPTION_BOOLEAN:
+		*(int *)options[i].value = 1;
+		break;

I've been looking at builtin-blame.c which IMO has some of the most obscure option parsing and maybe this can be changed to increment in order to support stuff like changing the meaning by passing the same arg multiple times (e.g. "-C -C -C") better.

Blame option parsing also sports (enum) flags being masked together, this can of course be rewritten to a boolean option followed by masking when parse_options is done (to keep it sane).

parse-options.c line 59

Quoted 3 lines
parse-options.c new file +3 -0
+	case OPTION_STRING:
+		if (value == NULL)
+			die("option %s requires a value.", (*argv)[-1]);

Maybe change this ...

parse-options.c line 64

Quoted 5 lines
parse-options.c new file +5 -0
+		*(const char **)options[i].value = value;
+		break;
+	case OPTION_INTEGER:
+		if (value == NULL)
+			die("option %s requires a value.", (*argv)[-1]);

... and this to:

		if (!value) {
			error("option %s requires a value.", (*argv)[-1]);
			usage(usage_string);
		}
Quoted 22 lines
parse-options.c new file +10 -0
+		*(int *)options[i].value = atoi(value);
+		break;
+	default:
+		assert(0);
+	}
+
+	(*argv)++;
+
+	return 1;
+}
parse-options.h new file +6 -0
new file mode 100644
index 0000000..39399c3
@@ -0,0 +1,29 @@
+#ifndef PARSE_OPTIONS_H
+#define PARSE_OPTIONS_H
+
+enum option_type {
+    OPTION_BOOLEAN,
+    OPTION_STRING,

parse-options.h line 16

Quoted 10 lines
parse-options.h new file +10 -0
+    OPTION_INTEGER,
+    OPTION_LAST,
+};
+
+struct option {
+    enum option_type type;
+    const char *long_name;
+    char short_name;
+    void *value;
+};

Space vs tab indentation.

One of the last things I miss from Cogito is the nice abbreviated help messages that was available via '-h'. I don't know if it would be acceptable (at least for the main porcelain commands) to put this functionality into the option parser by adding a "description" member to struct option and have parse_options print a nice:

<error message if any>
<usage string>
<option summary>

on failure, or, if that is regarded as too verbose, simply when -h is detected.

parse-options.h line 23

Quoted 7 lines
parse-options.h new file +7 -0
+
+/* Parse the given options against the list of known options.  The
+ * order of the option structs matters, in that ambiguous
+ * abbreviations (eg, --in could be short for --include or
+ * --interactive) are matched by the first option that share the
+ * prefix.
+ */

This prefix aware option parsing has not been ported over to the other builtins when they were lifted from shell code. It might be nice to have of course. Is it really needed?

parse-options.h line 27

Quoted 4 lines
parse-options.h new file +4 -0
+
+extern int parse_options(const char ***argv,
+			 struct option *options, int count,
+			 const char *usage_string);

I think the interface could be improved a bit. For example, it doesn't need to count argument since the last entry in the options array is OPTION_LAST and thus the size can be detected that way.

Also, I think for this to be more usable for other built-in programs it shouldn't modify argv, but instead take both argc and argv (so we don't need to have code like "*++(*argv)" ;), parse _all_ options in one go, and return the index (of argv) for any remaining options.

Then the following:

	while (parse_options(argv, commit_options, ARRAY_SIZE(commit_options),
		builtin_commit_usage))
		;

becomes:

	int i;
	...
	i = parse_options(argc, argv, commit_options, builtin_commit_usage);

This fits better with how option parsing is currently done. Take builtin-add for example:

	for (i = 1 ; i < argc ; i++) {
		const char *arg = argv[i];
		/* ... */
	}
	if (argc <= i)
		usage(builtin_rm_usage);

[ BTW, blame option parsing actually wants to know if "--" has been seen,
but I think that can be worked around by simply checking argv[i - 1] after calling the option parser. ]

parse-options.h line 29

Quoted 2 lines
parse-options.h new file +2 -0
+
+#endif

OK, I will stop these ramblings here. I hope the fact that I read your patch both back and forth and added comments in the process didn't make it too confusing.

Signature · 1 line
Jonas Fonseca
#9 lore raw
Quoted 7 lines · Jonas Fonseca

Hello Kristian,

I have some comments on your patch. Some of the "improvement" might have to wait until after your builtin-commit changes hits git.git. However, if we could agree on some of the general changes, I could start porting other of the main porcelain commands to use the option parser without depending on the state of the remaining builtin-commit series.

Hi Jonas,

That's sounds like a good plan. In fact, in you want to update the patch with your changes (they all sound good) and start porting over some of the other builtins feel free. I don't have much time follow up on these comments right now, but I will get to it eventually - unless you beat me to it of course ;) I will update builtin-commit.c to work with whatever changes you introduce once I get around to updating that patch.

Quoted 42 lines

Kristian Høgsberg <[email protected]> wrote Thu, Sep 27, 2007:

Quoted 41 lines
---
 Makefile        |    2 +-
 parse-options.c |   74 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 parse-options.h |   29 +++++++++++++++++++++
 3 files changed, 104 insertions(+), 1 deletions(-)
 create mode 100644 parse-options.c
 create mode 100644 parse-options.h
Makefile +1 -1
index 62bdac6..d90e959 100644
@@ -310,7 +310,7 @@ LIB_OBJS = \
 	alloc.o merge-file.o path-list.o help.o unpack-trees.o $(DIFF_OBJS) \
 	color.o wt-status.o archive-zip.o archive-tar.o shallow.o utf8.o \
 	convert.o attr.o decorate.o progress.o mailmap.o symlinks.o remote.o \
-	transport.o bundle.o
+	transport.o bundle.o parse-options.o
 
 BUILTIN_OBJS = \
 	builtin-add.o \
parse-options.c new file +13 -0
new file mode 100644
index 0000000..2fb30cd
@@ -0,0 +1,74 @@
+#include "git-compat-util.h"
+#include "parse-options.h"
+
+int parse_options(const char ***argv,
+		  struct option *options, int count,
+		  const char *usage_string)
+{
+	const char *value, *eq;
+	int i;
+
+	if (**argv == NULL)
+		return 0;
+	if ((**argv)[0] != '-')
Quoted 8 lines

parse-options.c line 16

Quoted 3 lines
parse-options.c new file +3 -0
+		return 0;
+	if (!strcmp(**argv, "--"))
+		return 0;

I don't know if this is a bug, but you do not remove "--" from argv, which is later (in the patch that adds builtin-commit.c) passed to add_files_to_cache and then get_pathspec where it is not removed or detected either.

That's an oversight, good catch.

Quoted 40 lines
Quoted 26 lines
parse-options.c new file +26 -0
+
+	value = NULL;
+	for (i = 0; i < count; i++) {
+		if ((**argv)[1] == '-') {
+			if (!prefixcmp(options[i].long_name, **argv + 2)) {
+				if (options[i].type != OPTION_BOOLEAN)
+					value = *++(*argv);
+				goto match;
+			}
+
+			eq = strchr(**argv + 2, '=');
+			if (eq && options[i].type != OPTION_BOOLEAN &&
+			    !strncmp(**argv + 2,
+				     options[i].long_name, eq - **argv - 2)) {
+				value = eq + 1;
+				goto match;
+			}
+		}
+
+		if ((**argv)[1] == options[i].short_name) {
+			if ((**argv)[2] == '\0') {
+				if (options[i].type != OPTION_BOOLEAN)
+					value = *++(*argv);
+				goto match;
+			}
+

parse-options.c line 52

Quoted 10 lines
parse-options.c new file +10 -0
+			if (options[i].type != OPTION_BOOLEAN) {
+				value = **argv + 2;
+				goto match;
+			}
+		}
+	}
+
+	usage(usage_string);
+
+ match:

I think the goto can be avoided by simply breaking out of the above loop when an option has been found and add ...

Quoted 8 lines

parse-options.c line 53

Quoted 1 line
parse-options.c new file +1 -0
+	switch (options[i].type) {
	case OPTION_LAST
		usage(usage_string);
		break;

parse-options.c line 56

Quoted 3 lines
parse-options.c new file +3 -0
+	case OPTION_BOOLEAN:
+		*(int *)options[i].value = 1;
+		break;

Yeah, that looks nicer. I think the goto structure is a leftover from when there was more logic between the loop and the switch. It's good to get some fresh eyes on this code. Junio didn't like the OPTION_LAST terminator, so I changed the interface to take a count. We can do

	if (i == count)
		usage();
	else switch (options[i].type) {
		...
	}

of course.

Quoted 4 lines

I've been looking at builtin-blame.c which IMO has some of the most obscure option parsing and maybe this can be changed to increment in order to support stuff like changing the meaning by passing the same arg multiple times (e.g. "-C -C -C") better.

That would be fine, yes.

Quoted 3 lines

Blame option parsing also sports (enum) flags being masked together, this can of course be rewritten to a boolean option followed by masking when parse_options is done (to keep it sane).

Yup.

Quoted 14 lines

parse-options.c line 59

Quoted 3 lines
parse-options.c new file +3 -0
+	case OPTION_STRING:
+		if (value == NULL)
+			die("option %s requires a value.", (*argv)[-1]);

Maybe change this ...

parse-options.c line 64

Quoted 5 lines
parse-options.c new file +5 -0
+		*(const char **)options[i].value = value;
+		break;
+	case OPTION_INTEGER:
+		if (value == NULL)
+			die("option %s requires a value.", (*argv)[-1]);

... and this to:

Quoted 4 lines
		if (!value) {
			error("option %s requires a value.", (*argv)[-1]);
			usage(usage_string);
		}

Sure, that's friendlier.

Quoted 41 lines
Quoted 22 lines
parse-options.c new file +10 -0
+		*(int *)options[i].value = atoi(value);
+		break;
+	default:
+		assert(0);
+	}
+
+	(*argv)++;
+
+	return 1;
+}
parse-options.h new file +6 -0
new file mode 100644
index 0000000..39399c3
@@ -0,0 +1,29 @@
+#ifndef PARSE_OPTIONS_H
+#define PARSE_OPTIONS_H
+
+enum option_type {
+    OPTION_BOOLEAN,
+    OPTION_STRING,

parse-options.h line 16

Quoted 10 lines
parse-options.h new file +10 -0
+    OPTION_INTEGER,
+    OPTION_LAST,
+};
+
+struct option {
+    enum option_type type;
+    const char *long_name;
+    char short_name;
+    void *value;
+};

Space vs tab indentation.

One of the last things I miss from Cogito is the nice abbreviated help messages that was available via '-h'. I don't know if it would be acceptable (at least for the main porcelain commands) to put this functionality into the option parser by adding a "description" member to struct option and have parse_options print a nice:

Quoted 6 lines

<error message if any>
<usage string>
<option summary>

on failure, or, if that is regarded as too verbose, simply when -h is detected.

Yeah, that might be nice. We can add it in a follow-on patch, if the list agrees that it's a good thing, I guess.

Quoted 11 lines

parse-options.h line 23

Quoted 7 lines
parse-options.h new file +7 -0
+
+/* Parse the given options against the list of known options.  The
+ * order of the option structs matters, in that ambiguous
+ * abbreviations (eg, --in could be short for --include or
+ * --interactive) are matched by the first option that share the
+ * prefix.
+ */

This prefix aware option parsing has not been ported over to the other builtins when they were lifted from shell code. It might be nice to have of course. Is it really needed?

I don't ever use it myself and I think it's more confusing than helpful. I only added it to avoid introducing behavior changes in the port. I don't have strong feelings either way.

Quoted 8 lines

parse-options.h line 27

Quoted 4 lines
parse-options.h new file +4 -0
+
+extern int parse_options(const char ***argv,
+			 struct option *options, int count,
+			 const char *usage_string);

I think the interface could be improved a bit. For example, it doesn't need to count argument since the last entry in the options array is OPTION_LAST and thus the size can be detected that way.

Hehe, yeah, that's how I did it first. I don't have a strong preference for terminator elements vs. ARRAY_SIZE(), but Junio prefers the ARRAY_SIZE() approach, I guess. At this point I'm just trying the get the patches upstream...

Quoted 20 lines

Also, I think for this to be more usable for other built-in programs it shouldn't modify argv, but instead take both argc and argv (so we don't need to have code like "*++(*argv)" ;), parse _all_ options in one go, and return the index (of argv) for any remaining options.

Then the following:

	while (parse_options(argv, commit_options, ARRAY_SIZE(commit_options),
		builtin_commit_usage))
		;

becomes:

	int i;
	...
	i = parse_options(argc, argv, commit_options, builtin_commit_usage);

This fits better with how option parsing is currently done. Take builtin-add for example:

Quoted 6 lines
	for (i = 1 ; i < argc ; i++) {
		const char *arg = argv[i];
		/* ... */
	}
	if (argc <= i)
		usage(builtin_rm_usage);

No objections, I think that looks better too.

Quoted 10 lines

[ BTW, blame option parsing actually wants to know if "--" has been seen,
but I think that can be worked around by simply checking argv[i - 1] after calling the option parser. ]

parse-options.h line 29

Quoted 2 lines
parse-options.h new file +2 -0
+
+#endif

OK, I will stop these ramblings here. I hope the fact that I read your patch both back and forth and added comments in the process didn't make it too confusing.

Heh, that's what I do myself :)

thanks for the comments,
Kristian

#10 lore raw

Hi,

Quoted 12 lines · Kristian H?gsberg
Quoted 10 lines · Jonas Fonseca

One of the last things I miss from Cogito is the nice abbreviated help messages that was available via '-h'. I don't know if it would be acceptable (at least for the main porcelain commands) to put this functionality into the option parser by adding a "description" member to struct option and have parse_options print a nice:

<error message if any>
<usage string>
<option summary>

Quoted 5 lines
Quoted 2 lines

on failure, or, if that is regarded as too verbose, simply when -h is detected.

Yeah, that might be nice. We can add it in a follow-on patch, if the list agrees that it's a good thing, I guess.

That's a good idea; I would put the usage string there, too.

Quoted 12 lines
Quoted 11 lines

parse-options.h line 23

Quoted 7 lines
parse-options.h new file +7 -0
+
+/* Parse the given options against the list of known options.  The
+ * order of the option structs matters, in that ambiguous
+ * abbreviations (eg, --in could be short for --include or
+ * --interactive) are matched by the first option that share the
+ * prefix.
+ */

This prefix aware option parsing has not been ported over to the other builtins when they were lifted from shell code. It might be nice to have of course. Is it really needed?

Quoted 3 lines

I don't ever use it myself and I think it's more confusing than helpful. I only added it to avoid introducing behavior changes in the port. I don't have strong feelings either way.

It might be convenient, but I think that it is really more confusing than helpful, especially with options that share a prefix. Besides, we have good completion for bash now (and I hear that this "zsh" thing also has quite good completion), I recommend <TAB> over prefix DWIMery.

Quoted 9 lines
Quoted 8 lines

parse-options.h line 27

Quoted 4 lines
parse-options.h new file +4 -0
+
+extern int parse_options(const char ***argv,
+			 struct option *options, int count,
+			 const char *usage_string);

I think the interface could be improved a bit. For example, it doesn't need to count argument since the last entry in the options array is OPTION_LAST and thus the size can be detected that way.

Quoted 4 lines

Hehe, yeah, that's how I did it first. I don't have a strong preference for terminator elements vs. ARRAY_SIZE(), but Junio prefers the ARRAY_SIZE() approach, I guess. At this point I'm just trying the get the patches upstream...

FWIW I like the ARRAY_SIZE() approach better, too, since it is less error prone.

Ciao,
Dscho

#11 lore raw

Johannes Schindelin <[email protected]> wrote Mon, Oct 01, 2007:

Quoted 11 lines · Kristian H?gsberg
Quoted 10 lines · Jonas Fonseca
Quoted 8 lines

parse-options.h line 27

Quoted 4 lines
parse-options.h new file +4 -0
+
+extern int parse_options(const char ***argv,
+			 struct option *options, int count,
+			 const char *usage_string);

I think the interface could be improved a bit. For example, it doesn't need to count argument since the last entry in the options array is OPTION_LAST and thus the size can be detected that way.

Quoted 7 lines
Quoted 4 lines

Hehe, yeah, that's how I did it first. I don't have a strong preference for terminator elements vs. ARRAY_SIZE(), but Junio prefers the ARRAY_SIZE() approach, I guess. At this point I'm just trying the get the patches upstream...

FWIW I like the ARRAY_SIZE() approach better, too, since it is less error prone.

OK, I must have missed that comment. Good point.

Thanks for the comments both of you. It's great to have something to work from. However, I also fear it will also require that some extra flags or information is added to the option information to make it more generally usable. But I guess that is easier to discuss in the context of a patch.

Signature · 1 line
Jonas Fonseca
#12 lore raw
Quoted 20 lines · Jonas Fonseca

Johannes Schindelin <[email protected]> wrote Mon, Oct 01, 2007:

Quoted 11 lines · Kristian H?gsberg
Quoted 10 lines · Jonas Fonseca
Quoted 8 lines

parse-options.h line 27

Quoted 4 lines
parse-options.h new file +4 -0
+
+extern int parse_options(const char ***argv,
+			 struct option *options, int count,
+			 const char *usage_string);

I think the interface could be improved a bit. For example, it doesn't need to count argument since the last entry in the options array is OPTION_LAST and thus the size can be detected that way.

Quoted 7 lines
Quoted 4 lines

Hehe, yeah, that's how I did it first. I don't have a strong preference for terminator elements vs. ARRAY_SIZE(), but Junio prefers the ARRAY_SIZE() approach, I guess. At this point I'm just trying the get the patches upstream...

FWIW I like the ARRAY_SIZE() approach better, too, since it is less error prone.

Quoted 7 lines

OK, I must have missed that comment. Good point.

Thanks for the comments both of you. It's great to have something to work from. However, I also fear it will also require that some extra flags or information is added to the option information to make it more generally usable. But I guess that is easier to discuss in the context of a patch.

I just sent an updated option parser patch that incorporates your suggestions along with a patch that ports builtin-add.c to use it. I looked briefly into porting over a few other builtins, but you're right, we need a couple of extra features for this to be really worthwile:

* OPTION_SET_FLAG: sets the bit (we need to add a bit value that the option parser can or in)
* OPTION_CLEAR_FLAG: clear the bit
* OPTION_ADD: adds the value to the destination integer
* OPTION_CALLBACK: calls the given function when the option is matched. We'll need this for builtin-grep that has positional args such as --and etc.

Also, the option parser should probably verify that a string option isn't passed more than once. Bundling of single letter options would be nice to add. But the patch I just sent out should be a good start, and it lets us move forward with builtin-commit.c.

cheers,
Kristian

#13 lore raw
In reply to Jonas Fonseca

Hi,

Quoted 4 lines · Jonas Fonseca

Also, I think for this to be more usable for other built-in programs it shouldn't modify argv, but instead take both argc and argv (so we don't need to have code like "*++(*argv)" ;), parse _all_ options in one go, and return the index (of argv) for any remaining options.

We _have_ to modify argv. For example, "git log master -p" is perfectly valid.

Ciao,
Dscho

#14 lore raw

Johannes Schindelin <[email protected]> wrote Mon, Oct 01, 2007:

Quoted 11 lines

Hi,

Quoted 4 lines · Jonas Fonseca

Also, I think for this to be more usable for other built-in programs it shouldn't modify argv, but instead take both argc and argv (so we don't need to have code like "*++(*argv)" ;), parse _all_ options in one go, and return the index (of argv) for any remaining options.

We _have_ to modify argv. For example, "git log master -p" is perfectly valid.

Ah, yes this could be nice to also finally have (more universally) in git. But for this to be possible I don't see any reason for it to modify the pointer to argv. Instead, it can just reshuffle entries in argv.

Signature · 1 line
Jonas Fonseca
#15 lore raw

Hi,

Quoted 9 lines · Jonas Fonseca

Johannes Schindelin <[email protected]> wrote Mon, Oct 01, 2007:

Quoted 7 lines · Jonas Fonseca
Quoted 5 lines

Also, I think for this to be more usable for other built-in programs it shouldn't modify argv, but instead take both argc and argv (so we don't need to have code like "*++(*argv)" ;), parse _all_ options in one go, and return the index (of argv) for any remaining options.

Quoted 6 lines
Quoted 2 lines

We _have_ to modify argv. For example, "git log master -p" is perfectly valid.

Ah, yes this could be nice to also finally have (more universally) in git. But for this to be possible I don't see any reason for it to modify the pointer to argv. Instead, it can just reshuffle entries in argv.

In that case, I misunderstood you. Indeed, I'd only reshuffle the entries of argv.

Ciao,
Dscho