diff options
Diffstat (limited to 'remote.c')
-rw-r--r-- | remote.c | 253 |
1 files changed, 107 insertions, 146 deletions
@@ -8,6 +8,7 @@ #include "tag.h" #include "string-list.h" #include "mergesort.h" +#include "argv-array.h" enum map_direction { FROM_SRC, FROM_DST }; @@ -54,9 +55,6 @@ static const char *pushremote_name; static struct rewrites rewrites; static struct rewrites rewrites_push; -#define BUF_SIZE (2048) -static char buffer[BUF_SIZE]; - static int valid_remote(const struct remote *remote) { return (!!remote->url) || (!!remote->foreign_vcs); @@ -65,7 +63,6 @@ static int valid_remote(const struct remote *remote) static const char *alias_url(const char *url, struct rewrites *r) { int i, j; - char *ret; struct counted_string *longest; int longest_i; @@ -86,11 +83,7 @@ static const char *alias_url(const char *url, struct rewrites *r) if (!longest) return url; - ret = xmalloc(r->rewrite[longest_i]->baselen + - (strlen(url) - longest->len) + 1); - strcpy(ret, r->rewrite[longest_i]->base); - strcpy(ret + r->rewrite[longest_i]->baselen, url + longest->len); - return ret; + return xstrfmt("%s%s", r->rewrite[longest_i]->base, url + longest->len); } static void add_push_refspec(struct remote *remote, const char *ref) @@ -248,106 +241,77 @@ static void add_instead_of(struct rewrite *rewrite, const char *instead_of) rewrite->instead_of_nr++; } +static const char *skip_spaces(const char *s) +{ + while (isspace(*s)) + s++; + return s; +} + static void read_remotes_file(struct remote *remote) { + struct strbuf buf = STRBUF_INIT; FILE *f = fopen(git_path("remotes/%s", remote->name), "r"); if (!f) return; remote->origin = REMOTE_REMOTES; - while (fgets(buffer, BUF_SIZE, f)) { - int value_list; - char *s, *p; - - if (starts_with(buffer, "URL:")) { - value_list = 0; - s = buffer + 4; - } else if (starts_with(buffer, "Push:")) { - value_list = 1; - s = buffer + 5; - } else if (starts_with(buffer, "Pull:")) { - value_list = 2; - s = buffer + 5; - } else - continue; - - while (isspace(*s)) - s++; - if (!*s) - continue; + while (strbuf_getline(&buf, f, '\n') != EOF) { + const char *v; - p = s + strlen(s); - while (isspace(p[-1])) - *--p = 0; + strbuf_rtrim(&buf); - switch (value_list) { - case 0: - add_url_alias(remote, xstrdup(s)); - break; - case 1: - add_push_refspec(remote, xstrdup(s)); - break; - case 2: - add_fetch_refspec(remote, xstrdup(s)); - break; - } + if (skip_prefix(buf.buf, "URL:", &v)) + add_url_alias(remote, xstrdup(skip_spaces(v))); + else if (skip_prefix(buf.buf, "Push:", &v)) + add_push_refspec(remote, xstrdup(skip_spaces(v))); + else if (skip_prefix(buf.buf, "Pull:", &v)) + add_fetch_refspec(remote, xstrdup(skip_spaces(v))); } + strbuf_release(&buf); fclose(f); } static void read_branches_file(struct remote *remote) { char *frag; - struct strbuf branch = STRBUF_INIT; - int n = 1000; - FILE *f = fopen(git_path("branches/%.*s", n, remote->name), "r"); - char *s, *p; - int len; + struct strbuf buf = STRBUF_INIT; + FILE *f = fopen(git_path("branches/%s", remote->name), "r"); if (!f) return; - s = fgets(buffer, BUF_SIZE, f); + + strbuf_getline(&buf, f, '\n'); fclose(f); - if (!s) - return; - while (isspace(*s)) - s++; - if (!*s) + strbuf_trim(&buf); + if (!buf.len) { + strbuf_release(&buf); return; + } + remote->origin = REMOTE_BRANCHES; - p = s + strlen(s); - while (isspace(p[-1])) - *--p = 0; - len = p - s; - p = xmalloc(len + 1); - strcpy(p, s); /* * 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. + * fetched and stored in the local branch matching the + * remote name. */ - frag = strchr(p, '#'); - if (frag) { + frag = strchr(buf.buf, '#'); + if (frag) *(frag++) = '\0'; - strbuf_addf(&branch, "refs/heads/%s", frag); - } else - strbuf_addstr(&branch, "refs/heads/master"); + else + frag = "master"; + + add_url_alias(remote, strbuf_detach(&buf, NULL)); + add_fetch_refspec(remote, xstrfmt("refs/heads/%s:refs/heads/%s", + frag, remote->name)); - strbuf_addf(&branch, ":refs/heads/%s", remote->name); - add_url_alias(remote, p); - add_fetch_refspec(remote, strbuf_detach(&branch, NULL)); /* * Cogito compatible push: push current HEAD to remote #branch * (master if missing) */ - strbuf_init(&branch, 0); - strbuf_addstr(&branch, "HEAD"); - if (frag) - strbuf_addf(&branch, ":refs/heads/%s", frag); - else - strbuf_addstr(&branch, ":refs/heads/master"); - add_push_refspec(remote, strbuf_detach(&branch, NULL)); + add_push_refspec(remote, xstrfmt("HEAD:refs/heads/%s", frag)); remote->fetch_tags = 1; /* always auto-follow */ } @@ -492,7 +456,7 @@ static void alias_all_urls(void) static void read_config(void) { static int loaded; - unsigned char sha1[20]; + struct object_id oid; const char *head_ref; int flag; @@ -501,7 +465,7 @@ static void read_config(void) loaded = 1; current_branch = NULL; - head_ref = resolve_ref_unsafe("HEAD", 0, sha1, &flag); + head_ref = resolve_ref_unsafe("HEAD", 0, oid.hash, &flag); if (head_ref && (flag & REF_ISSYMREF) && skip_prefix(head_ref, "refs/heads/", &head_ref)) { current_branch = make_branch(head_ref, 0); @@ -580,12 +544,12 @@ static struct refspec *parse_refspec_internal(int nr_refspec, const char **refsp flags = REFNAME_ALLOW_ONELEVEL | (is_glob ? REFNAME_REFSPEC_PATTERN : 0); if (fetch) { - unsigned char unused[40]; + struct object_id unused; /* LHS */ if (!*rs[i].src) ; /* empty is ok; it means "HEAD" */ - else if (llen == 40 && !get_sha1_hex(rs[i].src, unused)) + else if (llen == GIT_SHA1_HEXSZ && !get_oid_hex(rs[i].src, &unused)) rs[i].exact_sha1 = 1; /* ok */ else if (!check_refname_format(rs[i].src, flags)) ; /* valid looking ref is ok */ @@ -1111,14 +1075,14 @@ static void tail_link_ref(struct ref *ref, struct ref ***tail) static struct ref *alloc_delete_ref(void) { struct ref *ref = alloc_ref("(delete)"); - hashclr(ref->new_sha1); + oidclr(&ref->new_oid); return ref; } static int try_explicit_object_name(const char *name, struct ref **match) { - unsigned char sha1[20]; + struct object_id oid; if (!*name) { if (match) @@ -1126,12 +1090,12 @@ static int try_explicit_object_name(const char *name, return 0; } - if (get_sha1(name, sha1)) + if (get_sha1(name, oid.hash)) return -1; if (match) { *match = alloc_ref(name); - hashcpy((*match)->new_sha1, sha1); + oidcpy(&(*match)->new_oid, &oid); } return 0; } @@ -1146,10 +1110,10 @@ static struct ref *make_linked_ref(const char *name, struct ref ***tail) static char *guess_ref(const char *name, struct ref *peer) { struct strbuf buf = STRBUF_INIT; - unsigned char sha1[20]; + struct object_id oid; const char *r = resolve_ref_unsafe(peer->name, RESOLVE_REF_READING, - sha1, NULL); + oid.hash, NULL); if (!r) return NULL; @@ -1207,12 +1171,12 @@ static int match_explicit(struct ref *src, struct ref *dst, return -1; if (!dst_value) { - unsigned char sha1[20]; + struct object_id oid; int flag; dst_value = resolve_ref_unsafe(matched_src->name, RESOLVE_REF_READING, - sha1, &flag); + oid.hash, &flag); if (!dst_value || ((flag & REF_ISSYMREF) && !starts_with(dst_value, "refs/heads/"))) @@ -1226,7 +1190,7 @@ static int match_explicit(struct ref *src, struct ref *dst, case 0: if (starts_with(dst_value, "refs/")) matched_dst = make_linked_ref(dst_value, dst_tail); - else if (is_null_sha1(matched_src->new_sha1)) + else if (is_null_oid(&matched_src->new_oid)) error("unable to delete '%s': remote ref does not exist", dst_value); else if ((dst_guess = guess_ref(dst_value, matched_src))) @@ -1328,13 +1292,13 @@ struct tips { int nr, alloc; }; -static void add_to_tips(struct tips *tips, const unsigned char *sha1) +static void add_to_tips(struct tips *tips, const struct object_id *oid) { struct commit *commit; - if (is_null_sha1(sha1)) + if (is_null_oid(oid)) return; - commit = lookup_commit_reference_gently(sha1, 1); + commit = lookup_commit_reference_gently(oid->hash, 1); if (!commit || (commit->object.flags & TMP_MARK)) return; commit->object.flags |= TMP_MARK; @@ -1357,10 +1321,10 @@ static void add_missing_tags(struct ref *src, struct ref **dst, struct ref ***ds memset(&sent_tips, 0, sizeof(sent_tips)); for (ref = *dst; ref; ref = ref->next) { if (ref->peer_ref && - !is_null_sha1(ref->peer_ref->new_sha1)) - add_to_tips(&sent_tips, ref->peer_ref->new_sha1); + !is_null_oid(&ref->peer_ref->new_oid)) + add_to_tips(&sent_tips, &ref->peer_ref->new_oid); else - add_to_tips(&sent_tips, ref->old_sha1); + add_to_tips(&sent_tips, &ref->old_oid); if (starts_with(ref->name, "refs/tags/")) string_list_append(&dst_tag, ref->name); } @@ -1374,7 +1338,7 @@ static void add_missing_tags(struct ref *src, struct ref **dst, struct ref ***ds continue; /* not a tag */ if (string_list_has_string(&dst_tag, ref->name)) continue; /* they already have it */ - if (sha1_object_info(ref->new_sha1, NULL) != OBJ_TAG) + if (sha1_object_info(ref->new_oid.hash, NULL) != OBJ_TAG) continue; /* be conservative */ item = string_list_append(&src_tag, ref->name); item->util = ref; @@ -1394,9 +1358,9 @@ static void add_missing_tags(struct ref *src, struct ref **dst, struct ref ***ds struct ref *dst_ref; struct commit *commit; - if (is_null_sha1(ref->new_sha1)) + if (is_null_oid(&ref->new_oid)) continue; - commit = lookup_commit_reference_gently(ref->new_sha1, 1); + commit = lookup_commit_reference_gently(ref->new_oid.hash, 1); if (!commit) /* not pushing a commit, which is not an error */ continue; @@ -1410,7 +1374,7 @@ static void add_missing_tags(struct ref *src, struct ref **dst, struct ref ***ds /* Add it in */ dst_ref = make_linked_ref(ref->name, dst_tail); - hashcpy(dst_ref->new_sha1, ref->new_sha1); + oidcpy(&dst_ref->new_oid, &ref->new_oid); dst_ref->peer_ref = copy_ref(ref); } } @@ -1517,7 +1481,7 @@ int match_push_refs(struct ref *src, struct ref **dst, /* Create a new one and link it */ dst_peer = make_linked_ref(dst_name, &dst_tail); - hashcpy(dst_peer->new_sha1, ref->new_sha1); + oidcpy(&dst_peer->new_oid, &ref->new_oid); string_list_insert(&dst_ref_index, dst_peer->name)->util = dst_peer; } @@ -1569,13 +1533,13 @@ void set_ref_status_for_push(struct ref *remote_refs, int send_mirror, int reject_reason = 0; if (ref->peer_ref) - hashcpy(ref->new_sha1, ref->peer_ref->new_sha1); + oidcpy(&ref->new_oid, &ref->peer_ref->new_oid); else if (!send_mirror) continue; - ref->deletion = is_null_sha1(ref->new_sha1); + ref->deletion = is_null_oid(&ref->new_oid); if (!ref->deletion && - !hashcmp(ref->old_sha1, ref->new_sha1)) { + !oidcmp(&ref->old_oid, &ref->new_oid)) { ref->status = REF_STATUS_UPTODATE; continue; } @@ -1594,7 +1558,7 @@ void set_ref_status_for_push(struct ref *remote_refs, int send_mirror, */ if (ref->expect_old_sha1) { if (ref->expect_old_no_trackback || - hashcmp(ref->old_sha1, ref->old_sha1_expect)) + oidcmp(&ref->old_oid, &ref->old_oid_expect)) reject_reason = REF_STATUS_REJECT_STALE; } @@ -1618,15 +1582,15 @@ void set_ref_status_for_push(struct ref *remote_refs, int send_mirror, * passing the --force argument */ - else if (!ref->deletion && !is_null_sha1(ref->old_sha1)) { + else if (!ref->deletion && !is_null_oid(&ref->old_oid)) { if (starts_with(ref->name, "refs/tags/")) reject_reason = REF_STATUS_REJECT_ALREADY_EXISTS; - else if (!has_sha1_file(ref->old_sha1)) + else if (!has_object_file(&ref->old_oid)) reject_reason = REF_STATUS_REJECT_FETCH_FIRST; - else if (!lookup_commit_reference_gently(ref->old_sha1, 1) || - !lookup_commit_reference_gently(ref->new_sha1, 1)) + else if (!lookup_commit_reference_gently(ref->old_oid.hash, 1) || + !lookup_commit_reference_gently(ref->new_oid.hash, 1)) reject_reason = REF_STATUS_REJECT_NEEDS_FORCE; - else if (!ref_newer(ref->new_sha1, ref->old_sha1)) + else if (!ref_newer(&ref->new_oid, &ref->old_oid)) reject_reason = REF_STATUS_REJECT_NONFASTFORWARD; } @@ -1645,7 +1609,7 @@ static void set_merge(struct branch *ret) { struct remote *remote; char *ref; - unsigned char sha1[20]; + struct object_id oid; int i; if (!ret) @@ -1671,7 +1635,7 @@ static void set_merge(struct branch *ret) strcmp(ret->remote_name, ".")) continue; if (dwim_ref(ret->merge_name[i], strlen(ret->merge_name[i]), - sha1, &ref) == 1) + oid.hash, &ref) == 1) ret->merge[i]->dst = ref; else ret->merge[i]->dst = xstrdup(ret->merge_name[i]); @@ -1831,10 +1795,10 @@ const char *branch_get_push(struct branch *branch, struct strbuf *err) static int ignore_symref_update(const char *refname) { - unsigned char sha1[20]; + struct object_id oid; int flag; - if (!resolve_ref_unsafe(refname, 0, sha1, &flag)) + if (!resolve_ref_unsafe(refname, 0, oid.hash, &flag)) return 0; /* non-existing refs are OK */ return (flag & REF_ISSYMREF); } @@ -1925,7 +1889,7 @@ int get_fetch_map(const struct ref *remote_refs, if (refspec->exact_sha1) { ref_map = alloc_ref(name); - get_sha1_hex(name, ref_map->old_sha1); + get_oid_hex(name, &ref_map->old_oid); } else { ref_map = get_remote_ref(remote_refs, name); } @@ -1966,7 +1930,7 @@ int resolve_remote_symref(struct ref *ref, struct ref *list) return 0; for (; list; list = list->next) if (!strcmp(ref->symref, list->name)) { - hashcpy(ref->old_sha1, list->old_sha1); + oidcpy(&ref->old_oid, &list->old_oid); return 0; } return 1; @@ -1980,7 +1944,7 @@ static void unmark_and_free(struct commit_list *list, unsigned int mark) } } -int ref_newer(const unsigned char *new_sha1, const unsigned char *old_sha1) +int ref_newer(const struct object_id *new_oid, const struct object_id *old_oid) { struct object *o; struct commit *old, *new; @@ -1991,12 +1955,12 @@ int ref_newer(const unsigned char *new_sha1, const unsigned char *old_sha1) * Both new and old must be commit-ish and new is descendant of * old. Otherwise we require --force. */ - o = deref_tag(parse_object(old_sha1), NULL, 0); + o = deref_tag(parse_object(old_oid->hash), NULL, 0); if (!o || o->type != OBJ_COMMIT) return 0; old = (struct commit *) o; - o = deref_tag(parse_object(new_sha1), NULL, 0); + o = deref_tag(parse_object(new_oid->hash), NULL, 0); if (!o || o->type != OBJ_COMMIT) return 0; new = (struct commit *) o; @@ -2031,12 +1995,11 @@ int ref_newer(const unsigned char *new_sha1, const unsigned char *old_sha1) int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs, const char **upstream_name) { - unsigned char sha1[20]; + struct object_id oid; struct commit *ours, *theirs; - char symmetric[84]; struct rev_info revs; - const char *rev_argv[10], *base; - int rev_argc; + const char *base; + struct argv_array argv = ARGV_ARRAY_INIT; /* Cannot stat unless we are marked to build on top of somebody else. */ base = branch_get_upstream(branch, NULL); @@ -2046,15 +2009,15 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs, return -1; /* Cannot stat if what we used to build on no longer exists */ - if (read_ref(base, sha1)) + if (read_ref(base, oid.hash)) return -1; - theirs = lookup_commit_reference(sha1); + theirs = lookup_commit_reference(oid.hash); if (!theirs) return -1; - if (read_ref(branch->refname, sha1)) + if (read_ref(branch->refname, oid.hash)) return -1; - ours = lookup_commit_reference(sha1); + ours = lookup_commit_reference(oid.hash); if (!ours) return -1; @@ -2065,19 +2028,15 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs, } /* Run "rev-list --left-right ours...theirs" internally... */ - rev_argc = 0; - rev_argv[rev_argc++] = NULL; - rev_argv[rev_argc++] = "--left-right"; - rev_argv[rev_argc++] = symmetric; - rev_argv[rev_argc++] = "--"; - rev_argv[rev_argc] = NULL; - - strcpy(symmetric, sha1_to_hex(ours->object.sha1)); - strcpy(symmetric + 40, "..."); - strcpy(symmetric + 43, sha1_to_hex(theirs->object.sha1)); + argv_array_push(&argv, ""); /* ignored */ + argv_array_push(&argv, "--left-right"); + argv_array_pushf(&argv, "%s...%s", + oid_to_hex(&ours->object.oid), + oid_to_hex(&theirs->object.oid)); + argv_array_push(&argv, "--"); init_revisions(&revs, NULL); - setup_revisions(rev_argc, rev_argv, &revs, NULL); + setup_revisions(argv.argc, argv.argv, &revs, NULL); if (prepare_revision_walk(&revs)) die("revision walk setup failed"); @@ -2097,6 +2056,8 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs, /* clear object flags smudged by the above traversal */ clear_commit_marks(ours, ALL_REV_FLAGS); clear_commit_marks(theirs, ALL_REV_FLAGS); + + argv_array_clear(&argv); return 0; } @@ -2179,7 +2140,7 @@ static int one_local_ref(const char *refname, const struct object_id *oid, len = strlen(refname) + 1; ref = xcalloc(1, sizeof(*ref) + len); - hashcpy(ref->new_sha1, oid->hash); + oidcpy(&ref->new_oid, oid); memcpy(ref->name, refname, len); **local_tail = ref; *local_tail = &ref->next; @@ -2216,7 +2177,7 @@ struct ref *guess_remote_head(const struct ref *head, /* If refs/heads/master could be right, it is. */ if (!all) { r = find_ref_by_name(refs, "refs/heads/master"); - if (r && !hashcmp(r->old_sha1, head->old_sha1)) + if (r && !oidcmp(&r->old_oid, &head->old_oid)) return copy_ref(r); } @@ -2224,7 +2185,7 @@ struct ref *guess_remote_head(const struct ref *head, for (r = refs; r; r = r->next) { if (r != head && starts_with(r->name, "refs/heads/") && - !hashcmp(r->old_sha1, head->old_sha1)) { + !oidcmp(&r->old_oid, &head->old_oid)) { *tail = copy_ref(r); tail = &((*tail)->next); if (!all) @@ -2272,7 +2233,7 @@ static int get_stale_heads_cb(const char *refname, const struct object_id *oid, if (stale) { struct ref *ref = make_linked_ref(refname, &info->stale_refs_tail); - hashcpy(ref->new_sha1, oid->hash); + oidcpy(&ref->new_oid, oid); } clean_exit: @@ -2367,14 +2328,14 @@ int is_empty_cas(const struct push_cas_option *cas) * If we cannot do so, return negative to signal an error. */ static int remote_tracking(struct remote *remote, const char *refname, - unsigned char sha1[20]) + struct object_id *oid) { char *dst; dst = apply_refspecs(remote->fetch, remote->fetch_refspec_nr, refname); if (!dst) return -1; /* no tracking ref for refname at remote */ - if (read_ref(dst, sha1)) + if (read_ref(dst, oid->hash)) return -1; /* we know what the tracking ref is but we cannot read it */ return 0; } @@ -2392,8 +2353,8 @@ static void apply_cas(struct push_cas_option *cas, continue; ref->expect_old_sha1 = 1; if (!entry->use_tracking) - hashcpy(ref->old_sha1_expect, cas->entry[i].expect); - else if (remote_tracking(remote, ref->name, ref->old_sha1_expect)) + hashcpy(ref->old_oid_expect.hash, cas->entry[i].expect); + else if (remote_tracking(remote, ref->name, &ref->old_oid_expect)) ref->expect_old_no_trackback = 1; return; } @@ -2403,7 +2364,7 @@ static void apply_cas(struct push_cas_option *cas, return; ref->expect_old_sha1 = 1; - if (remote_tracking(remote, ref->name, ref->old_sha1_expect)) + if (remote_tracking(remote, ref->name, &ref->old_oid_expect)) ref->expect_old_no_trackback = 1; } |