Skip to content

Commit d1b3b81

Browse files
stefanbellergitster
authored andcommitted
submodule init: warn about falling back to a local path
When a submodule is initialized, the config variable 'submodule.<name>.url' is set depending on the value of the same variable in the .gitmodules file. When the URL indicates to be relative, then the url is computed relative to its default remote. The default remote cannot be determined accurately in all cases, such that it falls back to 'origin'. The 'origin' remote may not exist, though. In that case we give up looking for a suitable remote and we'll just assume it to be a local relative path. This can be confusing to users as there is a lot of guessing involved, which is not obvious to the user. So in the corner case of assuming a local autoritative truth, warn the user to lessen the confusion. This behavior was introduced in 4d68932 (submodule add: allow relative repository path even when no url is set, 2011-06-06), which shared the code with submodule-init and then ported to C in 3604242 (submodule: port init from shell to C, 2016-04-15). In case of submodule-add, this behavior makes sense in some use cases[1], however for submodule-init there does not seem to be an immediate obvious use case to fall back to a local submodule. However there might be, so warn instead of die here. While adding the warning, also clarify the behavior of relative URLs in the documentation. [1] e.g. http://stackoverflow.com/questions/8721984/git-ignore-files-for-public-repository-but-not-for-private "store a secret locally in a submodule, with no intention to publish it" Reported-by: Shawn Pearce <[email protected]> Signed-off-by: Stefan Beller <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e7e07d5 commit d1b3b81

File tree

2 files changed

+27
-19
lines changed

2 files changed

+27
-19
lines changed

Documentation/git-submodule.txt

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,17 @@ configuration entries unless `--name` is used to specify a logical name.
7373
+
7474
<repository> is the URL of the new submodule's origin repository.
7575
This may be either an absolute URL, or (if it begins with ./
76-
or ../), the location relative to the superproject's origin
76+
or ../), the location relative to the superproject's default remote
7777
repository (Please note that to specify a repository 'foo.git'
7878
which is located right next to a superproject 'bar.git', you'll
7979
have to use '../foo.git' instead of './foo.git' - as one might expect
8080
when following the rules for relative URLs - because the evaluation
8181
of relative URLs in Git is identical to that of relative directories).
82-
If the superproject doesn't have an origin configured
82+
+
83+
The default remote is the remote of the remote tracking branch
84+
of the current branch. If no such remote tracking branch exists or
85+
the HEAD is detached, "origin" is assumed to be the default remote.
86+
If the superproject doesn't have a default remote configured
8387
the superproject is its own authoritative upstream and the current
8488
working directory is used instead.
8589
+
@@ -118,18 +122,24 @@ too (and can also report changes to a submodule's work tree).
118122

119123
init [--] [<path>...]::
120124
Initialize the submodules recorded in the index (which were
121-
added and committed elsewhere) by copying submodule
122-
names and urls from .gitmodules to .git/config.
123-
Optional <path> arguments limit which submodules will be initialized.
124-
It will also copy the value of `submodule.$name.update` into
125-
.git/config.
126-
The key used in .git/config is `submodule.$name.url`.
127-
This command does not alter existing information in .git/config.
128-
You can then customize the submodule clone URLs in .git/config
129-
for your local setup and proceed to `git submodule update`;
130-
you can also just use `git submodule update --init` without
131-
the explicit 'init' step if you do not intend to customize
132-
any submodule locations.
125+
added and committed elsewhere) by setting `submodule.$name.url`
126+
in .git/config. It uses the same setting from .gitmodules as
127+
a template. If the URL is relative, it will be resolved using
128+
the default remote. If there is no default remote, the current
129+
repository will be assumed to be upstream.
130+
+
131+
Optional <path> arguments limit which submodules will be initialized.
132+
If no path is specified, all submodules are initialized.
133+
+
134+
When present, it will also copy the value of `submodule.$name.update`.
135+
This command does not alter existing information in .git/config.
136+
You can then customize the submodule clone URLs in .git/config
137+
for your local setup and proceed to `git submodule update`;
138+
you can also just use `git submodule update --init` without
139+
the explicit 'init' step if you do not intend to customize
140+
any submodule locations.
141+
+
142+
See the add subcommand for the defintion of default remote.
133143

134144
deinit [-f|--force] (--all|[--] <path>...)::
135145
Unregister the given submodules, i.e. remove the whole

builtin/submodule--helper.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -356,12 +356,10 @@ static void init_submodule(const char *path, const char *prefix, int quiet)
356356
strbuf_addf(&remotesb, "remote.%s.url", remote);
357357
free(remote);
358358

359-
if (git_config_get_string(remotesb.buf, &remoteurl))
360-
/*
361-
* The repository is its own
362-
* authoritative upstream
363-
*/
359+
if (git_config_get_string(remotesb.buf, &remoteurl)) {
360+
warning(_("could not lookup configuration '%s'. Assuming this repository is its own authoritative upstream."), remotesb.buf);
364361
remoteurl = xgetcwd();
362+
}
365363
relurl = relative_url(remoteurl, url, NULL);
366364
strbuf_release(&remotesb);
367365
free(remoteurl);

0 commit comments

Comments
 (0)