url: move scheme detection to URL header/source

Move enum url_scheme and url_get_scheme()
from connect.c to url.h and url.c
so that other code can identify
a URL's scheme without depending
on connect.c.

No behavior change. url_get_scheme() still dies
on an unrecognized scheme name, with the same
translated message as before.

scheme_name() stays in connect.c
because it has no other callers.

Signed-off-by: Matheus Afonso Martins Moreira <matheus@matheusmoreira.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
main
Matheus Afonso Martins Moreira 2026-05-02 05:28:37 +00:00 committed by Junio C Hamano
parent 51fcf73014
commit d48e36a8a2
3 changed files with 29 additions and 22 deletions

View File

@ -700,13 +700,6 @@ int server_supports(const char *feature)
return !!server_feature_value(feature, NULL);
}

enum url_scheme {
URL_SCHEME_LOCAL = 1,
URL_SCHEME_FILE,
URL_SCHEME_SSH,
URL_SCHEME_GIT
};

static const char *url_scheme_name(enum url_scheme scheme)
{
switch (scheme) {
@ -722,21 +715,6 @@ static const char *url_scheme_name(enum url_scheme scheme)
}
}

static enum url_scheme url_get_scheme(const char *name)
{
if (!strcmp(name, "ssh"))
return URL_SCHEME_SSH;
if (!strcmp(name, "git"))
return URL_SCHEME_GIT;
if (!strcmp(name, "git+ssh")) /* deprecated - do not use */
return URL_SCHEME_SSH;
if (!strcmp(name, "ssh+git")) /* deprecated - do not use */
return URL_SCHEME_SSH;
if (!strcmp(name, "file"))
return URL_SCHEME_FILE;
die(_("protocol '%s' is not supported"), name);
}

static char *host_end(char **hoststart, int removebrackets)
{
char *host = *hoststart;

16
url.c
View File

@ -1,4 +1,5 @@
#include "git-compat-util.h"
#include "gettext.h"
#include "hex-ll.h"
#include "strbuf.h"
#include "url.h"
@ -140,3 +141,18 @@ int url_is_local_not_ssh(const char *url)
return !colon || (slash && slash < colon) ||
(has_dos_drive_prefix(url) && is_valid_path(url));
}

enum url_scheme url_get_scheme(const char *name)
{
if (!strcmp(name, "ssh"))
return URL_SCHEME_SSH;
if (!strcmp(name, "git"))
return URL_SCHEME_GIT;
if (!strcmp(name, "git+ssh")) /* deprecated - do not use */
return URL_SCHEME_SSH;
if (!strcmp(name, "ssh+git")) /* deprecated - do not use */
return URL_SCHEME_SSH;
if (!strcmp(name, "file"))
return URL_SCHEME_FILE;
die(_("protocol '%s' is not supported"), name);
}

13
url.h
View File

@ -23,6 +23,19 @@ void str_end_url_with_slash(const char *url, char **dest);

int url_is_local_not_ssh(const char *url);

enum url_scheme {
URL_SCHEME_LOCAL = 1,
URL_SCHEME_FILE,
URL_SCHEME_SSH,
URL_SCHEME_GIT,
};

/*
* Identify the URL scheme by name. Dies if the name does not match
* any scheme that Git knows about.
*/
enum url_scheme url_get_scheme(const char *name);

/*
* The set of unreserved characters as per STD66 (RFC3986) is
* '[A-Za-z0-9-._~]'. These characters are safe to appear in URI