Skip to content

Commit d071d94

Browse files
joshtriplettgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 6ea71fe commit d071d94

File tree

1 file changed

+28
-25
lines changed

1 file changed

+28
-25
lines changed

remote.c

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ struct rewrite {
2828
int instead_of_nr;
2929
int instead_of_alloc;
3030
};
31+
struct rewrites {
32+
struct rewrite **rewrite;
33+
int rewrite_alloc;
34+
int rewrite_nr;
35+
};
3136

3237
static struct remote **remotes;
3338
static int remotes_alloc;
@@ -41,14 +46,12 @@ static struct branch *current_branch;
4146
static const char *default_remote_name;
4247
static int explicit_default_remote_name;
4348

44-
static struct rewrite **rewrite;
45-
static int rewrite_alloc;
46-
static int rewrite_nr;
49+
static struct rewrites rewrites;
4750

4851
#define BUF_SIZE (2048)
4952
static char buffer[BUF_SIZE];
5053

51-
static const char *alias_url(const char *url)
54+
static const char *alias_url(const char *url, struct rewrites *r)
5255
{
5356
int i, j;
5457
char *ret;
@@ -57,25 +60,25 @@ static const char *alias_url(const char *url)
5760

5861
longest = NULL;
5962
longest_i = -1;
60-
for (i = 0; i < rewrite_nr; i++) {
61-
if (!rewrite[i])
63+
for (i = 0; i < r->rewrite_nr; i++) {
64+
if (!r->rewrite[i])
6265
continue;
63-
for (j = 0; j < rewrite[i]->instead_of_nr; j++) {
64-
if (!prefixcmp(url, rewrite[i]->instead_of[j].s) &&
66+
for (j = 0; j < r->rewrite[i]->instead_of_nr; j++) {
67+
if (!prefixcmp(url, r->rewrite[i]->instead_of[j].s) &&
6568
(!longest ||
66-
longest->len < rewrite[i]->instead_of[j].len)) {
67-
longest = &(rewrite[i]->instead_of[j]);
69+
longest->len < r->rewrite[i]->instead_of[j].len)) {
70+
longest = &(r->rewrite[i]->instead_of[j]);
6871
longest_i = i;
6972
}
7073
}
7174
}
7275
if (!longest)
7376
return url;
7477

75-
ret = xmalloc(rewrite[longest_i]->baselen +
78+
ret = xmalloc(r->rewrite[longest_i]->baselen +
7679
(strlen(url) - longest->len) + 1);
77-
strcpy(ret, rewrite[longest_i]->base);
78-
strcpy(ret + rewrite[longest_i]->baselen, url + longest->len);
80+
strcpy(ret, r->rewrite[longest_i]->base);
81+
strcpy(ret + r->rewrite[longest_i]->baselen, url + longest->len);
7982
return ret;
8083
}
8184

@@ -103,7 +106,7 @@ static void add_url(struct remote *remote, const char *url)
103106

104107
static void add_url_alias(struct remote *remote, const char *url)
105108
{
106-
add_url(remote, alias_url(url));
109+
add_url(remote, alias_url(url, &rewrites));
107110
}
108111

109112
static void add_pushurl(struct remote *remote, const char *pushurl)
@@ -169,22 +172,22 @@ static struct branch *make_branch(const char *name, int len)
169172
return ret;
170173
}
171174

172-
static struct rewrite *make_rewrite(const char *base, int len)
175+
static struct rewrite *make_rewrite(struct rewrites *r, const char *base, int len)
173176
{
174177
struct rewrite *ret;
175178
int i;
176179

177-
for (i = 0; i < rewrite_nr; i++) {
180+
for (i = 0; i < r->rewrite_nr; i++) {
178181
if (len
179-
? (len == rewrite[i]->baselen &&
180-
!strncmp(base, rewrite[i]->base, len))
181-
: !strcmp(base, rewrite[i]->base))
182-
return rewrite[i];
182+
? (len == r->rewrite[i]->baselen &&
183+
!strncmp(base, r->rewrite[i]->base, len))
184+
: !strcmp(base, r->rewrite[i]->base))
185+
return r->rewrite[i];
183186
}
184187

185-
ALLOC_GROW(rewrite, rewrite_nr + 1, rewrite_alloc);
188+
ALLOC_GROW(r->rewrite, r->rewrite_nr + 1, r->rewrite_alloc);
186189
ret = xcalloc(1, sizeof(struct rewrite));
187-
rewrite[rewrite_nr++] = ret;
190+
r->rewrite[r->rewrite_nr++] = ret;
188191
if (len) {
189192
ret->base = xstrndup(base, len);
190193
ret->baselen = len;
@@ -355,7 +358,7 @@ static int handle_config(const char *key, const char *value, void *cb)
355358
subkey = strrchr(name, '.');
356359
if (!subkey)
357360
return 0;
358-
rewrite = make_rewrite(name, subkey - name);
361+
rewrite = make_rewrite(&rewrites, name, subkey - name);
359362
if (!strcmp(subkey, ".insteadof")) {
360363
if (!value)
361364
return config_error_nonbool(key);
@@ -433,10 +436,10 @@ static void alias_all_urls(void)
433436
if (!remotes[i])
434437
continue;
435438
for (j = 0; j < remotes[i]->url_nr; j++) {
436-
remotes[i]->url[j] = alias_url(remotes[i]->url[j]);
439+
remotes[i]->url[j] = alias_url(remotes[i]->url[j], &rewrites);
437440
}
438441
for (j = 0; j < remotes[i]->pushurl_nr; j++) {
439-
remotes[i]->pushurl[j] = alias_url(remotes[i]->pushurl[j]);
442+
remotes[i]->pushurl[j] = alias_url(remotes[i]->pushurl[j], &rewrites);
440443
}
441444
}
442445
}

0 commit comments

Comments
 (0)