Wrap rewrite globals in a struct in preparation for adding another set

remote.c has a global set of URL rewrites, accessed by alias_url and
make_rewrite.  Wrap them in a new "struct rewrites", passed to alias_url
and make_rewrite.  This allows adding other sets of rewrites.

Signed-off-by: Josh Triplett <josh@joshtriplett.org>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Josh Triplett 2009-09-07 01:56:00 -07:00 committed by Junio C Hamano
parent 6ea71fe7d3
commit d071d94296

View File

@ -28,6 +28,11 @@ struct rewrite {
int instead_of_nr; int instead_of_nr;
int instead_of_alloc; int instead_of_alloc;
}; };
struct rewrites {
struct rewrite **rewrite;
int rewrite_alloc;
int rewrite_nr;
};
static struct remote **remotes; static struct remote **remotes;
static int remotes_alloc; static int remotes_alloc;
@ -41,14 +46,12 @@ static struct branch *current_branch;
static const char *default_remote_name; static const char *default_remote_name;
static int explicit_default_remote_name; static int explicit_default_remote_name;
static struct rewrite **rewrite; static struct rewrites rewrites;
static int rewrite_alloc;
static int rewrite_nr;
#define BUF_SIZE (2048) #define BUF_SIZE (2048)
static char buffer[BUF_SIZE]; static char buffer[BUF_SIZE];
static const char *alias_url(const char *url) static const char *alias_url(const char *url, struct rewrites *r)
{ {
int i, j; int i, j;
char *ret; char *ret;
@ -57,14 +60,14 @@ static const char *alias_url(const char *url)
longest = NULL; longest = NULL;
longest_i = -1; longest_i = -1;
for (i = 0; i < rewrite_nr; i++) { for (i = 0; i < r->rewrite_nr; i++) {
if (!rewrite[i]) if (!r->rewrite[i])
continue; continue;
for (j = 0; j < rewrite[i]->instead_of_nr; j++) { for (j = 0; j < r->rewrite[i]->instead_of_nr; j++) {
if (!prefixcmp(url, rewrite[i]->instead_of[j].s) && if (!prefixcmp(url, r->rewrite[i]->instead_of[j].s) &&
(!longest || (!longest ||
longest->len < rewrite[i]->instead_of[j].len)) { longest->len < r->rewrite[i]->instead_of[j].len)) {
longest = &(rewrite[i]->instead_of[j]); longest = &(r->rewrite[i]->instead_of[j]);
longest_i = i; longest_i = i;
} }
} }
@ -72,10 +75,10 @@ static const char *alias_url(const char *url)
if (!longest) if (!longest)
return url; return url;
ret = xmalloc(rewrite[longest_i]->baselen + ret = xmalloc(r->rewrite[longest_i]->baselen +
(strlen(url) - longest->len) + 1); (strlen(url) - longest->len) + 1);
strcpy(ret, rewrite[longest_i]->base); strcpy(ret, r->rewrite[longest_i]->base);
strcpy(ret + rewrite[longest_i]->baselen, url + longest->len); strcpy(ret + r->rewrite[longest_i]->baselen, url + longest->len);
return ret; return ret;
} }
@ -103,7 +106,7 @@ static void add_url(struct remote *remote, const char *url)
static void add_url_alias(struct remote *remote, const char *url) static void add_url_alias(struct remote *remote, const char *url)
{ {
add_url(remote, alias_url(url)); add_url(remote, alias_url(url, &rewrites));
} }
static void add_pushurl(struct remote *remote, const char *pushurl) static void add_pushurl(struct remote *remote, const char *pushurl)
@ -169,22 +172,22 @@ static struct branch *make_branch(const char *name, int len)
return ret; return ret;
} }
static struct rewrite *make_rewrite(const char *base, int len) static struct rewrite *make_rewrite(struct rewrites *r, const char *base, int len)
{ {
struct rewrite *ret; struct rewrite *ret;
int i; int i;
for (i = 0; i < rewrite_nr; i++) { for (i = 0; i < r->rewrite_nr; i++) {
if (len if (len
? (len == rewrite[i]->baselen && ? (len == r->rewrite[i]->baselen &&
!strncmp(base, rewrite[i]->base, len)) !strncmp(base, r->rewrite[i]->base, len))
: !strcmp(base, rewrite[i]->base)) : !strcmp(base, r->rewrite[i]->base))
return rewrite[i]; return r->rewrite[i];
} }
ALLOC_GROW(rewrite, rewrite_nr + 1, rewrite_alloc); ALLOC_GROW(r->rewrite, r->rewrite_nr + 1, r->rewrite_alloc);
ret = xcalloc(1, sizeof(struct rewrite)); ret = xcalloc(1, sizeof(struct rewrite));
rewrite[rewrite_nr++] = ret; r->rewrite[r->rewrite_nr++] = ret;
if (len) { if (len) {
ret->base = xstrndup(base, len); ret->base = xstrndup(base, len);
ret->baselen = len; ret->baselen = len;
@ -355,7 +358,7 @@ static int handle_config(const char *key, const char *value, void *cb)
subkey = strrchr(name, '.'); subkey = strrchr(name, '.');
if (!subkey) if (!subkey)
return 0; return 0;
rewrite = make_rewrite(name, subkey - name); rewrite = make_rewrite(&rewrites, name, subkey - name);
if (!strcmp(subkey, ".insteadof")) { if (!strcmp(subkey, ".insteadof")) {
if (!value) if (!value)
return config_error_nonbool(key); return config_error_nonbool(key);
@ -433,10 +436,10 @@ static void alias_all_urls(void)
if (!remotes[i]) if (!remotes[i])
continue; continue;
for (j = 0; j < remotes[i]->url_nr; j++) { for (j = 0; j < remotes[i]->url_nr; j++) {
remotes[i]->url[j] = alias_url(remotes[i]->url[j]); remotes[i]->url[j] = alias_url(remotes[i]->url[j], &rewrites);
} }
for (j = 0; j < remotes[i]->pushurl_nr; j++) { for (j = 0; j < remotes[i]->pushurl_nr; j++) {
remotes[i]->pushurl[j] = alias_url(remotes[i]->pushurl[j]); remotes[i]->pushurl[j] = alias_url(remotes[i]->pushurl[j], &rewrites);
} }
} }
} }