All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCHv4 0/3] submodule--helper: Have some refactoring only patches first
@ 2015-09-01 18:24 Stefan Beller
  2015-09-01 18:24 ` [PATCHv4 1/3] submodule: implement `list` as a builtin helper Stefan Beller
                   ` (2 more replies)
  0 siblings, 3 replies; 12+ messages in thread
From: Stefan Beller @ 2015-09-01 18:24 UTC (permalink / raw)
  To: sbeller, gitster, sunshine
  Cc: git, jrnieder, johannes.schindelin, Jens.Lehmann, peff

I added all suggestions from Eric and rewrote the main function
to not have hardcoded all the commands we're introducing.

diff to patch series 3 below.

Stefan Beller (3):
  submodule: implement `list` as a builtin helper
  submodule: implement `name` as a builtin helper
  submodule: implement `clone` as a builtin helper

 .gitignore                  |   1 +
 Makefile                    |   1 +
 builtin.h                   |   1 +
 builtin/submodule--helper.c | 299 ++++++++++++++++++++++++++++++++++++++++++++
 git-submodule.sh            | 166 +++---------------------
 git.c                       |   1 +
 6 files changed, 319 insertions(+), 150 deletions(-)
 create mode 100644 builtin/submodule--helper.c


diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
index 0669641..63f535a 100644
--- a/builtin/submodule--helper.c
+++ b/builtin/submodule--helper.c
@@ -33,7 +33,7 @@ static int module_list_compute(int argc, const char **argv,
 		ps_matched = xcalloc(pathspec->nr, 1);
 
 	if (read_cache() < 0)
-		die("index file corrupt");
+		die(_("index file corrupt"));
 
 	for (i = 0; i < active_nr; i++) {
 		const struct cache_entry *ce = active_cache[i];
@@ -79,7 +79,7 @@ static int module_list(int argc, const char **argv, const char *prefix)
 	};
 
 	const char *const git_submodule_helper_usage[] = {
-		N_("git submodule--helper module_list [--prefix=<path>] [<path>...]"),
+		N_("git submodule--helper list [--prefix=<path>] [<path>...]"),
 		NULL
 	};
 
@@ -110,21 +110,20 @@ static int module_name(int argc, const char **argv, const char *prefix)
 {
 	const struct submodule *sub;
 
-	if (argc != 1)
+	if (argc != 2)
 		usage("git submodule--helper module_name <path>\n");
 
 	gitmodules_config();
-	sub = submodule_from_path(null_sha1, argv[0]);
+	sub = submodule_from_path(null_sha1, argv[1]);
 
 	if (!sub)
 		die(N_("No submodule mapping found in .gitmodules for path '%s'"),
-		    argv[0]);
+		    argv[1]);
 
 	printf("%s\n", sub->name);
 
 	return 0;
 }
