Skip to content

Commit eb05349

Browse files
jonathantanmygitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9cb3cab commit eb05349

File tree

4 files changed

+35
-22
lines changed

4 files changed

+35
-22
lines changed

http-push.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ enum transfer_state {
117117

118118
struct transfer_request {
119119
struct object *obj;
120+
struct packed_git *target;
120121
char *url;
121122
char *dest;
122123
struct remote_lock *lock;
@@ -314,17 +315,18 @@ static void start_fetch_packed(struct transfer_request *request)
314315
release_request(request);
315316
return;
316317
}
318+
close_pack_index(target);
319+
request->target = target;
317320

318321
fprintf(stderr, "Fetching pack %s\n",
319322
hash_to_hex(target->hash));
320323
fprintf(stderr, " which contains %s\n", oid_to_hex(&request->obj->oid));
321324

322-
preq = new_http_pack_request(target, repo->url);
325+
preq = new_http_pack_request(target->hash, repo->url);
323326
if (preq == NULL) {
324327
repo->can_update_info_refs = 0;
325328
return;
326329
}
327-
preq->lst = &repo->packs;
328330

329331
/* Make sure there isn't another open request for this pack */
330332
while (check_request) {
@@ -597,6 +599,8 @@ static void finish_request(struct transfer_request *request)
597599
}
598600
if (fail)
599601
repo->can_update_info_refs = 0;
602+
else
603+
http_install_packfile(request->target, &repo->packs);
600604
release_request(request);
601605
}
602606
}

http-walker.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,7 @@ static int http_fetch_pack(struct walker *walker, struct alt_base *repo, unsigne
439439
target = find_sha1_pack(sha1, repo->packs);
440440
if (!target)
441441
return -1;
442+
close_pack_index(target);
442443

443444
if (walker->get_verbosely) {
444445
fprintf(stderr, "Getting pack %s\n",
@@ -447,10 +448,9 @@ static int http_fetch_pack(struct walker *walker, struct alt_base *repo, unsigne
447448
hash_to_hex(sha1));
448449
}
449450

450-
preq = new_http_pack_request(target, repo->base);
451+
preq = new_http_pack_request(target->hash, repo->base);
451452
if (preq == NULL)
452453
goto abort;
453-
preq->lst = &repo->packs;
454454
preq->slot->results = &results;
455455

456456
if (start_active_slot(preq->slot)) {
@@ -469,6 +469,7 @@ static int http_fetch_pack(struct walker *walker, struct alt_base *repo, unsigne
469469
release_http_pack_request(preq);
470470
if (ret)
471471
return ret;
472+
http_install_packfile(target, &repo->packs);
472473

473474
return 0;
474475

http.c

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2268,22 +2268,13 @@ void release_http_pack_request(struct http_pack_request *preq)
22682268

22692269
int finish_http_pack_request(struct http_pack_request *preq)
22702270
{
2271-
struct packed_git **lst;
2272-
struct packed_git *p = preq->target;
22732271
struct child_process ip = CHILD_PROCESS_INIT;
22742272
int tmpfile_fd;
22752273
int ret = 0;
22762274

2277-
close_pack_index(p);
2278-
22792275
fclose(preq->packfile);
22802276
preq->packfile = NULL;
22812277

2282-
lst = preq->lst;
2283-
while (*lst != p)
2284-
lst = &((*lst)->next);
2285-
*lst = (*lst)->next;
2286-
22872278
tmpfile_fd = xopen(preq->tmpfile.buf, O_RDONLY);
22882279

22892280
argv_array_push(&ip.args, "index-pack");
@@ -2297,30 +2288,40 @@ int finish_http_pack_request(struct http_pack_request *preq)
22972288
goto cleanup;
22982289
}
22992290

2300-
install_packed_git(the_repository, p);
23012291
cleanup:
23022292
close(tmpfile_fd);
23032293
unlink(preq->tmpfile.buf);
23042294
return ret;
23052295
}
23062296

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+
23072309
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)
23092311
{
23102312
off_t prev_posn = 0;
23112313
struct strbuf buf = STRBUF_INIT;
23122314
struct http_pack_request *preq;
23132315

23142316
preq = xcalloc(1, sizeof(*preq));
23152317
strbuf_init(&preq->tmpfile, 0);
2316-
preq->target = target;
23172318

23182319
end_url_with_slash(&buf, base_url);
23192320
strbuf_addf(&buf, "objects/pack/pack-%s.pack",
2320-
hash_to_hex(target->hash));
2321+
hash_to_hex(packed_git_hash));
23212322
preq->url = strbuf_detach(&buf, NULL);
23222323

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));
23242325
preq->packfile = fopen(preq->tmpfile.buf, "a");
23252326
if (!preq->packfile) {
23262327
error("Unable to open local file %s for pack",
@@ -2344,7 +2345,7 @@ struct http_pack_request *new_http_pack_request(
23442345
if (http_is_verbose)
23452346
fprintf(stderr,
23462347
"Resuming fetch of pack %s at byte %"PRIuMAX"\n",
2347-
hash_to_hex(target->hash),
2348+
hash_to_hex(packed_git_hash),
23482349
(uintmax_t)prev_posn);
23492350
http_opt_request_remainder(preq->slot->curl, prev_posn);
23502351
}

http.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -216,18 +216,25 @@ int http_get_info_packs(const char *base_url,
216216

217217
struct http_pack_request {
218218
char *url;
219-
struct packed_git *target;
220-
struct packed_git **lst;
221219
FILE *packfile;
222220
struct strbuf tmpfile;
223221
struct active_request_slot *slot;
224222
};
225223

226224
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);
228226
int finish_http_pack_request(struct http_pack_request *preq);
229227
void release_http_pack_request(struct http_pack_request *preq);
230228

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+
231238
/* Helpers for fetching object */
232239
struct http_object_request {
233240
char *url;

0 commit comments

Comments
 (0)