|
9 | 9 | #include "submodule-config.h"
|
10 | 10 | #include "string-list.h"
|
11 | 11 | #include "run-command.h"
|
| 12 | +#include "remote.h" |
| 13 | +#include "refs.h" |
| 14 | +#include "connect.h" |
| 15 | + |
| 16 | +static char *get_default_remote(void) |
| 17 | +{ |
| 18 | + char *dest = NULL, *ret; |
| 19 | + unsigned char sha1[20]; |
| 20 | + struct strbuf sb = STRBUF_INIT; |
| 21 | + const char *refname = resolve_ref_unsafe("HEAD", 0, sha1, NULL); |
| 22 | + |
| 23 | + if (!refname) |
| 24 | + die(_("No such ref: %s"), "HEAD"); |
| 25 | + |
| 26 | + /* detached HEAD */ |
| 27 | + if (!strcmp(refname, "HEAD")) |
| 28 | + return xstrdup("origin"); |
| 29 | + |
| 30 | + if (!skip_prefix(refname, "refs/heads/", &refname)) |
| 31 | + die(_("Expecting a full ref name, got %s"), refname); |
| 32 | + |
| 33 | + strbuf_addf(&sb, "branch.%s.remote", refname); |
| 34 | + if (git_config_get_string(sb.buf, &dest)) |
| 35 | + ret = xstrdup("origin"); |
| 36 | + else |
| 37 | + ret = dest; |
| 38 | + |
| 39 | + strbuf_release(&sb); |
| 40 | + return ret; |
| 41 | +} |
| 42 | + |
| 43 | +static int starts_with_dot_slash(const char *str) |
| 44 | +{ |
| 45 | + return str[0] == '.' && is_dir_sep(str[1]); |
| 46 | +} |
| 47 | + |
| 48 | +static int starts_with_dot_dot_slash(const char *str) |
| 49 | +{ |
| 50 | + return str[0] == '.' && str[1] == '.' && is_dir_sep(str[2]); |
| 51 | +} |
| 52 | + |
| 53 | +/* |
| 54 | + * Returns 1 if it was the last chop before ':'. |
| 55 | + */ |
| 56 | +static int chop_last_dir(char **remoteurl, int is_relative) |
| 57 | +{ |
| 58 | + char *rfind = find_last_dir_sep(*remoteurl); |
| 59 | + if (rfind) { |
| 60 | + *rfind = '\0'; |
| 61 | + return 0; |
| 62 | + } |
| 63 | + |
| 64 | + rfind = strrchr(*remoteurl, ':'); |
| 65 | + if (rfind) { |
| 66 | + *rfind = '\0'; |
| 67 | + return 1; |
| 68 | + } |
| 69 | + |
| 70 | + if (is_relative || !strcmp(".", *remoteurl)) |
| 71 | + die(_("cannot strip one component off url '%s'"), |
| 72 | + *remoteurl); |
| 73 | + |
| 74 | + free(*remoteurl); |
| 75 | + *remoteurl = xstrdup("."); |
| 76 | + return 0; |
| 77 | +} |
| 78 | + |
| 79 | +/* |
| 80 | + * The `url` argument is the URL that navigates to the submodule origin |
| 81 | + * repo. When relative, this URL is relative to the superproject origin |
| 82 | + * URL repo. The `up_path` argument, if specified, is the relative |
| 83 | + * path that navigates from the submodule working tree to the superproject |
| 84 | + * working tree. Returns the origin URL of the submodule. |
| 85 | + * |
| 86 | + * Return either an absolute URL or filesystem path (if the superproject |
| 87 | + * origin URL is an absolute URL or filesystem path, respectively) or a |
| 88 | + * relative file system path (if the superproject origin URL is a relative |
| 89 | + * file system path). |
| 90 | + * |
| 91 | + * When the output is a relative file system path, the path is either |
| 92 | + * relative to the submodule working tree, if up_path is specified, or to |
| 93 | + * the superproject working tree otherwise. |
| 94 | + * |
| 95 | + * NEEDSWORK: This works incorrectly on the domain and protocol part. |
| 96 | + * remote_url url outcome expectation |
| 97 | + * http://a.com/b ../c http://a.com/c as is |
| 98 | + * http://a.com/b ../../c http://c error out |
| 99 | + * http://a.com/b ../../../c http:/c error out |
| 100 | + * http://a.com/b ../../../../c http:c error out |
| 101 | + * http://a.com/b ../../../../../c .:c error out |
| 102 | + * NEEDSWORK: Given how chop_last_dir() works, this function is broken |
| 103 | + * when a local part has a colon in its path component, too. |
| 104 | + */ |
| 105 | +static char *relative_url(const char *remote_url, |
| 106 | + const char *url, |
| 107 | + const char *up_path) |
| 108 | +{ |
| 109 | + int is_relative = 0; |
| 110 | + int colonsep = 0; |
| 111 | + char *out; |
| 112 | + char *remoteurl = xstrdup(remote_url); |
| 113 | + struct strbuf sb = STRBUF_INIT; |
| 114 | + size_t len = strlen(remoteurl); |
| 115 | + |
| 116 | + if (is_dir_sep(remoteurl[len])) |
| 117 | + remoteurl[len] = '\0'; |
| 118 | + |
| 119 | + if (!url_is_local_not_ssh(remoteurl) || is_absolute_path(remoteurl)) |
| 120 | + is_relative = 0; |
| 121 | + else { |
| 122 | + is_relative = 1; |
| 123 | + /* |
| 124 | + * Prepend a './' to ensure all relative |
| 125 | + * remoteurls start with './' or '../' |
| 126 | + */ |
| 127 | + if (!starts_with_dot_slash(remoteurl) && |
| 128 | + !starts_with_dot_dot_slash(remoteurl)) { |
| 129 | + strbuf_reset(&sb); |
| 130 | + strbuf_addf(&sb, "./%s", remoteurl); |
| 131 | + free(remoteurl); |
| 132 | + remoteurl = strbuf_detach(&sb, NULL); |
| 133 | + } |
| 134 | + } |
| 135 | + /* |
| 136 | + * When the url starts with '../', remove that and the |
| 137 | + * last directory in remoteurl. |
| 138 | + */ |
| 139 | + while (url) { |
| 140 | + if (starts_with_dot_dot_slash(url)) { |
| 141 | + url += 3; |
| 142 | + colonsep |= chop_last_dir(&remoteurl, is_relative); |
| 143 | + } else if (starts_with_dot_slash(url)) |
| 144 | + url += 2; |
| 145 | + else |
| 146 | + break; |
| 147 | + } |
| 148 | + strbuf_reset(&sb); |
| 149 | + strbuf_addf(&sb, "%s%s%s", remoteurl, colonsep ? ":" : "/", url); |
| 150 | + free(remoteurl); |
| 151 | + |
| 152 | + if (starts_with_dot_slash(sb.buf)) |
| 153 | + out = xstrdup(sb.buf + 2); |
| 154 | + else |
| 155 | + out = xstrdup(sb.buf); |
| 156 | + strbuf_reset(&sb); |
| 157 | + |
| 158 | + if (!up_path || !is_relative) |
| 159 | + return out; |
| 160 | + |
| 161 | + strbuf_addf(&sb, "%s%s", up_path, out); |
| 162 | + free(out); |
| 163 | + return strbuf_detach(&sb, NULL); |
| 164 | +} |
| 165 | + |
| 166 | +static int resolve_relative_url(int argc, const char **argv, const char *prefix) |
| 167 | +{ |
| 168 | + char *remoteurl = NULL; |
| 169 | + char *remote = get_default_remote(); |
| 170 | + const char *up_path = NULL; |
| 171 | + char *res; |
| 172 | + const char *url; |
| 173 | + struct strbuf sb = STRBUF_INIT; |
| 174 | + |
| 175 | + if (argc != 2 && argc != 3) |
| 176 | + die("resolve-relative-url only accepts one or two arguments"); |
| 177 | + |
| 178 | + url = argv[1]; |
| 179 | + strbuf_addf(&sb, "remote.%s.url", remote); |
| 180 | + free(remote); |
| 181 | + |
| 182 | + if (git_config_get_string(sb.buf, &remoteurl)) |
| 183 | + /* the repository is its own authoritative upstream */ |
| 184 | + remoteurl = xgetcwd(); |
| 185 | + |
| 186 | + if (argc == 3) |
| 187 | + up_path = argv[2]; |
| 188 | + |
| 189 | + res = relative_url(remoteurl, url, up_path); |
| 190 | + puts(res); |
| 191 | + free(res); |
| 192 | + free(remoteurl); |
| 193 | + return 0; |
| 194 | +} |
| 195 | + |
| 196 | +static int resolve_relative_url_test(int argc, const char **argv, const char *prefix) |
| 197 | +{ |
| 198 | + char *remoteurl, *res; |
| 199 | + const char *up_path, *url; |
| 200 | + |
| 201 | + if (argc != 4) |
| 202 | + die("resolve-relative-url-test only accepts three arguments: <up_path> <remoteurl> <url>"); |
| 203 | + |
| 204 | + up_path = argv[1]; |
| 205 | + remoteurl = xstrdup(argv[2]); |
| 206 | + url = argv[3]; |
| 207 | + |
| 208 | + if (!strcmp(up_path, "(null)")) |
| 209 | + up_path = NULL; |
| 210 | + |
| 211 | + res = relative_url(remoteurl, url, up_path); |
| 212 | + puts(res); |
| 213 | + free(res); |
| 214 | + free(remoteurl); |
| 215 | + return 0; |
| 216 | +} |
12 | 217 |
|
13 | 218 | struct module_list {
|
14 | 219 | const struct cache_entry **entries;
|
@@ -504,7 +709,9 @@ static struct cmd_struct commands[] = {
|
504 | 709 | {"list", module_list},
|
505 | 710 | {"name", module_name},
|
506 | 711 | {"clone", module_clone},
|
507 |
| - {"update-clone", update_clone} |
| 712 | + {"update-clone", update_clone}, |
| 713 | + {"resolve-relative-url", resolve_relative_url}, |
| 714 | + {"resolve-relative-url-test", resolve_relative_url_test}, |
508 | 715 | };
|
509 | 716 |
|
510 | 717 | int cmd_submodule__helper(int argc, const char **argv, const char *prefix)
|
|
0 commit comments