-
 static int clone_submodule(const char *path, const char *gitdir, const char *url,
 			   const char *depth, const char *reference, int quiet)
 {
@@ -166,7 +165,7 @@ static int module_clone(int argc, const char **argv, const char *prefix)
 	struct strbuf rel_path = STRBUF_INIT;
 	struct strbuf sb = STRBUF_INIT;
 
-	struct option module_update_options[] = {
+	struct option module_clone_options[] = {
 		OPT_STRING(0, "prefix", &alternative_path,
 			   N_("path"),
 			   N_("alternative anchor for relative paths")),
@@ -189,38 +188,41 @@ static int module_clone(int argc, const char **argv, const char *prefix)
 		OPT_END()
 	};
 
-	static const char * const git_submodule_helper_usage[] = {
-		N_("git submodule--helper update [--prefix=<path>] [--quiet] [--remote] [-N|--no-fetch]"
-		   "[-f|--force] [--rebase|--merge] [--reference <repository>]"
-		   "[--depth <depth>] [--recursive] [--] [<path>...]"),
+	const char * const git_submodule_helper_usage[] = {
+		N_("git submodule--helper clone [--prefix=<path>] [--quiet] "
+		   "[--reference <repository>] [--name <name>] [--url <url>]"
+		   "[--depth <depth>] [--] [<path>...]"),
 		NULL
 	};
 
-	argc = parse_options(argc, argv, prefix, module_update_options,
+	argc = parse_options(argc, argv, prefix, module_clone_options,
 			     git_submodule_helper_usage, 0);
 
 	strbuf_addf(&sb, "%s/modules/%s", get_git_dir(), name);
 	sm_gitdir = strbuf_detach(&sb, NULL);
 
 	if (!file_exists(sm_gitdir)) {
-		safe_create_leading_directories_const(sm_gitdir);
+		if (safe_create_leading_directories_const(sm_gitdir) < 0)
+			die(_("could not create directory '%s'"), sm_gitdir);
 		if (clone_submodule(path, sm_gitdir, url, depth, reference, quiet))
-			die(N_("Clone of '%s' into submodule path '%s' failed"),
+			die(N_("clone of '%s' into submodule path '%s' failed"),
 			    url, path);
 	} else {
-		safe_create_leading_directories_const(path);
-		unlink(sm_gitdir);
+		if (safe_create_leading_directories_const(path) < 0)
+			die(_("could not create directory '%s'"), path);
+		if (unlink(sm_gitdir) < 0)
+			die_errno(_("failed to delete '%s'"), sm_gitdir);
 	}
 
 	/* Write a .git file in the submodule to redirect to the superproject. */
-	if (alternative_path && !strcmp(alternative_path, "")) {
+	if (alternative_path && *alternative_path)) {
 		p = relative_path(path, alternative_path, &sb);
 		strbuf_reset(&sb);
 	} else
 		p = path;
 
 	if (safe_create_leading_directories_const(p) < 0)
-		die("Could not create directory '%s'", p);
+		die(_("could not create directory '%s'"), p);
 
 	strbuf_addf(&sb, "%s/.git", p);
 
@@ -228,31 +230,32 @@ static int module_clone(int argc, const char **argv, const char *prefix)
 		die(_("could not create leading directories of '%s'"), sb.buf);
 	submodule_dot_git = fopen(sb.buf, "w");
 	if (!submodule_dot_git)
-		die ("Cannot open file '%s': %s", sb.buf, strerror(errno));
+		die (_("cannot open file '%s': %s"), sb.buf, strerror(errno));
 
 	fprintf(submodule_dot_git, "gitdir: %s\n",
 		relative_path(sm_gitdir, path, &rel_path));
 	if (fclose(submodule_dot_git))
-		die("Could not close file %s", sb.buf);
+		die(_("could not close file %s"), sb.buf);
 	strbuf_reset(&sb);
 
-	/* Redirect the worktree of the submodule in the superprojects config */
+	/* Redirect the worktree of the submodule in the superproject's config */
+	if (strbuf_getcwd(&sb))
+		die_errno(_("unable to get current working directory"));
+
 	if (!is_absolute_path(sm_gitdir)) {
-		char *s = (char*)sm_gitdir;
 		if (strbuf_getcwd(&sb))
-			die_errno("unable to get current working directory");
+			die_errno(_("unable to get current working directory"));
 		strbuf_addf(&sb, "/%s", sm_gitdir);
+		free(sm_gitdir);
 		sm_gitdir = strbuf_detach(&sb, NULL);
-		free(s);
 	}
 
-	if (strbuf_getcwd(&sb))
-		die_errno("unable to get current working directory");
+
 	strbuf_addf(&sb, "/%s", path);
 
 	p = git_pathdup_submodule(path, "config");
 	if (!p)
-		die("Could not get submodule directory for '%s'", path);
+		die(_("could not get submodule directory for '%s'"), path);
 	git_config_set_in_file(p, "core.worktree",
 			       relative_path(sb.buf, sm_gitdir, &rel_path));
 	strbuf_release(&sb);
@@ -260,23 +263,37 @@ static int module_clone(int argc, const char **argv, const char *prefix)
 	return 0;
 }
 
+struct cmd_struct {
+	const char *cmd;
+	int (*fct)(int, const char **, const char *);
+};
+
+static struct cmd_struct commands[] = {
+	{"list", module_list},
+	{"name", module_name},
+	{"clone", module_clone},
+};
+
 int cmd_submodule__helper(int argc, const char **argv, const char *prefix)
 {
+	int i;
 	if (argc < 2)
-		die(N_("fatal: submodule--helper subcommand must be called with "
-		       "a subcommand, which are module-list, module-name, "
-		       "module-clone\n"));
+		goto out;
 
-	if (!strcmp(argv[1], "module-list"))
-		return module_list(argc - 1, argv + 1, prefix);
+	for (i = 0; i < ARRAY_SIZE(commands); i++)
+		if (!strcmp(argv[1], commands[i].cmd))
+			return commands[i].fct(argc - 1, argv + 1, prefix);
 
-	if (!strcmp(argv[1], "module-name"))
-		return module_name(argc - 2, argv + 2, prefix);
+out:
+	if (argc > 1)
+		fprintf(stderr, _("fatal: '%s' is not a valid submodule--helper "
+				  "subcommand, which are:\n"), argv[1]);
+	else
+		fprintf(stderr, _("fatal: submodule--helper subcommand must be "
+				  "called with a subcommand, which are:\n"));
 
-	if (!strcmp(argv[1], "module-clone"))
-		return module_clone(argc - 1, argv + 1, prefix);
+	for (i = 0; i < ARRAY_SIZE(commands); i++)
+		fprintf(stderr, "\t%s\n", commands[i].cmd);
 
-	die(N_("fatal: '%s' is not a valid submodule--helper subcommand, "
-	       "which are module-list, module-name, module-clone\n"),
-	    argv[1]);
+	exit(129);
 }
diff --git a/git-submodule.sh b/git-submodule.sh
index d1523ea..7cfdc2c 100755
--- a/git-submodule.sh
+++ b/git-submodule.sh
@@ -338,7 +338,7 @@ Use -f if you really want to add it." >&2
 				echo "$(eval_gettext "Reactivating local git directory for submodule '\$sm_name'.")"
 			fi
 		fi
-		git submodule--helper module-clone ${GIT_QUIET:+--quiet} --prefix "$wt_prefix" --path "$sm_path" --name "$sm_name" --url "$realrepo" "$reference" "$depth" || exit
+		git submodule--helper clone ${GIT_QUIET:+--quiet} --prefix "$wt_prefix" --path "$sm_path" --name "$sm_name" --url "$realrepo" "$reference" "$depth" || exit
 		(
 			clear_local_git_env
 			cd "$sm_path" &&
@@ -398,7 +398,7 @@ cmd_foreach()
 	# command in the subshell (and a recursive call to this function)
 	exec 3<&0
 
-	git submodule--helper module-list --prefix "$wt_prefix"|
+	git submodule--helper list --prefix "$wt_prefix"|
 	while read mode sha1 stage sm_path
 	do
 		die_if_unmatched "$mode"
@@ -406,7 +406,7 @@ cmd_foreach()
 		then
 			displaypath=$(relative_path "$sm_path")
 			say "$(eval_gettext "Entering '\$prefix\$displaypath'")"
-			name=$(git submodule--helper module-name "$sm_path")
+			name=$(git submodule--helper name "$sm_path")
 			(
 				prefix="$prefix$sm_path/"
 				clear_local_git_env
@@ -458,11 +458,11 @@ cmd_init()
 		shift
 	done
 
-	git submodule--helper module-list --prefix "$wt_prefix" "$@" |
+	git submodule--helper list --prefix "$wt_prefix" "$@" |
 	while read mode sha1 stage sm_path
 	do
 		die_if_unmatched "$mode"
-		name=$(git submodule--helper module-name "$sm_path") || exit
+		name=$(git submodule--helper name "$sm_path") || exit
 
 		displaypath=$(relative_path "$sm_path")
 
@@ -540,11 +540,11 @@ cmd_deinit()
 		die "$(eval_gettext "Use '.' if you really want to deinitialize all submodules")"
 	fi
 
-	git submodule--helper module-list --prefix "$wt_prefix" "$@" |
+	git submodule--helper list --prefix "$wt_prefix" "$@" |
 	while read mode sha1 stage sm_path
 	do
 		die_if_unmatched "$mode"
-		name=$(git submodule--helper module-name "$sm_path") || exit
+		name=$(git submodule--helper name "$sm_path") || exit
 
 		displaypath=$(relative_path "$sm_path")
 
@@ -656,7 +656,7 @@ cmd_update()
 	fi
 
 	cloned_modules=
-	git submodule--helper module-list --prefix "$wt_prefix" "$@" | {
+	git submodule--helper list --prefix "$wt_prefix" "$@" | {
 	err=
 	while read mode sha1 stage sm_path
 	do
@@ -666,7 +666,7 @@ cmd_update()
 			echo >&2 "Skipping unmerged submodule $prefix$sm_path"
 			continue
 		fi
-		name=$(git submodule--helper module-name "$sm_path") || exit
+		name=$(git submodule--helper name "$sm_path") || exit
 		url=$(git config submodule."$name".url)
 		branch=$(get_submodule_config "$name" branch master)
 		if ! test -z "$update"
@@ -700,7 +700,7 @@ Maybe you want to use 'update --init'?")"
 
 		if ! test -d "$sm_path"/.git && ! test -f "$sm_path"/.git
 		then
-			git submodule--helper module-clone ${GIT_QUIET:+--quiet} --prefix "$prefix" --path "$sm_path" --name "$name" --url "$url" "$reference" "$depth" || exit
+			git submodule--helper clone ${GIT_QUIET:+--quiet} --prefix "$prefix" --path "$sm_path" --name "$name" --url "$url" "$reference" "$depth" || exit
 			cloned_modules="$cloned_modules;$name"
 			subsha1=
 		else
@@ -930,7 +930,7 @@ cmd_summary() {
 			# Respect the ignore setting for --for-status.
 			if test -n "$for_status"
 			then
-				name=$(git submodule--helper module-name "$sm_path")
+				name=$(git submodule--helper name "$sm_path")
 				ignore_config=$(get_submodule_config "$name" ignore none)
 				test $status != A && test $ignore_config = all && continue
 			fi
@@ -1088,11 +1088,11 @@ cmd_status()
 		shift
 	done
 
-	git submodule--helper module-list --prefix "$wt_prefix" "$@" |
+	git submodule--helper list --prefix "$wt_prefix" "$@" |
 	while read mode sha1 stage sm_path
 	do
 		die_if_unmatched "$mode"
-		name=$(git submodule--helper module-name "$sm_path") || exit
+		name=$(git submodule--helper name "$sm_path") || exit
 		url=$(git config submodule."$name".url)
 		displaypath=$(relative_path "$prefix$sm_path")
 		if test "$stage" = U
@@ -1165,11 +1165,11 @@ cmd_sync()
 		esac
 	done
 	cd_to_toplevel
-	git submodule--helper module-list --prefix "$wt_prefix" "$@" |
+	git submodule--helper list --prefix "$wt_prefix" "$@" |
 	while read mode sha1 stage sm_path
 	do
 		die_if_unmatched "$mode"
-		name=$(git submodule--helper module-name "$sm_path")
+		name=$(git submodule--helper name "$sm_path")
 		url=$(git config -f .gitmodules --get submodule."$name".url)
 
 		# Possibly a url relative to parent


-- 
2.5.0.256.g89f8063.dirty

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

* [PATCHv4 1/3] submodule: implement `list` as a builtin helper
  2015-09-01 18:24 [PATCHv4 0/3] submodule--helper: Have some refactoring only patches first Stefan Beller
@ 2015-09-01 18:24 ` Stefan Beller
  2015-09-01 19:55   ` Junio C Hamano
  2015-09-01 23:17   ` Eric Sunshine
  2015-09-01 18:24 ` [PATCHv4 2/3] submodule: implement `name` " Stefan Beller
  2015-09-01 18:24 ` [PATCHv4 3/3] submodule: implement `clone` " Stefan Beller
  2 siblings, 2 replies; 12+ messages in thread
From: Stefan Beller @ 2015-09-01 18:24 UTC (permalink / raw)
  To: sbeller, gitster, sunshine
  Cc: git, jrnieder, johannes.schindelin, Jens.Lehmann, peff

Most of the submodule operations work on a set of submodules.
Calculating and using this set is usually done via:

       module_list "$@" | {
           while read mode sha1 stage sm_path
           do
                # the actual operation
           done
       }

Currently the function `module_list` is implemented in the
git-submodule.sh as a shell script wrapping a perl script.
The rewrite is in C, such that it is faster and can later be
easily adapted when other functions are rewritten in C.

git-submodule.sh, similar to the builtin commands, will navigate
to the top-most directory of the repository and keep the
subdirectory as a variable. As the helper is called from
within the git-submodule.sh script, we are already navigated
to the root level, but the path arguments are still relative
to the subdirectory we were in when calling git-submodule.sh.
That's why there is a `--prefix` option pointing to an alternative
path which to anchor relative path arguments.

Signed-off-by: Stefan Beller <sbeller@google.com>
---
 .gitignore                  |   1 +
 Makefile                    |   1 +
 builtin.h                   |   1 +
 builtin/submodule--helper.c | 137 ++++++++++++++++++++++++++++++++++++++++++++
 git-submodule.sh            |  54 ++---------------
 git.c                       |   1 +
 6 files changed, 147 insertions(+), 48 deletions(-)
 create mode 100644 builtin/submodule--helper.c

diff --git a/.gitignore b/.gitignore
index 4fd81ba..1c2f832 100644
--- a/.gitignore
+++ b/.gitignore
@@ -155,6 +155,7 @@
 /git-status
 /git-stripspace
 /git-submodule
+/git-submodule--helper
 /git-svn
 /git-symbolic-ref
 /git-tag
diff --git a/Makefile b/Makefile
index 24b636d..d434e73 100644
--- a/Makefile
+++ b/Makefile
@@ -901,6 +901,7 @@ BUILTIN_OBJS += builtin/shortlog.o
 BUILTIN_OBJS += builtin/show-branch.o
 BUILTIN_OBJS += builtin/show-ref.o
 BUILTIN_OBJS += builtin/stripspace.o
+BUILTIN_OBJS += builtin/submodule--helper.o
 BUILTIN_OBJS += builtin/symbolic-ref.o
 BUILTIN_OBJS += builtin/tag.o
 BUILTIN_OBJS += builtin/unpack-file.o
diff --git a/builtin.h b/builtin.h
index 839483d..924e6c4 100644
--- a/builtin.h
+++ b/builtin.h
@@ -119,6 +119,7 @@ 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_submodule__helper(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);
 extern int cmd_tar_tree(int argc, const char **argv, const char *prefix);
diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
new file mode 100644
index 0000000..16d9abe
--- /dev/null
+++ b/builtin/submodule--helper.c
@@ -0,0 +1,137 @@
+#include "builtin.h"
+#include "cache.h"
+#include "parse-options.h"
+#include "quote.h"
+#include "pathspec.h"
+#include "dir.h"
+#include "utf8.h"
+
+static const struct cache_entry **ce_entries;
+static int ce_alloc, ce_used;
+
+static int module_list_compute(int argc, const char **argv,
+				const char *prefix,
+				struct pathspec *pathspec)
+{
+	int i, result = 0;
+	char *max_prefix, *ps_matched = NULL;
+	int max_prefix_len;
+	parse_pathspec(pathspec, 0,
+		       PATHSPEC_PREFER_FULL |
+		       PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP,
+		       prefix, argv);
+
+	/* Find common prefix for all pathspec's */
+	max_prefix = common_prefix(pathspec);
+	max_prefix_len = max_prefix ? strlen(max_prefix) : 0;
+
+	if (pathspec->nr)
+		ps_matched = xcalloc(pathspec->nr, 1);
+
+	if (read_cache() < 0)
+		die(_("index file corrupt"));
+
+	for (i = 0; i < active_nr; i++) {
+		const struct cache_entry *ce = active_cache[i];
+
+		if (!match_pathspec(pathspec, ce->name, ce_namelen(ce),
+				    max_prefix_len, ps_matched,
+				    S_ISGITLINK(ce->ce_mode) | S_ISDIR(ce->ce_mode)))
+			continue;
+
+		if (S_ISGITLINK(ce->ce_mode)) {
+			ALLOC_GROW(ce_entries, ce_used + 1, ce_alloc);
+			ce_entries[ce_used++] = ce;
+		}
+
+		while (i + 1 < active_nr && !strcmp(ce->name, active_cache[i + 1]->name))
+			/*
+			 * Skip entries with the same name in different stages
+			 * to make sure an entry is returned only once.
+			 */
+			i++;
+	}
+	free(max_prefix);
+
+	if (ps_matched && report_path_error(ps_matched, pathspec, prefix))
+		result = -1;
+
+	free(ps_matched);
+
+	return result;
+}
+
+static int module_list(int argc, const char **argv, const char *prefix)
+{
+	int i;
+	struct pathspec pathspec;
+	const char *alternative_path;
+
+	struct option module_list_options[] = {
+		OPT_STRING(0, "prefix", &alternative_path,
+			   N_("path"),
+			   N_("alternative anchor for relative paths")),
+		OPT_END()
+	};
+
+	const char *const git_submodule_helper_usage[] = {
+		N_("git submodule--helper list [--prefix=<path>] [<path>...]"),
+		NULL
+	};
+
+	argc = parse_options(argc, argv, prefix, module_list_options,
+			     git_submodule_helper_usage, 0);
+
+	if (module_list_compute(argc, argv, alternative_path
+					    ? alternative_path
+					    : prefix, &pathspec) < 0) {
+		printf("#unmatched\n");
+		return 1;
+	}
+
+	for (i = 0; i < ce_used; i++) {
+		const struct cache_entry *ce = ce_entries[i];
+
+		if (ce_stage(ce))
+			printf("%06o %s U\t", ce->ce_mode, sha1_to_hex(null_sha1));
+		else
+			printf("%06o %s %d\t", ce->ce_mode, sha1_to_hex(ce->sha1), ce_stage(ce));
+
+		utf8_fprintf(stdout, "%s\n", ce->name);
+	}
+	return 0;
+}
+
+
+struct cmd_struct {
+	const char *cmd;
+	int (*fct)(int, const char **, const char *);
+};
+
+static struct cmd_struct commands[] = {
+	{"list", module_list},
+};
+
+int cmd_submodule__helper(int argc, const char **argv, const char *prefix)
+{
+	int i;
+	if (argc < 2)
+		goto out;
+
+	for (i = 0; i < ARRAY_SIZE(commands); i++)
+		if (!strcmp(argv[1], commands[i].cmd))
+			return commands[i].fct(argc - 1, argv + 1, prefix);
+
+out:
+	if (argc > 1)
+		fprintf(stderr, _("fatal: '%s' is not a valid submodule--helper "
+				  "subcommand, which are:\n"), argv[1]);
+	else
+		fprintf(stderr, _("fatal: submodule--helper subcommand must be "
+				  "called with a subcommand, which are:\n"));
+
+	for (i = 0; i < ARRAY_SIZE(commands); i++)
+		fprintf(stderr, "\t%s\n", commands[i].cmd);
+
+	exit(129);
+}
diff --git a/git-submodule.sh b/git-submodule.sh
index 36797c3..95c04fc 100755
--- a/git-submodule.sh
+++ b/git-submodule.sh
@@ -145,48 +145,6 @@ relative_path ()
 	echo "$result$target"
 }
 
-#
-# Get submodule info for registered submodules
-# $@ = path to limit submodule list
-#
-module_list()
-{
-	eval "set $(git rev-parse --sq --prefix "$wt_prefix" -- "$@")"
-	(
-		git ls-files -z --error-unmatch --stage -- "$@" ||
-		echo "unmatched pathspec exists"
-	) |
-	@@PERL@@ -e '
-	my %unmerged = ();
-	my ($null_sha1) = ("0" x 40);
-	my @out = ();
-	my $unmatched = 0;
-	$/ = "\0";
-	while (<STDIN>) {
-		if (/^unmatched pathspec/) {
-			$unmatched = 1;
-			next;
-		}
-		chomp;
-		my ($mode, $sha1, $stage, $path) =
-			/^([0-7]+) ([0-9a-f]{40}) ([0-3])\t(.*)$/;
-		next unless $mode eq "160000";
-		if ($stage ne "0") {
-			if (!$unmerged{$path}++) {
-				push @out, "$mode $null_sha1 U\t$path\n";
-			}
-			next;
-		}
-		push @out, "$_\n";
-	}
-	if ($unmatched) {
-		print "#unmatched\n";
-	} else {
-		print for (@out);
-	}
-	'
-}
-
 die_if_unmatched ()
 {
 	if test "$1" = "#unmatched"
@@ -532,7 +490,7 @@ cmd_foreach()
 	# command in the subshell (and a recursive call to this function)
 	exec 3<&0
 
-	module_list |
+	git submodule--helper list --prefix "$wt_prefix"|
 	while read mode sha1 stage sm_path
 	do
 		die_if_unmatched "$mode"
@@ -592,7 +550,7 @@ cmd_init()
 		shift
 	done
 
-	module_list "$@" |
+	git submodule--helper list --prefix "$wt_prefix" "$@" |
 	while read mode sha1 stage sm_path
 	do
 		die_if_unmatched "$mode"
@@ -674,7 +632,7 @@ cmd_deinit()
 		die "$(eval_gettext "Use '.' if you really want to deinitialize all submodules")"
 	fi
 
-	module_list "$@" |
+	git submodule--helper list --prefix "$wt_prefix" "$@" |
 	while read mode sha1 stage sm_path
 	do
 		die_if_unmatched "$mode"
@@ -790,7 +748,7 @@ cmd_update()
 	fi
 
 	cloned_modules=
-	module_list "$@" | {
+	git submodule--helper list --prefix "$wt_prefix" "$@" | {
 	err=
 	while read mode sha1 stage sm_path
 	do
@@ -1222,7 +1180,7 @@ cmd_status()
 		shift
 	done
 
-	module_list "$@" |
+	git submodule--helper list --prefix "$wt_prefix" "$@" |
 	while read mode sha1 stage sm_path
 	do
 		die_if_unmatched "$mode"
@@ -1299,7 +1257,7 @@ cmd_sync()
 		esac
 	done
 	cd_to_toplevel
-	module_list "$@" |
+	git submodule--helper list --prefix "$wt_prefix" "$@" |
 	while read mode sha1 stage sm_path
 	do
 		die_if_unmatched "$mode"
diff --git a/git.c b/git.c
index 55c327c..deecba0 100644
--- a/git.c
+++ b/git.c
@@ -469,6 +469,7 @@ static struct cmd_struct commands[] = {
 	{ "stage", cmd_add, RUN_SETUP | NEED_WORK_TREE },
 	{ "status", cmd_status, RUN_SETUP | NEED_WORK_TREE },
 	{ "stripspace", cmd_stripspace },
+	{ "submodule--helper", cmd_submodule__helper, RUN_SETUP },
 	{ "symbolic-ref", cmd_symbolic_ref, RUN_SETUP },
 	{ "tag", cmd_tag, RUN_SETUP },
 	{ "unpack-file", cmd_unpack_file, RUN_SETUP },
-- 
2.5.0.256.g89f8063.dirty

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

* [PATCHv4 2/3] submodule: implement `name` as a builtin helper
  2015-09-01 18:24 [PATCHv4 0/3] submodule--helper: Have some refactoring only patches first Stefan Beller
  2015-09-01 18:24 ` [PATCHv4 1/3] submodule: implement `list` as a builtin helper Stefan Beller
@ 2015-09-01 18:24 ` Stefan Beller
  2015-09-01 19:01   ` Eric Sunshine
  2015-09-01 18:24 ` [PATCHv4 3/3] submodule: implement `clone` " Stefan Beller
  2 siblings, 1 reply; 12+ messages in thread
From: Stefan Beller @ 2015-09-01 18:24 UTC (permalink / raw)
  To: sbeller, gitster, sunshine
  Cc: git, jrnieder, johannes.schindelin, Jens.Lehmann, peff

This implements the helper `module_name` in C instead of shell,
yielding a nice performance boost.

Before this patch, I measured a time (best out of three):

  $ time ./t7400-submodule-basic.sh  >/dev/null
    real	0m11.066s
    user	0m3.348s
    sys	0m8.534s

With this patch applied I measured (also best out of three)

  $ time ./t7400-submodule-basic.sh  >/dev/null
    real	0m10.063s
    user	0m3.044s
    sys	0m7.487s

Signed-off-by: Stefan Beller <sbeller@google.com>
---
 builtin/submodule--helper.c | 22 ++++++++++++++++++++++
 git-submodule.sh            | 32 +++++++-------------------------
 2 files changed, 29 insertions(+), 25 deletions(-)

diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
index 16d9abe..f5e408a 100644
--- a/builtin/submodule--helper.c
+++ b/builtin/submodule--helper.c
@@ -5,6 +5,9 @@
 #include "pathspec.h"
 #include "dir.h"
 #include "utf8.h"
+#include "submodule.h"
+#include "submodule-config.h"
+#include "string-list.h"
 
 static const struct cache_entry **ce_entries;
 static int ce_alloc, ce_used;
@@ -102,6 +105,24 @@ static int module_list(int argc, const char **argv, const char *prefix)
 	return 0;
 }
 
+static int module_name(int argc, const char **argv, const char *prefix)
+{
+	const struct submodule *sub;
+
+	if (argc != 2)
+		usage("git submodule--helper module_name <path>\n");
+
+	gitmodules_config();
+	sub = submodule_from_path(null_sha1, argv[1]);
+
+	if (!sub)
+		die(N_("No submodule mapping found in .gitmodules for path '%s'"),
+		    argv[1]);
+
+	printf("%s\n", sub->name);
+
+	return 0;
+}
 
 struct cmd_struct {
 	const char *cmd;
@@ -110,6 +131,7 @@ struct cmd_struct {
 
 static struct cmd_struct commands[] = {
 	{"list", module_list},
+	{"name", module_name},
 };
 
 int cmd_submodule__helper(int argc, const char **argv, const char *prefix)
diff --git a/git-submodule.sh b/git-submodule.sh
index 95c04fc..2be8da2 100755
--- a/git-submodule.sh
+++ b/git-submodule.sh
@@ -178,24 +178,6 @@ get_submodule_config () {
 	printf '%s' "${value:-$default}"
 }
 
-
-#
-# Map submodule path to submodule name
-#
-# $1 = path
-#
-module_name()
-{
-	# Do we have "submodule.<something>.path = $1" defined in .gitmodules file?
-	sm_path="$1"
-	re=$(printf '%s\n' "$1" | sed -e 's/[].[^$\\*]/\\&/g')
-	name=$( git config -f .gitmodules --get-regexp '^submodule\..*\.path$' |
-		sed -n -e 's|^submodule\.\(.*\)\.path '"$re"'$|\1|p' )
-	test -z "$name" &&
-	die "$(eval_gettext "No submodule mapping found in .gitmodules for path '\$sm_path'")"
-	printf '%s\n' "$name"
-}
-
 #
 # Clone a submodule
 #
@@ -498,7 +480,7 @@ cmd_foreach()
 		then
 			displaypath=$(relative_path "$sm_path")
 			say "$(eval_gettext "Entering '\$prefix\$displaypath'")"
-			name=$(module_name "$sm_path")
+			name=$(git submodule--helper name "$sm_path")
 			(
 				prefix="$prefix$sm_path/"
 				clear_local_git_env
@@ -554,7 +536,7 @@ cmd_init()
 	while read mode sha1 stage sm_path
 	do
 		die_if_unmatched "$mode"
-		name=$(module_name "$sm_path") || exit
+		name=$(git submodule--helper name "$sm_path") || exit
 
 		displaypath=$(relative_path "$sm_path")
 
@@ -636,7 +618,7 @@ cmd_deinit()
 	while read mode sha1 stage sm_path
 	do
 		die_if_unmatched "$mode"
-		name=$(module_name "$sm_path") || exit
+		name=$(git submodule--helper name "$sm_path") || exit
 
 		displaypath=$(relative_path "$sm_path")
 
@@ -758,7 +740,7 @@ cmd_update()
 			echo >&2 "Skipping unmerged submodule $prefix$sm_path"
 			continue
 		fi
-		name=$(module_name "$sm_path") || exit
+		name=$(git submodule--helper name "$sm_path") || exit
 		url=$(git config submodule."$name".url)
 		branch=$(get_submodule_config "$name" branch master)
 		if ! test -z "$update"
@@ -1022,7 +1004,7 @@ cmd_summary() {
 			# Respect the ignore setting for --for-status.
 			if test -n "$for_status"
 			then
-				name=$(module_name "$sm_path")
+				name=$(git submodule--helper name "$sm_path")
 				ignore_config=$(get_submodule_config "$name" ignore none)
 				test $status != A && test $ignore_config = all && continue
 			fi
@@ -1184,7 +1166,7 @@ cmd_status()
 	while read mode sha1 stage sm_path
 	do
 		die_if_unmatched "$mode"
-		name=$(module_name "$sm_path") || exit
+		name=$(git submodule--helper name "$sm_path") || exit
 		url=$(git config submodule."$name".url)
 		displaypath=$(relative_path "$prefix$sm_path")
 		if test "$stage" = U
@@ -1261,7 +1243,7 @@ cmd_sync()
 	while read mode sha1 stage sm_path
 	do
 		die_if_unmatched "$mode"
-		name=$(module_name "$sm_path")
+		name=$(git submodule--helper name "$sm_path")
 		url=$(git config -f .gitmodules --get submodule."$name".url)
 
 		# Possibly a url relative to parent
-- 
2.5.0.256.g89f8063.dirty

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

* [PATCHv4 3/3] submodule: implement `clone` as a builtin helper
  2015-09-01 18:24 [PATCHv4 0/3] submodule--helper: Have some refactoring only patches first Stefan Beller
  2015-09-01 18:24 ` [PATCHv4 1/3] submodule: implement `list` as a builtin helper Stefan Beller
  2015-09-01 18:24 ` [PATCHv4 2/3] submodule: implement `name` " Stefan Beller
@ 2015-09-01 18:24 ` Stefan Beller
  2015-09-01 18:52   ` Eric Sunshine
  2 siblings, 1 reply; 12+ messages in thread
From: Stefan Beller @ 2015-09-01 18:24 UTC (permalink / raw)
  To: sbeller, gitster, sunshine
  Cc: git, jrnieder, johannes.schindelin, Jens.Lehmann, peff

This converts implements the helper `module_clone`. This functionality is
needed for converting `git submodule update` later on, which we want to
add threading to.

Signed-off-by: Stefan Beller <sbeller@google.com>
---
 builtin/submodule--helper.c | 140 ++++++++++++++++++++++++++++++++++++++++++++
 git-submodule.sh            |  80 +------------------------
 2 files changed, 143 insertions(+), 77 deletions(-)

diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
index f5e408a..63f535a 100644
--- a/builtin/submodule--helper.c
+++ b/builtin/submodule--helper.c
@@ -8,6 +8,7 @@
 #include "submodule.h"
 #include "submodule-config.h"
 #include "string-list.h"
+#include "run-command.h"
 
 static const struct cache_entry **ce_entries;
 static int ce_alloc, ce_used;
@@ -123,6 +124,144 @@ static int module_name(int argc, const char **argv, const char *prefix)
 
 	return 0;
 }
+static int clone_submodule(const char *path, const char *gitdir, const char *url,
+			   const char *depth, const char *reference, int quiet)
+{
+	struct child_process cp;
+	child_process_init(&cp);
+
+	argv_array_push(&cp.args, "clone");
+	argv_array_push(&cp.args, "--no-checkout");
+	if (quiet)
+		argv_array_push(&cp.args, "--quiet");
+	if (depth && *depth)
+		argv_array_pushl(&cp.args, "--depth", depth, NULL);
+	if (reference && *reference)
+		argv_array_pushl(&cp.args, "--reference", reference, NULL);
+	if (gitdir && *gitdir)
+		argv_array_pushl(&cp.args, "--separate-git-dir", gitdir, NULL);
+
+	argv_array_push(&cp.args, url);
+	argv_array_push(&cp.args, path);
+
+	cp.git_cmd = 1;
+	cp.env = local_repo_env;
+
+	cp.no_stdin = 1;
+	cp.no_stdout = 1;
+	cp.no_stderr = 1;
+
+	return run_command(&cp);
+}
+
+static int module_clone(int argc, const char **argv, const char *prefix)
+{
+	const char *path = NULL, *name = NULL, *url = NULL;
+	const char *reference = NULL, *depth = NULL;
+	const char *alternative_path = NULL, *p;
+	int quiet = 0;
+	FILE *submodule_dot_git;
+	char *sm_gitdir;
+	struct strbuf rel_path = STRBUF_INIT;
+	struct strbuf sb = STRBUF_INIT;
+
+	struct option module_clone_options[] = {
+		OPT_STRING(0, "prefix", &alternative_path,
+			   N_("path"),
+			   N_("alternative anchor for relative paths")),
+		OPT_STRING(0, "path", &path,
+			   N_("path"),
+			   N_("where the new submodule will be cloned to")),
+		OPT_STRING(0, "name", &name,
+			   N_("string"),
+			   N_("name of the new submodule")),
+		OPT_STRING(0, "url", &url,
+			   N_("string"),
+			   N_("url where to clone the submodule from")),
+		OPT_STRING(0, "reference", &reference,
+			   N_("string"),
+			   N_("reference repository")),
+		OPT_STRING(0, "depth", &depth,
+			   N_("string"),
+			   N_("depth for shallow clones")),
+		OPT__QUIET(&quiet, "Suppress output for cloning a submodule"),
+		OPT_END()
+	};
+
+	const char * const git_submodule_helper_usage[] = {
+		N_("git submodule--helper clone [--prefix=<path>] [--quiet] "
+		   "[--reference <repository>] [--name <name>] [--url <url>]"
+		   "[--depth <depth>] [--] [<path>...]"),
+		NULL
+	};
+
+	argc = parse_options(argc, argv, prefix, module_clone_options,
+			     git_submodule_helper_usage, 0);
+
+	strbuf_addf(&sb, "%s/modules/%s", get_git_dir(), name);
+	sm_gitdir = strbuf_detach(&sb, NULL);
+
+	if (!file_exists(sm_gitdir)) {
+		if (safe_create_leading_directories_const(sm_gitdir) < 0)
+			die(_("could not create directory '%s'"), sm_gitdir);
+		if (clone_submodule(path, sm_gitdir, url, depth, reference, quiet))
+			die(N_("clone of '%s' into submodule path '%s' failed"),
+			    url, path);
+	} else {
+		if (safe_create_leading_directories_const(path) < 0)
+			die(_("could not create directory '%s'"), path);
+		if (unlink(sm_gitdir) < 0)
+			die_errno(_("failed to delete '%s'"), sm_gitdir);
+	}
+
+	/* Write a .git file in the submodule to redirect to the superproject. */
+	if (alternative_path && *alternative_path)) {
+		p = relative_path(path, alternative_path, &sb);
+		strbuf_reset(&sb);
+	} else
+		p = path;
+
+	if (safe_create_leading_directories_const(p) < 0)
+		die(_("could not create directory '%s'"), p);
+
+	strbuf_addf(&sb, "%s/.git", p);
+
+	if (safe_create_leading_directories_const(sb.buf) < 0)
+		die(_("could not create leading directories of '%s'"), sb.buf);
+	submodule_dot_git = fopen(sb.buf, "w");
+	if (!submodule_dot_git)
+		die (_("cannot open file '%s': %s"), sb.buf, strerror(errno));
+
+	fprintf(submodule_dot_git, "gitdir: %s\n",
+		relative_path(sm_gitdir, path, &rel_path));
+	if (fclose(submodule_dot_git))
+		die(_("could not close file %s"), sb.buf);
+	strbuf_reset(&sb);
+
+	/* Redirect the worktree of the submodule in the superproject's config */
+	if (strbuf_getcwd(&sb))
+		die_errno(_("unable to get current working directory"));
+
+	if (!is_absolute_path(sm_gitdir)) {
+		if (strbuf_getcwd(&sb))
+			die_errno(_("unable to get current working directory"));
+		strbuf_addf(&sb, "/%s", sm_gitdir);
+		free(sm_gitdir);
+		sm_gitdir = strbuf_detach(&sb, NULL);
+	}
+
+
+	strbuf_addf(&sb, "/%s", path);
+
+	p = git_pathdup_submodule(path, "config");
+	if (!p)
+		die(_("could not get submodule directory for '%s'"), path);
+	git_config_set_in_file(p, "core.worktree",
+			       relative_path(sb.buf, sm_gitdir, &rel_path));
+	strbuf_release(&sb);
+	free(sm_gitdir);
+	return 0;
+}
 
 struct cmd_struct {
 	const char *cmd;
@@ -132,6 +271,7 @@ struct cmd_struct {
 static struct cmd_struct commands[] = {
 	{"list", module_list},
 	{"name", module_name},
+	{"clone", module_clone},
 };
 
 int cmd_submodule__helper(int argc, const char **argv, const char *prefix)
diff --git a/git-submodule.sh b/git-submodule.sh
index 2be8da2..7cfdc2c 100755
--- a/git-submodule.sh
+++ b/git-submodule.sh
@@ -178,80 +178,6 @@ get_submodule_config () {
 	printf '%s' "${value:-$default}"
 }
 
-#
-# Clone a submodule
-#
-# $1 = submodule path
-# $2 = submodule name
-# $3 = URL to clone
-# $4 = reference repository to reuse (empty for independent)
-# $5 = depth argument for shallow clones (empty for deep)
-#
-# Prior to calling, cmd_update checks that a possibly existing
-# path is not a git repository.
-# Likewise, cmd_add checks that path does not exist at all,
-# since it is the location of a new submodule.
-#
-module_clone()
-{
-	sm_path=$1
-	name=$2
-	url=$3
-	reference="$4"
-	depth="$5"
-	quiet=
-	if test -n "$GIT_QUIET"
-	then
-		quiet=-q
-	fi
-
-	gitdir=
-	gitdir_base=
-	base_name=$(dirname "$name")
-
-	gitdir=$(git rev-parse --git-dir)
-	gitdir_base="$gitdir/modules/$base_name"
-	gitdir="$gitdir/modules/$name"
-
-	if test -d "$gitdir"
-	then
-		mkdir -p "$sm_path"
-		rm -f "$gitdir/index"
-	else
-		mkdir -p "$gitdir_base"
-		(
-			clear_local_git_env
-			git clone $quiet ${depth:+"$depth"} -n ${reference:+"$reference"} \
-				--separate-git-dir "$gitdir" "$url" "$sm_path"
-		) ||
-		die "$(eval_gettext "Clone of '\$url' into submodule path '\$sm_path' failed")"
-	fi
-
-	# We already are at the root of the work tree but cd_to_toplevel will
-	# resolve any symlinks that might be present in $PWD
-	a=$(cd_to_toplevel && cd "$gitdir" && pwd)/
-	b=$(cd_to_toplevel && cd "$sm_path" && pwd)/
-	# Remove all common leading directories after a sanity check
-	if test "${a#$b}" != "$a" || test "${b#$a}" != "$b"; then
-		die "$(eval_gettext "Gitdir '\$a' is part of the submodule path '\$b' or vice versa")"
-	fi
-	while test "${a%%/*}" = "${b%%/*}"
-	do
-		a=${a#*/}
-		b=${b#*/}
-	done
-	# Now chop off the trailing '/'s that were added in the beginning
-	a=${a%/}
-	b=${b%/}
-
-	# Turn each leading "*/" component into "../"
-	rel=$(printf '%s\n' "$b" | sed -e 's|[^/][^/]*|..|g')
-	printf '%s\n' "gitdir: $rel/$a" >"$sm_path/.git"
-
-	rel=$(printf '%s\n' "$a" | sed -e 's|[^/][^/]*|..|g')
-	(clear_local_git_env; cd "$sm_path" && GIT_WORK_TREE=. git config core.worktree "$rel/$b")
-}
-
 isnumber()
 {
 	n=$(($1 + 0)) 2>/dev/null && test "$n" = "$1"
@@ -301,7 +227,7 @@ cmd_add()
 			shift
 			;;
 		--depth=*)
-			depth=$1
+			depth="$1"
 			;;
 		--)
 			shift
@@ -412,7 +338,7 @@ Use -f if you really want to add it." >&2
 				echo "$(eval_gettext "Reactivating local git directory for submodule '\$sm_name'.")"
 			fi
 		fi
-		module_clone "$sm_path" "$sm_name" "$realrepo" "$reference" "$depth" || exit
+		git submodule--helper clone ${GIT_QUIET:+--quiet} --prefix "$wt_prefix" --path "$sm_path" --name "$sm_name" --url "$realrepo" "$reference" "$depth" || exit
 		(
 			clear_local_git_env
 			cd "$sm_path" &&
@@ -774,7 +700,7 @@ Maybe you want to use 'update --init'?")"
 
 		if ! test -d "$sm_path"/.git && ! test -f "$sm_path"/.git
 		then
-			module_clone "$sm_path" "$name" "$url" "$reference" "$depth" || exit
+			git submodule--helper clone ${GIT_QUIET:+--quiet} --prefix "$prefix" --path "$sm_path" --name "$name" --url "$url" "$reference" "$depth" || exit
 			cloned_modules="$cloned_modules;$name"
 			subsha1=
 		else
-- 
2.5.0.256.g89f8063.dirty

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

* Re: [PATCHv4 3/3] submodule: implement `clone` as a builtin helper
  2015-09-01 18:24 ` [PATCHv4 3/3] submodule: implement `clone` " Stefan Beller
@ 2015-09-01 18:52   ` Eric Sunshine
  2015-09-01 19:05     ` Jeff King
  2015-09-01 19:13     ` Junio C Hamano
  0 siblings, 2 replies; 12+ messages in thread
From: Eric Sunshine @ 2015-09-01 18:52 UTC (permalink / raw)
  To: Stefan Beller
  Cc: Junio C Hamano, Git List, Jonathan Nieder, Johannes Schindelin,
	Jens Lehmann, Jeff King

On Tue, Sep 1, 2015 at 2:24 PM, Stefan Beller <sbeller@google.com> wrote:
> This converts implements the helper `module_clone`. This functionality is

Mentioned previously[1]: "converts implements"?

[1]: http://article.gmane.org/gmane.comp.version-control.git/276966

> needed for converting `git submodule update` later on, which we want to
> add threading to.
>
> Signed-off-by: Stefan Beller <sbeller@google.com>
> ---
> diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
> index f5e408a..63f535a 100644
> --- a/builtin/submodule--helper.c
> +++ b/builtin/submodule--helper.c
> +static int module_clone(int argc, const char **argv, const char *prefix)
> +{
> +       const char *path = NULL, *name = NULL, *url = NULL;
> +       const char *reference = NULL, *depth = NULL;
> +       const char *alternative_path = NULL, *p;
> +       int quiet = 0;
> +       FILE *submodule_dot_git;
> +       char *sm_gitdir;
> +       struct strbuf rel_path = STRBUF_INIT;
> +       struct strbuf sb = STRBUF_INIT;
> +
> +       struct option module_clone_options[] = {
> +               OPT_STRING(0, "prefix", &alternative_path,
> +                          N_("path"),
> +                          N_("alternative anchor for relative paths")),
> +               OPT_STRING(0, "path", &path,
> +                          N_("path"),
> +                          N_("where the new submodule will be cloned to")),
> +               OPT_STRING(0, "name", &name,
> +                          N_("string"),
> +                          N_("name of the new submodule")),
> +               OPT_STRING(0, "url", &url,
> +                          N_("string"),
> +                          N_("url where to clone the submodule from")),
> +               OPT_STRING(0, "reference", &reference,
> +                          N_("string"),
> +                          N_("reference repository")),
> +               OPT_STRING(0, "depth", &depth,
> +                          N_("string"),
> +                          N_("depth for shallow clones")),
> +               OPT__QUIET(&quiet, "Suppress output for cloning a submodule"),
> +               OPT_END()
> +       };
> +
> +       const char * const git_submodule_helper_usage[] = {

Style: *const

> +               N_("git submodule--helper clone [--prefix=<path>] [--quiet] "
> +                  "[--reference <repository>] [--name <name>] [--url <url>]"
> +                  "[--depth <depth>] [--] [<path>...]"),
> +               NULL
> +       };
> +
> +       argc = parse_options(argc, argv, prefix, module_clone_options,
> +                            git_submodule_helper_usage, 0);
> +
> +       strbuf_addf(&sb, "%s/modules/%s", get_git_dir(), name);
> +       sm_gitdir = strbuf_detach(&sb, NULL);
> +
> +       if (!file_exists(sm_gitdir)) {
> +               if (safe_create_leading_directories_const(sm_gitdir) < 0)
> +                       die(_("could not create directory '%s'"), sm_gitdir);
> +               if (clone_submodule(path, sm_gitdir, url, depth, reference, quiet))
> +                       die(N_("clone of '%s' into submodule path '%s' failed"),
> +                           url, path);

s/N_/_/

> +       } else {
> +               if (safe_create_leading_directories_const(path) < 0)
> +                       die(_("could not create directory '%s'"), path);
> +               if (unlink(sm_gitdir) < 0)
> +                       die_errno(_("failed to delete '%s'"), sm_gitdir);
> +       }
> +
> +       /* Write a .git file in the submodule to redirect to the superproject. */
> +       if (alternative_path && *alternative_path)) {
> +               p = relative_path(path, alternative_path, &sb);
> +               strbuf_reset(&sb);
> +       } else
> +               p = path;
> +
> +       if (safe_create_leading_directories_const(p) < 0)
> +               die(_("could not create directory '%s'"), p);
> +
> +       strbuf_addf(&sb, "%s/.git", p);
> +
> +       if (safe_create_leading_directories_const(sb.buf) < 0)
> +               die(_("could not create leading directories of '%s'"), sb.buf);
> +       submodule_dot_git = fopen(sb.buf, "w");
> +       if (!submodule_dot_git)
> +               die (_("cannot open file '%s': %s"), sb.buf, strerror(errno));

Style: drop space before '('

Also, can't you use die_errno() here?

> +       fprintf(submodule_dot_git, "gitdir: %s\n",
> +               relative_path(sm_gitdir, path, &rel_path));
> +       if (fclose(submodule_dot_git))
> +               die(_("could not close file %s"), sb.buf);
> +       strbuf_reset(&sb);
> +
> +       /* Redirect the worktree of the submodule in the superproject's config */
> +       if (strbuf_getcwd(&sb))
> +               die_errno(_("unable to get current working directory"));
> +
> +       if (!is_absolute_path(sm_gitdir)) {
> +               if (strbuf_getcwd(&sb))
> +                       die_errno(_("unable to get current working directory"));

Why does this need to call getcwd() on 'sb' when it was called
immediately above the conditional and its value hasn't changed?

> +               strbuf_addf(&sb, "/%s", sm_gitdir);
> +               free(sm_gitdir);
> +               sm_gitdir = strbuf_detach(&sb, NULL);
> +       }
> +
> +
> +       strbuf_addf(&sb, "/%s", path);
> +
> +       p = git_pathdup_submodule(path, "config");
> +       if (!p)
> +               die(_("could not get submodule directory for '%s'"), path);
> +       git_config_set_in_file(p, "core.worktree",
> +                              relative_path(sb.buf, sm_gitdir, &rel_path));
> +       strbuf_release(&sb);

Is 'rel_path' being leaked here?

> +       free(sm_gitdir);
> +       return 0;
> +}
>
>  struct cmd_struct {
>         const char *cmd;
> @@ -132,6 +271,7 @@ struct cmd_struct {
>  static struct cmd_struct commands[] = {
>         {"list", module_list},
>         {"name", module_name},
> +       {"clone", module_clone},
>  };

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

* Re: [PATCHv4 2/3] submodule: implement `name` as a builtin helper
  2015-09-01 18:24 ` [PATCHv4 2/3] submodule: implement `name` " Stefan Beller
@ 2015-09-01 19:01   ` Eric Sunshine
  0 siblings, 0 replies; 12+ messages in thread
From: Eric Sunshine @ 2015-09-01 19:01 UTC (permalink / raw)
  To: Stefan Beller
  Cc: Junio C Hamano, Git List, Jonathan Nieder, Johannes Schindelin,
	Jens Lehmann, Jeff King

On Tue, Sep 1, 2015 at 2:24 PM, Stefan Beller <sbeller@google.com> wrote:
> This implements the helper `module_name` in C instead of shell,

You probably want s/module_name/name/ or state more explicitly:

    Reimplement `module_name` shell function in C as `name`.

or something.

> yielding a nice performance boost.
>
> Signed-off-by: Stefan Beller <sbeller@google.com>
> ---
> diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
> index 16d9abe..f5e408a 100644
> --- a/builtin/submodule--helper.c
> +++ b/builtin/submodule--helper.c
> @@ -102,6 +105,24 @@ static int module_list(int argc, const char **argv, const char *prefix)
> +static int module_name(int argc, const char **argv, const char *prefix)
> +{
> +       const struct submodule *sub;
> +
> +       if (argc != 2)
> +               usage("git submodule--helper module_name <path>\n");

Mentioned previously[1]:

usage(_("..."));
s/module_name/name/
s/\\n//

[1]: http://article.gmane.org/gmane.comp.version-control.git/276965

> +       gitmodules_config();
> +       sub = submodule_from_path(null_sha1, argv[1]);
> +
> +       if (!sub)
> +               die(N_("No submodule mapping found in .gitmodules for path '%s'"),
> +                   argv[1]);

s/N_/_/
s/No/no/

> +       printf("%s\n", sub->name);
> +
> +       return 0;
> +}
>
>  struct cmd_struct {
>         const char *cmd;
> @@ -110,6 +131,7 @@ struct cmd_struct {
>
>  static struct cmd_struct commands[] = {
>         {"list", module_list},
> +       {"name", module_name},
>  };

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

* Re: [PATCHv4 3/3] submodule: implement `clone` as a builtin helper
  2015-09-01 18:52   ` Eric Sunshine
@ 2015-09-01 19:05     ` Jeff King
  2015-09-01 19:15       ` Eric Sunshine
  2015-09-01 19:13     ` Junio C Hamano
  1 sibling, 1 reply; 12+ messages in thread
From: Jeff King @ 2015-09-01 19:05 UTC (permalink / raw)
  To: Eric Sunshine
  Cc: Stefan Beller, Junio C Hamano, Git List, Jonathan Nieder,
	Johannes Schindelin, Jens Lehmann

On Tue, Sep 01, 2015 at 02:52:54PM -0400, Eric Sunshine wrote:

> > +       /* Redirect the worktree of the submodule in the superproject's config */
> > +       if (strbuf_getcwd(&sb))
> > +               die_errno(_("unable to get current working directory"));
> > +
> > +       if (!is_absolute_path(sm_gitdir)) {
> > +               if (strbuf_getcwd(&sb))
> > +                       die_errno(_("unable to get current working directory"));
> 
> Why does this need to call getcwd() on 'sb' when it was called
> immediately above the conditional and its value hasn't changed?

Even weirder, the next code is:

> 
> > +               strbuf_addf(&sb, "/%s", sm_gitdir);
> > +               free(sm_gitdir);
> > +               sm_gitdir = strbuf_detach(&sb, NULL);
> > +       }
> > +
> > +
> > +       strbuf_addf(&sb, "/%s", path);

So if it _was_ an absolute path, we are adding "/$path" to nothing
(having just detached the strbuf in the conditional above). That seems
weird.

-Peff

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

* Re: [PATCHv4 3/3] submodule: implement `clone` as a builtin helper
  2015-09-01 18:52   ` Eric Sunshine
  2015-09-01 19:05     ` Jeff King
@ 2015-09-01 19:13     ` Junio C Hamano
  1 sibling, 0 replies; 12+ messages in thread
From: Junio C Hamano @ 2015-09-01 19:13 UTC (permalink / raw)
  To: Eric Sunshine
  Cc: Stefan Beller, Git List, Jonathan Nieder, Johannes Schindelin,
	Jens Lehmann, Jeff King

Eric Sunshine <sunshine@sunshineco.com> writes:

> On Tue, Sep 1, 2015 at 2:24 PM, Stefan Beller <sbeller@google.com> wrote:
>> This converts implements the helper `module_clone`. This functionality is
>
> Mentioned previously[1]: "converts implements"?
>
> [1]: http://article.gmane.org/gmane.comp.version-control.git/276966

Stefan, perhaps another round of proofreading before sending would
have helped.

I am neutral with asterisk around const myself.  I think I saw this
often spelled with SP on both sides, i.e.

	$ git grep ' \* const ' \*.c | wc -l
        121
        $ git grep ' \*const ' \*.c | wc -l
        45

Majority of the latter, when viewed without pipe to wc, tells me
they are mostly borrowed code.

All the other comments from you on this patch makes sense to me.

Thanks.

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

* Re: [PATCHv4 3/3] submodule: implement `clone` as a builtin helper
  2015-09-01 19:05     ` Jeff King
@ 2015-09-01 19:15       ` Eric Sunshine
  0 siblings, 0 replies; 12+ messages in thread
From: Eric Sunshine @ 2015-09-01 19:15 UTC (permalink / raw)
  To: Jeff King
  Cc: Stefan Beller, Junio C Hamano, Git List, Jonathan Nieder,
	Johannes Schindelin, Jens Lehmann

On Tue, Sep 1, 2015 at 3:05 PM, Jeff King <peff@peff.net> wrote:
> On Tue, Sep 01, 2015 at 02:52:54PM -0400, Eric Sunshine wrote:
>> > +       /* Redirect the worktree of the submodule in the superproject's config */
>> > +       if (strbuf_getcwd(&sb))
>> > +               die_errno(_("unable to get current working directory"));
>> > +
>> > +       if (!is_absolute_path(sm_gitdir)) {
>> > +               if (strbuf_getcwd(&sb))
>> > +                       die_errno(_("unable to get current working directory"));
>>
>> Why does this need to call getcwd() on 'sb' when it was called
>> immediately above the conditional and its value hasn't changed?
>
> Even weirder, the next code is:
>
>> > +               strbuf_addf(&sb, "/%s", sm_gitdir);
>> > +               free(sm_gitdir);
>> > +               sm_gitdir = strbuf_detach(&sb, NULL);
>> > +       }
>> > +
>> > +
>> > +       strbuf_addf(&sb, "/%s", path);
>
> So if it _was_ an absolute path, we are adding "/$path" to nothing
> (having just detached the strbuf in the conditional above). That seems
> weird.

Indeed, I saw that too, but didn't mention it since this appears to be
an incomplete refactoring from the previous round, and my hope was
that mentioning the getcwd() oddity would be enough to trigger a
thorough check of the code before sending the next version.

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

* Re: [PATCHv4 1/3] submodule: implement `list` as a builtin helper
  2015-09-01 18:24 ` [PATCHv4 1/3] submodule: implement `list` as a builtin helper Stefan Beller
@ 2015-09-01 19:55   ` Junio C Hamano
  2015-09-02  7:20     ` Junio C Hamano
  2015-09-01 23:17   ` Eric Sunshine
  1 sibling, 1 reply; 12+ messages in thread
From: Junio C Hamano @ 2015-09-01 19:55 UTC (permalink / raw)
  To: Stefan Beller
  Cc: sunshine, git, jrnieder, johannes.schindelin, Jens.Lehmann, peff

Stefan Beller <sbeller@google.com> writes:

> +static int module_list_compute(int argc, const char **argv,
> +				const char *prefix,
> +				struct pathspec *pathspec)
> +{
> +	int i, result = 0;
> +	char *max_prefix, *ps_matched = NULL;
> +	int max_prefix_len;
> +	parse_pathspec(pathspec, 0,
> +		       PATHSPEC_PREFER_FULL |
> +		       PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP,
> +		       prefix, argv);
> +
> +	/* Find common prefix for all pathspec's */
> +	max_prefix = common_prefix(pathspec);
> +	max_prefix_len = max_prefix ? strlen(max_prefix) : 0;
> +
> +	if (pathspec->nr)
> +		ps_matched = xcalloc(pathspec->nr, 1);
> +
> +	if (read_cache() < 0)
> +		die(_("index file corrupt"));
> +
> +	for (i = 0; i < active_nr; i++) {
> +		const struct cache_entry *ce = active_cache[i];
> +
> +		if (!match_pathspec(pathspec, ce->name, ce_namelen(ce),
> +				    max_prefix_len, ps_matched,
> +				    S_ISGITLINK(ce->ce_mode) | S_ISDIR(ce->ce_mode)))
> +			continue;
> +
> +		if (S_ISGITLINK(ce->ce_mode)) {
> +			ALLOC_GROW(ce_entries, ce_used + 1, ce_alloc);
> +			ce_entries[ce_used++] = ce;
> +		}
> +
> +		while (i + 1 < active_nr && !strcmp(ce->name, active_cache[i + 1]->name))
> +			/*
> +			 * Skip entries with the same name in different stages
> +			 * to make sure an entry is returned only once.
> +			 */
> +			i++;
> +	}

I hate myself not catching this earlier, but I suspect that this is
not quite a faithful rewrite of the original.  It changes behaviour
when a conflicted path records a non submodule in stage #1 and a
submodule in stage #2 (or stage #3), doesn't it?

The original scripted version will see the stage #1 entry and skips
it because it is not a submodule, then will see the stage #2 entry
and because the path is not yet in the %unmerged hash, and it will
push it to @out.

This loop instead sees a non-submodule entry at stage #1, skips it
(because it is not a submodule), then goes on to skip the entries
with the same name, including the stage #2 entry that _is_ a
submodule.

I think the clever "we are done with this entry, so let's skip all
others that have the same name" optimization should be done only
when we did handle an entry with the same name.  I.e. something like
this squashed in.
	
--------------------------------------------------

diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
index 16d9abe..c4aff0c 100644
--- a/builtin/submodule--helper.c
+++ b/builtin/submodule--helper.c
@@ -39,12 +39,13 @@ static int module_list_compute(int argc, const char **argv,
 				    S_ISGITLINK(ce->ce_mode) | S_ISDIR(ce->ce_mode)))
 			continue;
 
-		if (S_ISGITLINK(ce->ce_mode)) {
-			ALLOC_GROW(ce_entries, ce_used + 1, ce_alloc);
-			ce_entries[ce_used++] = ce;
-		}
+		if (!S_ISGITLINK(ce->ce_mode))
+			continue;
 
-		while (i + 1 < active_nr && !strcmp(ce->name, active_cache[i + 1]->name))
+		ALLOC_GROW(ce_entries, ce_used + 1, ce_alloc);
+		ce_entries[ce_used++] = ce;
+		while (i + 1 < active_nr &&
+		       !strcmp(ce->name, active_cache[i + 1]->name))
 			/*
 			 * Skip entries with the same name in different stages
 			 * to make sure an entry is returned only once.

--------------------------------------------------

> +static int module_list(int argc, const char **argv, const char *prefix)
> +{
> +	int i;
> +	struct pathspec pathspec;
> +	const char *alternative_path;
> +
> +	struct option module_list_options[] = {
> +		OPT_STRING(0, "prefix", &alternative_path,
> +			   N_("path"),
> +			   N_("alternative anchor for relative paths")),
> +		OPT_END()
> +	};

Do we really need this alternative_path variable?  The "--prefix"
option that overrides the passed-in variable prefix would be easier
to understand if it touched the same variable, i.e.

--------------------------------------------------

diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
index 16d9abe..387539f 100644
--- a/builtin/submodule--helper.c
+++ b/builtin/submodule--helper.c
@@ -65,10 +66,9 @@ static int module_list(int argc, const char **argv, const char *prefix)
 {
 	int i;
 	struct pathspec pathspec;
-	const char *alternative_path;
 
 	struct option module_list_options[] = {
-		OPT_STRING(0, "prefix", &alternative_path,
+		OPT_STRING(0, "prefix", &prefix,
 			   N_("path"),
 			   N_("alternative anchor for relative paths")),
 		OPT_END()
@@ -82,9 +82,7 @@ static int module_list(int argc, const char **argv, const char *prefix)
 	argc = parse_options(argc, argv, prefix, module_list_options,
 			     git_submodule_helper_usage, 0);
 
-	if (module_list_compute(argc, argv, alternative_path
-					    ? alternative_path
-					    : prefix, &pathspec) < 0) {
+	if (module_list_compute(argc, argv, prefix, &pathspec) < 0) {
 		printf("#unmatched\n");
 		return 1;
 	}

--------------------------------------------------

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

* Re: [PATCHv4 1/3] submodule: implement `list` as a builtin helper
  2015-09-01 18:24 ` [PATCHv4 1/3] submodule: implement `list` as a builtin helper Stefan Beller
  2015-09-01 19:55   ` Junio C Hamano
@ 2015-09-01 23:17   ` Eric Sunshine
  1 sibling, 0 replies; 12+ messages in thread
From: Eric Sunshine @ 2015-09-01 23:17 UTC (permalink / raw)
  To: Stefan Beller
  Cc: Junio C Hamano, Git List, Jonathan Nieder, Johannes Schindelin,
	Jens Lehmann, Jeff King

On Tue, Sep 1, 2015 at 2:24 PM, Stefan Beller <sbeller@google.com> wrote:
> Most of the submodule operations work on a set of submodules.
> Calculating and using this set is usually done via:
>
>        module_list "$@" | {
>            while read mode sha1 stage sm_path
>            do
>                 # the actual operation
>            done
>        }
>
> Currently the function `module_list` is implemented in the
> git-submodule.sh as a shell script wrapping a perl script.
> The rewrite is in C, such that it is faster and can later be
> easily adapted when other functions are rewritten in C.
>
> [...]
>
> Signed-off-by: Stefan Beller <sbeller@google.com>
> ---
> +struct cmd_struct {
> +       const char *cmd;
> +       int (*fct)(int, const char **, const char *);

It would be better to stick with a more idiomatic name such as 'f' or
'func' than invent an entirely new one ('fct').

> +};
> +
> +static struct cmd_struct commands[] = {
> +       {"list", module_list},
> +};
> +
> +int cmd_submodule__helper(int argc, const char **argv, const char *prefix)
> +{
> +       int i;
> +       if (argc < 2)
> +               goto out;
> +
> +       for (i = 0; i < ARRAY_SIZE(commands); i++)
> +               if (!strcmp(argv[1], commands[i].cmd))
> +                       return commands[i].fct(argc - 1, argv + 1, prefix);
> +
> +out:
> +       if (argc > 1)
> +               fprintf(stderr, _("fatal: '%s' is not a valid submodule--helper "
> +                                 "subcommand, which are:\n"), argv[1]);
> +       else
> +               fprintf(stderr, _("fatal: submodule--helper subcommand must be "
> +                                 "called with a subcommand, which are:\n"));
> +
> +       for (i = 0; i < ARRAY_SIZE(commands); i++)
> +               fprintf(stderr, "\t%s\n", commands[i].cmd);

A couple observations:

1. git-submodule--helper isn't the only Git command featuring
subcommands which could benefit from this dispatching and error
reporting code, so making it re-usable would be sensible rather than
hiding it away inside this one (very low-level, not user-facing)
command. If you go that route, it would deserve its own patch series,
and well thought out design and interface. However...

2. I realize that the suggestion of listing available subcommands was
put forth tentatively by Junio[1], but it seems overkill for a command
like this which is not user-facing, and is inconsistent with other
subcommand-bearing commands. At this stage, it should be sufficient
merely to emit a plain error message (without any usage information):

    unrecognized subcommand: %s

    missing subcommand

at which point the user can consult the man page or "git
subcommand--helper -h" to find out what went wrong.

[1]: http://article.gmane.org/gmane.comp.version-control.git/276935

> +
> +       exit(129);
> +}

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

* Re: [PATCHv4 1/3] submodule: implement `list` as a builtin helper
  2015-09-01 19:55   ` Junio C Hamano
@ 2015-09-02  7:20     ` Junio C Hamano
  0 siblings, 0 replies; 12+ messages in thread
From: Junio C Hamano @ 2015-09-02  7:20 UTC (permalink / raw)
  To: Stefan Beller
  Cc: sunshine, git, jrnieder, johannes.schindelin, Jens.Lehmann, peff

Junio C Hamano <gitster@pobox.com> writes:

> Stefan Beller <sbeller@google.com> writes:
>
>> +static int module_list_compute(int argc, const char **argv,
>> +				const char *prefix,
>> +				struct pathspec *pathspec)
>> +{
>> ...
>> +	for (i = 0; i < active_nr; i++) {
>> +		const struct cache_entry *ce = active_cache[i];
>> +
>> +		if (!match_pathspec(pathspec, ce->name, ce_namelen(ce),
>> +				    max_prefix_len, ps_matched,
>> +				    S_ISGITLINK(ce->ce_mode) | S_ISDIR(ce->ce_mode)))
>> +			continue;

Another minor thing I noticed here is that ce->ce_mode would never
be S_ISDIR().

It is not immediately clear if it is necessary to pass is_dir=true
upon S_ISGITLINK(ce->ce_mode) to match_pathspec(), but I think that
is probably a right thing to do.  The only difference this makes, I
think, is when a pathspec ends with a slash.  E.g. when you have a
submodule at path ce->ce_name == "dir" and the caller says "dir/".
Then DO_MATCH_DIRECTORY logic would say "dir/" does match "dir".

So taken together with the remainder of the loop, perhaps

        for (i = 0; i < active_nr; i++) {
                ce = active_cache[i];

                if (!S_ISGITLINK(ce->ce_mode) ||
                    !match_pathspec(..., is_dir=1))
                        continue;

                ALLOC_GROW(ce_entries, ce_nr + 1, ce_alloc);
                ce_entries[ce_nr++] = ce;
                while (...)
                        skip the entries with the same name;
        }

would be what we want.  Pathspec matching is much more expensive
than ce_mode check, and after passing that check, you know the last
parameter to the match_pathspec() at that point, so the above
structure would also make sense from performance point of view, I
think.  And of course, the structure makes it clear that we only
care about GITLINK entries and nothing else in this loop, so it is
good from readability point of view, too.

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

end of thread, other threads:[~2015-09-02  7:21 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-09-01 18:24 [PATCHv4 0/3] submodule--helper: Have some refactoring only patches first Stefan Beller
2015-09-01 18:24 ` [PATCHv4 1/3] submodule: implement `list` as a builtin helper Stefan Beller
2015-09-01 19:55   ` Junio C Hamano
2015-09-02  7:20     ` Junio C Hamano
2015-09-01 23:17   ` Eric Sunshine
2015-09-01 18:24 ` [PATCHv4 2/3] submodule: implement `name` " Stefan Beller
2015-09-01 19:01   ` Eric Sunshine
2015-09-01 18:24 ` [PATCHv4 3/3] submodule: implement `clone` " Stefan Beller
2015-09-01 18:52   ` Eric Sunshine
2015-09-01 19:05     ` Jeff King
2015-09-01 19:15       ` Eric Sunshine
2015-09-01 19:13     ` Junio C Hamano

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.