summaryrefslogtreecommitdiff
path: root/submodule.c
diff options
context:
space:
mode:
authorJens Lehmann <Jens.Lehmann@web.de>2011-03-06 23:10:46 +0100
committerJunio C Hamano <gitster@pobox.com>2011-03-09 13:10:35 -0800
commit88a21979c5717e3f37b9691e90b6dbf2b94c751a (patch)
tree4ab2508d6ea972df446b45b56b470330610176d2 /submodule.c
parent046613c5465e4fc0611f93a5ef31d3815fb50c22 (diff)
downloadgit-88a21979c5717e3f37b9691e90b6dbf2b94c751a.tar.gz
fetch/pull: recurse into submodules when necessary
To be able to access all commits of populated submodules referenced by the superproject it is sufficient to only then let "git fetch" recurse into a submodule when the new commits fetched in the superproject record new commits for it. Having these commits present is extremely useful when using the "--submodule" option to "git diff" (which is what "git gui" and "gitk" do since 1.6.6), as all submodule commits needed for creating a descriptive output can be accessed. Also merging submodule commits (added in 1.7.3) depends on the submodule commits in question being present to work. Last but not least this enables disconnected operation when using submodules, as all commits necessary for a successful "git submodule update -N" will have been fetched automatically. So we choose this mode as the default for fetch and pull. Before a new or changed ref from upstream is updated in update_local_ref() "git rev-list <new-sha1> --not --branches --remotes" is used to determine all newly fetched commits. These are then walked and diffed against their parent(s) to see if a submodule has been changed. If that is the case, its path is stored to be fetched after the superproject fetch is completed. Using the "--recurse-submodules" or the "--no-recurse-submodules" option disables the examination of the fetched refs because the result will be ignored anyway. There is currently no infrastructure for storing deleted and new submodules in the .git directory of the superproject. That's why fetch and pull for now only fetch submodules that are already checked out and are not renamed. In t7403 the "--no-recurse-submodules" argument had to be added to "git pull" to avoid failure because of the moved upstream submodule repo. Thanks-to: Jonathan Nieder <jrnieder@gmail.com> Thanks-to: Heiko Voigt <hvoigt@hvoigt.net> Signed-off-by: Jens Lehmann <Jens.Lehmann@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'submodule.c')
-rw-r--r--submodule.c106
1 files changed, 99 insertions, 7 deletions
diff --git a/submodule.c b/submodule.c
index 6f1c10722f..e2486952bc 100644
--- a/submodule.c
+++ b/submodule.c
@@ -12,7 +12,8 @@
struct string_list config_name_for_path;
struct string_list config_fetch_recurse_submodules_for_name;
struct string_list config_ignore_for_name;
-static int config_fetch_recurse_submodules;
+static int config_fetch_recurse_submodules = RECURSE_SUBMODULES_ON_DEMAND;
+struct string_list changed_submodule_paths;
static int add_submodule_odb(const char *path)
{
@@ -152,6 +153,20 @@ void handle_ignore_submodules_arg(struct diff_options *diffopt,
die("bad --ignore-submodules argument: %s", arg);
}
+int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg)
+{
+ switch (git_config_maybe_bool(opt, arg)) {
+ case 1:
+ return RECURSE_SUBMODULES_ON;
+ case 0:
+ return RECURSE_SUBMODULES_OFF;
+ default:
+ if (!strcmp(arg, "on-demand"))
+ return RECURSE_SUBMODULES_ON_DEMAND;
+ die("bad %s argument: %s", opt, arg);
+ }
+}
+
void show_submodule_summary(FILE *f, const char *path,
unsigned char one[20], unsigned char two[20],
unsigned dirty_submodule,
@@ -248,27 +263,95 @@ void set_config_fetch_recurse_submodules(int value)
config_fetch_recurse_submodules = value;
}
+static void submodule_collect_changed_cb(struct diff_queue_struct *q,
+ struct diff_options *options,
+ void *data)
+{
+ int i;
+ for (i = 0; i < q->nr; i++) {
+ struct diff_filepair *p = q->queue[i];
+ if (!S_ISGITLINK(p->two->mode))
+ continue;
+
+ if (S_ISGITLINK(p->one->mode)) {
+ /* NEEDSWORK: We should honor the name configured in
+ * the .gitmodules file of the commit we are examining
+ * here to be able to correctly follow submodules
+ * being moved around. */
+ struct string_list_item *path;
+ path = unsorted_string_list_lookup(&changed_submodule_paths, p->two->path);
+ if (!path)
+ string_list_append(&changed_submodule_paths, xstrdup(p->two->path));
+ } else {
+ /* Submodule is new or was moved here */
+ /* NEEDSWORK: When the .git directories of submodules
+ * live inside the superprojects .git directory some
+ * day we should fetch new submodules directly into
+ * that location too when config or options request
+ * that so they can be checked out from there. */
+ continue;
+ }
+ }
+}
+
+void check_for_new_submodule_commits(unsigned char new_sha1[20])
+{
+ struct rev_info rev;
+ struct commit *commit;
+ const char *argv[] = {NULL, NULL, "--not", "--all", NULL};
+ int argc = ARRAY_SIZE(argv) - 1;
+
+ init_revisions(&rev, NULL);
+ argv[1] = xstrdup(sha1_to_hex(new_sha1));
+ setup_revisions(argc, argv, &rev, NULL);
+ if (prepare_revision_walk(&rev))
+ die("revision walk setup failed");
+
+ /*
+ * Collect all submodules (whether checked out or not) for which new
+ * commits have been recorded upstream in "changed_submodule_paths".
+ */
+ while ((commit = get_revision(&rev))) {
+ struct commit_list *parent = commit->parents;
+ while (parent) {
+ struct diff_options diff_opts;
+ diff_setup(&diff_opts);
+ diff_opts.output_format |= DIFF_FORMAT_CALLBACK;
+ diff_opts.format_callback = submodule_collect_changed_cb;
+ if (diff_setup_done(&diff_opts) < 0)
+ die("diff_setup_done failed");
+ diff_tree_sha1(parent->item->object.sha1, commit->object.sha1, "", &diff_opts);
+ diffcore_std(&diff_opts);
+ diff_flush(&diff_opts);
+ parent = parent->next;
+ }
+ }
+ free((char *)argv[1]);
+}
+
int fetch_populated_submodules(int num_options, const char **options,
const char *prefix, int ignore_config,
int quiet)
{
- int i, result = 0, argc = 0;
+ int i, result = 0, argc = 0, default_argc;
struct child_process cp;
const char **argv;
struct string_list_item *name_for_path;
const char *work_tree = get_git_work_tree();
if (!work_tree)
- return 0;
+ goto out;
if (!the_index.initialized)
if (read_cache() < 0)
die("index file corrupt");
- /* 4: "fetch" (options) "--submodule-prefix" prefix NULL */
- argv = xcalloc(num_options + 4, sizeof(const char *));
+ /* 6: "fetch" (options) --recurse-submodules-default default "--submodule-prefix" prefix NULL */
+ argv = xcalloc(num_options + 6, sizeof(const char *));
argv[argc++] = "fetch";
for (i = 0; i < num_options; i++)
argv[argc++] = options[i];
+ argv[argc++] = "--recurse-submodules-default";
+ default_argc = argc++;
argv[argc++] = "--submodule-prefix";
memset(&cp, 0, sizeof(cp));
@@ -282,7 +365,7 @@ int fetch_populated_submodules(int num_options, const char **options,
struct strbuf submodule_git_dir = STRBUF_INIT;
struct strbuf submodule_prefix = STRBUF_INIT;
struct cache_entry *ce = active_cache[i];
- const char *git_dir, *name;
+ const char *git_dir, *name, *default_argv;
if (!S_ISGITLINK(ce->ce_mode))
continue;
@@ -292,6 +375,7 @@ int fetch_populated_submodules(int num_options, const char **options,
if (name_for_path)
name = name_for_path->util;
+ default_argv = "yes";
if (!ignore_config) {
struct string_list_item *fetch_recurse_submodules_option;
fetch_recurse_submodules_option = unsorted_string_list_lookup(&config_fetch_recurse_submodules_for_name, name);
@@ -299,8 +383,13 @@ int fetch_populated_submodules(int num_options, const char **options,
if (!fetch_recurse_submodules_option->util)
continue;
} else {
- if (!config_fetch_recurse_submodules)
+ if (config_fetch_recurse_submodules == RECURSE_SUBMODULES_OFF)
continue;
+ if (config_fetch_recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND) {
+ if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name))
+ continue;
+ default_argv = "on-demand";
+ }
}
}
@@ -314,6 +403,7 @@ int fetch_populated_submodules(int num_options, const char **options,
if (!quiet)
printf("Fetching submodule %s%s\n", prefix, ce->name);
cp.dir = submodule_path.buf;
+ argv[default_argc] = default_argv;
argv[argc] = submodule_prefix.buf;
if (run_command(&cp))
result = 1;
@@ -323,6 +413,8 @@ int fetch_populated_submodules(int num_options, const char **options,
strbuf_release(&submodule_prefix);
}
free(argv);
+out:
+ string_list_clear(&changed_submodule_paths, 1);
return result;
}