diff options
author | Jens Lehmann <Jens.Lehmann@web.de> | 2013-08-06 21:15:11 +0200 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2013-08-06 14:10:35 -0700 |
commit | 0656781fadca17cc51fb1c30f265c251ebe12819 (patch) | |
tree | fb523dbf6f695b22ecd39809c18ded669da0715f /submodule.c | |
parent | 5fee995244e13fd59500425d49038b00499396f8 (diff) | |
download | git-0656781fadca17cc51fb1c30f265c251ebe12819.tar.gz |
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>
Diffstat (limited to 'submodule.c')
-rw-r--r-- | submodule.c | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/submodule.c b/submodule.c index fb5134a43b..d4e8276f17 100644 --- a/submodule.c +++ b/submodule.c @@ -47,6 +47,40 @@ 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; +} + void stage_updated_gitmodules(void) { struct strbuf buf = STRBUF_INIT; |