Make walker.fetch_ref() take a struct ref.

This simplifies a few things, makes a few things slightly more
complicated, but, more importantly, allows that, when struct ref can
represent a symref, http_fetch_ref() can return one.

Incidentally makes the string that http_fetch_ref() gets include "refs/"
(if appropriate), because that's how the name field of struct ref works.
As far as I can tell, the usage in walker:interpret_target() wouldn't have
worked previously, if it ever would have been used, which it wouldn't
(since the fetch process uses the hash instead of the name of the ref
there).

Signed-off-by: Daniel Barkalow <barkalow@iabervon.org>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Daniel Barkalow 2008-04-26 15:53:09 -04:00 committed by Junio C Hamano
parent 36c79d2bf8
commit c13b2633f4
6 changed files with 36 additions and 27 deletions

View File

@ -1759,15 +1759,16 @@ static int one_local_ref(const char *refname, const unsigned char *sha1, int fla
static void one_remote_ref(char *refname) static void one_remote_ref(char *refname)
{ {
struct ref *ref; struct ref *ref;
unsigned char remote_sha1[20];
struct object *obj; struct object *obj;
int len = strlen(refname) + 1;
if (http_fetch_ref(remote->url, refname + 5 /* "refs/" */, ref = alloc_ref(strlen(refname) + 1);
remote_sha1) != 0) { strcpy(ref->name, refname);
if (http_fetch_ref(remote->url, ref) != 0) {
fprintf(stderr, fprintf(stderr,
"Unable to fetch ref %s from %s\n", "Unable to fetch ref %s from %s\n",
refname, remote->url); refname, remote->url);
free(ref);
return; return;
} }
@ -1775,18 +1776,15 @@ static void one_remote_ref(char *refname)
* Fetch a copy of the object if it doesn't exist locally - it * Fetch a copy of the object if it doesn't exist locally - it
* may be required for updating server info later. * may be required for updating server info later.
*/ */
if (remote->can_update_info_refs && !has_sha1_file(remote_sha1)) { if (remote->can_update_info_refs && !has_sha1_file(ref->old_sha1)) {
obj = lookup_unknown_object(remote_sha1); obj = lookup_unknown_object(ref->old_sha1);
if (obj) { if (obj) {
fprintf(stderr, " fetch %s for %s\n", fprintf(stderr, " fetch %s for %s\n",
sha1_to_hex(remote_sha1), refname); sha1_to_hex(ref->old_sha1), refname);
add_fetch_request(obj); add_fetch_request(obj);
} }
} }
ref = xcalloc(1, sizeof(*ref) + len);
hashcpy(ref->old_sha1, remote_sha1);
memcpy(ref->name, refname, len);
*remote_tail = ref; *remote_tail = ref;
remote_tail = &ref->next; remote_tail = &ref->next;
} }
@ -1891,33 +1889,37 @@ static void mark_edges_uninteresting(struct commit_list *list)
static void add_remote_info_ref(struct remote_ls_ctx *ls) static void add_remote_info_ref(struct remote_ls_ctx *ls)
{ {
struct strbuf *buf = (struct strbuf *)ls->userData; struct strbuf *buf = (struct strbuf *)ls->userData;
unsigned char remote_sha1[20];
struct object *o; struct object *o;
int len; int len;
char *ref_info; char *ref_info;
struct ref *ref;
if (http_fetch_ref(remote->url, ls->dentry_name + 5 /* "refs/" */, ref = alloc_ref(strlen(ls->dentry_name) + 1);
remote_sha1) != 0) { strcpy(ref->name, ls->dentry_name);
if (http_fetch_ref(remote->url, ref) != 0) {
fprintf(stderr, fprintf(stderr,
"Unable to fetch ref %s from %s\n", "Unable to fetch ref %s from %s\n",
ls->dentry_name, remote->url); ls->dentry_name, remote->url);
aborted = 1; aborted = 1;
free(ref);
return; return;
} }
o = parse_object(remote_sha1); o = parse_object(ref->old_sha1);
if (!o) { if (!o) {
fprintf(stderr, fprintf(stderr,
"Unable to parse object %s for remote ref %s\n", "Unable to parse object %s for remote ref %s\n",
sha1_to_hex(remote_sha1), ls->dentry_name); sha1_to_hex(ref->old_sha1), ls->dentry_name);
aborted = 1; aborted = 1;
free(ref);
return; return;
} }
len = strlen(ls->dentry_name) + 42; len = strlen(ls->dentry_name) + 42;
ref_info = xcalloc(len + 1, 1); ref_info = xcalloc(len + 1, 1);
sprintf(ref_info, "%s %s\n", sprintf(ref_info, "%s %s\n",
sha1_to_hex(remote_sha1), ls->dentry_name); sha1_to_hex(ref->old_sha1), ls->dentry_name);
fwrite_buffer(ref_info, 1, len, buf); fwrite_buffer(ref_info, 1, len, buf);
free(ref_info); free(ref_info);
@ -1932,6 +1934,7 @@ static void add_remote_info_ref(struct remote_ls_ctx *ls)
free(ref_info); free(ref_info);
} }
} }
free(ref);
} }
static void update_remote_info_refs(struct remote_lock *lock) static void update_remote_info_refs(struct remote_lock *lock)

