2
#include "remote.h"
3
#include "refs.h"
4
5
+struct rewrite {
6
+ const char *base;
7
+ const char **instead_of;
8
+ int instead_of_nr;
9
+ int instead_of_alloc;
10
+};
11
+
12
static struct remote **remotes;
13
static int remotes_alloc;
14
static int remotes_nr;
20
static struct branch *current_branch;
21
static const char *default_remote_name;
22
23
+static struct rewrite **rewrite;
24
+static int rewrite_alloc;
25
+static int rewrite_nr;
26
+
27
#define BUF_SIZE (2048)
28
static char buffer[BUF_SIZE];
29
30
+static const char *alias_url(const char *url)
31
+{
32
+ int i, j;
33
+ for (i = 0; i < rewrite_nr; i++) {
34
+ if (!rewrite[i])
35
+ continue;
36
+ for (j = 0; j < rewrite[i]->instead_of_nr; j++) {
37
+ if (!prefixcmp(url, rewrite[i]->instead_of[j])) {
38
+ char *ret = malloc(strlen(rewrite[i]->base) -
39
+ strlen(rewrite[i]->instead_of[j]) +
40
+ strlen(url) + 1);
41
+ strcpy(ret, rewrite[i]->base);
42
+ strcat(ret, url + strlen(rewrite[i]->instead_of[j]));
43
+ return ret;
44
+ }
45
+ }
46
+ }
47
+ return url;
48
+}
49
+
50
static void add_push_refspec(struct remote *remote, const char *ref)
51
{
52
ALLOC_GROW(remote->push_refspec,
69
remote->url[remote->url_nr++] = url;
70
}
71
72
+static void add_url_alias(struct remote *remote, const char *url)
73
+{
74
+ add_url(remote, alias_url(url));
75
+}
76
+
77
static struct remote *make_remote(const char *name, int len)
78
{
79
struct remote *ret;
131
return ret;
132
}
133
134
+static struct rewrite *make_rewrite(const char *base, int len)
135
+{
136
+ struct rewrite *ret;
137
+ int i;
138
+
139
+ for (i = 0; i < rewrite_nr; i++) {
140
+ if (len ? (!strncmp(base, rewrite[i]->base, len) &&
141
+ !rewrite[i]->base[len]) :
142
+ !strcmp(base, rewrite[i]->base))
143
+ return rewrite[i];
144
+ }
145
+
146
+ ALLOC_GROW(rewrite, rewrite_nr + 1, rewrite_alloc);
147
+ ret = xcalloc(1, sizeof(struct rewrite));
148
+ rewrite[rewrite_nr++] = ret;
149
+ if (len)
150
+ ret->base = xstrndup(base, len);
151
+ else
152
+ ret->base = xstrdup(base);
153
+
154
+ return ret;
155
+}
156
+
157
+static void add_instead_of(struct rewrite *rewrite, const char *instead_of)
158
+{
159
+ ALLOC_GROW(rewrite->instead_of, rewrite->instead_of_nr + 1, rewrite->instead_of_alloc);
160
+ rewrite->instead_of[rewrite->instead_of_nr++] = instead_of;
161
+}
162
+
163
static void read_remotes_file(struct remote *remote)
164
{
165
FILE *f = fopen(git_path("remotes/%s", remote->name), "r");
193
194
switch (value_list) {
195
case 0:
131
- add_url(remote, xstrdup(s));
196
+ add_url_alias(remote, xstrdup(s));
197
break;
198
case 1:
199
add_push_refspec(remote, xstrdup(s));
245
} else {
246
branch = "refs/heads/master";
247
}
183
- add_url(remote, p);
248
+ add_url_alias(remote, p);
249
add_fetch_refspec(remote, branch);
250
remote->fetch_tags = 1; /* always auto-follow */
251
}
275
}
276
return 0;
277
}
278
+ if (!prefixcmp(key, "url.")) {
279
+ struct rewrite *rewrite;
280
+ name = key + 5;
281
+ subkey = strrchr(name, '.');
282
+ if (!subkey)
283
+ return 0;
284
+ rewrite = make_rewrite(name, subkey - name);
285
+ if (!strcmp(subkey, ".insteadof")) {
286
+ if (!value)
287
+ return config_error_nonbool(key);
288
+ add_instead_of(rewrite, xstrdup(value));
289
+ }
290
+ }
291
if (prefixcmp(key, "remote."))
292
return 0;
293
name = key + 7;
339
return 0;
340
}
341
342
+static void alias_all_urls(void)
343
+{
344
+ int i, j;
345
+ for (i = 0; i < remotes_nr; i++) {
346
+ if (!remotes[i])
347
+ continue;
348
+ for (j = 0; j < remotes[i]->url_nr; j++) {
349
+ remotes[i]->url[j] = alias_url(remotes[i]->url[j]);
350
+ }
351
+ }
352
+}
353
+
354
static void read_config(void)
355
{
356
unsigned char sha1[20];
367
make_branch(head_ref + strlen("refs/heads/"), 0);
368
}
369
git_config(handle_config);
370
+ alias_all_urls();
371
}
372
373
struct refspec *parse_ref_spec(int nr_refspec, const char **refspec)
433
read_branches_file(ret);
434
}
435
if (!ret->url)
345
- add_url(ret, name);
436
+ add_url_alias(ret, name);
437
if (!ret->url)
438
return NULL;
439
ret->fetch = parse_ref_spec(ret->fetch_refspec_nr, ret->fetch_refspec);