Skip to content

Commit 0656781

Browse files
jlehmanngitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5fee995 commit 0656781

File tree

5 files changed

+121
-1
lines changed

5 files changed

+121
-1
lines changed

Documentation/git-mv.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ SUBMODULES
4949
Moving a submodule using a gitfile (which means they were cloned
5050
with a Git version 1.7.8 or newer) will update the gitfile and
5151
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).
5254

5355
GIT
5456
---

builtin/mv.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ static struct lock_file lock_file;
5858

5959
int cmd_mv(int argc, const char **argv, const char *prefix)
6060
{
61-
int i, newfd;
61+
int i, newfd, gitmodules_modified = 0;
6262
int verbose = 0, show_only = 0, force = 0, ignore_errors = 0;
6363
struct option builtin_mv_options[] = {
6464
OPT__VERBOSE(&verbose, N_("be verbose")),
@@ -72,6 +72,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
7272
struct stat st;
7373
struct string_list src_for_dst = STRING_LIST_INIT_NODUP;
7474

75+
gitmodules_config();
7576
git_config(git_default_config, NULL);
7677

7778
argc = parse_options(argc, argv, prefix, builtin_mv_options,
@@ -125,6 +126,8 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
125126
struct strbuf submodule_dotgit = STRBUF_INIT;
126127
if (!S_ISGITLINK(active_cache[first]->ce_mode))
127128
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"));
128131
strbuf_addf(&submodule_dotgit, "%s/.git", src);
129132
submodule_gitfile[i] = read_gitfile(submodule_dotgit.buf);
130133
if (submodule_gitfile[i])
@@ -229,6 +232,8 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
229232
die_errno (_("renaming '%s' failed"), src);
230233
if (submodule_gitfile[i])
231234
connect_work_tree_and_git_dir(dst, submodule_gitfile[i]);
235+
if (!update_path_in_gitmodules(src, dst))
236+
gitmodules_modified = 1;
232237
}
233238

234239
if (mode == WORKING_DIRECTORY)
@@ -240,6 +245,9 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
240245
rename_cache_entry_at(pos, dst);
241246
}
242247

248+
if (gitmodules_modified)
249+
stage_updated_gitmodules();
250+
243251
if (active_cache_changed) {
244252
if (write_cache(newfd, active_cache, active_nr) ||
245253
commit_locked_index(&lock_file))

submodule.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,40 @@ int is_staging_gitmodules_ok(void)
4747
return !gitmodules_is_modified;
4848
}
4949

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+
5084
void stage_updated_gitmodules(void)
5185
{
5286
struct strbuf buf = STRBUF_INIT;

submodule.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ enum {
1212
};
1313

1414
int is_staging_gitmodules_ok(void);
15+
int update_path_in_gitmodules(const char *oldpath, const char *newpath);
1516
void stage_updated_gitmodules(void);
1617
void set_diffopt_flags_from_submodule_config(struct diff_options *diffopt,
1718
const char *path);

t/t7001-mv.sh

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,8 +308,83 @@ test_expect_success 'git mv moves a submodule with gitfile' '
308308
cd mod/sub &&
309309
git status
310310
) &&
311+
echo mod/sub >expected &&
312+
git config -f .gitmodules submodule.sub.path >actual &&
313+
test_cmp expected actual &&
311314
git update-index --refresh &&
312315
git diff-files --quiet
313316
'
314317

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+
315390
test_done

0 commit comments

Comments
 (0)