All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2 0/4]
@ 2014-01-02 16:12 Sebastian Schuberth
  2014-01-02 16:15 ` [PATCH v2 1/4] Consistently use the term "builtin" instead of "internal command" Sebastian Schuberth
                   ` (3 more replies)
  0 siblings, 4 replies; 16+ messages in thread
From: Sebastian Schuberth @ 2014-01-02 16:12 UTC (permalink / raw)
  To: Git Mailing List; +Cc: Junio C Hamano, Christian Couder

This is the second iteration of the patches in

http://www.spinics.net/lists/git/msg222428.html
http://www.spinics.net/lists/git/msg222429.html

which

* adds a commit to use the term "builtin" instead of "internal command",
* also modifies the docs accordingly,
* moves the is_builtin() declaration to the existing builtin.h,
* finally moves all builtin-related definitions to a new builtin.c file.

Sebastian Schuberth (4):
  Consistently use the term "builtin" instead of "internal command"
  Call load_command_list() only when it is needed
  Speed up is_git_command() by checking early for internal commands
  Move builtin-related implementations to a new builtin.c file

 Documentation/technical/api-builtin.txt |   4 +-
 Makefile                                |   1 +
 builtin.c                               | 225 ++++++++++++++++++++++++++++++
 builtin.h                               |  23 +++
 builtin/help.c                          |   6 +-
 git.c                                   | 238 +-------------------------------
 6 files changed, 262 insertions(+), 235 deletions(-)
 create mode 100644 builtin.c

-- 
1.8.3-mingw-1

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

* [PATCH v2 1/4] Consistently use the term "builtin" instead of "internal command"
  2014-01-02 16:12 [PATCH v2 0/4] Sebastian Schuberth
@ 2014-01-02 16:15 ` Sebastian Schuberth
  2014-01-02 20:31   ` Jonathan Nieder
  2014-01-02 16:16 ` [PATCH v2 2/4] Call load_command_list() only when it is needed Sebastian Schuberth
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 16+ messages in thread
From: Sebastian Schuberth @ 2014-01-02 16:15 UTC (permalink / raw)
  To: Git Mailing List; +Cc: Junio C Hamano, Christian Couder


Signed-off-by: Sebastian Schuberth <sschuberth@gmail.com>
---
 Documentation/technical/api-builtin.txt |  2 +-
 git.c                                   | 14 +++++++-------
 2 files changed, 8 insertions(+), 8 deletions(-)

diff --git a/Documentation/technical/api-builtin.txt b/Documentation/technical/api-builtin.txt
index f3c1357..150a02a 100644
--- a/Documentation/technical/api-builtin.txt
+++ b/Documentation/technical/api-builtin.txt
@@ -14,7 +14,7 @@ Git:
 
 . Add the external declaration for the function to `builtin.h`.
 
-. Add the command to `commands[]` table in `handle_internal_command()`,
+. Add the command to `commands[]` table in `handle_builtin()`,
   defined in `git.c`.  The entry should look like:
 
 	{ "foo", cmd_foo, <options> },
diff --git a/git.c b/git.c
index 3799514..89ab5d7 100644
--- a/git.c
+++ b/git.c
@@ -332,7 +332,7 @@ static int run_builtin(struct cmd_struct *p, int argc, const char **argv)
 	return 0;
 }
 
-static void handle_internal_command(int argc, const char **argv)
+static void handle_builtin(int argc, const char **argv)
 {
 	const char *cmd = argv[0];
 	static struct cmd_struct commands[] = {
@@ -517,8 +517,8 @@ static int run_argv(int *argcp, const char ***argv)
 	int done_alias = 0;
 
 	while (1) {
-		/* See if it's an internal command */
-		handle_internal_command(*argcp, *argv);
+		/* See if it's a builtin */
+		handle_builtin(*argcp, *argv);
 
 		/* .. then try the external ones */
 		execv_dashed_external(*argv);
@@ -563,14 +563,14 @@ int main(int argc, char **av)
 	 *  - cannot execute it externally (since it would just do
 	 *    the same thing over again)
 	 *
-	 * So we just directly call the internal command handler, and
-	 * die if that one cannot handle it.
+	 * So we just directly call the builtin handler, and die if
+	 * that one cannot handle it.
 	 */
 	if (starts_with(cmd, "git-")) {
 		cmd += 4;
 		argv[0] = cmd;
-		handle_internal_command(argc, argv);
-		die("cannot handle %s internally", cmd);
+		handle_builtin(argc, argv);
+		die("cannot handle %s as a builtin", cmd);
 	}
 
 	/* Look for flags.. */
-- 
1.8.3-mingw-1

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

* [PATCH v2 2/4] Call load_command_list() only when it is needed
  2014-01-02 16:12 [PATCH v2 0/4] Sebastian Schuberth
  2014-01-02 16:15 ` [PATCH v2 1/4] Consistently use the term "builtin" instead of "internal command" Sebastian Schuberth
