mv: update the path entry in .gitmodules for moved submodules

Currently using "git mv" on a submodule moves the submodule's work tree in that of the superproject. But the submodule's path setting in .gitmodules is left untouched, which is now inconsistent with the work tree and makes git commands that rely on the proper path -> name mapping (like status and diff) behave strangely. Let "git mv" help here by not only moving the submodule's work tree but also updating the "submodule.<submodule name>.path" setting from the .gitmodules file and stage both. This doesn't happen when no .gitmodules file is found and only issues a warning when it doesn't have a section for this submodule. This is because the user might just use plain gitlinks without the .gitmodules file or has already updated the path setting by hand before issuing the "git mv" command (in which case the warning reminds him that mv would have done that for him). Only when .gitmodules is found and contains merge conflicts the mv command will fail and tell the user to resolve the conflict before trying again. Also extend the man page to inform the user about this new feature. Signed-off-by: Jens Lehmann <Jens.Lehmann@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jens Lehmann committed Aug 6, 2013 at 21:15 UTC 0656781fadca17cc51fb1c30f265c251ebe12819
5 files changed +121 -1
Documentation/git-mv.txt
+2
@@ -49,6 +49,8 @@ SUBMODULES
49 Moving a submodule using a gitfile (which means they were cloned
50 with a Git version 1.7.8 or newer) will update the gitfile and
51 core.worktree setting to make the submodule work in the new location.
52 +It also will attempt to update the submodule.<name>.path setting in
53 +the linkgit:gitmodules[5] file and stage that file (unless -n is used).
54
55 GIT
56 ---
builtin/mv.c
+9 -1
@@ -58,7 +58,7 @@ static struct lock_file lock_file;
58
59 int cmd_mv(int argc, const char **argv, const char *prefix)
60 {
61 - int i, newfd;
61 + int i, newfd, gitmodules_modified = 0;
62 int verbose = 0, show_only = 0, force = 0, ignore_errors = 0;
63 struct option builtin_mv_options[] = {
64 OPT__VERBOSE(&verbose, N_("be verbose")),
@@ -72,6 +72,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
72 struct stat st;
73 struct string_list src_for_dst = STRING_LIST_INIT_NODUP;
74
75 + gitmodules_config();
76 git_config(git_default_config, NULL);
77
78 argc = parse_options(argc, argv, prefix, builtin_mv_options,
@@ -125,6 +126,8 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
126 struct strbuf submodule_dotgit = STRBUF_INIT;
127 if (!S_ISGITLINK(active_cache[first]->ce_mode))
128 die (_("Huh? Directory %s is in index and no submodule?"), src);
129 + if (!is_staging_gitmodules_ok())
130 + die (_("Please, stage your changes to .gitmodules or stash them to proceed"));
131 strbuf_addf(&submodule_dotgit, "%s/.git", src);
132 submodule_gitfile[i] = read_gitfile(submodule_dotgit.buf);
133 if (submodule_gitfile[i])
@@ -229,6 +232,8 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
232 die_errno (_("renaming '%s' failed"), src);
233 if (submodule_gitfile[i])
234 connect_work_tree_and_git_dir(dst, submodule_gitfile[i]);
235 + if (!update_path_in_gitmodules(src, dst))
236 + gitmodules_modified = 1;
237 }
238
239 if (mode == WORKING_DIRECTORY)
@@ -240,6 +245,9 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
245 rename_cache_entry_at(pos, dst);
246 }
247
248 + if (gitmodules_modified)
249 + stage_updated_gitmodules();
250 +
251 if (active_cache_changed) {
252 if (write_cache(newfd, active_cache, active_nr) ||
253 commit_locked_index(&lock_file))
submodule.c
+34
@@ -47,6 +47,40 @@ int is_staging_gitmodules_ok(void)
47 return !gitmodules_is_modified;
48 }
49
50 +/*
51 + * Try to update the "path" entry in the "submodule.<name>" section of the
52 + * .gitmodules file. Return 0 only if a .gitmodules file was found, a section
53 + * with the correct path=<oldpath> setting was found and we could update it.
54 + */
55 +int update_path_in_gitmodules(const char *oldpath, const char *newpath)
56 +{
57 + struct strbuf entry = STRBUF_INIT;
58 + struct string_list_item *path_option;
59 +
60 + if (!file_exists(".gitmodules")) /* Do nothing without .gitmodules */
61 + return -1;
62 +
63 + if (gitmodules_is_unmerged)
64 + die(_("Cannot change unmerged .gitmodules, resolve merge conflicts first"));
65 +
66 + path_option = unsorted_string_list_lookup(&config_name_for_path, oldpath);
67 + if (!path_option) {
68 + warning(_("Could not find section in .gitmodules where path=%s"), oldpath);
69 + return -1;
70 + }
71 + strbuf_addstr(&entry, "submodule.");
72 + strbuf_addstr(&entry, path_option->util);
73 + strbuf_addstr(&entry, ".path");
74 + if (git_config_set_in_file(".gitmodules", entry.buf, newpath) < 0) {
75 + /* Maybe the user already did that, don't error out here */
76 + warning(_("Could not update .gitmodules entry %s"), entry.buf);
77 + strbuf_release(&entry);
78 + return -1;
79 + }
80 + strbuf_release(&entry);
81 + return 0;
82 +}
83 +
84 void stage_updated_gitmodules(void)
85 {
86 struct strbuf buf = STRBUF_INIT;
submodule.h
+1
@@ -12,6 +12,7 @@ enum {
12 };
13
14 int is_staging_gitmodules_ok(void);
15 +int update_path_in_gitmodules(const char *oldpath, const char *newpath);
16 void stage_updated_gitmodules(void);
17 void set_diffopt_flags_from_submodule_config(struct diff_options *diffopt,
18 const char *path);
t/t7001-mv.sh
+75
@@ -308,8 +308,83 @@ test_expect_success 'git mv moves a submodule with gitfile' '
308 cd mod/sub &&
309 git status
310 ) &&
311 + echo mod/sub >expected &&
312 + git config -f .gitmodules submodule.sub.path >actual &&
313 + test_cmp expected actual &&
314 git update-index --refresh &&
315 git diff-files --quiet
316 '
317
318 +test_expect_success 'mv does not complain when no .gitmodules file is found' '
319 + rm -rf mod/sub &&
320 + git reset --hard &&
321 + git submodule update &&
322 + git rm .gitmodules &&
323 + entry="$(git ls-files --stage sub | cut -f 1)" &&
324 + git mv sub mod/sub 2>actual.err &&
325 + ! test -s actual.err &&
326 + ! test -e sub &&
327 + [ "$entry" = "$(git ls-files --stage mod/sub | cut -f 1)" ] &&
328 + (
329 + cd mod/sub &&
330 + git status
331 + ) &&
332 + git update-index --refresh &&
333 + git diff-files --quiet
334 +'
335 +
336 +test_expect_success 'mv will error out on a modified .gitmodules file unless staged' '
337 + rm -rf mod/sub &&
338 + git reset --hard &&
339 + git submodule update &&
340 + git config -f .gitmodules foo.bar true &&
341 + entry="$(git ls-files --stage sub | cut -f 1)" &&
342 + test_must_fail git mv sub mod/sub 2>actual.err &&
343 + test -s actual.err &&
344 + test -e sub &&
345 + git diff-files --quiet -- sub &&
346 + git add .gitmodules &&
347 + git mv sub mod/sub 2>actual.err &&
348 + ! test -s actual.err &&
349 + ! test -e sub &&
350 + [ "$entry" = "$(git ls-files --stage mod/sub | cut -f 1)" ] &&
351 + (
352 + cd mod/sub &&
353 + git status
354 + ) &&
355 + git update-index --refresh &&
356 + git diff-files --quiet
357 +'
358 +
359 +test_expect_success 'mv issues a warning when section is not found in .gitmodules' '
360 + rm -rf mod/sub &&
361 + git reset --hard &&
362 + git submodule update &&
363 + git config -f .gitmodules --remove-section submodule.sub &&
364 + git add .gitmodules &&
365 + entry="$(git ls-files --stage sub | cut -f 1)" &&
366 + echo "warning: Could not find section in .gitmodules where path=sub" >expect.err &&
367 + git mv sub mod/sub 2>actual.err &&
368 + test_i18ncmp expect.err actual.err &&
369 + ! test -e sub &&
370 + [ "$entry" = "$(git ls-files --stage mod/sub | cut -f 1)" ] &&
371 + (
372 + cd mod/sub &&
373 + git status
374 + ) &&
375 + git update-index --refresh &&
376 + git diff-files --quiet
377 +'
378 +
379 +test_expect_success 'mv --dry-run does not touch the submodule or .gitmodules' '
380 + rm -rf mod/sub &&
381 + git reset --hard &&
382 + git submodule update &&
383 + git mv -n sub mod/sub 2>actual.err &&
384 + test -f sub/.git &&
385 + git diff-index --exit-code HEAD &&
386 + git update-index --refresh &&
387 + git diff-files --quiet -- sub .gitmodules
388 +'
389 +
390 test_done