View File

@ -888,10 +888,10 @@ static int fetch(struct walker *walker, unsigned char *sha1)
data->alt->base); data->alt->base);
} }
static int fetch_ref(struct walker *walker, char *ref, unsigned char *sha1) static int fetch_ref(struct walker *walker, struct ref *ref)
{ {
struct walker_data *data = walker->data; struct walker_data *data = walker->data;
return http_fetch_ref(data->alt->base, ref, sha1); return http_fetch_ref(data->alt->base, ref);
} }
static void cleanup(struct walker *walker) static void cleanup(struct walker *walker)

13
http.c
View File

@ -589,8 +589,9 @@ static char *quote_ref_url(const char *base, const char *ref)
len += 2; /* extra two hex plus replacement % */ len += 2; /* extra two hex plus replacement % */
qref = xmalloc(len); qref = xmalloc(len);
memcpy(qref, base, baselen); memcpy(qref, base, baselen);
memcpy(qref + baselen, "/refs/", 6); dp = qref + baselen;
for (cp = ref, dp = qref + baselen + 6; (ch = *cp) != 0; cp++) { *(dp++) = '/';
for (cp = ref; (ch = *cp) != 0; cp++) {
if (needs_quote(ch)) { if (needs_quote(ch)) {
*dp++ = '%'; *dp++ = '%';
*dp++ = hex((ch >> 4) & 0xF); *dp++ = hex((ch >> 4) & 0xF);
@ -604,7 +605,7 @@ static char *quote_ref_url(const char *base, const char *ref)
return qref; return qref;
} }
int http_fetch_ref(const char *base, const char *ref, unsigned char *sha1) int http_fetch_ref(const char *base, struct ref *ref)
{ {
char *url; char *url;
struct strbuf buffer = STRBUF_INIT; struct strbuf buffer = STRBUF_INIT;
@ -612,7 +613,7 @@ int http_fetch_ref(const char *base, const char *ref, unsigned char *sha1)
struct slot_results results; struct slot_results results;
int ret; int ret;
url = quote_ref_url(base, ref); url = quote_ref_url(base, ref->name);
slot = get_active_slot(); slot = get_active_slot();
slot->results = &results; slot->results = &results;
curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer); curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
@ -624,12 +625,12 @@ int http_fetch_ref(const char *base, const char *ref, unsigned char *sha1)
if (results.curl_result == CURLE_OK) { if (results.curl_result == CURLE_OK) {
strbuf_rtrim(&buffer); strbuf_rtrim(&buffer);
if (buffer.len == 40) if (buffer.len == 40)
ret = get_sha1_hex(buffer.buf, sha1); ret = get_sha1_hex(buffer.buf, ref->old_sha1);
else else
ret = 1; ret = 1;
} else { } else {
ret = error("Couldn't get %s for %s\n%s", ret = error("Couldn't get %s for %s\n%s",
url, ref, curl_errorstr); url, ref->name, curl_errorstr);
} }
} else { } else {
ret = error("Unable to start request"); ret = error("Unable to start request");

2
http.h
View File

@ -105,6 +105,6 @@ static inline int missing__target(int code, int result)
#define missing_target(a) missing__target((a)->http_code, (a)->curl_result) #define missing_target(a) missing__target((a)->http_code, (a)->curl_result)
extern int http_fetch_ref(const char *base, const char *ref, unsigned char *sha1); extern int http_fetch_ref(const char *base, struct ref *ref);
#endif /* HTTP_H */ #endif /* HTTP_H */

View File

@ -190,9 +190,14 @@ static int interpret_target(struct walker *walker, char *target, unsigned char *
if (!get_sha1_hex(target, sha1)) if (!get_sha1_hex(target, sha1))
return 0; return 0;
if (!check_ref_format(target)) { if (!check_ref_format(target)) {
if (!walker->fetch_ref(walker, target, sha1)) { struct ref *ref = alloc_ref(strlen(target));
strcpy(ref->name, target);
if (!walker->fetch_ref(walker, ref)) {
hashcpy(sha1, ref->old_sha1);
free(ref);
return 0; return 0;
} }
free(ref);
} }
return -1; return -1;
} }

View File

@ -5,7 +5,7 @@
struct walker { struct walker {
void *data; void *data;
int (*fetch_ref)(struct walker *, char *ref, unsigned char *sha1); int (*fetch_ref)(struct walker *, struct ref *ref);
void (*prefetch)(struct walker *, unsigned char *sha1); void (*prefetch)(struct walker *, unsigned char *sha1);
int (*fetch)(struct walker *, unsigned char *sha1); int (*fetch)(struct walker *, unsigned char *sha1);
void (*cleanup)(struct walker *); void (*cleanup)(struct walker *);