http*: add http_get_info_packs
PATCH 15/18 http*: add http_get_info_packs
http-push.c and http-walker.c no longer have to use fetch_index or setup_index; they simply need to use http_get_info_packs, a new http method, in their fetch_indices implementations.
The code handling the fetching of pack indices in http-push.c and http-walker.c, namely, the fetch_index and setup_index methods, have been refactored and moved to http.c.
fetch_index has been moved to http.c, and is not meant to be used outside of http.c. It invokes end_url_with_slash with base_url; apart from that change, the code is identical.
setup_index has been moved and renamed to fetch_http_pack_index.
--- http-push.c | 172 +----------------------------------------------------- http-walker.c | 179 ++------------------------------------------------------ http.c | 167 +++++++++++++++++++++++++++++++++++++++++++++++++++++ http.h | 4 + 4 files changed, 182 insertions(+), 340 deletions(-)
http-push.c +4 -168
@@ -950,182 +950,18 @@ static int add_send_request(struct object *obj, struct remote_lock *lock)return 1 ; } -static int fetch_index (unsigned char *sha1) -{ -int ret =0 ; -char *hex =xstrdup (sha1_to_hex (sha1)); -char *filename; -char *url; -char tmpfile[PATH_MAX ]; -long prev_posn =0 ; -char range[RANGE_HEADER_SIZE ]; -struct curl_slist *range_header =NULL ; - -FILE *indexfile; -struct active_request_slot *slot; -struct slot_results results; - -/* Don't use the index if the pack isn't there */ - url =xmalloc (strlen (repo->url) +64 ); -sprintf (url,"%sobjects/pack/pack-%s.pack" , repo->url, hex); - slot =get_active_slot (); - slot->results = &results; -curl_easy_setopt (slot->curl,CURLOPT_URL , url); -curl_easy_setopt (slot->curl,CURLOPT_NOBODY ,1 ); -if (start_active_slot (slot)) { -run_active_slot (slot); -if (results.curl_result !=CURLE_OK ) { - ret =error ("Unable to verify pack %s is available" , - hex); - goto cleanup_pack; - } - }else { - ret =error ("Unable to start request" ); - goto cleanup_pack; - } - -if (has_pack_index (sha1)) { - ret =0 ; - goto cleanup_pack; - } - -if (push_verbosely) -fprintf (stderr,"Getting index for pack %s\n" , hex); - -sprintf (url,"%sobjects/pack/pack-%s.idx" , repo->url, hex); - - filename =sha1_pack_index_name (sha1); -snprintf (tmpfile,sizeof (tmpfile),"%s.temp" , filename); - indexfile =fopen (tmpfile,"a" ); -if (!indexfile) { - ret =error ("Unable to open local file %s for pack index" , - tmpfile); - goto cleanup_pack; - } - - slot =get_active_slot (); - slot->results = &results; -curl_easy_setopt (slot->curl,CURLOPT_NOBODY ,0 ); -curl_easy_setopt (slot->curl,CURLOPT_HTTPGET ,1 ); -curl_easy_setopt (slot->curl,CURLOPT_FILE , indexfile); -curl_easy_setopt (slot->curl,CURLOPT_WRITEFUNCTION , fwrite); -curl_easy_setopt (slot->curl,CURLOPT_URL , url); -curl_easy_setopt (slot->curl,CURLOPT_HTTPHEADER , no_pragma_header); - slot->local = indexfile; - -/* -* If there is data present from a previous transfer attempt, -* resume where it left off -*/ - prev_posn =ftell (indexfile); -if (prev_posn>0 ) { -if (push_verbosely) -fprintf (stderr, -"Resuming fetch of index for pack %s at byte %ld\n" , - hex, prev_posn); -sprintf (range,"Range: bytes=%ld-" , prev_posn); - range_header =curl_slist_append (range_header, range); -curl_easy_setopt (slot->curl,CURLOPT_HTTPHEADER , range_header); - } - -if (start_active_slot (slot)) { -run_active_slot (slot); -if (results.curl_result !=CURLE_OK ) { - ret =error ("Unable to get pack index %s\n%s" , url, - curl_errorstr); - goto cleanup_index; - } - }else { - ret =error ("Unable to start request" ); - goto cleanup_index; - } - - ret =move_temp_to_file (tmpfile, filename); - -cleanup_index: -fclose (indexfile); -cleanup_pack: -free (url); -free (hex); -return ret; -} - -static int setup_index (unsigned char *sha1) -{ -struct packed_git *new_pack; - -if (fetch_index (sha1)) -return -1 ; - - new_pack =parse_pack_index (sha1); -if (!new_pack) -return -1 ;/* parse_pack_index() already issued error message */ - new_pack->next = repo->packs; - repo->packs = new_pack; -return 0 ; -} -static int fetch_indices (void ) { -unsigned char sha1[20 ]; -char *url; -struct strbuf buffer =STRBUF_INIT ; -char *data; -int i =0 ; - -struct active_request_slot *slot;struct slot_results results;if (push_verbosely)fprintf (stderr,"Getting pack list\n" ); - url =xmalloc (strlen (repo->url) +20 ); -sprintf (url,"%sobjects/info/packs" , repo->url); - - slot =get_active_slot (); - slot->results = &results; -curl_easy_setopt (slot->curl,CURLOPT_FILE , &buffer); -curl_easy_setopt (slot->curl,CURLOPT_WRITEFUNCTION , fwrite_buffer); -curl_easy_setopt (slot->curl,CURLOPT_URL , url); -curl_easy_setopt (slot->curl,CURLOPT_HTTPHEADER ,NULL ); -if (start_active_slot (slot)) { -run_active_slot (slot); -if (results.curl_result !=CURLE_OK ) { -strbuf_release (&buffer); -free (url); -if (results.http_code ==404 ) -return 0 ; -else -return error ("%s" , curl_errorstr); - } - }else { -strbuf_release (&buffer); -free (url); -return error ("Unable to start request" ); - } -free (url); - - data = buffer.buf; -while (i < buffer.len) { -switch (data[i]) { -case 'P' : - i++; -if (i +52 < buffer.len && - !prefixcmp (data + i," pack-" ) && - !prefixcmp (data + i +46 ,".pack\n" )) { -get_sha1_hex (data + i +6 , sha1); -setup_index (sha1); - i +=51 ; -break ; - } -default : -while (data[i] !='\n' ) - i++; - } - i++; - } +if (http_get_info_packs (repo->url, &repo->packs) ==HTTP_OK ) +return 0 ; +else +return -1 ; -strbuf_release (&buffer);return 0 ; }
http-walker.c +7 -172
201 lines not on this page · raw message
http.c +167 -0
170 lines not on this page · raw message
http.h +4 -0
@@ -146,4 +146,8 @@ int http_error(const char *url, int ret);extern int http_fetch_ref (const char *base,struct ref *ref); +/* Helpers for fetching packs */ +extern int http_get_info_packs (const char *base_url, +struct packed_git **packs_head); +#endif /* HTTP_H */
--
1.6.3.1
Quoted 3 lines · Tay Ray Chuan
fetch_indexhas been moved tohttp.c, and is not meant to be used outside ofhttp.c. It invokesend_url_with_slashwithbase_url; apart from that change, the code is identical.
Move fetch_index() to http.c; this is not meant to be ...
Quoted 1 line
setup_indexhas been moved and renamed tofetch_http_pack_index.
Move setup_index() to http.c, and rename it to fetch_http_pack_index(), as it now is a library function and there are many other kinds of "index" in git.
It makes me wonder if fetch_index(), even though it is static to http.c, should be renamed somehow to reduce confusion between it and fetch_http_pack_index().
You have decl-after-statement and a handful of variable that are no longer used. Please squash this in when rerolling.
http-push.c | 2 -- http-walker.c | 1 - http.c | 10 +++------- 3 files changed, 3 insertions(+), 10 deletions(-)
http-push.c +0 -2
@@ -952,8 +952,6 @@ static int add_send_request(struct object *obj, struct remote_lock *lock)static int fetch_indices (void ) { -struct slot_results results; -if (push_verbosely)fprintf (stderr,"Getting pack list\n" );
http-walker.c +0 -1
@@ -567,7 +567,6 @@ static void fetch_alternates(struct walker *walker, const char *base)static int fetch_indices (struct walker *walker,struct alt_base *repo) {int ret =0 ; -struct slot_results results;if (repo->got_indices)return 0 ;
http.c +3 -7
@@ -914,10 +914,10 @@ int fetch_http_pack_index(struct packed_git **packs_head, unsigned char *sha1,int http_get_info_packs (const char *base_url,struct packed_git **packs_head) { -int ret =0 ; -char *url; +int ret =0 , i =0 ; +char *url, *data;struct strbuf buf =STRBUF_INIT ; -struct http_info_packs_request *req; +unsigned char sha1[20 ];end_url_with_slash (&buf, base_url);strbuf_addstr (&buf,"objects/info/packs" );
@@ -927,10 +927,6 @@ int http_get_info_packs(const char *base_url, struct packed_git **packs_head)if (ret !=HTTP_OK ) goto cleanup; -char *data; -int i =0 ; -unsigned char sha1[20 ]; - data = buf.buf;while (i < buf.len) {switch (data[i]) {
Hi,
Quoted 3 lines · Junio C Hamano
It makes me wonder if
fetch_index(), even though it is static tohttp.c, should be renamed somehow to reduce confusion between it andfetch_http_pack_index().
Indeed. What I'm now doing is
fetch_index --> fetch_pack_indexsetup_index --> fetch_and_setup_pack_index
Quoted 7 lines
You have decl-after-statement and a handful of variable that are no longer used. Please squash this in when rerolling.
http-push.c | 2 --
http-walker.c | 1 -
http.c | 10 +++-------3 files changed, 3 insertions(+), 10 deletions(-)
Thanks for including a patch.
Signature · 2 lines
Cheers, Ray Chuan