@ 2014-01-02 16:16 ` Sebastian Schuberth
  2014-01-02 16:17 ` [PATCH v2 3/4] Speed up is_git_command() by checking early for internal commands Sebastian Schuberth
  2014-01-02 16:17 ` [PATCH v2 4/4] Move builtin-related implementations to a new builtin.c file Sebastian Schuberth
  3 siblings, 0 replies; 16+ messages in thread
From: Sebastian Schuberth @ 2014-01-02 16:16 UTC (permalink / raw)
  To: Git Mailing List; +Cc: Junio C Hamano, Christian Couder

This avoids list_commands_in_dir() being called when not needed which is
quite slow due to file I/O in order to list matching files in a directory.

Signed-off-by: Sebastian Schuberth <sschuberth@gmail.com>
---
 builtin/help.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/builtin/help.c b/builtin/help.c
index cc17e67..b6fc15e 100644
--- a/builtin/help.c
+++ b/builtin/help.c
@@ -288,6 +288,7 @@ static struct cmdnames main_cmds, other_cmds;
 
 static int is_git_command(const char *s)
 {
+	load_command_list("git-", &main_cmds, &other_cmds);
 	return is_in_cmdlist(&main_cmds, s) ||
 		is_in_cmdlist(&other_cmds, s);
 }
@@ -449,7 +450,6 @@ int cmd_help(int argc, const char **argv, const char *prefix)
 	int nongit;
 	const char *alias;
 	enum help_format parsed_help_format;
-	load_command_list("git-", &main_cmds, &other_cmds);
 
 	argc = parse_options(argc, argv, prefix, builtin_help_options,
 			builtin_help_usage, 0);
@@ -458,6 +458,7 @@ int cmd_help(int argc, const char **argv, const char *prefix)
 	if (show_all) {
 		git_config(git_help_config, NULL);
 		printf(_("usage: %s%s"), _(git_usage_string), "\n\n");
+		load_command_list("git-", &main_cmds, &other_cmds);
 		list_commands(colopts, &main_cmds, &other_cmds);
 	}
 
-- 
1.8.3-mingw-1

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

* [PATCH v2 3/4] Speed up is_git_command() by checking early for internal commands
  2014-01-02 16:12 [PATCH v2 0/4] Sebastian Schuberth
  2014-01-02 16:15 ` [PATCH v2 1/4] Consistently use the term "builtin" instead of "internal command" Sebastian Schuberth
  2014-01-02 16:16 ` [PATCH v2 2/4] Call load_command_list() only when it is needed Sebastian Schuberth
@ 2014-01-02 16:17 ` Sebastian Schuberth
  2014-01-02 19:41   ` Junio C Hamano
  2014-01-03 16:49   ` Kent R. Spillner
  2014-01-02 16:17 ` [PATCH v2 4/4] Move builtin-related implementations to a new builtin.c file Sebastian Schuberth
  3 siblings, 2 replies; 16+ messages in thread
From: Sebastian Schuberth @ 2014-01-02 16:17 UTC (permalink / raw)
  To: Git Mailing List; +Cc: Junio C Hamano, Christian Couder

Since 2dce956 is_git_command() is a bit slow as it does file I/O in the
call to list_commands_in_dir(). Avoid the file I/O by adding an early
check for internal commands.

Signed-off-by: Sebastian Schuberth <sschuberth@gmail.com>
---
 Documentation/technical/api-builtin.txt |   4 +-
 builtin.h                               |   2 +
 builtin/help.c                          |   3 +
 git.c                                   | 242 +++++++++++++++++---------------
 4 files changed, 134 insertions(+), 117 deletions(-)

diff --git a/Documentation/technical/api-builtin.txt b/Documentation/technical/api-builtin.txt
index 150a02a..e3d6e7a 100644
--- a/Documentation/technical/api-builtin.txt
+++ b/Documentation/technical/api-builtin.txt
@@ -14,8 +14,8 @@ Git:
 
 . Add the external declaration for the function to `builtin.h`.
 
-. Add the command to `commands[]` table in `handle_builtin()`,
-  defined in `git.c`.  The entry should look like:
+. Add the command to the `commands[]` table defined in `git.c`.
+  The entry should look like:
 
 	{ "foo", cmd_foo, <options> },
 +
diff --git a/builtin.h b/builtin.h
index d4afbfe..c47c110 100644
--- a/builtin.h
+++ b/builtin.h
@@ -27,6 +27,8 @@ extern int fmt_merge_msg(struct strbuf *in, struct strbuf *out,
 
 extern int textconv_object(const char *path, unsigned mode, const unsigned char *sha1, int sha1_valid, char **buf, unsigned long *buf_size);
 
+extern int is_builtin(const char *s);
+
 extern int cmd_add(int argc, const char **argv, const char *prefix);
 extern int cmd_annotate(int argc, const char **argv, const char *prefix);
 extern int cmd_apply(int argc, const char **argv, const char *prefix);
diff --git a/builtin/help.c b/builtin/help.c
index b6fc15e..1fdefeb 100644
--- a/builtin/help.c
+++ b/builtin/help.c
@@ -288,6 +288,9 @@ static struct cmdnames main_cmds, other_cmds;
 
 static int is_git_command(const char *s)
 {
+	if (is_builtin(s))
+		return 1;
+
 	load_command_list("git-", &main_cmds, &other_cmds);
 	return is_in_cmdlist(&main_cmds, s) ||
 		is_in_cmdlist(&other_cmds, s);
diff --git a/git.c b/git.c
index 89ab5d7..bba4378 100644
--- a/git.c
+++ b/git.c
@@ -332,124 +332,136 @@ static int run_builtin(struct cmd_struct *p, int argc, const char **argv)
 	return 0;
 }
 
+static struct cmd_struct commands[] = {
+	{ "add", cmd_add, RUN_SETUP | NEED_WORK_TREE },
+	{ "annotate", cmd_annotate, RUN_SETUP },
+	{ "apply", cmd_apply, RUN_SETUP_GENTLY },
+	{ "archive", cmd_archive },
+	{ "bisect--helper", cmd_bisect__helper, RUN_SETUP },
+	{ "blame", cmd_blame, RUN_SETUP },
+	{ "branch", cmd_branch, RUN_SETUP },
+	{ "bundle", cmd_bundle, RUN_SETUP_GENTLY },
+	{ "cat-file", cmd_cat_file, RUN_SETUP },
+	{ "check-attr", cmd_check_attr, RUN_SETUP },
+	{ "check-ignore", cmd_check_ignore, RUN_SETUP | NEED_WORK_TREE },
+	{ "check-mailmap", cmd_check_mailmap, RUN_SETUP },
+	{ "check-ref-format", cmd_check_ref_format },
+	{ "checkout", cmd_checkout, RUN_SETUP | NEED_WORK_TREE },
+	{ "checkout-index", cmd_checkout_index,
+		RUN_SETUP | NEED_WORK_TREE},
+	{ "cherry", cmd_cherry, RUN_SETUP },
+	{ "cherry-pick", cmd_cherry_pick, RUN_SETUP | NEED_WORK_TREE },
+	{ "clean", cmd_clean, RUN_SETUP | NEED_WORK_TREE },
+	{ "clone", cmd_clone },
+	{ "column", cmd_column, RUN_SETUP_GENTLY },
+	{ "commit", cmd_commit, RUN_SETUP | NEED_WORK_TREE },
+	{ "commit-tree", cmd_commit_tree, RUN_SETUP },
+	{ "config", cmd_config, RUN_SETUP_GENTLY },
+	{ "count-objects", cmd_count_objects, RUN_SETUP },
+	{ "credential", cmd_credential, RUN_SETUP_GENTLY },
+	{ "describe", cmd_describe, RUN_SETUP },
+	{ "diff", cmd_diff },
+	{ "diff-files", cmd_diff_files, RUN_SETUP | NEED_WORK_TREE },
+	{ "diff-index", cmd_diff_index, RUN_SETUP },
+	{ "diff-tree", cmd_diff_tree, RUN_SETUP },
+	{ "fast-export", cmd_fast_export, RUN_SETUP },
+	{ "fetch", cmd_fetch, RUN_SETUP },
+	{ "fetch-pack", cmd_fetch_pack, RUN_SETUP },
+	{ "fmt-merge-msg", cmd_fmt_merge_msg, RUN_SETUP },
+	{ "for-each-ref", cmd_for_each_ref, RUN_SETUP },
+	{ "format-patch", cmd_format_patch, RUN_SETUP },
+	{ "fsck", cmd_fsck, RUN_SETUP },
+	{ "fsck-objects", cmd_fsck, RUN_SETUP },
+	{ "gc", cmd_gc, RUN_SETUP },
+	{ "get-tar-commit-id", cmd_get_tar_commit_id },
+	{ "grep", cmd_grep, RUN_SETUP_GENTLY },
+	{ "hash-object", cmd_hash_object },
+	{ "help", cmd_help },
+	{ "index-pack", cmd_index_pack, RUN_SETUP_GENTLY },
+	{ "init", cmd_init_db },
+	{ "init-db", cmd_init_db },
+	{ "log", cmd_log, RUN_SETUP },
+	{ "ls-files", cmd_ls_files, RUN_SETUP },
+	{ "ls-remote", cmd_ls_remote, RUN_SETUP_GENTLY },
+	{ "ls-tree", cmd_ls_tree, RUN_SETUP },
+	{ "mailinfo", cmd_mailinfo },
+	{ "mailsplit", cmd_mailsplit },
+	{ "merge", cmd_merge, RUN_SETUP | NEED_WORK_TREE },
+	{ "merge-base", cmd_merge_base, RUN_SETUP },
+	{ "merge-file", cmd_merge_file, RUN_SETUP_GENTLY },
+	{ "merge-index", cmd_merge_index, RUN_SETUP },
+	{ "merge-ours", cmd_merge_ours, RUN_SETUP },
+	{ "merge-recursive", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE },
+	{ "merge-recursive-ours", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE },
+	{ "merge-recursive-theirs", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE },
+	{ "merge-subtree", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE },
+	{ "merge-tree", cmd_merge_tree, RUN_SETUP },
+	{ "mktag", cmd_mktag, RUN_SETUP },
+	{ "mktree", cmd_mktree, RUN_SETUP },
+	{ "mv", cmd_mv, RUN_SETUP | NEED_WORK_TREE },
+	{ "name-rev", cmd_name_rev, RUN_SETUP },
+	{ "notes", cmd_notes, RUN_SETUP },
+	{ "pack-objects", cmd_pack_objects, RUN_SETUP },
+	{ "pack-redundant", cmd_pack_redundant, RUN_SETUP },
+	{ "pack-refs", cmd_pack_refs, RUN_SETUP },
+	{ "patch-id", cmd_patch_id },
+	{ "pickaxe", cmd_blame, RUN_SETUP },
+	{ "prune", cmd_prune, RUN_SETUP },
+	{ "prune-packed", cmd_prune_packed, RUN_SETUP },
+	{ "push", cmd_push, RUN_SETUP },
+	{ "read-tree", cmd_read_tree, RUN_SETUP },
+	{ "receive-pack", cmd_receive_pack },
+	{ "reflog", cmd_reflog, RUN_SETUP },
+	{ "remote", cmd_remote, RUN_SETUP },
+	{ "remote-ext", cmd_remote_ext },
+	{ "remote-fd", cmd_remote_fd },
+	{ "repack", cmd_repack, RUN_SETUP },
+	{ "replace", cmd_replace, RUN_SETUP },
+	{ "rerere", cmd_rerere, RUN_SETUP },
+	{ "reset", cmd_reset, RUN_SETUP },
+	{ "rev-list", cmd_rev_list, RUN_SETUP },
+	{ "rev-parse", cmd_rev_parse },
+	{ "revert", cmd_revert, RUN_SETUP | NEED_WORK_TREE },
+	{ "rm", cmd_rm, RUN_SETUP },
+	{ "send-pack", cmd_send_pack, RUN_SETUP },
+	{ "shortlog", cmd_shortlog, RUN_SETUP_GENTLY | USE_PAGER },
+	{ "show", cmd_show, RUN_SETUP },
+	{ "show-branch", cmd_show_branch, RUN_SETUP },
+	{ "show-ref", cmd_show_ref, RUN_SETUP },
+	{ "stage", cmd_add, RUN_SETUP | NEED_WORK_TREE },
+	{ "status", cmd_status, RUN_SETUP | NEED_WORK_TREE },
+	{ "stripspace", cmd_stripspace },
+	{ "symbolic-ref", cmd_symbolic_ref, RUN_SETUP },
+	{ "tag", cmd_tag, RUN_SETUP },
+	{ "unpack-file", cmd_unpack_file, RUN_SETUP },
+	{ "unpack-objects", cmd_unpack_objects, RUN_SETUP },
+	{ "update-index", cmd_update_index, RUN_SETUP },
+	{ "update-ref", cmd_update_ref, RUN_SETUP },
+	{ "update-server-info", cmd_update_server_info, RUN_SETUP },
+	{ "upload-archive", cmd_upload_archive },
+	{ "upload-archive--writer", cmd_upload_archive_writer },
+	{ "var", cmd_var, RUN_SETUP_GENTLY },
+	{ "verify-pack", cmd_verify_pack },
+	{ "verify-tag", cmd_verify_tag, RUN_SETUP },
+	{ "version", cmd_version },
+	{ "whatchanged", cmd_whatchanged, RUN_SETUP },
+	{ "write-tree", cmd_write_tree, RUN_SETUP },
+};
+
+int is_builtin(const char *s)
+{
+	int i;
+	for (i = 0; i < ARRAY_SIZE(commands); i++) {
+		struct cmd_struct *p = commands+i;
+		if (!strcmp(s, p->cmd))
+			return 1;
+	}
+	return 0;
+}
+
 static void handle_builtin(int argc, const char **argv)
 {
 	const char *cmd = argv[0];
-	static struct cmd_struct commands[] = {
-		{ "add", cmd_add, RUN_SETUP | NEED_WORK_TREE },
-		{ "annotate", cmd_annotate, RUN_SETUP },
-		{ "apply", cmd_apply, RUN_SETUP_GENTLY },
-		{ "archive", cmd_archive },
-		{ "bisect--helper", cmd_bisect__helper, RUN_SETUP },
-		{ "blame", cmd_blame, RUN_SETUP },
-		{ "branch", cmd_branch, RUN_SETUP },
-		{ "bundle", cmd_bundle, RUN_SETUP_GENTLY },
-		{ "cat-file", cmd_cat_file, RUN_SETUP },
-		{ "check-attr", cmd_check_attr, RUN_SETUP },
-		{ "check-ignore", cmd_check_ignore, RUN_SETUP | NEED_WORK_TREE },
-		{ "check-mailmap", cmd_check_mailmap, RUN_SETUP },
-		{ "check-ref-format", cmd_check_ref_format },
-		{ "checkout", cmd_checkout, RUN_SETUP | NEED_WORK_TREE },
-		{ "checkout-index", cmd_checkout_index,
-			RUN_SETUP | NEED_WORK_TREE},
-		{ "cherry", cmd_cherry, RUN_SETUP },
-		{ "cherry-pick", cmd_cherry_pick, RUN_SETUP | NEED_WORK_TREE },
-		{ "clean", cmd_clean, RUN_SETUP | NEED_WORK_TREE },
-		{ "clone", cmd_clone },
-		{ "column", cmd_column, RUN_SETUP_GENTLY },
-		{ "commit", cmd_commit, RUN_SETUP | NEED_WORK_TREE },
-		{ "commit-tree", cmd_commit_tree, RUN_SETUP },
-		{ "config", cmd_config, RUN_SETUP_GENTLY },
-		{ "count-objects", cmd_count_objects, RUN_SETUP },
-		{ "credential", cmd_credential, RUN_SETUP_GENTLY },
-		{ "describe", cmd_describe, RUN_SETUP },
-		{ "diff", cmd_diff },
-		{ "diff-files", cmd_diff_files, RUN_SETUP | NEED_WORK_TREE },
-		{ "diff-index", cmd_diff_index, RUN_SETUP },
-		{ "diff-tree", cmd_diff_tree, RUN_SETUP },
-		{ "fast-export", cmd_fast_export, RUN_SETUP },
-		{ "fetch", cmd_fetch, RUN_SETUP },
-		{ "fetch-pack", cmd_fetch_pack, RUN_SETUP },
-		{ "fmt-merge-msg", cmd_fmt_merge_msg, RUN_SETUP },
-		{ "for-each-ref", cmd_for_each_ref, RUN_SETUP },
-		{ "format-patch", cmd_format_patch, RUN_SETUP },
-		{ "fsck", cmd_fsck, RUN_SETUP },
-		{ "fsck-objects", cmd_fsck, RUN_SETUP },
-		{ "gc", cmd_gc, RUN_SETUP },
-		{ "get-tar-commit-id", cmd_get_tar_commit_id },
-		{ "grep", cmd_grep, RUN_SETUP_GENTLY },
-		{ "hash-object", cmd_hash_object },
-		{ "help", cmd_help },
-		{ "index-pack", cmd_index_pack, RUN_SETUP_GENTLY },
-		{ "init", cmd_init_db },
-		{ "init-db", cmd_init_db },
-		{ "log", cmd_log, RUN_SETUP },
-		{ "ls-files", cmd_ls_files, RUN_SETUP },
-		{ "ls-remote", cmd_ls_remote, RUN_SETUP_GENTLY },
-		{ "ls-tree", cmd_ls_tree, RUN_SETUP },
-		{ "mailinfo", cmd_mailinfo },
-		{ "mailsplit", cmd_mailsplit },
-		{ "merge", cmd_merge, RUN_SETUP | NEED_WORK_TREE },
-		{ "merge-base", cmd_merge_base, RUN_SETUP },
-		{ "merge-file", cmd_merge_file, RUN_SETUP_GENTLY },
-		{ "merge-index", cmd_merge_index, RUN_SETUP },
-		{ "merge-ours", cmd_merge_ours, RUN_SETUP },
-		{ "merge-recursive", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE },
-		{ "merge-recursive-ours", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE },
-		{ "merge-recursive-theirs", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE },
-		{ "merge-subtree", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE },
-		{ "merge-tree", cmd_merge_tree, RUN_SETUP },
-		{ "mktag", cmd_mktag, RUN_SETUP },
-		{ "mktree", cmd_mktree, RUN_SETUP },
-		{ "mv", cmd_mv, RUN_SETUP | NEED_WORK_TREE },
-		{ "name-rev", cmd_name_rev, RUN_SETUP },
-		{ "notes", cmd_notes, RUN_SETUP },
-		{ "pack-objects", cmd_pack_objects, RUN_SETUP },
-		{ "pack-redundant", cmd_pack_redundant, RUN_SETUP },
-		{ "pack-refs", cmd_pack_refs, RUN_SETUP },
-		{ "patch-id", cmd_patch_id },
-		{ "pickaxe", cmd_blame, RUN_SETUP },
-		{ "prune", cmd_prune, RUN_SETUP },
-		{ "prune-packed", cmd_prune_packed, RUN_SETUP },
-		{ "push", cmd_push, RUN_SETUP },
-		{ "read-tree", cmd_read_tree, RUN_SETUP },
-		{ "receive-pack", cmd_receive_pack },
-		{ "reflog", cmd_reflog, RUN_SETUP },
-		{ "remote", cmd_remote, RUN_SETUP },
-		{ "remote-ext", cmd_remote_ext },
-		{ "remote-fd", cmd_remote_fd },
-		{ "repack", cmd_repack, RUN_SETUP },
-		{ "replace", cmd_replace, RUN_SETUP },
-		{ "rerere", cmd_rerere, RUN_SETUP },
-		{ "reset", cmd_reset, RUN_SETUP },
-		{ "rev-list", cmd_rev_list, RUN_SETUP },
-		{ "rev-parse", cmd_rev_parse },
-		{ "revert", cmd_revert, RUN_SETUP | NEED_WORK_TREE },
-		{ "rm", cmd_rm, RUN_SETUP },
-		{ "send-pack", cmd_send_pack, RUN_SETUP },
-		{ "shortlog", cmd_shortlog, RUN_SETUP_GENTLY | USE_PAGER },
-		{ "show", cmd_show, RUN_SETUP },
-		{ "show-branch", cmd_show_branch, RUN_SETUP },
-		{ "show-ref", cmd_show_ref, RUN_SETUP },
-		{ "stage", cmd_add, RUN_SETUP | NEED_WORK_TREE },
-		{ "status", cmd_status, RUN_SETUP | NEED_WORK_TREE },
-		{ "stripspace", cmd_stripspace },
-		{ "symbolic-ref", cmd_symbolic_ref, RUN_SETUP },
-		{ "tag", cmd_tag, RUN_SETUP },
-		{ "unpack-file", cmd_unpack_file, RUN_SETUP },
-		{ "unpack-objects", cmd_unpack_objects, RUN_SETUP },
-		{ "update-index", cmd_update_index, RUN_SETUP },
-		{ "update-ref", cmd_update_ref, RUN_SETUP },
-		{ "update-server-info", cmd_update_server_info, RUN_SETUP },
-		{ "upload-archive", cmd_upload_archive },
-		{ "upload-archive--writer", cmd_upload_archive_writer },
-		{ "var", cmd_var, RUN_SETUP_GENTLY },
-		{ "verify-pack", cmd_verify_pack },
-		{ "verify-tag", cmd_verify_tag, RUN_SETUP },
-		{ "version", cmd_version },
-		{ "whatchanged", cmd_whatchanged, RUN_SETUP },
-		{ "write-tree", cmd_write_tree, RUN_SETUP },
-	};
 	int i;
 	static const char ext[] = STRIP_EXTENSION;
 
-- 
1.8.3-mingw-1

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

* [PATCH v2 4/4] Move builtin-related implementations to a new builtin.c file
  2014-01-02 16:12 [PATCH v2 0/4] Sebastian Schuberth
                   ` (2 preceding siblings ...)
  2014-01-02 16:17 ` [PATCH v2 3/4] Speed up is_git_command() by checking early for internal commands Sebastian Schuberth
@ 2014-01-02 16:17 ` Sebastian Schuberth
  2014-01-02 19:43   ` Junio C Hamano
  3 siblings, 1 reply; 16+ messages in thread
From: Sebastian Schuberth @ 2014-01-02 16:17 UTC (permalink / raw)
  To: Git Mailing List; +Cc: Junio C Hamano, Christian Couder

Signed-off-by: Sebastian Schuberth <sschuberth@gmail.com>
---
 Documentation/technical/api-builtin.txt |   2 +-
 Makefile                                |   1 +
 builtin.c                               | 225 ++++++++++++++++++++++++++++++
 builtin.h                               |  21 +++
 git.c                                   | 238 --------------------------------
 5 files changed, 248 insertions(+), 239 deletions(-)
 create mode 100644 builtin.c

diff --git a/Documentation/technical/api-builtin.txt b/Documentation/technical/api-builtin.txt
index e3d6e7a..d1d946c 100644
--- a/Documentation/technical/api-builtin.txt
+++ b/Documentation/technical/api-builtin.txt
@@ -14,7 +14,7 @@ Git:
 
 . Add the external declaration for the function to `builtin.h`.
 
-. Add the command to the `commands[]` table defined in `git.c`.
+. Add the command to the `commands[]` table defined in `builtin.c`.
   The entry should look like:
 
 	{ "foo", cmd_foo, <options> },
diff --git a/Makefile b/Makefile
index b4af1e2..2d947e8 100644
--- a/Makefile
+++ b/Makefile
@@ -763,6 +763,7 @@ LIB_OBJS += base85.o
 LIB_OBJS += bisect.o
 LIB_OBJS += blob.o
 LIB_OBJS += branch.o
+LIB_OBJS += builtin.o
 LIB_OBJS += bulk-checkin.o
 LIB_OBJS += bundle.o
 LIB_OBJS += cache-tree.o
diff --git a/builtin.c b/builtin.c
new file mode 100644
index 0000000..6bdeb7c
--- /dev/null
+++ b/builtin.c
@@ -0,0 +1,225 @@
+#include "builtin.h"
+
+static struct cmd_struct commands[] = {
+	{ "add", cmd_add, RUN_SETUP | NEED_WORK_TREE },
+	{ "annotate", cmd_annotate, RUN_SETUP },
+	{ "apply", cmd_apply, RUN_SETUP_GENTLY },
+	{ "archive", cmd_archive },
+	{ "bisect--helper", cmd_bisect__helper, RUN_SETUP },
+	{ "blame", cmd_blame, RUN_SETUP },
+	{ "branch", cmd_branch, RUN_SETUP },
+	{ "bundle", cmd_bundle, RUN_SETUP_GENTLY },
+	{ "cat-file", cmd_cat_file, RUN_SETUP },
+	{ "check-attr", cmd_check_attr, RUN_SETUP },
+	{ "check-ignore", cmd_check_ignore, RUN_SETUP | NEED_WORK_TREE },
+	{ "check-mailmap", cmd_check_mailmap, RUN_SETUP },
+	{ "check-ref-format", cmd_check_ref_format },
+	{ "checkout", cmd_checkout, RUN_SETUP | NEED_WORK_TREE },
+	{ "checkout-index", cmd_checkout_index,
+		RUN_SETUP | NEED_WORK_TREE},
+	{ "cherry", cmd_cherry, RUN_SETUP },
+	{ "cherry-pick", cmd_cherry_pick, RUN_SETUP | NEED_WORK_TREE },
+	{ "clean", cmd_clean, RUN_SETUP | NEED_WORK_TREE },
+	{ "clone", cmd_clone },
+	{ "column", cmd_column, RUN_SETUP_GENTLY },
+	{ "commit", cmd_commit, RUN_SETUP | NEED_WORK_TREE },
+	{ "commit-tree", cmd_commit_tree, RUN_SETUP },
+	{ "config", cmd_config, RUN_SETUP_GENTLY },
+	{ "count-objects", cmd_count_objects, RUN_SETUP },
+	{ "credential", cmd_credential, RUN_SETUP_GENTLY },
+	{ "describe", cmd_describe, RUN_SETUP },
+	{ "diff", cmd_diff },
+	{ "diff-files", cmd_diff_files, RUN_SETUP | NEED_WORK_TREE },
+	{ "diff-index", cmd_diff_index, RUN_SETUP },
+	{ "diff-tree", cmd_diff_tree, RUN_SETUP },
+	{ "fast-export", cmd_fast_export, RUN_SETUP },
+	{ "fetch", cmd_fetch, RUN_SETUP },
+	{ "fetch-pack", cmd_fetch_pack, RUN_SETUP },
+	{ "fmt-merge-msg", cmd_fmt_merge_msg, RUN_SETUP },
+	{ "for-each-ref", cmd_for_each_ref, RUN_SETUP },
+	{ "format-patch", cmd_format_patch, RUN_SETUP },
+	{ "fsck", cmd_fsck, RUN_SETUP },
+	{ "fsck-objects", cmd_fsck, RUN_SETUP },
+	{ "gc", cmd_gc, RUN_SETUP },
+	{ "get-tar-commit-id", cmd_get_tar_commit_id },
+	{ "grep", cmd_grep, RUN_SETUP_GENTLY },
+	{ "hash-object", cmd_hash_object },
+	{ "help", cmd_help },
+	{ "index-pack", cmd_index_pack, RUN_SETUP_GENTLY },
+	{ "init", cmd_init_db },
+	{ "init-db", cmd_init_db },
+	{ "log", cmd_log, RUN_SETUP },
+	{ "ls-files", cmd_ls_files, RUN_SETUP },
+	{ "ls-remote", cmd_ls_remote, RUN_SETUP_GENTLY },
+	{ "ls-tree", cmd_ls_tree, RUN_SETUP },
+	{ "mailinfo", cmd_mailinfo },
+	{ "mailsplit", cmd_mailsplit },
+	{ "merge", cmd_merge, RUN_SETUP | NEED_WORK_TREE },
+	{ "merge-base", cmd_merge_base, RUN_SETUP },
+	{ "merge-file", cmd_merge_file, RUN_SETUP_GENTLY },
+	{ "merge-index", cmd_merge_index, RUN_SETUP },
+	{ "merge-ours", cmd_merge_ours, RUN_SETUP },
+	{ "merge-recursive", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE },
+	{ "merge-recursive-ours", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE },
+	{ "merge-recursive-theirs", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE },
+	{ "merge-subtree", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE },
+	{ "merge-tree", cmd_merge_tree, RUN_SETUP },
+	{ "mktag", cmd_mktag, RUN_SETUP },
+	{ "mktree", cmd_mktree, RUN_SETUP },
+	{ "mv", cmd_mv, RUN_SETUP | NEED_WORK_TREE },
+	{ "name-rev", cmd_name_rev, RUN_SETUP },
+	{ "notes", cmd_notes, RUN_SETUP },
+	{ "pack-objects", cmd_pack_objects, RUN_SETUP },
+	{ "pack-redundant", cmd_pack_redundant, RUN_SETUP },
+	{ "pack-refs", cmd_pack_refs, RUN_SETUP },
+	{ "patch-id", cmd_patch_id },
+	{ "pickaxe", cmd_blame, RUN_SETUP },
+	{ "prune", cmd_prune, RUN_SETUP },
+	{ "prune-packed", cmd_prune_packed, RUN_SETUP },
+	{ "push", cmd_push, RUN_SETUP },
+	{ "read-tree", cmd_read_tree, RUN_SETUP },
+	{ "receive-pack", cmd_receive_pack },
+	{ "reflog", cmd_reflog, RUN_SETUP },
+	{ "remote", cmd_remote, RUN_SETUP },
+	{ "remote-ext", cmd_remote_ext },
+	{ "remote-fd", cmd_remote_fd },
+	{ "repack", cmd_repack, RUN_SETUP },
+	{ "replace", cmd_replace, RUN_SETUP },
+	{ "rerere", cmd_rerere, RUN_SETUP },
+	{ "reset", cmd_reset, RUN_SETUP },
+	{ "rev-list", cmd_rev_list, RUN_SETUP },
+	{ "rev-parse", cmd_rev_parse },
+	{ "revert", cmd_revert, RUN_SETUP | NEED_WORK_TREE },
+	{ "rm", cmd_rm, RUN_SETUP },
+	{ "send-pack", cmd_send_pack, RUN_SETUP },
+	{ "shortlog", cmd_shortlog, RUN_SETUP_GENTLY | USE_PAGER },
+	{ "show", cmd_show, RUN_SETUP },
+	{ "show-branch", cmd_show_branch, RUN_SETUP },
+	{ "show-ref", cmd_show_ref, RUN_SETUP },
+	{ "stage", cmd_add, RUN_SETUP | NEED_WORK_TREE },
+	{ "status", cmd_status, RUN_SETUP | NEED_WORK_TREE },
+	{ "stripspace", cmd_stripspace },
+	{ "symbolic-ref", cmd_symbolic_ref, RUN_SETUP },
+	{ "tag", cmd_tag, RUN_SETUP },
+	{ "unpack-file", cmd_unpack_file, RUN_SETUP },
+	{ "unpack-objects", cmd_unpack_objects, RUN_SETUP },
+	{ "update-index", cmd_update_index, RUN_SETUP },
+	{ "update-ref", cmd_update_ref, RUN_SETUP },
+	{ "update-server-info", cmd_update_server_info, RUN_SETUP },
+	{ "upload-archive", cmd_upload_archive },
+	{ "upload-archive--writer", cmd_upload_archive_writer },
+	{ "var", cmd_var, RUN_SETUP_GENTLY },
+	{ "verify-pack", cmd_verify_pack },
+	{ "verify-tag", cmd_verify_tag, RUN_SETUP },
+	{ "version", cmd_version },
+	{ "whatchanged", cmd_whatchanged, RUN_SETUP },
+	{ "write-tree", cmd_write_tree, RUN_SETUP },
+};
+
+int use_pager = -1;
+
+void commit_pager_choice(void) {
+	switch (use_pager) {
+	case 0:
+		setenv("GIT_PAGER", "cat", 1);
+		break;
+	case 1:
+		setup_pager();
+		break;
+	default:
+		break;
+	}
+}
+
+int is_builtin(const char *s)
+{
+	int i;
+	for (i = 0; i < ARRAY_SIZE(commands); i++) {
+		struct cmd_struct *p = commands+i;
+		if (!strcmp(s, p->cmd))
+			return 1;
+	}
+	return 0;
+}
+
+void handle_builtin(int argc, const char **argv)
+{
+	const char *cmd = argv[0];
+	int i;
+	static const char ext[] = STRIP_EXTENSION;
+
+	if (sizeof(ext) > 1) {
+		i = strlen(argv[0]) - strlen(ext);
+		if (i > 0 && !strcmp(argv[0] + i, ext)) {
+			char *argv0 = xstrdup(argv[0]);
+			argv[0] = cmd = argv0;
+			argv0[i] = '\0';
+		}
+	}
+
+	/* Turn "git cmd --help" into "git help cmd" */
+	if (argc > 1 && !strcmp(argv[1], "--help")) {
+		argv[1] = argv[0];
+		argv[0] = cmd = "help";
+	}
+
+	for (i = 0; i < ARRAY_SIZE(commands); i++) {
+		struct cmd_struct *p = commands+i;
+		if (strcmp(p->cmd, cmd))
+			continue;
+		exit(run_builtin(p, argc, argv));
+	}
+}
+
+int run_builtin(struct cmd_struct *p, int argc, const char **argv)
+{
+	int status, help;
+	struct stat st;
+	const char *prefix;
+
+	prefix = NULL;
+	help = argc == 2 && !strcmp(argv[1], "-h");
+	if (!help) {
+		if (p->option & RUN_SETUP)
+			prefix = setup_git_directory();
+		if (p->option & RUN_SETUP_GENTLY) {
+			int nongit_ok;
+			prefix = setup_git_directory_gently(&nongit_ok);
+		}
+
+		if (use_pager == -1 && p->option & (RUN_SETUP | RUN_SETUP_GENTLY))
+			use_pager = check_pager_config(p->cmd);
+		if (use_pager == -1 && p->option & USE_PAGER)
+			use_pager = 1;
+
+		if ((p->option & (RUN_SETUP | RUN_SETUP_GENTLY)) &&
+		    startup_info->have_repository) /* get_git_dir() may set up repo, avoid that */
+			trace_repo_setup(prefix);
+	}
+	commit_pager_choice();
+
+	if (!help && p->option & NEED_WORK_TREE)
+		setup_work_tree();
+
+	trace_argv_printf(argv, "trace: built-in: git");
+
+	status = p->fn(argc, argv, prefix);
+	if (status)
+		return status;
+
+	/* Somebody closed stdout? */
+	if (fstat(fileno(stdout), &st))
+		return 0;
+	/* Ignore write errors for pipes and sockets.. */
+	if (S_ISFIFO(st.st_mode) || S_ISSOCK(st.st_mode))
+		return 0;
+
+	/* Check for ENOSPC and EIO errors.. */
+	if (fflush(stdout))
+		die_errno("write failure on standard output");
+	if (ferror(stdout))
+		die("unknown write failure on standard output");
+	if (fclose(stdout))
+		die_errno("close failed on standard output");
+	return 0;
+}
diff --git a/builtin.h b/builtin.h
index c47c110..9388505 100644
--- a/builtin.h
+++ b/builtin.h
@@ -27,7 +27,28 @@ extern int fmt_merge_msg(struct strbuf *in, struct strbuf *out,
 
 extern int textconv_object(const char *path, unsigned mode, const unsigned char *sha1, int sha1_valid, char **buf, unsigned long *buf_size);
 
+#define RUN_SETUP		(1<<0)
+#define RUN_SETUP_GENTLY	(1<<1)
+#define USE_PAGER		(1<<2)
+/*
+ * require working tree to be present -- anything uses this needs
+ * RUN_SETUP for reading from the configuration file.
+ */
+#define NEED_WORK_TREE		(1<<3)
+
+struct cmd_struct {
+	const char *cmd;
+	int (*fn)(int, const char **, const char *);
+	int option;
+};
+
+extern int use_pager;
+
+extern void commit_pager_choice(void);
+
 extern int is_builtin(const char *s);
+extern void handle_builtin(int argc, const char **argv);
+extern int run_builtin(struct cmd_struct *p, int argc, const char **argv);
 
 extern int cmd_add(int argc, const char **argv, const char *prefix);
 extern int cmd_annotate(int argc, const char **argv, const char *prefix);
diff --git a/git.c b/git.c
index bba4378..c93e545 100644
--- a/git.c
+++ b/git.c
@@ -19,20 +19,6 @@ const char git_more_info_string[] =
 	   "to read about a specific subcommand or concept.");
 
 static struct startup_info git_startup_info;
-static int use_pager = -1;
-
-static void commit_pager_choice(void) {
-	switch (use_pager) {
-	case 0:
-		setenv("GIT_PAGER", "cat", 1);
-		break;
-	case 1:
-		setup_pager();
-		break;
-	default:
-		break;
-	}
-}
 
 static int handle_options(const char ***argv, int *argc, int *envchanged)
 {
@@ -264,230 +250,6 @@ static int handle_alias(int *argcp, const char ***argv)
 	return ret;
 }
 
-#define RUN_SETUP		(1<<0)
-#define RUN_SETUP_GENTLY	(1<<1)
-#define USE_PAGER		(1<<2)
-/*
- * require working tree to be present -- anything uses this needs
- * RUN_SETUP for reading from the configuration file.
- */
-#define NEED_WORK_TREE		(1<<3)
-
-struct cmd_struct {
-	const char *cmd;
-	int (*fn)(int, const char **, const char *);
-	int option;
-};
-
-static int run_builtin(struct cmd_struct *p, int argc, const char **argv)
-{
-	int status, help;
-	struct stat st;
-	const char *prefix;
-
-	prefix = NULL;
-	help = argc == 2 && !strcmp(argv[1], "-h");
-	if (!help) {
-		if (p->option & RUN_SETUP)
-			prefix = setup_git_directory();
-		if (p->option & RUN_SETUP_GENTLY) {
-			int nongit_ok;
-			prefix = setup_git_directory_gently(&nongit_ok);
-		}
-
-		if (use_pager == -1 && p->option & (RUN_SETUP | RUN_SETUP_GENTLY))
-			use_pager = check_pager_config(p->cmd);
-		if (use_pager == -1 && p->option & USE_PAGER)
-			use_pager = 1;
-
-		if ((p->option & (RUN_SETUP | RUN_SETUP_GENTLY)) &&
-		    startup_info->have_repository) /* get_git_dir() may set up repo, avoid that */
-			trace_repo_setup(prefix);
-	}
-	commit_pager_choice();
-
-	if (!help && p->option & NEED_WORK_TREE)
-		setup_work_tree();
-
-	trace_argv_printf(argv, "trace: built-in: git");
-
-	status = p->fn(argc, argv, prefix);
-	if (status)
-		return status;
-
-	/* Somebody closed stdout? */
-	if (fstat(fileno(stdout), &st))
-		return 0;
-	/* Ignore write errors for pipes and sockets.. */
-	if (S_ISFIFO(st.st_mode) || S_ISSOCK(st.st_mode))
-		return 0;
-
-	/* Check for ENOSPC and EIO errors.. */
-	if (fflush(stdout))
-		die_errno("write failure on standard output");
-	if (ferror(stdout))
-		die("unknown write failure on standard output");
-	if (fclose(stdout))
-		die_errno("close failed on standard output");
-	return 0;
-}
-
-static struct cmd_struct commands[] = {
-	{ "add", cmd_add, RUN_SETUP | NEED_WORK_TREE },
-	{ "annotate", cmd_annotate, RUN_SETUP },
-	{ "apply", cmd_apply, RUN_SETUP_GENTLY },
-	{ "archive", cmd_archive },
-	{ "bisect--helper", cmd_bisect__helper, RUN_SETUP },
-	{ "blame", cmd_blame, RUN_SETUP },
-	{ "branch", cmd_branch, RUN_SETUP },
-	{ "bundle", cmd_bundle, RUN_SETUP_GENTLY },
-	{ "cat-file", cmd_cat_file, RUN_SETUP },
-	{ "check-attr", cmd_check_attr, RUN_SETUP },
-	{ "check-ignore", cmd_check_ignore, RUN_SETUP | NEED_WORK_TREE },
-	{ "check-mailmap", cmd_check_mailmap, RUN_SETUP },
-	{ "check-ref-format", cmd_check_ref_format },
-	{ "checkout", cmd_checkout, RUN_SETUP | NEED_WORK_TREE },
-	{ "checkout-index", cmd_checkout_index,
-		RUN_SETUP | NEED_WORK_TREE},
-	{ "cherry", cmd_cherry, RUN_SETUP },
-	{ "cherry-pick", cmd_cherry_pick, RUN_SETUP | NEED_WORK_TREE },
-	{ "clean", cmd_clean, RUN_SETUP | NEED_WORK_TREE },
-	{ "clone", cmd_clone },
-	{ "column", cmd_column, RUN_SETUP_GENTLY },
-	{ "commit", cmd_commit, RUN_SETUP | NEED_WORK_TREE },
-	{ "commit-tree", cmd_commit_tree, RUN_SETUP },
-	{ "config", cmd_config, RUN_SETUP_GENTLY },
-	{ "count-objects", cmd_count_objects, RUN_SETUP },
-	{ "credential", cmd_credential, RUN_SETUP_GENTLY },
-	{ "describe", cmd_describe, RUN_SETUP },
-	{ "diff", cmd_diff },
-	{ "diff-files", cmd_diff_files, RUN_SETUP | NEED_WORK_TREE },
-	{ "diff-index", cmd_diff_index, RUN_SETUP },
-	{ "diff-tree", cmd_diff_tree, RUN_SETUP },
-	{ "fast-export", cmd_fast_export, RUN_SETUP },
-	{ "fetch", cmd_fetch, RUN_SETUP },
-	{ "fetch-pack", cmd_fetch_pack, RUN_SETUP },
-	{ "fmt-merge-msg", cmd_fmt_merge_msg, RUN_SETUP },
-	{ "for-each-ref", cmd_for_each_ref, RUN_SETUP },
-	{ "format-patch", cmd_format_patch, RUN_SETUP },
-	{ "fsck", cmd_fsck, RUN_SETUP },
-	{ "fsck-objects", cmd_fsck, RUN_SETUP },
-	{ "gc", cmd_gc, RUN_SETUP },
-	{ "get-tar-commit-id", cmd_get_tar_commit_id },
-	{ "grep", cmd_grep, RUN_SETUP_GENTLY },
-	{ "hash-object", cmd_hash_object },
-	{ "help", cmd_help },
-	{ "index-pack", cmd_index_pack, RUN_SETUP_GENTLY },
-	{ "init", cmd_init_db },
-	{ "init-db", cmd_init_db },
-	{ "log", cmd_log, RUN_SETUP },
-	{ "ls-files", cmd_ls_files, RUN_SETUP },
-	{ "ls-remote", cmd_ls_remote, RUN_SETUP_GENTLY },
-	{ "ls-tree", cmd_ls_tree, RUN_SETUP },
-	{ "mailinfo", cmd_mailinfo },
-	{ "mailsplit", cmd_mailsplit },
-	{ "merge", cmd_merge, RUN_SETUP | NEED_WORK_TREE },
-	{ "merge-base", cmd_merge_base, RUN_SETUP },
-	{ "merge-file", cmd_merge_file, RUN_SETUP_GENTLY },
-	{ "merge-index", cmd_merge_index, RUN_SETUP },
-	{ "merge-ours", cmd_merge_ours, RUN_SETUP },
-	{ "merge-recursive", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE },
-	{ "merge-recursive-ours", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE },
-	{ "merge-recursive-theirs", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE },
-	{ "merge-subtree", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE },
-	{ "merge-tree", cmd_merge_tree, RUN_SETUP },
-	{ "mktag", cmd_mktag, RUN_SETUP },
-	{ "mktree", cmd_mktree, RUN_SETUP },
-	{ "mv", cmd_mv, RUN_SETUP | NEED_WORK_TREE },
-	{ "name-rev", cmd_name_rev, RUN_SETUP },
-	{ "notes", cmd_notes, RUN_SETUP },
-	{ "pack-objects", cmd_pack_objects, RUN_SETUP },
-	{ "pack-redundant", cmd_pack_redundant, RUN_SETUP },
-	{ "pack-refs", cmd_pack_refs, RUN_SETUP },
-	{ "patch-id", cmd_patch_id },
-	{ "pickaxe", cmd_blame, RUN_SETUP },
-	{ "prune", cmd_prune, RUN_SETUP },
-	{ "prune-packed", cmd_prune_packed, RUN_SETUP },
-	{ "push", cmd_push, RUN_SETUP },
-	{ "read-tree", cmd_read_tree, RUN_SETUP },
-	{ "receive-pack", cmd_receive_pack },
-	{ "reflog", cmd_reflog, RUN_SETUP },
-	{ "remote", cmd_remote, RUN_SETUP },
-	{ "remote-ext", cmd_remote_ext },
-	{ "remote-fd", cmd_remote_fd },
-	{ "repack", cmd_repack, RUN_SETUP },
-	{ "replace", cmd_replace, RUN_SETUP },
-	{ "rerere", cmd_rerere, RUN_SETUP },
-	{ "reset", cmd_reset, RUN_SETUP },
-	{ "rev-list", cmd_rev_list, RUN_SETUP },
-	{ "rev-parse", cmd_rev_parse },
-	{ "revert", cmd_revert, RUN_SETUP | NEED_WORK_TREE },
-	{ "rm", cmd_rm, RUN_SETUP },
-	{ "send-pack", cmd_send_pack, RUN_SETUP },
-	{ "shortlog", cmd_shortlog, RUN_SETUP_GENTLY | USE_PAGER },
-	{ "show", cmd_show, RUN_SETUP },
-	{ "show-branch", cmd_show_branch, RUN_SETUP },
-	{ "show-ref", cmd_show_ref, RUN_SETUP },
-	{ "stage", cmd_add, RUN_SETUP | NEED_WORK_TREE },
-	{ "status", cmd_status, RUN_SETUP | NEED_WORK_TREE },
-	{ "stripspace", cmd_stripspace },
-	{ "symbolic-ref", cmd_symbolic_ref, RUN_SETUP },
-	{ "tag", cmd_tag, RUN_SETUP },
-	{ "unpack-file", cmd_unpack_file, RUN_SETUP },
-	{ "unpack-objects", cmd_unpack_objects, RUN_SETUP },
-	{ "update-index", cmd_update_index, RUN_SETUP },
-	{ "update-ref", cmd_update_ref, RUN_SETUP },
-	{ "update-server-info", cmd_update_server_info, RUN_SETUP },
-	{ "upload-archive", cmd_upload_archive },
-	{ "upload-archive--writer", cmd_upload_archive_writer },
-	{ "var", cmd_var, RUN_SETUP_GENTLY },
-	{ "verify-pack", cmd_verify_pack },
-	{ "verify-tag", cmd_verify_tag, RUN_SETUP },
-	{ "version", cmd_version },
-	{ "whatchanged", cmd_whatchanged, RUN_SETUP },
-	{ "write-tree", cmd_write_tree, RUN_SETUP },
-};
-
-int is_builtin(const char *s)
-{
-	int i;
-	for (i = 0; i < ARRAY_SIZE(commands); i++) {
-		struct cmd_struct *p = commands+i;
-		if (!strcmp(s, p->cmd))
-			return 1;
-	}
-	return 0;
-}
-
-static void handle_builtin(int argc, const char **argv)
-{
-	const char *cmd = argv[0];
-	int i;
-	static const char ext[] = STRIP_EXTENSION;
-
-	if (sizeof(ext) > 1) {
-		i = strlen(argv[0]) - strlen(ext);
-		if (i > 0 && !strcmp(argv[0] + i, ext)) {
-			char *argv0 = xstrdup(argv[0]);
-			argv[0] = cmd = argv0;
-			argv0[i] = '\0';
-		}
-	}
-
-	/* Turn "git cmd --help" into "git help cmd" */
-	if (argc > 1 && !strcmp(argv[1], "--help")) {
-		argv[1] = argv[0];
-		argv[0] = cmd = "help";
-	}
-
-	for (i = 0; i < ARRAY_SIZE(commands); i++) {
-		struct cmd_struct *p = commands+i;
-		if (strcmp(p->cmd, cmd))
-			continue;
-		exit(run_builtin(p, argc, argv));
-	}
-}
-
 static void execv_dashed_external(const char **argv)
 {
 	struct strbuf cmd = STRBUF_INIT;
-- 
1.8.3-mingw-1

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

* Re: [PATCH v2 3/4] Speed up is_git_command() by checking early for internal commands
  2014-01-02 16:17 ` [PATCH v2 3/4] Speed up is_git_command() by checking early for internal commands Sebastian Schuberth
