diff options
Diffstat (limited to 'submodule.c')
-rw-r--r-- | submodule.c | 167 |
1 files changed, 138 insertions, 29 deletions
diff --git a/submodule.c b/submodule.c index c0f93c208c..15e90d1c10 100644 --- a/submodule.c +++ b/submodule.c @@ -10,6 +10,7 @@ #include "string-list.h" #include "sha1-array.h" #include "argv-array.h" +#include "blob.h" static struct string_list config_name_for_path; static struct string_list config_fetch_recurse_submodules_for_name; @@ -30,6 +31,95 @@ static struct sha1_array ref_tips_after_fetch; */ static int gitmodules_is_unmerged; +/* + * This flag is set if the .gitmodules file had unstaged modifications on + * startup. This must be checked before allowing modifications to the + * .gitmodules file with the intention to stage them later, because when + * continuing we would stage the modifications the user didn't stage herself + * too. That might change in a future version when we learn to stage the + * changes we do ourselves without staging any previous modifications. + */ +static int gitmodules_is_modified; + + +int is_staging_gitmodules_ok(void) +{ + return !gitmodules_is_modified; +} + +/* + * Try to update the "path" entry in the "submodule.<name>" section of the + * .gitmodules file. Return 0 only if a .gitmodules file was found, a section + * with the correct path=<oldpath> setting was found and we could update it. + */ +int update_path_in_gitmodules(const char *oldpath, const char *newpath) +{ + struct strbuf entry = STRBUF_INIT; + struct string_list_item *path_option; + + if (!file_exists(".gitmodules")) /* Do nothing without .gitmodules */ + return -1; + + if (gitmodules_is_unmerged) + die(_("Cannot change unmerged .gitmodules, resolve merge conflicts first")); + + path_option = unsorted_string_list_lookup(&config_name_for_path, oldpath); + if (!path_option) { + warning(_("Could not find section in .gitmodules where path=%s"), oldpath); + return -1; + } + strbuf_addstr(&entry, "submodule."); + strbuf_addstr(&entry, path_option->util); + strbuf_addstr(&entry, ".path"); + if (git_config_set_in_file(".gitmodules", entry.buf, newpath) < 0) { + /* Maybe the user already did that, don't error out here */ + warning(_("Could not update .gitmodules entry %s"), entry.buf); + strbuf_release(&entry); + return -1; + } + strbuf_release(&entry); + return 0; +} + +/* + * Try to remove the "submodule.<name>" section from .gitmodules where the given + * path is configured. Return 0 only if a .gitmodules file was found, a section + * with the correct path=<path> setting was found and we could remove it. + */ +int remove_path_from_gitmodules(const char *path) +{ + struct strbuf sect = STRBUF_INIT; + struct string_list_item *path_option; + + if (!file_exists(".gitmodules")) /* Do nothing without .gitmodules */ + return -1; + + if (gitmodules_is_unmerged) + die(_("Cannot change unmerged .gitmodules, resolve merge conflicts first")); + + path_option = unsorted_string_list_lookup(&config_name_for_path, path); + if (!path_option) { + warning(_("Could not find section in .gitmodules where path=%s"), path); + return -1; + } + strbuf_addstr(§, "submodule."); + strbuf_addstr(§, path_option->util); + if (git_config_rename_section_in_file(".gitmodules", sect.buf, NULL) < 0) { + /* Maybe the user already did that, don't error out here */ + warning(_("Could not remove .gitmodules entry for %s"), path); + strbuf_release(§); + return -1; + } + strbuf_release(§); + return 0; +} + +void stage_updated_gitmodules(void) +{ + if (add_file_to_cache(".gitmodules", 0)) + die(_("staging updated .gitmodules failed")); +} + static int add_submodule_odb(const char *path) { struct strbuf objects_directory = STRBUF_INIT; @@ -88,7 +178,7 @@ void set_diffopt_flags_from_submodule_config(struct diff_options *diffopt, int submodule_config(const char *var, const char *value, void *cb) { - if (!prefixcmp(var, "submodule.")) + if (starts_with(var, "submodule.")) return parse_submodule_config_option(var, value); else if (!strcmp(var, "fetch.recursesubmodules")) { config_fetch_recurse_submodules = parse_fetch_recurse_submodules_arg(var, value); @@ -116,6 +206,11 @@ void gitmodules_config(void) !memcmp(ce->name, ".gitmodules", 11)) gitmodules_is_unmerged = 1; } + } else if (pos < active_nr) { + struct stat st; + if (lstat(".gitmodules", &st) == 0 && + ce_match_stat(active_cache[pos], &st, 0) & DATA_CHANGED) + gitmodules_is_modified = 1; } if (!gitmodules_is_unmerged) @@ -206,7 +301,7 @@ static int prepare_submodule_summary(struct rev_info *rev, const char *path, left->object.flags |= SYMMETRIC_LEFT; add_pending_object(rev, &left->object, path); add_pending_object(rev, &right->object, path); - merge_bases = get_merge_bases(left, right, 1); + merge_bases = get_merge_bases(left, right); if (merge_bases) { if (merge_bases->item == left) *fast_forward = 1; @@ -327,7 +422,8 @@ void set_config_fetch_recurse_submodules(int value) config_fetch_recurse_submodules = value; } -static int has_remote(const char *refname, const unsigned char *sha1, int flags, void *cb_data) +static int has_remote(const char *refname, const struct object_id *oid, + int flags, void *cb_data) { return 1; } @@ -338,13 +434,12 @@ static int submodule_needs_pushing(const char *path, const unsigned char sha1[20 return 0; if (for_each_remote_ref_submodule(path, has_remote, NULL) > 0) { - struct child_process cp; + struct child_process cp = CHILD_PROCESS_INIT; const char *argv[] = {"rev-list", NULL, "--not", "--remotes", "-n", "1" , NULL}; struct strbuf buf = STRBUF_INIT; int needs_pushing = 0; argv[1] = sha1_to_hex(sha1); - memset(&cp, 0, sizeof(cp)); cp.argv = argv; cp.env = local_repo_env; cp.git_cmd = 1; @@ -429,10 +524,9 @@ static int push_submodule(const char *path) return 1; if (for_each_remote_ref_submodule(path, has_remote, NULL) > 0) { - struct child_process cp; + struct child_process cp = CHILD_PROCESS_INIT; const char *argv[] = {"push", NULL}; - memset(&cp, 0, sizeof(cp)); cp.argv = argv; cp.env = local_repo_env; cp.git_cmd = 1; @@ -449,10 +543,7 @@ static int push_submodule(const char *path) int push_unpushed_submodules(unsigned char new_sha1[20], const char *remotes_name) { int i, ret = 1; - struct string_list needs_pushing; - - memset(&needs_pushing, 0, sizeof(struct string_list)); - needs_pushing.strdup_strings = 1; + struct string_list needs_pushing = STRING_LIST_INIT_DUP; if (!find_unpushed_submodules(new_sha1, remotes_name, &needs_pushing)) return 1; @@ -477,22 +568,19 @@ static int is_submodule_commit_present(const char *path, unsigned char sha1[20]) if (!add_submodule_odb(path) && lookup_commit_reference(sha1)) { /* Even if the submodule is checked out and the commit is * present, make sure it is reachable from a ref. */ - struct child_process cp; + struct child_process cp = CHILD_PROCESS_INIT; const char *argv[] = {"rev-list", "-n", "1", NULL, "--not", "--all", NULL}; struct strbuf buf = STRBUF_INIT; argv[3] = sha1_to_hex(sha1); - memset(&cp, 0, sizeof(cp)); cp.argv = argv; cp.env = local_repo_env; cp.git_cmd = 1; cp.no_stdin = 1; - cp.out = -1; cp.dir = path; - if (!run_command(&cp) && !strbuf_read(&buf, cp.out, 1024)) + if (!capture_command(&cp, &buf, 1024) && !buf.len) is_present = 1; - close(cp.out); strbuf_release(&buf); } return is_present; @@ -529,10 +617,10 @@ static void submodule_collect_changed_cb(struct diff_queue_struct *q, } } -static int add_sha1_to_array(const char *ref, const unsigned char *sha1, +static int add_sha1_to_array(const char *ref, const struct object_id *oid, int flags, void *data) { - sha1_array_append(data, sha1); + sha1_array_append(data, oid->hash); return 0; } @@ -603,7 +691,7 @@ int fetch_populated_submodules(const struct argv_array *options, int quiet) { int i, result = 0; - struct child_process cp; + struct child_process cp = CHILD_PROCESS_INIT; struct argv_array argv = ARGV_ARRAY_INIT; struct string_list_item *name_for_path; const char *work_tree = get_git_work_tree(); @@ -619,7 +707,6 @@ int fetch_populated_submodules(const struct argv_array *options, argv_array_push(&argv, "--recurse-submodules-default"); /* default value, "--submodule-prefix" and its value are added later */ - memset(&cp, 0, sizeof(cp)); cp.env = local_repo_env; cp.git_cmd = 1; cp.no_stdin = 1; @@ -702,7 +789,7 @@ out: unsigned is_submodule_modified(const char *path, int ignore_untracked) { ssize_t len; - struct child_process cp; + struct child_process cp = CHILD_PROCESS_INIT; const char *argv[] = { "status", "--porcelain", @@ -729,7 +816,6 @@ unsigned is_submodule_modified(const char *path, int ignore_untracked) if (ignore_untracked) argv[2] = "-uno"; - memset(&cp, 0, sizeof(cp)); cp.argv = argv; cp.env = local_repo_env; cp.git_cmd = 1; @@ -770,7 +856,7 @@ unsigned is_submodule_modified(const char *path, int ignore_untracked) int submodule_uses_gitfile(const char *path) { - struct child_process cp; + struct child_process cp = CHILD_PROCESS_INIT; const char *argv[] = { "submodule", "foreach", @@ -791,7 +877,6 @@ int submodule_uses_gitfile(const char *path) strbuf_release(&buf); /* Now test that all nested submodules use a gitfile too */ - memset(&cp, 0, sizeof(cp)); cp.argv = argv; cp.env = local_repo_env; cp.git_cmd = 1; @@ -807,9 +892,8 @@ int submodule_uses_gitfile(const char *path) int ok_to_remove_submodule(const char *path) { - struct stat st; ssize_t len; - struct child_process cp; + struct child_process cp = CHILD_PROCESS_INIT; const char *argv[] = { "status", "--porcelain", @@ -820,13 +904,12 @@ int ok_to_remove_submodule(const char *path) struct strbuf buf = STRBUF_INIT; int ok_to_remove = 1; - if ((lstat(path, &st) < 0) || is_empty_dir(path)) + if (!file_exists(path) || is_empty_dir(path)) return 1; if (!submodule_uses_gitfile(path)) return 0; - memset(&cp, 0, sizeof(cp)); cp.argv = argv; cp.env = local_repo_env; cp.git_cmd = 1; @@ -870,7 +953,7 @@ static int find_first_merges(struct object_array *result, const char *path, sha1_to_hex(a->object.sha1)); init_revisions(&revs, NULL); rev_opts.submodule = path; - setup_revisions(sizeof(rev_args)/sizeof(char *)-1, rev_args, &revs, &rev_opts); + setup_revisions(ARRAY_SIZE(rev_args)-1, rev_args, &revs, &rev_opts); /* save all revisions from the above list that contain b */ if (prepare_revision_walk(&revs)) @@ -1010,3 +1093,29 @@ int merge_submodule(unsigned char result[20], const char *path, free(merges.objects); return 0; } + +/* Update gitfile and core.worktree setting to connect work tree and git dir */ +void connect_work_tree_and_git_dir(const char *work_tree, const char *git_dir) +{ + struct strbuf file_name = STRBUF_INIT; + struct strbuf rel_path = STRBUF_INIT; + const char *real_work_tree = xstrdup(real_path(work_tree)); + + /* Update gitfile */ + strbuf_addf(&file_name, "%s/.git", work_tree); + write_file(file_name.buf, 1, "gitdir: %s\n", + relative_path(git_dir, real_work_tree, &rel_path)); + + /* Update core.worktree setting */ + strbuf_reset(&file_name); + strbuf_addf(&file_name, "%s/config", git_dir); + if (git_config_set_in_file(file_name.buf, "core.worktree", + relative_path(real_work_tree, git_dir, + &rel_path))) + die(_("Could not set core.worktree in %s"), + file_name.buf); + + strbuf_release(&file_name); + strbuf_release(&rel_path); + free((void *)real_work_tree); +} |