#include "remote.h"
#include "refs.h"
+struct counted_string {
+ size_t len;
+ const char *s;
+};
+struct rewrite {
+ const char *base;
+ size_t baselen;
+ struct counted_string *instead_of;
+ int instead_of_nr;
+ int instead_of_alloc;
+};
+
static struct remote **remotes;
-static int allocated_remotes;
+static int remotes_alloc;
+static int remotes_nr;
static struct branch **branches;
-static int allocated_branches;
+static int branches_alloc;
+static int branches_nr;
static struct branch *current_branch;
static const char *default_remote_name;
+static struct rewrite **rewrite;
+static int rewrite_alloc;
+static int rewrite_nr;
+
#define BUF_SIZE (2048)
static char buffer[BUF_SIZE];
+static const char *alias_url(const char *url)
+{
+ int i, j;
+ char *ret;
+ struct counted_string *longest;
+ int longest_i;
+
+ longest = NULL;
+ longest_i = -1;
+ for (i = 0; i < rewrite_nr; i++) {
+ if (!rewrite[i])
+ continue;
+ for (j = 0; j < rewrite[i]->instead_of_nr; j++) {
+ if (!prefixcmp(url, rewrite[i]->instead_of[j].s) &&
+ (!longest ||
+ longest->len < rewrite[i]->instead_of[j].len)) {
+ longest = &(rewrite[i]->instead_of[j]);
+ longest_i = i;
+ }
+ }
+ }
+ if (!longest)
+ return url;
+
+ ret = malloc(rewrite[longest_i]->baselen +
+ (strlen(url) - longest->len) + 1);
+ strcpy(ret, rewrite[longest_i]->base);
+ strcpy(ret + rewrite[longest_i]->baselen, url + longest->len);
+ return ret;
+}
+
static void add_push_refspec(struct remote *remote, const char *ref)
{
- int nr = remote->push_refspec_nr + 1;
- remote->push_refspec =
- xrealloc(remote->push_refspec, nr * sizeof(char *));
- remote->push_refspec[nr-1] = ref;
- remote->push_refspec_nr = nr;
+ ALLOC_GROW(remote->push_refspec,
+ remote->push_refspec_nr + 1,
+ remote->push_refspec_alloc);
+ remote->push_refspec[remote->push_refspec_nr++] = ref;
}
static void add_fetch_refspec(struct remote *remote, const char *ref)
{
- int nr = remote->fetch_refspec_nr + 1;
- remote->fetch_refspec =
- xrealloc(remote->fetch_refspec, nr * sizeof(char *));
- remote->fetch_refspec[nr-1] = ref;
- remote->fetch_refspec_nr = nr;
+ ALLOC_GROW(remote->fetch_refspec,
+ remote->fetch_refspec_nr + 1,
+ remote->fetch_refspec_alloc);
+ remote->fetch_refspec[remote->fetch_refspec_nr++] = ref;
}
static void add_url(struct remote *remote, const char *url)
{
- int nr = remote->url_nr + 1;
- remote->url =
- xrealloc(remote->url, nr * sizeof(char *));
- remote->url[nr-1] = url;
- remote->url_nr = nr;
+ ALLOC_GROW(remote->url, remote->url_nr + 1, remote->url_alloc);
+ remote->url[remote->url_nr++] = url;
+}
+
+static void add_url_alias(struct remote *remote, const char *url)
+{
+ add_url(remote, alias_url(url));
}
static struct remote *make_remote(const char *name, int len)
{
- int i, empty = -1;
+ struct remote *ret;
+ int i;
- for (i = 0; i < allocated_remotes; i++) {
- if (!remotes[i]) {
- if (empty < 0)
- empty = i;
- } else {
- if (len ? (!strncmp(name, remotes[i]->name, len) &&
- !remotes[i]->name[len]) :
- !strcmp(name, remotes[i]->name))
- return remotes[i];
- }
+ for (i = 0; i < remotes_nr; i++) {
+ if (len ? (!strncmp(name, remotes[i]->name, len) &&
+ !remotes[i]->name[len]) :
+ !strcmp(name, remotes[i]->name))
+ return remotes[i];
}
- if (empty < 0) {
- empty = allocated_remotes;
- allocated_remotes += allocated_remotes ? allocated_remotes : 1;
- remotes = xrealloc(remotes,
- sizeof(*remotes) * allocated_remotes);
- memset(remotes + empty, 0,
- (allocated_remotes - empty) * sizeof(*remotes));
- }
- remotes[empty] = xcalloc(1, sizeof(struct remote));
+ ret = xcalloc(1, sizeof(struct remote));
+ ALLOC_GROW(remotes, remotes_nr + 1, remotes_alloc);
+ remotes[remotes_nr++] = ret;
if (len)
- remotes[empty]->name = xstrndup(name, len);
+ ret->name = xstrndup(name, len);
else
- remotes[empty]->name = xstrdup(name);
- return remotes[empty];
+ ret->name = xstrdup(name);
+ return ret;
}
static void add_merge(struct branch *branch, const char *name)
{
- int nr = branch->merge_nr + 1;
- branch->merge_name =
- xrealloc(branch->merge_name, nr * sizeof(char *));
- branch->merge_name[nr-1] = name;
- branch->merge_nr = nr;
+ ALLOC_GROW(branch->merge_name, branch->merge_nr + 1,
+ branch->merge_alloc);
+ branch->merge_name[branch->merge_nr++] = name;
}
static struct branch *make_branch(const char *name, int len)
{
- int i, empty = -1;
+ struct branch *ret;
+ int i;
char *refname;
- for (i = 0; i < allocated_branches; i++) {
- if (!branches[i]) {
- if (empty < 0)
- empty = i;
- } else {
- if (len ? (!strncmp(name, branches[i]->name, len) &&
- !branches[i]->name[len]) :
- !strcmp(name, branches[i]->name))
- return branches[i];
- }
+ for (i = 0; i < branches_nr; i++) {
+ if (len ? (!strncmp(name, branches[i]->name, len) &&
+ !branches[i]->name[len]) :
+ !strcmp(name, branches[i]->name))
+ return branches[i];
}
- if (empty < 0) {
- empty = allocated_branches;
- allocated_branches += allocated_branches ? allocated_branches : 1;
- branches = xrealloc(branches,
- sizeof(*branches) * allocated_branches);
- memset(branches + empty, 0,
- (allocated_branches - empty) * sizeof(*branches));
- }
- branches[empty] = xcalloc(1, sizeof(struct branch));
+ ALLOC_GROW(branches, branches_nr + 1, branches_alloc);
+ ret = xcalloc(1, sizeof(struct branch));
+ branches[branches_nr++] = ret;
if (len)
- branches[empty]->name = xstrndup(name, len);
+ ret->name = xstrndup(name, len);
else
- branches[empty]->name = xstrdup(name);
+ ret->name = xstrdup(name);
refname = malloc(strlen(name) + strlen("refs/heads/") + 1);
strcpy(refname, "refs/heads/");
- strcpy(refname + strlen("refs/heads/"),
- branches[empty]->name);
- branches[empty]->refname = refname;
+ strcpy(refname + strlen("refs/heads/"), ret->name);
+ ret->refname = refname;
- return branches[empty];
+ return ret;
+}
+
+static struct rewrite *make_rewrite(const char *base, int len)
+{
+ struct rewrite *ret;
+ int i;
+
+ for (i = 0; i < rewrite_nr; i++) {
+ if (len
+ ? (len == rewrite[i]->baselen &&
+ !strncmp(base, rewrite[i]->base, len))
+ : !strcmp(base, rewrite[i]->base))
+ return rewrite[i];
+ }
+
+ ALLOC_GROW(rewrite, rewrite_nr + 1, rewrite_alloc);
+ ret = xcalloc(1, sizeof(struct rewrite));
+ rewrite[rewrite_nr++] = ret;
+ if (len) {
+ ret->base = xstrndup(base, len);
+ ret->baselen = len;
+ }
+ else {
+ ret->base = xstrdup(base);
+ ret->baselen = strlen(base);
+ }
+ return ret;
+}
+
+static void add_instead_of(struct rewrite *rewrite, const char *instead_of)
+{
+ ALLOC_GROW(rewrite->instead_of, rewrite->instead_of_nr + 1, rewrite->instead_of_alloc);
+ rewrite->instead_of[rewrite->instead_of_nr].s = instead_of;
+ rewrite->instead_of[rewrite->instead_of_nr].len = strlen(instead_of);
+ rewrite->instead_of_nr++;
}
static void read_remotes_file(struct remote *remote)
switch (value_list) {
case 0:
- add_url(remote, xstrdup(s));
+ add_url_alias(remote, xstrdup(s));
break;
case 1:
add_push_refspec(remote, xstrdup(s));
{
const char *slash = strchr(remote->name, '/');
char *frag;
- char *branch;
+ struct strbuf branch;
int n = slash ? slash - remote->name : 1000;
FILE *f = fopen(git_path("branches/%.*s", n, remote->name), "r");
char *s, *p;
strcpy(p, s);
if (slash)
strcat(p, slash);
+
+ /*
+ * With "slash", e.g. "git fetch jgarzik/netdev-2.6" when
+ * reading from $GIT_DIR/branches/jgarzik fetches "HEAD" from
+ * the partial URL obtained from the branches file plus
+ * "/netdev-2.6" and does not store it in any tracking ref.
+ * #branch specifier in the file is ignored.
+ *
+ * Otherwise, the branches file would have URL and optionally
+ * #branch specified. The "master" (or specified) branch is
+ * fetched and stored in the local branch of the same name.
+ */
+ strbuf_init(&branch, 0);
frag = strchr(p, '#');
if (frag) {
*(frag++) = '\0';
- branch = xmalloc(strlen(frag) + 12);
- strcpy(branch, "refs/heads/");
- strcat(branch, frag);
+ strbuf_addf(&branch, "refs/heads/%s", frag);
+ } else
+ strbuf_addstr(&branch, "refs/heads/master");
+ if (!slash) {
+ strbuf_addf(&branch, ":refs/heads/%s", remote->name);
} else {
- branch = "refs/heads/master";
+ strbuf_reset(&branch);
+ strbuf_addstr(&branch, "HEAD:");
}
- add_url(remote, p);
- add_fetch_refspec(remote, branch);
+ add_url_alias(remote, p);
+ add_fetch_refspec(remote, strbuf_detach(&branch, 0));
remote->fetch_tags = 1; /* always auto-follow */
}
subkey = strrchr(name, '.');
if (!subkey)
return 0;
- if (!value)
- return 0;
branch = make_branch(name, subkey - name);
if (!strcmp(subkey, ".remote")) {
+ if (!value)
+ return config_error_nonbool(key);
branch->remote_name = xstrdup(value);
if (branch == current_branch)
default_remote_name = branch->remote_name;
- } else if (!strcmp(subkey, ".merge"))
+ } else if (!strcmp(subkey, ".merge")) {
+ if (!value)
+ return config_error_nonbool(key);
add_merge(branch, xstrdup(value));
+ }
return 0;
}
+ if (!prefixcmp(key, "url.")) {
+ struct rewrite *rewrite;
+ name = key + 5;
+ subkey = strrchr(name, '.');
+ if (!subkey)
+ return 0;
+ rewrite = make_rewrite(name, subkey - name);
+ if (!strcmp(subkey, ".insteadof")) {
+ if (!value)
+ return config_error_nonbool(key);
+ add_instead_of(rewrite, xstrdup(value));
+ }
+ }
if (prefixcmp(key, "remote."))
return 0;
name = key + 7;
remote->fetch_tags = -1;
} else if (!strcmp(subkey, ".proxy")) {
remote->http_proxy = xstrdup(value);
- }
+ } else if (!strcmp(subkey, ".skipdefaultupdate"))
+ remote->skip_default_update = 1;
return 0;
}
+static void alias_all_urls(void)
+{
+ int i, j;
+ for (i = 0; i < remotes_nr; i++) {
+ if (!remotes[i])
+ continue;
+ for (j = 0; j < remotes[i]->url_nr; j++) {
+ remotes[i]->url[j] = alias_url(remotes[i]->url[j]);
+ }
+ }
+}
+
static void read_config(void)
{
unsigned char sha1[20];
make_branch(head_ref + strlen("refs/heads/"), 0);
}
git_config(handle_config);
+ alias_all_urls();
}
-struct refspec *parse_ref_spec(int nr_refspec, const char **refspec)
+static struct refspec *parse_refspec_internal(int nr_refspec, const char **refspec, int fetch)
{
int i;
+ int st;
struct refspec *rs = xcalloc(sizeof(*rs), nr_refspec);
+
for (i = 0; i < nr_refspec; i++) {
- const char *sp, *ep, *gp;
- sp = refspec[i];
- if (*sp == '+') {
+ size_t llen, rlen;
+ int is_glob;
+ const char *lhs, *rhs;
+
+ llen = rlen = is_glob = 0;
+
+ lhs = refspec[i];
+ if (*lhs == '+') {
rs[i].force = 1;
- sp++;
+ lhs++;
}
- gp = strchr(sp, '*');
- ep = strchr(sp, ':');
- if (gp && ep && gp > ep)
- gp = NULL;
- if (ep) {
- if (ep[1]) {
- const char *glob = strchr(ep + 1, '*');
- if (!glob)
- gp = NULL;
- if (gp)
- rs[i].dst = xstrndup(ep + 1,
- glob - ep - 1);
- else
- rs[i].dst = xstrdup(ep + 1);
+
+ rhs = strrchr(lhs, ':');
+ if (rhs) {
+ rhs++;
+ rlen = strlen(rhs);
+ is_glob = (2 <= rlen && !strcmp(rhs + rlen - 2, "/*"));
+ if (is_glob)
+ rlen -= 2;
+ rs[i].dst = xstrndup(rhs, rlen);
+ }
+
+ llen = (rhs ? (rhs - lhs - 1) : strlen(lhs));
+ if (2 <= llen && !memcmp(lhs + llen - 2, "/*", 2)) {
+ if ((rhs && !is_glob) || (!rhs && fetch))
+ goto invalid;
+ is_glob = 1;
+ llen -= 2;
+ } else if (rhs && is_glob) {
+ goto invalid;
+ }
+
+ rs[i].pattern = is_glob;
+ rs[i].src = xstrndup(lhs, llen);
+
+ if (fetch) {
+ /*
+ * LHS
+ * - empty is allowed; it means HEAD.
+ * - otherwise it must be a valid looking ref.
+ */
+ if (!*rs[i].src)
+ ; /* empty is ok */
+ else {
+ st = check_ref_format(rs[i].src);
+ if (st && st != CHECK_REF_FORMAT_ONELEVEL)
+ goto invalid;
+ }
+ /*
+ * RHS
+ * - missing is ok, and is same as empty.
+ * - empty is ok; it means not to store.
+ * - otherwise it must be a valid looking ref.
+ */
+ if (!rs[i].dst) {
+ ; /* ok */
+ } else if (!*rs[i].dst) {
+ ; /* ok */
+ } else {
+ st = check_ref_format(rs[i].dst);
+ if (st && st != CHECK_REF_FORMAT_ONELEVEL)
+ goto invalid;
}
} else {
- ep = sp + strlen(sp);
- }
- if (gp) {
- rs[i].pattern = 1;
- ep = gp;
+ /*
+ * LHS
+ * - empty is allowed; it means delete.
+ * - when wildcarded, it must be a valid looking ref.
+ * - otherwise, it must be an extended SHA-1, but
+ * there is no existing way to validate this.
+ */
+ if (!*rs[i].src)
+ ; /* empty is ok */
+ else if (is_glob) {
+ st = check_ref_format(rs[i].src);
+ if (st && st != CHECK_REF_FORMAT_ONELEVEL)
+ goto invalid;
+ }
+ else
+ ; /* anything goes, for now */
+ /*
+ * RHS
+ * - missing is allowed, but LHS then must be a
+ * valid looking ref.
+ * - empty is not allowed.
+ * - otherwise it must be a valid looking ref.
+ */
+ if (!rs[i].dst) {
+ st = check_ref_format(rs[i].src);
+ if (st && st != CHECK_REF_FORMAT_ONELEVEL)
+ goto invalid;
+ } else if (!*rs[i].dst) {
+ goto invalid;
+ } else {
+ st = check_ref_format(rs[i].dst);
+ if (st && st != CHECK_REF_FORMAT_ONELEVEL)
+ goto invalid;
+ }
}
- rs[i].src = xstrndup(sp, ep - sp);
}
return rs;
+
+ invalid:
+ die("Invalid refspec '%s'", refspec[i]);
+}
+
+struct refspec *parse_fetch_refspec(int nr_refspec, const char **refspec)
+{
+ return parse_refspec_internal(nr_refspec, refspec, 1);
+}
+
+struct refspec *parse_push_refspec(int nr_refspec, const char **refspec)
+{
+ return parse_refspec_internal(nr_refspec, refspec, 0);
+}
+
+static int valid_remote_nick(const char *name)
+{
+ if (!name[0] || /* not empty */
+ (name[0] == '.' && /* not "." */
+ (!name[1] || /* not ".." */
+ (name[1] == '.' && !name[2]))))
+ return 0;
+ return !strchr(name, '/'); /* no slash */
}
struct remote *remote_get(const char *name)
if (!name)
name = default_remote_name;
ret = make_remote(name, 0);
- if (name[0] != '/') {
+ if (valid_remote_nick(name)) {
if (!ret->url)
read_remotes_file(ret);
if (!ret->url)
read_branches_file(ret);
}
if (!ret->url)
- add_url(ret, name);
+ add_url_alias(ret, name);
if (!ret->url)
return NULL;
- ret->fetch = parse_ref_spec(ret->fetch_refspec_nr, ret->fetch_refspec);
- ret->push = parse_ref_spec(ret->push_refspec_nr, ret->push_refspec);
+ ret->fetch = parse_fetch_refspec(ret->fetch_refspec_nr, ret->fetch_refspec);
+ ret->push = parse_push_refspec(ret->push_refspec_nr, ret->push_refspec);
return ret;
}
{
int i, result = 0;
read_config();
- for (i = 0; i < allocated_remotes && !result; i++) {
+ for (i = 0; i < remotes_nr && !result; i++) {
struct remote *r = remotes[i];
if (!r)
continue;
if (!r->fetch)
- r->fetch = parse_ref_spec(r->fetch_refspec_nr,
- r->fetch_refspec);
+ r->fetch = parse_fetch_refspec(r->fetch_refspec_nr,
+ r->fetch_refspec);
if (!r->push)
- r->push = parse_ref_spec(r->push_refspec_nr,
- r->push_refspec);
+ r->push = parse_push_refspec(r->push_refspec_nr,
+ r->push_refspec);
result = fn(r, priv);
}
return result;
if (!fetch->dst)
continue;
if (fetch->pattern) {
- if (!prefixcmp(needle, key)) {
+ if (!prefixcmp(needle, key) &&
+ needle[strlen(key)] == '/') {
*result = xmalloc(strlen(value) +
strlen(needle) -
strlen(key) + 1);
struct ref *next;
while (ref) {
next = ref->next;
- if (ref->peer_ref)
- free(ref->peer_ref);
+ free(ref->peer_ref);
free(ref);
ref = next;
}
errs = 1;
if (!dst_value) {
+ unsigned char sha1[20];
+ int flag;
+
if (!matched_src)
return errs;
- dst_value = matched_src->name;
+ dst_value = resolve_ref(matched_src->name, sha1, 1, &flag);
+ if (!dst_value ||
+ ((flag & REF_ISSYMREF) &&
+ prefixcmp(dst_value, "refs/heads/")))
+ die("%s cannot be resolved to branch.",
+ matched_src->name);
}
switch (count_refspec_match(dst_value, dst, &matched_dst)) {
{
int i;
for (i = 0; i < rs_nr; i++) {
- if (rs[i].pattern && !prefixcmp(src->name, rs[i].src))
+ if (rs[i].pattern &&
+ !prefixcmp(src->name, rs[i].src) &&
+ src->name[strlen(rs[i].src)] == '/')
return rs + i;
}
return NULL;
int nr_refspec, const char **refspec, int flags)
{
struct refspec *rs =
- parse_ref_spec(nr_refspec, (const char **) refspec);
+ parse_push_refspec(nr_refspec, (const char **) refspec);
int send_all = flags & MATCH_REFS_ALL;
int send_mirror = flags & MATCH_REFS_MIRROR;
struct ref ***tail,
int missing_ok)
{
- struct ref *ref_map, *rm;
+ struct ref *ref_map, **rmp;
if (refspec->pattern) {
ref_map = get_expanded_map(remote_refs, refspec);
}
}
- for (rm = ref_map; rm; rm = rm->next) {
- if (rm->peer_ref && check_ref_format(rm->peer_ref->name + 5))
- die("* refusing to create funny ref '%s' locally",
- rm->peer_ref->name);
+ for (rmp = &ref_map; *rmp; ) {
+ if ((*rmp)->peer_ref) {
+ int st = check_ref_format((*rmp)->peer_ref->name + 5);
+ if (st && st != CHECK_REF_FORMAT_ONELEVEL) {
+ struct ref *ignore = *rmp;
+ error("* Ignoring funny ref '%s' locally",
+ (*rmp)->peer_ref->name);
+ *rmp = (*rmp)->next;
+ free(ignore->peer_ref);
+ free(ignore);
+ continue;
+ }
+ }
+ rmp = &((*rmp)->next);
}
if (ref_map)