submodule add: configure existing submodule url if not set
authorW. Trevor King <wking@tremily.us>
Sun, 2 Dec 2012 15:24:46 +0000 (10:24 -0500)
committerW. Trevor King <wking@tremily.us>
Sun, 2 Dec 2012 18:22:27 +0000 (13:22 -0500)
Note that relative repository arguments passed to

  $ git submodule add <repository> [<path>]

are expanded relative to the superproject's remote URL, not relative
to the current working directory.

Signed-off-by: W. Trevor King <wking@tremily.us>
git-submodule.sh

index e9c96a03613a8dfba73ab4d42b30031e3be3397f..75f138b7ae9349b24db4bba88f9a58d25b8502c8 100755 (executable)
@@ -495,7 +495,18 @@ Use -f if you really want to add it." >&2
                else
                        die "$(eval_gettext "'\$sm_path' already exists and is not a valid git repo")"
                fi
-
+               # if the submodule's remote URL is not set, set it
+               up_path=$(get_up_path "$sm_path")
+               sub_url=$(join_urls "$up_path" "$realrepo")
+               (
+                       clear_local_git_env
+                       cd "$sm_path" &&
+                       remote=$(get_default_remote) &&
+                       if ! git config remote."$remote".url > /dev/null 2>/dev/null
+                       then
+                               git config remote."$remote".url "$sub_url"
+                       fi
+               ) || die "$(eval_gettext "Unable to configure submodule remote '\$sm_path'")"
        else
 
                module_clone "$sm_path" "$realrepo" "$reference" || exit