@matheusbsbs / matheus-bagliano / commits / eb05349247
Fork 0

http: refactor finish_http_pack_request()

finish_http_pack_request() does multiple tasks, including some housekeeping on a struct packed_git - (1) closing its index, (2) removing it from a list, and (3) installing it. These concerns are independent of fetching a pack through HTTP: they are there only because (1) the calling code opens the pack's index before deciding to fetch it, (2) the calling code maintains a list of packfiles that can be fetched, and (3) the calling code fetches it in order to make use of its objects in the same process. In preparation for a subsequent commit, which adds a feature that does not need any of this housekeeping, remove (1), (2), and (3) from finish_http_pack_request(). (2) and (3) are now done by a helper function, and (1) is the responsibility of the caller (in this patch, done closer to the point where the pack index is opened). Signed-off-by: Jonathan Tan <jonathantanmy@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jonathan Tan committed Jun 10, 2020 at 13:57 UTC eb05349247415992644fc63ba0cf0c4821d4eef2
4 files changed +35 -22
http-push.c
+6 -2
@@ -117,6 +117,7 @@ enum transfer_state {
117
118 struct transfer_request {
119 struct object *obj;
120 + struct packed_git *target;
121 char *url;
122 char *dest;
123 struct remote_lock *lock;
@@ -314,17 +315,18 @@ static void start_fetch_packed(struct transfer_request *request)
315 release_request(request);
316 return;
317 }
318 + close_pack_index(target);
319 + request->target = target;
320
321 fprintf(stderr, "Fetching pack %s\n",
322 hash_to_hex(target->hash));
323 fprintf(stderr, " which contains %s\n", oid_to_hex(&request->obj->oid));
324
322 - preq = new_http_pack_request(target, repo->url);
325 + preq = new_http_pack_request(target->hash, repo->url);
326 if (preq == NULL) {
327 repo->can_update_info_refs = 0;
328 return;
329 }
327 - preq->lst = &repo->packs;
330
331 /* Make sure there isn't another open request for this pack */
332 while (check_request) {
@@ -597,6 +599,8 @@ static void finish_request(struct transfer_request *request)
599 }
600 if (fail)
601 repo->can_update_info_refs = 0;
602 + else
603 + http_install_packfile(request->target, &repo->packs);
604 release_request(request);
605 }
606 }
http-walker.c
+3 -2
@@ -439,6 +439,7 @@ static int http_fetch_pack(struct walker *walker, struct alt_base *repo, unsigne
439 target = find_sha1_pack(sha1, repo->packs);
440 if (!target)
441 return -1;
442 + close_pack_index(target);
443
444 if (walker->get_verbosely) {
445 fprintf(stderr, "Getting pack %s\n",
@@ -447,10 +448,9 @@ static int http_fetch_pack(struct walker *walker, struct alt_base *repo, unsigne
448 hash_to_hex(sha1));
449 }
450
450 - preq = new_http_pack_request(target, repo->base);
451 + preq = new_http_pack_request(target->hash, repo->base);
452 if (preq == NULL)
453 goto abort;
453 - preq->lst = &repo->packs;
454 preq->slot->results = &results;
455
456 if (start_active_slot(preq->slot)) {
@@ -469,6 +469,7 @@ static int http_fetch_pack(struct walker *walker, struct alt_base *repo, unsigne
469 release_http_pack_request(preq);
470 if (ret)
471 return ret;
472 + http_install_packfile(target, &repo->packs);
473
474 return 0;
475
http.c
+16 -15
@@ -2268,22 +2268,13 @@ void release_http_pack_request(struct http_pack_request *preq)
2268
2269 int finish_http_pack_request(struct http_pack_request *preq)
2270 {
2271 - struct packed_git **lst;
2272 - struct packed_git *p = preq->target;
2271 struct child_process ip = CHILD_PROCESS_INIT;
2272 int tmpfile_fd;
2273 int ret = 0;
2274
2277 - close_pack_index(p);
2278 -
2275 fclose(preq->packfile);
2276 preq->packfile = NULL;
2277
2282 - lst = preq->lst;
2283 - while (*lst != p)
2284 - lst = &((*lst)->next);
2285 - *lst = (*lst)->next;
2286 -
2278 tmpfile_fd = xopen(preq->tmpfile.buf, O_RDONLY);
2279
2280 argv_array_push(&ip.args, "index-pack");
@@ -2297,15 +2288,26 @@ int finish_http_pack_request(struct http_pack_request *preq)
2288 goto cleanup;
2289 }
2290
2300 - install_packed_git(the_repository, p);
2291 cleanup:
2292 close(tmpfile_fd);
2293 unlink(preq->tmpfile.buf);
2294 return ret;
2295 }
2296
2297 +void http_install_packfile(struct packed_git *p,
2298 + struct packed_git **list_to_remove_from)
2299 +{
2300 + struct packed_git **lst = list_to_remove_from;
2301 +
2302 + while (*lst != p)
2303 + lst = &((*lst)->next);
2304 + *lst = (*lst)->next;
2305 +
2306 + install_packed_git(the_repository, p);
2307 +}
2308 +
2309 struct http_pack_request *new_http_pack_request(
2308 - struct packed_git *target, const char *base_url)
2310 + const unsigned char *packed_git_hash, const char *base_url)
2311 {
2312 off_t prev_posn = 0;
2313 struct strbuf buf = STRBUF_INIT;
@@ -2313,14 +2315,13 @@ struct http_pack_request *new_http_pack_request(
2315
2316 preq = xcalloc(1, sizeof(*preq));
2317 strbuf_init(&preq->tmpfile, 0);
2316 - preq->target = target;
2318
2319 end_url_with_slash(&buf, base_url);
2320 strbuf_addf(&buf, "objects/pack/pack-%s.pack",
2320 - hash_to_hex(target->hash));
2321 + hash_to_hex(packed_git_hash));
2322 preq->url = strbuf_detach(&buf, NULL);
2323
2323 - strbuf_addf(&preq->tmpfile, "%s.temp", sha1_pack_name(target->hash));
2324 + strbuf_addf(&preq->tmpfile, "%s.temp", sha1_pack_name(packed_git_hash));
2325 preq->packfile = fopen(preq->tmpfile.buf, "a");
2326 if (!preq->packfile) {
2327 error("Unable to open local file %s for pack",
@@ -2344,7 +2345,7 @@ struct http_pack_request *new_http_pack_request(
2345 if (http_is_verbose)
2346 fprintf(stderr,
2347 "Resuming fetch of pack %s at byte %"PRIuMAX"\n",
2347 - hash_to_hex(target->hash),
2348 + hash_to_hex(packed_git_hash),
2349 (uintmax_t)prev_posn);
2350 http_opt_request_remainder(preq->slot->curl, prev_posn);
2351 }
http.h
+10 -3
@@ -216,18 +216,25 @@ int http_get_info_packs(const char *base_url,
216
217 struct http_pack_request {
218 char *url;
219 - struct packed_git *target;
220 - struct packed_git **lst;
219 FILE *packfile;
220 struct strbuf tmpfile;
221 struct active_request_slot *slot;
222 };
223
224 struct http_pack_request *new_http_pack_request(
227 - struct packed_git *target, const char *base_url);
225 + const unsigned char *packed_git_hash, const char *base_url);
226 int finish_http_pack_request(struct http_pack_request *preq);
227 void release_http_pack_request(struct http_pack_request *preq);
228
229 +/*
230 + * Remove p from the given list, and invoke install_packed_git() on it.
231 + *
232 + * This is a convenience function for users that have obtained a list of packs
233 + * from http_get_info_packs() and have chosen a specific pack to fetch.
234 + */
235 +void http_install_packfile(struct packed_git *p,
236 + struct packed_git **list_to_remove_from);
237 +
238 /* Helpers for fetching object */
239 struct http_object_request {
240 char *url;