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>
Daniel Barkalow committed
Apr 26, 2008 at 15:53 UTC
c13b2633f49e3e61b37973204793a4d9ef981175
6 files changed
+36
-27
http-push.c
+19
-16
@@ -1759,15 +1759,16 @@ static int one_local_ref(const char *refname, const unsigned char *sha1, int fla
1759
static void one_remote_ref(char *refname)
1760
{
1761
struct ref *ref;
1762
- unsigned char remote_sha1[20];
1762
struct object *obj;
1764
- int len = strlen(refname) + 1;
1763
1766
- if (http_fetch_ref(remote->url, refname + 5 /* "refs/" */,
1767
- remote_sha1) != 0) {
1764
+ ref = alloc_ref(strlen(refname) + 1);
1765
+ strcpy(ref->name, refname);
1766
+
1767
+ if (http_fetch_ref(remote->url, ref) != 0) {
1768
fprintf(stderr,
1769
"Unable to fetch ref %s from %s\n",
1770
refname, remote->url);
1771
+ free(ref);
1772
return;
1773
}
1774
@@ -1775,18 +1776,15 @@ static void one_remote_ref(char *refname)
1776
* Fetch a copy of the object if it doesn't exist locally - it
1777
* may be required for updating server info later.
1778
*/
1778
- if (remote->can_update_info_refs && !has_sha1_file(remote_sha1)) {
1779
- obj = lookup_unknown_object(remote_sha1);
1779
+ if (remote->can_update_info_refs && !has_sha1_file(ref->old_sha1)) {
1780
+ obj = lookup_unknown_object(ref->old_sha1);
1781
if (obj) {
1782
fprintf(stderr, " fetch %s for %s\n",
1782
- sha1_to_hex(remote_sha1), refname);
1783
+ sha1_to_hex(ref->old_sha1), refname);
1784
add_fetch_request(obj);
1785
}
1786
}
1787
1787
- ref = xcalloc(1, sizeof(*ref) + len);
1788
- hashcpy(ref->old_sha1, remote_sha1);
1789
- memcpy(ref->name, refname, len);
1788
*remote_tail = ref;
1789
remote_tail = &ref->next;
1790
}
@@ -1891,33 +1889,37 @@ static void mark_edges_uninteresting(struct commit_list *list)
1889
static void add_remote_info_ref(struct remote_ls_ctx *ls)
1890
{
1891
struct strbuf *buf = (struct strbuf *)ls->userData;
1894
- unsigned char remote_sha1[20];
1892
struct object *o;
1893
int len;
1894
char *ref_info;
1895
+ struct ref *ref;
1896
+
1897
+ ref = alloc_ref(strlen(ls->dentry_name) + 1);
1898
+ strcpy(ref->name, ls->dentry_name);
1899
1899
- if (http_fetch_ref(remote->url, ls->dentry_name + 5 /* "refs/" */,
1900
- remote_sha1) != 0) {
1900
+ if (http_fetch_ref(remote->url, ref) != 0) {
1901
fprintf(stderr,
1902
"Unable to fetch ref %s from %s\n",
1903
ls->dentry_name, remote->url);
1904
aborted = 1;
1905
+ free(ref);
1906
return;
1907
}
1908
1908
- o = parse_object(remote_sha1);
1909
+ o = parse_object(ref->old_sha1);
1910
if (!o) {
1911
fprintf(stderr,
1912
"Unable to parse object %s for remote ref %s\n",
1912
- sha1_to_hex(remote_sha1), ls->dentry_name);
1913
+ sha1_to_hex(ref->old_sha1), ls->dentry_name);
1914
aborted = 1;
1915
+ free(ref);
1916
return;
1917
}
1918
1919
len = strlen(ls->dentry_name) + 42;
1920
ref_info = xcalloc(len + 1, 1);
1921
sprintf(ref_info, "%s %s\n",
1920
- sha1_to_hex(remote_sha1), ls->dentry_name);
1922
+ sha1_to_hex(ref->old_sha1), ls->dentry_name);
1923
fwrite_buffer(ref_info, 1, len, buf);
1924
free(ref_info);
1925
@@ -1932,6 +1934,7 @@ static void add_remote_info_ref(struct remote_ls_ctx *ls)
1934
free(ref_info);
1935
}
1936
}
1937
+ free(ref);
1938
}
1939
1940
static void update_remote_info_refs(struct remote_lock *lock)
http-walker.c
+2
-2
@@ -888,10 +888,10 @@ static int fetch(struct walker *walker, unsigned char *sha1)
888
data->alt->base);
889
}
890
891
-static int fetch_ref(struct walker *walker, char *ref, unsigned char *sha1)
891
+static int fetch_ref(struct walker *walker, struct ref *ref)
892
{
893
struct walker_data *data = walker->data;
894
- return http_fetch_ref(data->alt->base, ref, sha1);
894
+ return http_fetch_ref(data->alt->base, ref);
895
}
896
897
static void cleanup(struct walker *walker)
http.c
+7
-6
@@ -589,8 +589,9 @@ static char *quote_ref_url(const char *base, const char *ref)
589
len += 2; /* extra two hex plus replacement % */
590
qref = xmalloc(len);
591
memcpy(qref, base, baselen);
592
- memcpy(qref + baselen, "/refs/", 6);
593
- for (cp = ref, dp = qref + baselen + 6; (ch = *cp) != 0; cp++) {
592
+ dp = qref + baselen;
593
+ *(dp++) = '/';
594
+ for (cp = ref; (ch = *cp) != 0; cp++) {
595
if (needs_quote(ch)) {
596
*dp++ = '%';
597
*dp++ = hex((ch >> 4) & 0xF);
@@ -604,7 +605,7 @@ static char *quote_ref_url(const char *base, const char *ref)
605
return qref;
606
}
607
607
-int http_fetch_ref(const char *base, const char *ref, unsigned char *sha1)
608
+int http_fetch_ref(const char *base, struct ref *ref)
609
{
610
char *url;
611
struct strbuf buffer = STRBUF_INIT;
@@ -612,7 +613,7 @@ int http_fetch_ref(const char *base, const char *ref, unsigned char *sha1)
613
struct slot_results results;
614
int ret;
615
615
- url = quote_ref_url(base, ref);
616
+ url = quote_ref_url(base, ref->name);
617
slot = get_active_slot();
618
slot->results = &results;
619
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)
625
if (results.curl_result == CURLE_OK) {
626
strbuf_rtrim(&buffer);
627
if (buffer.len == 40)
627
- ret = get_sha1_hex(buffer.buf, sha1);
628
+ ret = get_sha1_hex(buffer.buf, ref->old_sha1);
629
else
630
ret = 1;
631
} else {
632
ret = error("Couldn't get %s for %s\n%s",
632
- url, ref, curl_errorstr);
633
+ url, ref->name, curl_errorstr);
634
}
635
} else {
636
ret = error("Unable to start request");
http.h
+1
-1
@@ -105,6 +105,6 @@ static inline int missing__target(int code, int result)
105
106
#define missing_target(a) missing__target((a)->http_code, (a)->curl_result)
107
108
-extern int http_fetch_ref(const char *base, const char *ref, unsigned char *sha1);
108
+extern int http_fetch_ref(const char *base, struct ref *ref);
109
110
#endif /* HTTP_H */
walker.c
+6
-1
@@ -190,9 +190,14 @@ static int interpret_target(struct walker *walker, char *target, unsigned char *
190
if (!get_sha1_hex(target, sha1))
191
return 0;
192
if (!check_ref_format(target)) {
193
- if (!walker->fetch_ref(walker, target, sha1)) {
193
+ struct ref *ref = alloc_ref(strlen(target));
194
+ strcpy(ref->name, target);
195
+ if (!walker->fetch_ref(walker, ref)) {
196
+ hashcpy(sha1, ref->old_sha1);
197
+ free(ref);
198
return 0;
199
}
200
+ free(ref);
201
}
202
return -1;
203
}
walker.h
+1
-1
@@ -5,7 +5,7 @@
5
6
struct walker {
7
void *data;
8
- int (*fetch_ref)(struct walker *, char *ref, unsigned char *sha1);
8
+ int (*fetch_ref)(struct walker *, struct ref *ref);
9
void (*prefetch)(struct walker *, unsigned char *sha1);
10
int (*fetch)(struct walker *, unsigned char *sha1);
11
void (*cleanup)(struct walker *);