summaryrefslogtreecommitdiff
path: root/submodule.c
diff options
context:
space:
mode:
Diffstat (limited to 'submodule.c')
-rw-r--r--submodule.c185
1 files changed, 161 insertions, 24 deletions
diff --git a/submodule.c b/submodule.c
index bdeeff3a5d..da0b805493 100644
--- a/submodule.c
+++ b/submodule.c
@@ -1,4 +1,5 @@
#include "cache.h"
+#include "config.h"
#include "submodule-config.h"
#include "submodule.h"
#include "dir.h"
@@ -16,9 +17,10 @@
#include "quote.h"
#include "remote.h"
#include "worktree.h"
+#include "parse-options.h"
static int config_fetch_recurse_submodules = RECURSE_SUBMODULES_ON_DEMAND;
-static int config_update_recurse_submodules = RECURSE_SUBMODULES_DEFAULT;
+static int config_update_recurse_submodules = RECURSE_SUBMODULES_OFF;
static int parallel_jobs = 1;
static struct string_list changed_submodule_paths = STRING_LIST_INIT_DUP;
static int initialized_fetch_ref_tips;
@@ -153,7 +155,8 @@ void set_diffopt_flags_from_submodule_config(struct diff_options *diffopt,
}
}
-int submodule_config(const char *var, const char *value, void *cb)
+/* For loading from the .gitmodules file. */
+static int git_modules_config(const char *var, const char *value, void *cb)
{
if (!strcmp(var, "submodule.fetchjobs")) {
parallel_jobs = git_config_int(var, value);
@@ -169,6 +172,56 @@ int submodule_config(const char *var, const char *value, void *cb)
return 0;
}
+/* Loads all submodule settings from the config. */
+int submodule_config(const char *var, const char *value, void *cb)
+{
+ if (!strcmp(var, "submodule.recurse")) {
+ int v = git_config_bool(var, value) ?
+ RECURSE_SUBMODULES_ON : RECURSE_SUBMODULES_OFF;
+ config_update_recurse_submodules = v;
+ return 0;
+ } else {
+ return git_modules_config(var, value, cb);
+ }
+}
+
+/* Cheap function that only determines if we're interested in submodules at all */
+int git_default_submodule_config(const char *var, const char *value, void *cb)
+{
+ if (!strcmp(var, "submodule.recurse")) {
+ int v = git_config_bool(var, value) ?
+ RECURSE_SUBMODULES_ON : RECURSE_SUBMODULES_OFF;
+ config_update_recurse_submodules = v;
+ }
+ return 0;
+}
+
+int option_parse_recurse_submodules_worktree_updater(const struct option *opt,
+ const char *arg, int unset)
+{
+ if (unset) {
+ config_update_recurse_submodules = RECURSE_SUBMODULES_OFF;
+ return 0;
+ }
+ if (arg)
+ config_update_recurse_submodules =
+ parse_update_recurse_submodules_arg(opt->long_name,
+ arg);
+ else
+ config_update_recurse_submodules = RECURSE_SUBMODULES_ON;
+
+ return 0;
+}
+
+void load_submodule_cache(void)
+{
+ if (config_update_recurse_submodules == RECURSE_SUBMODULES_OFF)
+ return;
+
+ gitmodules_config();
+ git_config(submodule_config, NULL);
+}
+
void gitmodules_config(void)
{
const char *work_tree = get_git_work_tree();
@@ -196,7 +249,8 @@ void gitmodules_config(void)
}
if (!gitmodules_is_unmerged)
- git_config_from_file(submodule_config, gitmodules_path.buf, NULL);
+ git_config_from_file(git_modules_config,
+ gitmodules_path.buf, NULL);
strbuf_release(&gitmodules_path);
}
}
@@ -207,7 +261,7 @@ void gitmodules_config_sha1(const unsigned char *commit_sha1)
unsigned char sha1[20];
if (gitmodule_sha1_from_commit(commit_sha1, sha1, &rev)) {
- git_config_from_blob_sha1(submodule_config, rev.buf,
+ git_config_from_blob_sha1(git_modules_config, rev.buf,
sha1, NULL);
}
strbuf_release(&rev);
@@ -282,6 +336,69 @@ int is_submodule_populated_gently(const char *path, int *return_error_code)
return ret;
}
+/*
+ * Dies if the provided 'prefix' corresponds to an unpopulated submodule
+ */
+void die_in_unpopulated_submodule(const struct index_state *istate,
+ const char *prefix)
+{
+ int i, prefixlen;
+
+ if (!prefix)
+ return;
+
+ prefixlen = strlen(prefix);
+
+ for (i = 0; i < istate->cache_nr; i++) {
+ struct cache_entry *ce = istate->cache[i];
+ int ce_len = ce_namelen(ce);
+
+ if (!S_ISGITLINK(ce->ce_mode))
+ continue;
+ if (prefixlen <= ce_len)
+ continue;
+ if (strncmp(ce->name, prefix, ce_len))
+ continue;
+ if (prefix[ce_len] != '/')
+ continue;
+
+ die(_("in unpopulated submodule '%s'"), ce->name);
+ }
+}
+
+/*
+ * Dies if any paths in the provided pathspec descends into a submodule
+ */
+void die_path_inside_submodule(const struct index_state *istate,
+ const struct pathspec *ps)
+{
+ int i, j;
+
+ for (i = 0; i < istate->cache_nr; i++) {
+ struct cache_entry *ce = istate->cache[i];
+ int ce_len = ce_namelen(ce);
+
+ if (!S_ISGITLINK(ce->ce_mode))
+ continue;
+
+ for (j = 0; j < ps->nr ; j++) {
+ const struct pathspec_item *item = &ps->items[j];
+
+ if (item->len <= ce_len)
+ continue;
+ if (item->match[ce_len] != '/')
+ continue;
+ if (strncmp(ce->name, item->match, ce_len))
+ continue;
+ if (item->len == ce_len + 1)
+ continue;
+
+ die(_("Pathspec '%s' is in submodule '%.*s'"),
+ item->original, ce_len, ce->name);
+ }
+ }
+}
+
int parse_submodule_update_strategy(const char *value,
struct submodule_update_strategy *dst)
{
@@ -554,7 +671,8 @@ void show_submodule_inline_diff(FILE *f, const char *path,
cp.no_stdin = 1;
/* TODO: other options may need to be passed here. */
- argv_array_push(&cp.args, "diff");
+ argv_array_pushl(&cp.args, "diff", "--submodule=diff", NULL);
+
argv_array_pushf(&cp.args, "--line-prefix=%s", line_prefix);
if (DIFF_OPT_TST(o, REVERSE_DIFF)) {
argv_array_pushf(&cp.args, "--src-prefix=%s%s/",
@@ -596,11 +714,6 @@ void set_config_fetch_recurse_submodules(int value)
config_fetch_recurse_submodules = value;
}
-void set_config_update_recurse_submodules(int value)
-{
- config_update_recurse_submodules = value;
-}
-
int should_update_submodules(void)
{
return config_update_recurse_submodules == RECURSE_SUBMODULES_ON;
@@ -1356,7 +1469,7 @@ static int submodule_has_dirty_index(const struct submodule *sub)
{
struct child_process cp = CHILD_PROCESS_INIT;
- prepare_submodule_repo_env_no_git_dir(&cp.env_array);
+ prepare_submodule_repo_env(&cp.env_array);
cp.git_cmd = 1;
argv_array_pushl(&cp.args, "diff-index", "--quiet",
@@ -1373,7 +1486,7 @@ static int submodule_has_dirty_index(const struct submodule *sub)
static void submodule_reset_index(const char *path)
{
struct child_process cp = CHILD_PROCESS_INIT;
- prepare_submodule_repo_env_no_git_dir(&cp.env_array);
+ prepare_submodule_repo_env(&cp.env_array);
cp.git_cmd = 1;
cp.no_stdin = 1;
@@ -1402,6 +1515,23 @@ int submodule_move_head(const char *path,
int ret = 0;
struct child_process cp = CHILD_PROCESS_INIT;
const struct submodule *sub;
+ int *error_code_ptr, error_code;
+
+ if (!is_submodule_initialized(path))
+ return 0;
+
+ if (flags & SUBMODULE_MOVE_HEAD_FORCE)
+ /*
+ * Pass non NULL pointer to is_submodule_populated_gently
+ * to prevent die()-ing. We'll use connect_work_tree_and_git_dir
+ * to fixup the submodule in the force case later.
+ */
+ error_code_ptr = &error_code;
+ else
+ error_code_ptr = NULL;
+
+ if (old && !is_submodule_populated_gently(path, error_code_ptr))
+ return 0;
sub = submodule_from_path(null_sha1, path);
@@ -1420,18 +1550,24 @@ int submodule_move_head(const char *path,
absorb_git_dir_into_superproject("", path,
ABSORB_GITDIR_RECURSE_SUBMODULES);
} else {
- struct strbuf sb = STRBUF_INIT;
- strbuf_addf(&sb, "%s/modules/%s",
+ char *gitdir = xstrfmt("%s/modules/%s",
get_git_common_dir(), sub->name);
- connect_work_tree_and_git_dir(path, sb.buf);
- strbuf_release(&sb);
+ connect_work_tree_and_git_dir(path, gitdir);
+ free(gitdir);
/* make sure the index is clean as well */
submodule_reset_index(path);
}
+
+ if (old && (flags & SUBMODULE_MOVE_HEAD_FORCE)) {
+ char *gitdir = xstrfmt("%s/modules/%s",
+ get_git_common_dir(), sub->name);
+ connect_work_tree_and_git_dir(path, gitdir);
+ free(gitdir);
+ }
}
- prepare_submodule_repo_env_no_git_dir(&cp.env_array);
+ prepare_submodule_repo_env(&cp.env_array);
cp.git_cmd = 1;
cp.no_stdin = 1;
@@ -1439,7 +1575,7 @@ int submodule_move_head(const char *path,
argv_array_pushf(&cp.args, "--super-prefix=%s%s/",
get_super_prefix_or_empty(), path);
- argv_array_pushl(&cp.args, "read-tree", NULL);
+ argv_array_pushl(&cp.args, "read-tree", "--recurse-submodules", NULL);
if (flags & SUBMODULE_MOVE_HEAD_DRY_RUN)
argv_array_push(&cp.args, "-n");
@@ -1461,15 +1597,16 @@ int submodule_move_head(const char *path,
if (!(flags & SUBMODULE_MOVE_HEAD_DRY_RUN)) {
if (new) {
- struct child_process cp1 = CHILD_PROCESS_INIT;
+ child_process_init(&cp);
/* also set the HEAD accordingly */
- cp1.git_cmd = 1;
- cp1.no_stdin = 1;
- cp1.dir = path;
+ cp.git_cmd = 1;
+ cp.no_stdin = 1;
+ cp.dir = path;
- argv_array_pushl(&cp1.args, "update-ref", "HEAD", new, NULL);
+ prepare_submodule_repo_env(&cp.env_array);
+ argv_array_pushl(&cp.args, "update-ref", "HEAD", new, NULL);
- if (run_command(&cp1)) {
+ if (run_command(&cp)) {
ret = -1;
goto out;
}