@ 2014-01-02 19:41   ` Junio C Hamano
  2014-01-03 15:44     ` Jeff King
  2014-01-03 16:49   ` Kent R. Spillner
  1 sibling, 1 reply; 16+ messages in thread
From: Junio C Hamano @ 2014-01-02 19:41 UTC (permalink / raw)
  To: Sebastian Schuberth; +Cc: Git Mailing List, Christian Couder

Sebastian Schuberth <sschuberth@gmail.com> writes:

> Since 2dce956 is_git_command() is a bit slow as it does file I/O in the
> call to list_commands_in_dir(). Avoid the file I/O by adding an early
> check for internal commands.

I think it is a good thing to check with the list of built-in's
first, but in order to see if one name we already have at hand is a
git command, I have to wonder if it is the best we can do to collect
all the possible command names with load_command_list() and check
the membership.

There are two callers of is_in_cmdlist(), and the way they use the
function looks both very backwards.

 - builtin/help.c has a user supplied string that it wants to see if
   it is a git command (either on-disk in exec-path or a builtin),
   either to see if an alias in a config is really in effect, or to
   see if it is a command (i.e. invoke 'man' with git-<string>) or a
   concept (i.e. invoke 'man' with git<string>).  It feels that it
   should be a lot less work to just check with the builtin table
   and stat(<exec-path> + <string>) for either of these purposes (on
   Windows you may have to append .exe before checking and may also
   have to check with .com so you may have to do more than one
   stat(), but still).

   Even though help is not a performance critical codepath,
   optimizing this further so that we do not load the full set of
   commands unnecessarily may be in line with the theme of your
   series, I think.

 - builtin/merge.c is the same, but it is conceptually even worse.
   It has the end-user supplied string and wants to see if it is a
   valid strategy.  If the user wants to use a custom strategy, a
   single stat() to make sure if it exists should suffice, and the
   error codepath should load the command list to present the names
   of available ones in the error message.

Based on the above observation, I have a feeling that we will be
better off to aim for getting rid of is_in_cmdlist(), reimplement
is_git_command() to check with builtin and then do a stat (or two)
inside the exec-path, and update builtin/merge.c codepath to use
is_git_command() to see if the given name is indeed a strategy.

So in short, I very much like the part that moves the built-in
command list out of the main() function and makes is_git_command()
use it, but I think the primary implementation of is_git_command()
to check if the given string names an on-disk command is still not
very nice.

Thanks.

> Signed-off-by: Sebastian Schuberth <sschuberth@gmail.com>
> ---
>  Documentation/technical/api-builtin.txt |   4 +-
>  builtin.h                               |   2 +
>  builtin/help.c                          |   3 +
>  git.c                                   | 242 +++++++++++++++++---------------
>  4 files changed, 134 insertions(+), 117 deletions(-)
>
> diff --git a/Documentation/technical/api-builtin.txt b/Documentation/technical/api-builtin.txt
> index 150a02a..e3d6e7a 100644
> --- a/Documentation/technical/api-builtin.txt
> +++ b/Documentation/technical/api-builtin.txt
> @@ -14,8 +14,8 @@ Git:
>  
>  . Add the external declaration for the function to `builtin.h`.
>  
> -. Add the command to `commands[]` table in `handle_builtin()`,
> -  defined in `git.c`.  The entry should look like:
> +. Add the command to the `commands[]` table defined in `git.c`.
> +  The entry should look like:
>  
>  	{ "foo", cmd_foo, <options> },
>  +
> diff --git a/builtin.h b/builtin.h
> index d4afbfe..c47c110 100644
> --- a/builtin.h
> +++ b/builtin.h
> @@ -27,6 +27,8 @@ extern int fmt_merge_msg(struct strbuf *in, struct strbuf *out,
>  
>  extern int textconv_object(const char *path, unsigned mode, const unsigned char *sha1, int sha1_valid, char **buf, unsigned long *buf_size);
>  
> +extern int is_builtin(const char *s);
> +
>  extern int cmd_add(int argc, const char **argv, const char *prefix);
>  extern int cmd_annotate(int argc, const char **argv, const char *prefix);
>  extern int cmd_apply(int argc, const char **argv, const char *prefix);
> diff --git a/builtin/help.c b/builtin/help.c
> index b6fc15e..1fdefeb 100644
> --- a/builtin/help.c
> +++ b/builtin/help.c
> @@ -288,6 +288,9 @@ static struct cmdnames main_cmds, other_cmds;
>  
>  static int is_git_command(const char *s)
>  {
> +	if (is_builtin(s))
> +		return 1;
> +
>  	load_command_list("git-", &main_cmds, &other_cmds);
>  	return is_in_cmdlist(&main_cmds, s) ||
>  		is_in_cmdlist(&other_cmds, s);
> diff --git a/git.c b/git.c
> index 89ab5d7..bba4378 100644
> --- a/git.c
> +++ b/git.c
> @@ -332,124 +332,136 @@ static int run_builtin(struct cmd_struct *p, int argc, const char **argv)
>  	return 0;
>  }
>  
> +static struct cmd_struct commands[] = {
> +	{ "add", cmd_add, RUN_SETUP | NEED_WORK_TREE },
> +	{ "annotate", cmd_annotate, RUN_SETUP },
> +	{ "apply", cmd_apply, RUN_SETUP_GENTLY },
> +	{ "archive", cmd_archive },
> +	{ "bisect--helper", cmd_bisect__helper, RUN_SETUP },
> +	{ "blame", cmd_blame, RUN_SETUP },
> +	{ "branch", cmd_branch, RUN_SETUP },
> +	{ "bundle", cmd_bundle, RUN_SETUP_GENTLY },
> +	{ "cat-file", cmd_cat_file, RUN_SETUP },
> +	{ "check-attr", cmd_check_attr, RUN_SETUP },
> +	{ "check-ignore", cmd_check_ignore, RUN_SETUP | NEED_WORK_TREE },
> +	{ "check-mailmap", cmd_check_mailmap, RUN_SETUP },
> +	{ "check-ref-format", cmd_check_ref_format },
> +	{ "checkout", cmd_checkout, RUN_SETUP | NEED_WORK_TREE },
> +	{ "checkout-index", cmd_checkout_index,
> +		RUN_SETUP | NEED_WORK_TREE},
> +	{ "cherry", cmd_cherry, RUN_SETUP },
> +	{ "cherry-pick", cmd_cherry_pick, RUN_SETUP | NEED_WORK_TREE },
> +	{ "clean", cmd_clean, RUN_SETUP | NEED_WORK_TREE },
> +	{ "clone", cmd_clone },
> +	{ "column", cmd_column, RUN_SETUP_GENTLY },
> +	{ "commit", cmd_commit, RUN_SETUP | NEED_WORK_TREE },
> +	{ "commit-tree", cmd_commit_tree, RUN_SETUP },
> +	{ "config", cmd_config, RUN_SETUP_GENTLY },
> +	{ "count-objects", cmd_count_objects, RUN_SETUP },
> +	{ "credential", cmd_credential, RUN_SETUP_GENTLY },
> +	{ "describe", cmd_describe, RUN_SETUP },
> +	{ "diff", cmd_diff },
> +	{ "diff-files", cmd_diff_files, RUN_SETUP | NEED_WORK_TREE },
> +	{ "diff-index", cmd_diff_index, RUN_SETUP },
> +	{ "diff-tree", cmd_diff_tree, RUN_SETUP },
> +	{ "fast-export", cmd_fast_export, RUN_SETUP },
> +	{ "fetch", cmd_fetch, RUN_SETUP },
> +	{ "fetch-pack", cmd_fetch_pack, RUN_SETUP },
> +	{ "fmt-merge-msg", cmd_fmt_merge_msg, RUN_SETUP },
> +	{ "for-each-ref", cmd_for_each_ref, RUN_SETUP },
> +	{ "format-patch", cmd_format_patch, RUN_SETUP },
> +	{ "fsck", cmd_fsck, RUN_SETUP },
> +	{ "fsck-objects", cmd_fsck, RUN_SETUP },
> +	{ "gc", cmd_gc, RUN_SETUP },
> +	{ "get-tar-commit-id", cmd_get_tar_commit_id },
> +	{ "grep", cmd_grep, RUN_SETUP_GENTLY },
> +	{ "hash-object", cmd_hash_object },
> +	{ "help", cmd_help },
> +	{ "index-pack", cmd_index_pack, RUN_SETUP_GENTLY },
> +	{ "init", cmd_init_db },
> +	{ "init-db", cmd_init_db },
> +	{ "log", cmd_log, RUN_SETUP },
> +	{ "ls-files", cmd_ls_files, RUN_SETUP },
> +	{ "ls-remote", cmd_ls_remote, RUN_SETUP_GENTLY },
> +	{ "ls-tree", cmd_ls_tree, RUN_SETUP },
> +	{ "mailinfo", cmd_mailinfo },
> +	{ "mailsplit", cmd_mailsplit },
> +	{ "merge", cmd_merge, RUN_SETUP | NEED_WORK_TREE },
> +	{ "merge-base", cmd_merge_base, RUN_SETUP },
> +	{ "merge-file", cmd_merge_file, RUN_SETUP_GENTLY },
> +	{ "merge-index", cmd_merge_index, RUN_SETUP },
> +	{ "merge-ours", cmd_merge_ours, RUN_SETUP },
> +	{ "merge-recursive", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE },
> +	{ "merge-recursive-ours", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE },
> +	{ "merge-recursive-theirs", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE },
> +	{ "merge-subtree", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE },
> +	{ "merge-tree", cmd_merge_tree, RUN_SETUP },
> +	{ "mktag", cmd_mktag, RUN_SETUP },
> +	{ "mktree", cmd_mktree, RUN_SETUP },
> +	{ "mv", cmd_mv, RUN_SETUP | NEED_WORK_TREE },
> +	{ "name-rev", cmd_name_rev, RUN_SETUP },
> +	{ "notes", cmd_notes, RUN_SETUP },
> +	{ "pack-objects", cmd_pack_objects, RUN_SETUP },
> +	{ "pack-redundant", cmd_pack_redundant, RUN_SETUP },
> +	{ "pack-refs", cmd_pack_refs, RUN_SETUP },
> +	{ "patch-id", cmd_patch_id },
> +	{ "pickaxe", cmd_blame, RUN_SETUP },
> +	{ "prune", cmd_prune, RUN_SETUP },
> +	{ "prune-packed", cmd_prune_packed, RUN_SETUP },
> +	{ "push", cmd_push, RUN_SETUP },
> +	{ "read-tree", cmd_read_tree, RUN_SETUP },
> +	{ "receive-pack", cmd_receive_pack },
> +	{ "reflog", cmd_reflog, RUN_SETUP },
> +	{ "remote", cmd_remote, RUN_SETUP },
> +	{ "remote-ext", cmd_remote_ext },
> +	{ "remote-fd", cmd_remote_fd },
> +	{ "repack", cmd_repack, RUN_SETUP },
> +	{ "replace", cmd_replace, RUN_SETUP },
> +	{ "rerere", cmd_rerere, RUN_SETUP },
> +	{ "reset", cmd_reset, RUN_SETUP },
> +	{ "rev-list", cmd_rev_list, RUN_SETUP },
> +	{ "rev-parse", cmd_rev_parse },
> +	{ "revert", cmd_revert, RUN_SETUP | NEED_WORK_TREE },
> +	{ "rm", cmd_rm, RUN_SETUP },
> +	{ "send-pack", cmd_send_pack, RUN_SETUP },
> +	{ "shortlog", cmd_shortlog, RUN_SETUP_GENTLY | USE_PAGER },
> +	{ "show", cmd_show, RUN_SETUP },
> +	{ "show-branch", cmd_show_branch, RUN_SETUP },
> +	{ "show-ref", cmd_show_ref, RUN_SETUP },
> +	{ "stage", cmd_add, RUN_SETUP | NEED_WORK_TREE },
> +	{ "status", cmd_status, RUN_SETUP | NEED_WORK_TREE },
> +	{ "stripspace", cmd_stripspace },
> +	{ "symbolic-ref", cmd_symbolic_ref, RUN_SETUP },
> +	{ "tag", cmd_tag, RUN_SETUP },
> +	{ "unpack-file", cmd_unpack_file, RUN_SETUP },
> +	{ "unpack-objects", cmd_unpack_objects, RUN_SETUP },
> +	{ "update-index", cmd_update_index, RUN_SETUP },
> +	{ "update-ref", cmd_update_ref, RUN_SETUP },
> +	{ "update-server-info", cmd_update_server_info, RUN_SETUP },
> +	{ "upload-archive", cmd_upload_archive },
> +	{ "upload-archive--writer", cmd_upload_archive_writer },
> +	{ "var", cmd_var, RUN_SETUP_GENTLY },
> +	{ "verify-pack", cmd_verify_pack },
> +	{ "verify-tag", cmd_verify_tag, RUN_SETUP },
> +	{ "version", cmd_version },
> +	{ "whatchanged", cmd_whatchanged, RUN_SETUP },
> +	{ "write-tree", cmd_write_tree, RUN_SETUP },
> +};
> +
> +int is_builtin(const char *s)
> +{
> +	int i;
> +	for (i = 0; i < ARRAY_SIZE(commands); i++) {
> +		struct cmd_struct *p = commands+i;
> +		if (!strcmp(s, p->cmd))
> +			return 1;
> +	}
> +	return 0;
> +}
> +
>  static void handle_builtin(int argc, const char **argv)
>  {
>  	const char *cmd = argv[0];
> -	static struct cmd_struct commands[] = {
> -		{ "add", cmd_add, RUN_SETUP | NEED_WORK_TREE },
> -		{ "annotate", cmd_annotate, RUN_SETUP },
> -		{ "apply", cmd_apply, RUN_SETUP_GENTLY },
> -		{ "archive", cmd_archive },
> -		{ "bisect--helper", cmd_bisect__helper, RUN_SETUP },
> -		{ "blame", cmd_blame, RUN_SETUP },
> -		{ "branch", cmd_branch, RUN_SETUP },
> -		{ "bundle", cmd_bundle, RUN_SETUP_GENTLY },
> -		{ "cat-file", cmd_cat_file, RUN_SETUP },
> -		{ "check-attr", cmd_check_attr, RUN_SETUP },
> -		{ "check-ignore", cmd_check_ignore, RUN_SETUP | NEED_WORK_TREE },
> -		{ "check-mailmap", cmd_check_mailmap, RUN_SETUP },
> -		{ "check-ref-format", cmd_check_ref_format },
> -		{ "checkout", cmd_checkout, RUN_SETUP | NEED_WORK_TREE },
> -		{ "checkout-index", cmd_checkout_index,
> -			RUN_SETUP | NEED_WORK_TREE},
> -		{ "cherry", cmd_cherry, RUN_SETUP },
> -		{ "cherry-pick", cmd_cherry_pick, RUN_SETUP | NEED_WORK_TREE },
> -		{ "clean", cmd_clean, RUN_SETUP | NEED_WORK_TREE },
> -		{ "clone", cmd_clone },
> -		{ "column", cmd_column, RUN_SETUP_GENTLY },
> -		{ "commit", cmd_commit, RUN_SETUP | NEED_WORK_TREE },
> -		{ "commit-tree", cmd_commit_tree, RUN_SETUP },
> -		{ "config", cmd_config, RUN_SETUP_GENTLY },
> -		{ "count-objects", cmd_count_objects, RUN_SETUP },
> -		{ "credential", cmd_credential, RUN_SETUP_GENTLY },
> -		{ "describe", cmd_describe, RUN_SETUP },
> -		{ "diff", cmd_diff },
> -		{ "diff-files", cmd_diff_files, RUN_SETUP | NEED_WORK_TREE },
> -		{ "diff-index", cmd_diff_index, RUN_SETUP },
> -		{ "diff-tree", cmd_diff_tree, RUN_SETUP },
> -		{ "fast-export", cmd_fast_export, RUN_SETUP },
> -		{ "fetch", cmd_fetch, RUN_SETUP },
> -		{ "fetch-pack", cmd_fetch_pack, RUN_SETUP },
> -		{ "fmt-merge-msg", cmd_fmt_merge_msg, RUN_SETUP },
> -		{ "for-each-ref", cmd_for_each_ref, RUN_SETUP },
> -		{ "format-patch", cmd_format_patch, RUN_SETUP },
> -		{ "fsck", cmd_fsck, RUN_SETUP },
> -		{ "fsck-objects", cmd_fsck, RUN_SETUP },
> -		{ "gc", cmd_gc, RUN_SETUP },
> -		{ "get-tar-commit-id", cmd_get_tar_commit_id },
> -		{ "grep", cmd_grep, RUN_SETUP_GENTLY },
> -		{ "hash-object", cmd_hash_object },
> -		{ "help", cmd_help },
> -		{ "index-pack", cmd_index_pack, RUN_SETUP_GENTLY },
> -		{ "init", cmd_init_db },
> -		{ "init-db", cmd_init_db },
> -		{ "log", cmd_log, RUN_SETUP },
> -		{ "ls-files", cmd_ls_files, RUN_SETUP },
> -		{ "ls-remote", cmd_ls_remote, RUN_SETUP_GENTLY },
> -		{ "ls-tree", cmd_ls_tree, RUN_SETUP },
> -		{ "mailinfo", cmd_mailinfo },
> -		{ "mailsplit", cmd_mailsplit },
> -		{ "merge", cmd_merge, RUN_SETUP | NEED_WORK_TREE },
> -		{ "merge-base", cmd_merge_base, RUN_SETUP },
> -		{ "merge-file", cmd_merge_file, RUN_SETUP_GENTLY },
> -		{ "merge-index", cmd_merge_index, RUN_SETUP },
> -		{ "merge-ours", cmd_merge_ours, RUN_SETUP },
> -		{ "merge-recursive", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE },
> -		{ "merge-recursive-ours", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE },
> -		{ "merge-recursive-theirs", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE },
> -		{ "merge-subtree", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE },
> -		{ "merge-tree", cmd_merge_tree, RUN_SETUP },
> -		{ "mktag", cmd_mktag, RUN_SETUP },
> -		{ "mktree", cmd_mktree, RUN_SETUP },
> -		{ "mv", cmd_mv, RUN_SETUP | NEED_WORK_TREE },
> -		{ "name-rev", cmd_name_rev, RUN_SETUP },
> -		{ "notes", cmd_notes, RUN_SETUP },
> -		{ "pack-objects", cmd_pack_objects, RUN_SETUP },
> -		{ "pack-redundant", cmd_pack_redundant, RUN_SETUP },
> -		{ "pack-refs", cmd_pack_refs, RUN_SETUP },
> -		{ "patch-id", cmd_patch_id },
> -		{ "pickaxe", cmd_blame, RUN_SETUP },
> -		{ "prune", cmd_prune, RUN_SETUP },
> -		{ "prune-packed", cmd_prune_packed, RUN_SETUP },
> -		{ "push", cmd_push, RUN_SETUP },
> -		{ "read-tree", cmd_read_tree, RUN_SETUP },
> -		{ "receive-pack", cmd_receive_pack },
> -		{ "reflog", cmd_reflog, RUN_SETUP },
> -		{ "remote", cmd_remote, RUN_SETUP },
> -		{ "remote-ext", cmd_remote_ext },
> -		{ "remote-fd", cmd_remote_fd },
> -		{ "repack", cmd_repack, RUN_SETUP },
> -		{ "replace", cmd_replace, RUN_SETUP },
> -		{ "rerere", cmd_rerere, RUN_SETUP },
> -		{ "reset", cmd_reset, RUN_SETUP },
> -		{ "rev-list", cmd_rev_list, RUN_SETUP },
> -		{ "rev-parse", cmd_rev_parse },
> -		{ "revert", cmd_revert, RUN_SETUP | NEED_WORK_TREE },
> -		{ "rm", cmd_rm, RUN_SETUP },
> -		{ "send-pack", cmd_send_pack, RUN_SETUP },
> -		{ "shortlog", cmd_shortlog, RUN_SETUP_GENTLY | USE_PAGER },
> -		{ "show", cmd_show, RUN_SETUP },
> -		{ "show-branch", cmd_show_branch, RUN_SETUP },
> -		{ "show-ref", cmd_show_ref, RUN_SETUP },
> -		{ "stage", cmd_add, RUN_SETUP | NEED_WORK_TREE },
> -		{ "status", cmd_status, RUN_SETUP | NEED_WORK_TREE },
> -		{ "stripspace", cmd_stripspace },
> -		{ "symbolic-ref", cmd_symbolic_ref, RUN_SETUP },
> -		{ "tag", cmd_tag, RUN_SETUP },
> -		{ "unpack-file", cmd_unpack_file, RUN_SETUP },
> -		{ "unpack-objects", cmd_unpack_objects, RUN_SETUP },
> -		{ "update-index", cmd_update_index, RUN_SETUP },
> -		{ "update-ref", cmd_update_ref, RUN_SETUP },
> -		{ "update-server-info", cmd_update_server_info, RUN_SETUP },
> -		{ "upload-archive", cmd_upload_archive },
> -		{ "upload-archive--writer", cmd_upload_archive_writer },
> -		{ "var", cmd_var, RUN_SETUP_GENTLY },
> -		{ "verify-pack", cmd_verify_pack },
> -		{ "verify-tag", cmd_verify_tag, RUN_SETUP },
> -		{ "version", cmd_version },
> -		{ "whatchanged", cmd_whatchanged, RUN_SETUP },
> -		{ "write-tree", cmd_write_tree, RUN_SETUP },
> -	};
>  	int i;
>  	static const char ext[] = STRIP_EXTENSION;

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

* Re: [PATCH v2 4/4] Move builtin-related implementations to a new builtin.c file
  2014-01-02 16:17 ` [PATCH v2 4/4] Move builtin-related implementations to a new builtin.c file Sebastian Schuberth
@ 2014-01-02 19:43   ` Junio C Hamano
  2014-01-02 20:58     ` Sebastian Schuberth
  0 siblings, 1 reply; 16+ messages in thread
From: Junio C Hamano @ 2014-01-02 19:43 UTC (permalink / raw)
  To: Sebastian Schuberth; +Cc: Git Mailing List, Christian Couder

Sebastian Schuberth <sschuberth@gmail.com> writes:

> Signed-off-by: Sebastian Schuberth <sschuberth@gmail.com>
> ---
>  Documentation/technical/api-builtin.txt |   2 +-
>  Makefile                                |   1 +
>  builtin.c                               | 225 ++++++++++++++++++++++++++++++
>  builtin.h                               |  21 +++
>  git.c                                   | 238 --------------------------------
>  5 files changed, 248 insertions(+), 239 deletions(-)
>  create mode 100644 builtin.c

I'm sorry but I do not see a point in this.

It is not like builtin.c can be used outside the context of the main
Git program, and many helper functions you moved out of git.c that
used to be static want to be called from other places.

> diff --git a/Documentation/technical/api-builtin.txt b/Documentation/technical/api-builtin.txt
> index e3d6e7a..d1d946c 100644
> --- a/Documentation/technical/api-builtin.txt
> +++ b/Documentation/technical/api-builtin.txt
> @@ -14,7 +14,7 @@ Git:
>  
>  . Add the external declaration for the function to `builtin.h`.
>  
> -. Add the command to the `commands[]` table defined in `git.c`.
> +. Add the command to the `commands[]` table defined in `builtin.c`.
>    The entry should look like:
>  
>  	{ "foo", cmd_foo, <options> },
> diff --git a/Makefile b/Makefile
> index b4af1e2..2d947e8 100644
> --- a/Makefile
> +++ b/Makefile
> @@ -763,6 +763,7 @@ LIB_OBJS += base85.o
>  LIB_OBJS += bisect.o
>  LIB_OBJS += blob.o
>  LIB_OBJS += branch.o
> +LIB_OBJS += builtin.o
>  LIB_OBJS += bulk-checkin.o
>  LIB_OBJS += bundle.o
>  LIB_OBJS += cache-tree.o
> diff --git a/builtin.c b/builtin.c
> new file mode 100644
> index 0000000..6bdeb7c
> --- /dev/null
> +++ b/builtin.c
> @@ -0,0 +1,225 @@
> +#include "builtin.h"
> +
> +static struct cmd_struct commands[] = {
> +	{ "add", cmd_add, RUN_SETUP | NEED_WORK_TREE },
> +	{ "annotate", cmd_annotate, RUN_SETUP },
> +	{ "apply", cmd_apply, RUN_SETUP_GENTLY },
> +	{ "archive", cmd_archive },
> +	{ "bisect--helper", cmd_bisect__helper, RUN_SETUP },
> +	{ "blame", cmd_blame, RUN_SETUP },
> +	{ "branch", cmd_branch, RUN_SETUP },
> +	{ "bundle", cmd_bundle, RUN_SETUP_GENTLY },
> +	{ "cat-file", cmd_cat_file, RUN_SETUP },
> +	{ "check-attr", cmd_check_attr, RUN_SETUP },
> +	{ "check-ignore", cmd_check_ignore, RUN_SETUP | NEED_WORK_TREE },
> +	{ "check-mailmap", cmd_check_mailmap, RUN_SETUP },
> +	{ "check-ref-format", cmd_check_ref_format },
> +	{ "checkout", cmd_checkout, RUN_SETUP | NEED_WORK_TREE },
> +	{ "checkout-index", cmd_checkout_index,
> +		RUN_SETUP | NEED_WORK_TREE},
> +	{ "cherry", cmd_cherry, RUN_SETUP },
> +	{ "cherry-pick", cmd_cherry_pick, RUN_SETUP | NEED_WORK_TREE },
> +	{ "clean", cmd_clean, RUN_SETUP | NEED_WORK_TREE },
> +	{ "clone", cmd_clone },
> +	{ "column", cmd_column, RUN_SETUP_GENTLY },
> +	{ "commit", cmd_commit, RUN_SETUP | NEED_WORK_TREE },
> +	{ "commit-tree", cmd_commit_tree, RUN_SETUP },
> +	{ "config", cmd_config, RUN_SETUP_GENTLY },
> +	{ "count-objects", cmd_count_objects, RUN_SETUP },
> +	{ "credential", cmd_credential, RUN_SETUP_GENTLY },
> +	{ "describe", cmd_describe, RUN_SETUP },
> +	{ "diff", cmd_diff },
> +	{ "diff-files", cmd_diff_files, RUN_SETUP | NEED_WORK_TREE },
> +	{ "diff-index", cmd_diff_index, RUN_SETUP },
> +	{ "diff-tree", cmd_diff_tree, RUN_SETUP },
> +	{ "fast-export", cmd_fast_export, RUN_SETUP },
> +	{ "fetch", cmd_fetch, RUN_SETUP },
> +	{ "fetch-pack", cmd_fetch_pack, RUN_SETUP },
> +	{ "fmt-merge-msg", cmd_fmt_merge_msg, RUN_SETUP },
> +	{ "for-each-ref", cmd_for_each_ref, RUN_SETUP },
> +	{ "format-patch", cmd_format_patch, RUN_SETUP },
> +	{ "fsck", cmd_fsck, RUN_SETUP },
> +	{ "fsck-objects", cmd_fsck, RUN_SETUP },
> +	{ "gc", cmd_gc, RUN_SETUP },
> +	{ "get-tar-commit-id", cmd_get_tar_commit_id },
> +	{ "grep", cmd_grep, RUN_SETUP_GENTLY },
> +	{ "hash-object", cmd_hash_object },
> +	{ "help", cmd_help },
> +	{ "index-pack", cmd_index_pack, RUN_SETUP_GENTLY },
> +	{ "init", cmd_init_db },
> +	{ "init-db", cmd_init_db },
> +	{ "log", cmd_log, RUN_SETUP },
> +	{ "ls-files", cmd_ls_files, RUN_SETUP },
> +	{ "ls-remote", cmd_ls_remote, RUN_SETUP_GENTLY },
> +	{ "ls-tree", cmd_ls_tree, RUN_SETUP },
> +	{ "mailinfo", cmd_mailinfo },
> +	{ "mailsplit", cmd_mailsplit },
> +	{ "merge", cmd_merge, RUN_SETUP | NEED_WORK_TREE },
> +	{ "merge-base", cmd_merge_base, RUN_SETUP },
> +	{ "merge-file", cmd_merge_file, RUN_SETUP_GENTLY },
> +	{ "merge-index", cmd_merge_index, RUN_SETUP },
> +	{ "merge-ours", cmd_merge_ours, RUN_SETUP },
> +	{ "merge-recursive", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE },
> +	{ "merge-recursive-ours", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE },
> +	{ "merge-recursive-theirs", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE },
> +	{ "merge-subtree", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE },
> +	{ "merge-tree", cmd_merge_tree, RUN_SETUP },
> +	{ "mktag", cmd_mktag, RUN_SETUP },
> +	{ "mktree", cmd_mktree, RUN_SETUP },
> +	{ "mv", cmd_mv, RUN_SETUP | NEED_WORK_TREE },
> +	{ "name-rev", cmd_name_rev, RUN_SETUP },
> +	{ "notes", cmd_notes, RUN_SETUP },
> +	{ "pack-objects", cmd_pack_objects, RUN_SETUP },
> +	{ "pack-redundant", cmd_pack_redundant, RUN_SETUP },
> +	{ "pack-refs", cmd_pack_refs, RUN_SETUP },
> +	{ "patch-id", cmd_patch_id },
> +	{ "pickaxe", cmd_blame, RUN_SETUP },
> +	{ "prune", cmd_prune, RUN_SETUP },
> +	{ "prune-packed", cmd_prune_packed, RUN_SETUP },
> +	{ "push", cmd_push, RUN_SETUP },
> +	{ "read-tree", cmd_read_tree, RUN_SETUP },
> +	{ "receive-pack", cmd_receive_pack },
> +	{ "reflog", cmd_reflog, RUN_SETUP },
> +	{ "remote", cmd_remote, RUN_SETUP },
> +	{ "remote-ext", cmd_remote_ext },
> +	{ "remote-fd", cmd_remote_fd },
> +	{ "repack", cmd_repack, RUN_SETUP },
> +	{ "replace", cmd_replace, RUN_SETUP },
> +	{ "rerere", cmd_rerere, RUN_SETUP },
> +	{ "reset", cmd_reset, RUN_SETUP },
> +	{ "rev-list", cmd_rev_list, RUN_SETUP },
> +	{ "rev-parse", cmd_rev_parse },
> +	{ "revert", cmd_revert, RUN_SETUP | NEED_WORK_TREE },
> +	{ "rm", cmd_rm, RUN_SETUP },
> +	{ "send-pack", cmd_send_pack, RUN_SETUP },
> +	{ "shortlog", cmd_shortlog, RUN_SETUP_GENTLY | USE_PAGER },
> +	{ "show", cmd_show, RUN_SETUP },
> +	{ "show-branch", cmd_show_branch, RUN_SETUP },
> +	{ "show-ref", cmd_show_ref, RUN_SETUP },
> +	{ "stage", cmd_add, RUN_SETUP | NEED_WORK_TREE },
> +	{ "status", cmd_status, RUN_SETUP | NEED_WORK_TREE },
> +	{ "stripspace", cmd_stripspace },
> +	{ "symbolic-ref", cmd_symbolic_ref, RUN_SETUP },
> +	{ "tag", cmd_tag, RUN_SETUP },
> +	{ "unpack-file", cmd_unpack_file, RUN_SETUP },
> +	{ "unpack-objects", cmd_unpack_objects, RUN_SETUP },
> +	{ "update-index", cmd_update_index, RUN_SETUP },
> +	{ "update-ref", cmd_update_ref, RUN_SETUP },
> +	{ "update-server-info", cmd_update_server_info, RUN_SETUP },
> +	{ "upload-archive", cmd_upload_archive },
> +	{ "upload-archive--writer", cmd_upload_archive_writer },
> +	{ "var", cmd_var, RUN_SETUP_GENTLY },
> +	{ "verify-pack", cmd_verify_pack },
> +	{ "verify-tag", cmd_verify_tag, RUN_SETUP },
> +	{ "version", cmd_version },
> +	{ "whatchanged", cmd_whatchanged, RUN_SETUP },
> +	{ "write-tree", cmd_write_tree, RUN_SETUP },
> +};
> +
> +int use_pager = -1;
> +
> +void commit_pager_choice(void) {
> +	switch (use_pager) {
> +	case 0:
> +		setenv("GIT_PAGER", "cat", 1);
> +		break;
> +	case 1:
> +		setup_pager();
> +		break;
> +	default:
> +		break;
> +	}
> +}
> +
> +int is_builtin(const char *s)
> +{
> +	int i;
> +	for (i = 0; i < ARRAY_SIZE(commands); i++) {
> +		struct cmd_struct *p = commands+i;
> +		if (!strcmp(s, p->cmd))
> +			return 1;
> +	}
> +	return 0;
> +}
> +
> +void handle_builtin(int argc, const char **argv)
> +{
> +	const char *cmd = argv[0];
> +	int i;
> +	static const char ext[] = STRIP_EXTENSION;
> +
> +	if (sizeof(ext) > 1) {
> +		i = strlen(argv[0]) - strlen(ext);
> +		if (i > 0 && !strcmp(argv[0] + i, ext)) {
> +			char *argv0 = xstrdup(argv[0]);
> +			argv[0] = cmd = argv0;
> +			argv0[i] = '\0';
> +		}
> +	}
> +
> +	/* Turn "git cmd --help" into "git help cmd" */
> +	if (argc > 1 && !strcmp(argv[1], "--help")) {
> +		argv[1] = argv[0];
> +		argv[0] = cmd = "help";
> +	}
> +
> +	for (i = 0; i < ARRAY_SIZE(commands); i++) {
> +		struct cmd_struct *p = commands+i;
> +		if (strcmp(p->cmd, cmd))
> +			continue;
> +		exit(run_builtin(p, argc, argv));
> +	}
> +}
> +
> +int run_builtin(struct cmd_struct *p, int argc, const char **argv)
> +{
> +	int status, help;
> +	struct stat st;
> +	const char *prefix;
> +
> +	prefix = NULL;
> +	help = argc == 2 && !strcmp(argv[1], "-h");
> +	if (!help) {
> +		if (p->option & RUN_SETUP)
> +			prefix = setup_git_directory();
> +		if (p->option & RUN_SETUP_GENTLY) {
> +			int nongit_ok;
> +			prefix = setup_git_directory_gently(&nongit_ok);
> +		}
> +
> +		if (use_pager == -1 && p->option & (RUN_SETUP | RUN_SETUP_GENTLY))
> +			use_pager = check_pager_config(p->cmd);
> +		if (use_pager == -1 && p->option & USE_PAGER)
> +			use_pager = 1;
> +
> +		if ((p->option & (RUN_SETUP | RUN_SETUP_GENTLY)) &&
> +		    startup_info->have_repository) /* get_git_dir() may set up repo, avoid that */
> +			trace_repo_setup(prefix);
> +	}
> +	commit_pager_choice();
> +
> +	if (!help && p->option & NEED_WORK_TREE)
> +		setup_work_tree();
> +
> +	trace_argv_printf(argv, "trace: built-in: git");
> +
> +	status = p->fn(argc, argv, prefix);
> +	if (status)
> +		return status;
> +
> +	/* Somebody closed stdout? */
> +	if (fstat(fileno(stdout), &st))
> +		return 0;
> +	/* Ignore write errors for pipes and sockets.. */
> +	if (S_ISFIFO(st.st_mode) || S_ISSOCK(st.st_mode))
> +		return 0;
> +
> +	/* Check for ENOSPC and EIO errors.. */
> +	if (fflush(stdout))
> +		die_errno("write failure on standard output");
> +	if (ferror(stdout))
> +		die("unknown write failure on standard output");
> +	if (fclose(stdout))
> +		die_errno("close failed on standard output");
> +	return 0;
> +}
> diff --git a/builtin.h b/builtin.h
> index c47c110..9388505 100644
> --- a/builtin.h
> +++ b/builtin.h
> @@ -27,7 +27,28 @@ extern int fmt_merge_msg(struct strbuf *in, struct strbuf *out,
>  
>  extern int textconv_object(const char *path, unsigned mode, const unsigned char *sha1, int sha1_valid, char **buf, unsigned long *buf_size);
>  
> +#define RUN_SETUP		(1<<0)
> +#define RUN_SETUP_GENTLY	(1<<1)
> +#define USE_PAGER		(1<<2)
> +/*
> + * require working tree to be present -- anything uses this needs
> + * RUN_SETUP for reading from the configuration file.
> + */
> +#define NEED_WORK_TREE		(1<<3)
> +
> +struct cmd_struct {
> +	const char *cmd;
> +	int (*fn)(int, const char **, const char *);
> +	int option;
> +};
> +
> +extern int use_pager;
> +
> +extern void commit_pager_choice(void);
> +
>  extern int is_builtin(const char *s);
> +extern void handle_builtin(int argc, const char **argv);
> +extern int run_builtin(struct cmd_struct *p, int argc, const char **argv);
>  
>  extern int cmd_add(int argc, const char **argv, const char *prefix);
>  extern int cmd_annotate(int argc, const char **argv, const char *prefix);
> diff --git a/git.c b/git.c
> index bba4378..c93e545 100644
> --- a/git.c
> +++ b/git.c
> @@ -19,20 +19,6 @@ const char git_more_info_string[] =
>  	   "to read about a specific subcommand or concept.");
>  
>  static struct startup_info git_startup_info;
> -static int use_pager = -1;
> -
> -static void commit_pager_choice(void) {
> -	switch (use_pager) {
> -	case 0:
> -		setenv("GIT_PAGER", "cat", 1);
> -		break;
> -	case 1:
> -		setup_pager();
> -		break;
> -	default:
> -		break;
> -	}
> -}
>  
>  static int handle_options(const char ***argv, int *argc, int *envchanged)
>  {
> @@ -264,230 +250,6 @@ static int handle_alias(int *argcp, const char ***argv)
>  	return ret;
>  }
>  
> -#define RUN_SETUP		(1<<0)
> -#define RUN_SETUP_GENTLY	(1<<1)
> -#define USE_PAGER		(1<<2)
> -/*
> - * require working tree to be present -- anything uses this needs
> - * RUN_SETUP for reading from the configuration file.
> - */
> -#define NEED_WORK_TREE		(1<<3)
> -
> -struct cmd_struct {
> -	const char *cmd;
> -	int (*fn)(int, const char **, const char *);
> -	int option;
> -};
> -
> -static int run_builtin(struct cmd_struct *p, int argc, const char **argv)
> -{
> -	int status, help;
> -	struct stat st;
> -	const char *prefix;
> -
> -	prefix = NULL;
> -	help = argc == 2 && !strcmp(argv[1], "-h");
> -	if (!help) {
> -		if (p->option & RUN_SETUP)
> -			prefix = setup_git_directory();
> -		if (p->option & RUN_SETUP_GENTLY) {
> -			int nongit_ok;
> -			prefix = setup_git_directory_gently(&nongit_ok);
> -		}
> -
> -		if (use_pager == -1 && p->option & (RUN_SETUP | RUN_SETUP_GENTLY))
> -			use_pager = check_pager_config(p->cmd);
> -		if (use_pager == -1 && p->option & USE_PAGER)
> -			use_pager = 1;
> -
> -		if ((p->option & (RUN_SETUP | RUN_SETUP_GENTLY)) &&
> -		    startup_info->have_repository) /* get_git_dir() may set up repo, avoid that */
> -			trace_repo_setup(prefix);
> -	}
> -	commit_pager_choice();
> -
> -	if (!help && p->option & NEED_WORK_TREE)
> -		setup_work_tree();
> -
> -	trace_argv_printf(argv, "trace: built-in: git");
> -
> -	status = p->fn(argc, argv, prefix);
> -	if (status)
> -		return status;
> -
> -	/* Somebody closed stdout? */
> -	if (fstat(fileno(stdout), &st))
> -		return 0;
> -	/* Ignore write errors for pipes and sockets.. */
> -	if (S_ISFIFO(st.st_mode) || S_ISSOCK(st.st_mode))
> -		return 0;
> -
> -	/* Check for ENOSPC and EIO errors.. */
> -	if (fflush(stdout))
> -		die_errno("write failure on standard output");
> -	if (ferror(stdout))
> -		die("unknown write failure on standard output");
> -	if (fclose(stdout))
> -		die_errno("close failed on standard output");
> -	return 0;
> -}
> -
> -static struct cmd_struct commands[] = {
> -	{ "add", cmd_add, RUN_SETUP | NEED_WORK_TREE },
> -	{ "annotate", cmd_annotate, RUN_SETUP },
> -	{ "apply", cmd_apply, RUN_SETUP_GENTLY },
> -	{ "archive", cmd_archive },
> -	{ "bisect--helper", cmd_bisect__helper, RUN_SETUP },
> -	{ "blame", cmd_blame, RUN_SETUP },
> -	{ "branch", cmd_branch, RUN_SETUP },
> -	{ "bundle", cmd_bundle, RUN_SETUP_GENTLY },
> -	{ "cat-file", cmd_cat_file, RUN_SETUP },
> -	{ "check-attr", cmd_check_attr, RUN_SETUP },
> -	{ "check-ignore", cmd_check_ignore, RUN_SETUP | NEED_WORK_TREE },
> -	{ "check-mailmap", cmd_check_mailmap, RUN_SETUP },
> -	{ "check-ref-format", cmd_check_ref_format },
> -	{ "checkout", cmd_checkout, RUN_SETUP | NEED_WORK_TREE },
> -	{ "checkout-index", cmd_checkout_index,
> -		RUN_SETUP | NEED_WORK_TREE},
> -	{ "cherry", cmd_cherry, RUN_SETUP },
> -	{ "cherry-pick", cmd_cherry_pick, RUN_SETUP | NEED_WORK_TREE },
> -	{ "clean", cmd_clean, RUN_SETUP | NEED_WORK_TREE },
> -	{ "clone", cmd_clone },
> -	{ "column", cmd_column, RUN_SETUP_GENTLY },
> -	{ "commit", cmd_commit, RUN_SETUP | NEED_WORK_TREE },
> -	{ "commit-tree", cmd_commit_tree, RUN_SETUP },
> -	{ "config", cmd_config, RUN_SETUP_GENTLY },
> -	{ "count-objects", cmd_count_objects, RUN_SETUP },
> -	{ "credential", cmd_credential, RUN_SETUP_GENTLY },
> -	{ "describe", cmd_describe, RUN_SETUP },
> -	{ "diff", cmd_diff },
> -	{ "diff-files", cmd_diff_files, RUN_SETUP | NEED_WORK_TREE },
> -	{ "diff-index", cmd_diff_index, RUN_SETUP },
> -	{ "diff-tree", cmd_diff_tree, RUN_SETUP },
> -	{ "fast-export", cmd_fast_export, RUN_SETUP },
> -	{ "fetch", cmd_fetch, RUN_SETUP },
> -	{ "fetch-pack", cmd_fetch_pack, RUN_SETUP },
> -	{ "fmt-merge-msg", cmd_fmt_merge_msg, RUN_SETUP },
> -	{ "for-each-ref", cmd_for_each_ref, RUN_SETUP },
> -	{ "format-patch", cmd_format_patch, RUN_SETUP },
> -	{ "fsck", cmd_fsck, RUN_SETUP },
> -	{ "fsck-objects", cmd_fsck, RUN_SETUP },
> -	{ "gc", cmd_gc, RUN_SETUP },
> -	{ "get-tar-commit-id", cmd_get_tar_commit_id },
> -	{ "grep", cmd_grep, RUN_SETUP_GENTLY },
> -	{ "hash-object", cmd_hash_object },
> -	{ "help", cmd_help },
> -	{ "index-pack", cmd_index_pack, RUN_SETUP_GENTLY },
> -	{ "init", cmd_init_db },
> -	{ "init-db", cmd_init_db },
> -	{ "log", cmd_log, RUN_SETUP },
> -	{ "ls-files", cmd_ls_files, RUN_SETUP },
> -	{ "ls-remote", cmd_ls_remote, RUN_SETUP_GENTLY },
> -	{ "ls-tree", cmd_ls_tree, RUN_SETUP },
> -	{ "mailinfo", cmd_mailinfo },
> -	{ "mailsplit", cmd_mailsplit },
> -	{ "merge", cmd_merge, RUN_SETUP | NEED_WORK_TREE },
> -	{ "merge-base", cmd_merge_base, RUN_SETUP },
> -	{ "merge-file", cmd_merge_file, RUN_SETUP_GENTLY },
> -	{ "merge-index", cmd_merge_index, RUN_SETUP },
> -	{ "merge-ours", cmd_merge_ours, RUN_SETUP },
> -	{ "merge-recursive", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE },
> -	{ "merge-recursive-ours", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE },
> -	{ "merge-recursive-theirs", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE },
> -	{ "merge-subtree", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE },
> -	{ "merge-tree", cmd_merge_tree, RUN_SETUP },
> -	{ "mktag", cmd_mktag, RUN_SETUP },
> -	{ "mktree", cmd_mktree, RUN_SETUP },
> -	{ "mv", cmd_mv, RUN_SETUP | NEED_WORK_TREE },
> -	{ "name-rev", cmd_name_rev, RUN_SETUP },
> -	{ "notes", cmd_notes, RUN_SETUP },
> -	{ "pack-objects", cmd_pack_objects, RUN_SETUP },
> -	{ "pack-redundant", cmd_pack_redundant, RUN_SETUP },
> -	{ "pack-refs", cmd_pack_refs, RUN_SETUP },
> -	{ "patch-id", cmd_patch_id },
> -	{ "pickaxe", cmd_blame, RUN_SETUP },
> -	{ "prune", cmd_prune, RUN_SETUP },
> -	{ "prune-packed", cmd_prune_packed, RUN_SETUP },
> -	{ "push", cmd_push, RUN_SETUP },
> -	{ "read-tree", cmd_read_tree, RUN_SETUP },
> -	{ "receive-pack", cmd_receive_pack },
> -	{ "reflog", cmd_reflog, RUN_SETUP },
> -	{ "remote", cmd_remote, RUN_SETUP },
> -	{ "remote-ext", cmd_remote_ext },
> -	{ "remote-fd", cmd_remote_fd },
> -	{ "repack", cmd_repack, RUN_SETUP },
> -	{ "replace", cmd_replace, RUN_SETUP },
> -	{ "rerere", cmd_rerere, RUN_SETUP },
> -	{ "reset", cmd_reset, RUN_SETUP },
> -	{ "rev-list", cmd_rev_list, RUN_SETUP },
> -	{ "rev-parse", cmd_rev_parse },
> -	{ "revert", cmd_revert, RUN_SETUP | NEED_WORK_TREE },
> -	{ "rm", cmd_rm, RUN_SETUP },
> -	{ "send-pack", cmd_send_pack, RUN_SETUP },
> -	{ "shortlog", cmd_shortlog, RUN_SETUP_GENTLY | USE_PAGER },
> -	{ "show", cmd_show, RUN_SETUP },
> -	{ "show-branch", cmd_show_branch, RUN_SETUP },
> -	{ "show-ref", cmd_show_ref, RUN_SETUP },
> -	{ "stage", cmd_add, RUN_SETUP | NEED_WORK_TREE },
> -	{ "status", cmd_status, RUN_SETUP | NEED_WORK_TREE },
> -	{ "stripspace", cmd_stripspace },
> -	{ "symbolic-ref", cmd_symbolic_ref, RUN_SETUP },
> -	{ "tag", cmd_tag, RUN_SETUP },
> -	{ "unpack-file", cmd_unpack_file, RUN_SETUP },
> -	{ "unpack-objects", cmd_unpack_objects, RUN_SETUP },
> -	{ "update-index", cmd_update_index, RUN_SETUP },
> -	{ "update-ref", cmd_update_ref, RUN_SETUP },
> -	{ "update-server-info", cmd_update_server_info, RUN_SETUP },
> -	{ "upload-archive", cmd_upload_archive },
> -	{ "upload-archive--writer", cmd_upload_archive_writer },
> -	{ "var", cmd_var, RUN_SETUP_GENTLY },
> -	{ "verify-pack", cmd_verify_pack },
> -	{ "verify-tag", cmd_verify_tag, RUN_SETUP },
> -	{ "version", cmd_version },
> -	{ "whatchanged", cmd_whatchanged, RUN_SETUP },
> -	{ "write-tree", cmd_write_tree, RUN_SETUP },
> -};
> -
> -int is_builtin(const char *s)
> -{
> -	int i;
> -	for (i = 0; i < ARRAY_SIZE(commands); i++) {
> -		struct cmd_struct *p = commands+i;
> -		if (!strcmp(s, p->cmd))
> -			return 1;
> -	}
> -	return 0;
> -}
> -
> -static void handle_builtin(int argc, const char **argv)
> -{
> -	const char *cmd = argv[0];
> -	int i;
> -	static const char ext[] = STRIP_EXTENSION;
> -
> -	if (sizeof(ext) > 1) {
> -		i = strlen(argv[0]) - strlen(ext);
> -		if (i > 0 && !strcmp(argv[0] + i, ext)) {
> -			char *argv0 = xstrdup(argv[0]);
> -			argv[0] = cmd = argv0;
> -			argv0[i] = '\0';
> -		}
> -	}
> -
> -	/* Turn "git cmd --help" into "git help cmd" */
> -	if (argc > 1 && !strcmp(argv[1], "--help")) {
> -		argv[1] = argv[0];
> -		argv[0] = cmd = "help";
> -	}
> -
> -	for (i = 0; i < ARRAY_SIZE(commands); i++) {
> -		struct cmd_struct *p = commands+i;
> -		if (strcmp(p->cmd, cmd))
> -			continue;
> -		exit(run_builtin(p, argc, argv));
> -	}
> -}
> -
>  static void execv_dashed_external(const char **argv)
>  {
>  	struct strbuf cmd = STRBUF_INIT;

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

* Re: [PATCH v2 1/4] Consistently use the term "builtin" instead of "internal command"
  2014-01-02 16:15 ` [PATCH v2 1/4] Consistently use the term "builtin" instead of "internal command" Sebastian Schuberth
@ 2014-01-02 20:31   ` Jonathan Nieder
  2014-01-02 21:05     ` Sebastian Schuberth
  0 siblings, 1 reply; 16+ messages in thread
From: Jonathan Nieder @ 2014-01-02 20:31 UTC (permalink / raw)
  To: Sebastian Schuberth; +Cc: Git Mailing List, Junio C Hamano, Christian Couder

Hi,

Sebastian Schuberth wrote:

[...]
> --- a/Documentation/technical/api-builtin.txt
> +++ b/Documentation/technical/api-builtin.txt
> @@ -14,7 +14,7 @@ Git:
>  
>  . Add the external declaration for the function to `builtin.h`.
>  
> -. Add the command to `commands[]` table in `handle_internal_command()`,
> +. Add the command to `commands[]` table in `handle_builtin()`,

Makes sense.  Using consistent jargon makes for easier reading.

[...]
> +++ b/git.c
[...]
> @@ -563,14 +563,14 @@ int main(int argc, char **av)
[...]
>  	if (starts_with(cmd, "git-")) {
>  		cmd += 4;
>  		argv[0] = cmd;
> -		handle_internal_command(argc, argv);
> +		handle_builtin(argc, argv);
> -		die("cannot handle %s internally", cmd);
> +		die("cannot handle %s as a builtin", cmd);

I think this makes the user-visible message less clear.

Before when the user had a stale git-whatever link lingering in
gitexecdir, git would say

	fatal: cannot handle whatever internally

which tells me git was asked to handle the whatever command internally
and was unable to.  Afterward, it becomes

	fatal: cannot handle whatever as a builtin

which requires that I learn the jargon use of "builtin" as a noun.
busybox's analogous message is "applet not found".  It's less likely
to come up when using git because it requires having a stray link to
"git".  A message like

	$ git whatever
	fatal: whatever: no such built-in command

would just leave me wondering "I never claimed it was built-in; what's
going on?"  I think it would be simplest to keep it as

	$ git whatever
	fatal: cannot handle "whatever" internally

which at least makes it clear that this is a low-level error.

The rest of the patch looks good.

Thanks,
Jonathan

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

* Re: [PATCH v2 4/4] Move builtin-related implementations to a new builtin.c file
  2014-01-02 19:43   ` Junio C Hamano
@ 2014-01-02 20:58     ` Sebastian Schuberth
  0 siblings, 0 replies; 16+ messages in thread
From: Sebastian Schuberth @ 2014-01-02 20:58 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: Git Mailing List, Christian Couder

On Thu, Jan 2, 2014 at 8:43 PM, Junio C Hamano <gitster@pobox.com> wrote:

>>  Documentation/technical/api-builtin.txt |   2 +-
>>  Makefile                                |   1 +
>>  builtin.c                               | 225 ++++++++++++++++++++++++++++++
>>  builtin.h                               |  21 +++
>>  git.c                                   | 238 --------------------------------
>>  5 files changed, 248 insertions(+), 239 deletions(-)
>>  create mode 100644 builtin.c
>
> I'm sorry but I do not see a point in this.
>
> It is not like builtin.c can be used outside the context of the main
> Git program, and many helper functions you moved out of git.c that
> used to be static want to be called from other places.

I've added this commit because Christian suggested so in [1], and also
because it has always bothered me that the Git project does not define
a function in a file named after the header file that declares the
function. Anyway, I've made this the last commit in the series on
purpose, I can just drop it in the next re-roll.

[1] http://www.spinics.net/lists/git/msg222452.html.

-- 
Sebastian Schuberth

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

* Re: [PATCH v2 1/4] Consistently use the term "builtin" instead of "internal command"
  2014-01-02 20:31   ` Jonathan Nieder
@ 2014-01-02 21:05     ` Sebastian Schuberth
  2014-01-22 21:08       ` Sebastian Schuberth
  0 siblings, 1 reply; 16+ messages in thread
From: Sebastian Schuberth @ 2014-01-02 21:05 UTC (permalink / raw)
  To: Jonathan Nieder; +Cc: Git Mailing List, Junio C Hamano, Christian Couder

On Thu, Jan 2, 2014 at 9:31 PM, Jonathan Nieder <jrnieder@gmail.com> wrote:

> would just leave me wondering "I never claimed it was built-in; what's
> going on?"  I think it would be simplest to keep it as
>
>         $ git whatever
>         fatal: cannot handle "whatever" internally
>
> which at least makes it clear that this is a low-level error.

Right, I'll change this in a re-roll (using single-quotes for the command name).

> The rest of the patch looks good.

Thanks for the review.

-- 
Sebastian Schuberth

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

* Re: [PATCH v2 3/4] Speed up is_git_command() by checking early for internal commands
  2014-01-02 19:41   ` Junio C Hamano
@ 2014-01-03 15:44     ` Jeff King
  2014-01-03 18:00       ` Junio C Hamano
  0 siblings, 1 reply; 16+ messages in thread
From: Jeff King @ 2014-01-03 15:44 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: Sebastian Schuberth, Git Mailing List, Christian Couder

On Thu, Jan 02, 2014 at 11:41:05AM -0800, Junio C Hamano wrote:

>  - builtin/merge.c is the same, but it is conceptually even worse.
>    It has the end-user supplied string and wants to see if it is a
>    valid strategy.  If the user wants to use a custom strategy, a
>    single stat() to make sure if it exists should suffice, and the
>    error codepath should load the command list to present the names
>    of available ones in the error message.

Is it a single stat()? I think we would need to check each element of
$PATH. Though in practice, the exec-dir would be the first thing we
check, and where we would find the majority of hits. So it would still
be a win, as we would avoid touching anything but the exec-dir in the
common case.

-Peff

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

* Re: [PATCH v2 3/4] Speed up is_git_command() by checking early for internal commands
  2014-01-02 16:17 ` [PATCH v2 3/4] Speed up is_git_command() by checking early for internal commands Sebastian Schuberth
  2014-01-02 19:41   ` Junio C Hamano
@ 2014-01-03 16:49   ` Kent R. Spillner
  2014-01-05 13:42     ` Sebastian Schuberth
  1 sibling, 1 reply; 16+ messages in thread
From: Kent R. Spillner @ 2014-01-03 16:49 UTC (permalink / raw)
  To: Sebastian Schuberth; +Cc: git, Junio C Hamano, Christian Couder


> Since 2dce956 is_git_command() is a bit slow as it does file I/O in the
> call to list_commands_in_dir(). Avoid the file I/O by adding an early
> check for internal commands.

Considering the purpose of the series is it better to say "builtin" instead of "internal" in the commit message?

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

* Re: [PATCH v2 3/4] Speed up is_git_command() by checking early for internal commands
  2014-01-03 15:44     ` Jeff King
@ 2014-01-03 18:00       ` Junio C Hamano
  0 siblings, 0 replies; 16+ messages in thread
From: Junio C Hamano @ 2014-01-03 18:00 UTC (permalink / raw)
  To: Jeff King; +Cc: Sebastian Schuberth, Git Mailing List, Christian Couder

Jeff King <peff@peff.net> writes:

> On Thu, Jan 02, 2014 at 11:41:05AM -0800, Junio C Hamano wrote:
>
>>  - builtin/merge.c is the same, but it is conceptually even worse.
>>    It has the end-user supplied string and wants to see if it is a
>>    valid strategy.  If the user wants to use a custom strategy, a
>>    single stat() to make sure if it exists should suffice, and the
>>    error codepath should load the command list to present the names
>>    of available ones in the error message.
>
> Is it a single stat()? I think we would need to check each element of
> $PATH. 

Yeah, load_command_list() iterates over the members of env_path.

> Though in practice, the exec-dir would be the first thing we
> check, and where we would find the majority of hits. So it would still
> be a win, as we would avoid touching anything but the exec-dir in the
> common case.

Exactly.

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

* Re: [PATCH v2 3/4] Speed up is_git_command() by checking early for internal commands
  2014-01-03 16:49   ` Kent R. Spillner
@ 2014-01-05 13:42     ` Sebastian Schuberth
  2014-01-22 21:05       ` Sebastian Schuberth
  0 siblings, 1 reply; 16+ messages in thread
From: Sebastian Schuberth @ 2014-01-05 13:42 UTC (permalink / raw)
  To: Kent R. Spillner; +Cc: git, Junio C Hamano, Christian Couder

On Fri, Jan 3, 2014 at 5:49 PM, Kent R. Spillner <kspillner@acm.org> wrote:

>> Since 2dce956 is_git_command() is a bit slow as it does file I/O in the
>> call to list_commands_in_dir(). Avoid the file I/O by adding an early
>> check for internal commands.
>
> Considering the purpose of the series is it better to say "builtin" instead of "internal" in the commit message?

True, I'll fix this in a re-rool.

-- 
Sebastian Schuberth

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

* Re: [PATCH v2 3/4] Speed up is_git_command() by checking early for internal commands
  2014-01-05 13:42     ` Sebastian Schuberth
@ 2014-01-22 21:05       ` Sebastian Schuberth
  0 siblings, 0 replies; 16+ messages in thread
From: Sebastian Schuberth @ 2014-01-22 21:05 UTC (permalink / raw)
  To: Kent R. Spillner; +Cc: git, Junio C Hamano, Christian Couder

On 05.01.2014 14:42, Sebastian Schuberth wrote:

>>> Since 2dce956 is_git_command() is a bit slow as it does file I/O in the
>>> call to list_commands_in_dir(). Avoid the file I/O by adding an early
>>> check for internal commands.
>>
>> Considering the purpose of the series is it better to say "builtin" instead of "internal" in the commit message?
>
> True, I'll fix this in a re-rool.

Sorry for not coming up with the re-roll until now, but lucky Junio has 
fixed this himself in c6127fa which already is on master.

-- 
Sebastian Schuberth

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

* Re: [PATCH v2 1/4] Consistently use the term "builtin" instead of "internal command"
  2014-01-02 21:05     ` Sebastian Schuberth
@ 2014-01-22 21:08       ` Sebastian Schuberth
  0 siblings, 0 replies; 16+ messages in thread
From: Sebastian Schuberth @ 2014-01-22 21:08 UTC (permalink / raw)
  To: Jonathan Nieder; +Cc: Git Mailing List, Junio C Hamano, Christian Couder

On 02.01.2014 22:05, Sebastian Schuberth wrote:

>> would just leave me wondering "I never claimed it was built-in; what's
>> going on?"  I think it would be simplest to keep it as
>>
>>          $ git whatever
>>          fatal: cannot handle "whatever" internally
>>
>> which at least makes it clear that this is a low-level error.
>
> Right, I'll change this in a re-roll (using single-quotes for the command name).

Sorry for not coming up with the re-roll until now, and now it's too 
late to fixup the commit as it's already on master (3f784a4). Since this 
is just a minor wording issue I'll not follow this up anymore.

-- 
Sebastian Schuberth

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

end of thread, other threads:[~2014-01-22 21:09 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-01-02 16:12 [PATCH v2 0/4] Sebastian Schuberth
2014-01-02 16:15 ` [PATCH v2 1/4] Consistently use the term "builtin" instead of "internal command" Sebastian Schuberth
2014-01-02 20:31   ` Jonathan Nieder
2014-01-02 21:05     ` Sebastian Schuberth
2014-01-22 21:08       ` Sebastian Schuberth
2014-01-02 16:16 ` [PATCH v2 2/4] Call load_command_list() only when it is needed Sebastian Schuberth
2014-01-02 16:17 ` [PATCH v2 3/4] Speed up is_git_command() by checking early for internal commands Sebastian Schuberth
2014-01-02 19:41   ` Junio C Hamano
2014-01-03 15:44     ` Jeff King
2014-01-03 18:00       ` Junio C Hamano
2014-01-03 16:49   ` Kent R. Spillner
2014-01-05 13:42     ` Sebastian Schuberth
2014-01-22 21:05       ` Sebastian Schuberth
2014-01-02 16:17 ` [PATCH v2 4/4] Move builtin-related implementations to a new builtin.c file Sebastian Schuberth
2014-01-02 19:43   ` Junio C Hamano
2014-01-02 20:58     ` Sebastian Schuberth

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.