git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v2 2/9] http: refactor finish_http_pack_request()

From
JTJonathan Tan <jonathantanmy@google.com>
Date
Jun 10, 2020, 20:57 UTC
Message-ID
<048f6845669570edbf7de3da7f496c4e96592aad.1591821067.git.jonathantanmy@google.com>
In-Reply-To
<cover.1591821067.git.jonathantanmy@google.com>

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>
---
 http-push.c   |  8 ++++++--
 http-walker.c |  5 +++--
 http.c        | 31 ++++++++++++++++---------------
 http.h        | 13 ++++++++++---
 4 files changed, 35 insertions(+), 22 deletions(-)
diff --git a/http-push.c b/http-push.c
index 822f326599..ac7868ffee 100644
--- a/http-push.c
+++ b/http-push.c
@@ -117,6 +117,7 @@ enum transfer_state {
 
 struct transfer_request {
 	struct object *obj;
+	struct packed_git *target;
 	char *url;
 	char *dest;
 	struct remote_lock *lock;
@@ -314,17 +315,18 @@ static void start_fetch_packed(struct transfer_request *request)
 		release_request(request);
 		return;
 	}
+	close_pack_index(target);
+	request->target = target;
 
 	fprintf(stderr,	"Fetching pack %s\n",
 		hash_to_hex(target->hash));
 	fprintf(stderr, " which contains %s\n", oid_to_hex(&request->obj->oid));
 
-	preq = new_http_pack_request(target, repo->url);
+	preq = new_http_pack_request(target->hash, repo->url);
 	if (preq == NULL) {
 		repo->can_update_info_refs = 0;
 		return;
 	}
-	preq->lst = &repo->packs;
 
 	/* Make sure there isn't another open request for this pack */
 	while (check_request) {
@@ -597,6 +599,8 @@ static void finish_request(struct transfer_request *request)
 		}
 		if (fail)
 			repo->can_update_info_refs = 0;
+		else
+			http_install_packfile(request->target, &repo->packs);
 		release_request(request);
 	}
 }
diff --git a/http-walker.c b/http-walker.c
index fe15e325fa..4fb1235cd4 100644
--- a/http-walker.c
+++ b/http-walker.c
@@ -439,6 +439,7 @@ static int http_fetch_pack(struct walker *walker, struct alt_base *repo, unsigne
 	target = find_sha1_pack(sha1, repo->packs);
 	if (!target)
 		return -1;
+	close_pack_index(target);
 
 	if (walker->get_verbosely) {
 		fprintf(stderr, "Getting pack %s\n",
@@ -447,10 +448,9 @@ static int http_fetch_pack(struct walker *walker, struct alt_base *repo, unsigne
 			hash_to_hex(sha1));
 	}
 
-	preq = new_http_pack_request(target, repo->base);
+	preq = new_http_pack_request(target->hash, repo->base);
 	if (preq == NULL)
 		goto abort;
-	preq->lst = &repo->packs;
 	preq->slot->results = &results;
 
 	if (start_active_slot(preq->slot)) {
@@ -469,6 +469,7 @@ static int http_fetch_pack(struct walker *walker, struct alt_base *repo, unsigne
 	release_http_pack_request(preq);
 	if (ret)
 		return ret;
+	http_install_packfile(target, &repo->packs);
 
 	return 0;
 
diff --git a/http.c b/http.c
index 39cbd56702..4f6e1fb018 100644
--- a/http.c
+++ b/http.c
@@ -2268,22 +2268,13 @@ void release_http_pack_request(struct http_pack_request *preq)
 
 int finish_http_pack_request(struct http_pack_request *preq)
 {
-	struct packed_git **lst;
-	struct packed_git *p = preq->target;
 	struct child_process ip = CHILD_PROCESS_INIT;
 	int tmpfile_fd;
 	int ret = 0;
 
-	close_pack_index(p);
-
 	fclose(preq->packfile);
 	preq->packfile = NULL;
 
-	lst = preq->lst;
-	while (*lst != p)
-		lst = &((*lst)->next);
-	*lst = (*lst)->next;
-
 	tmpfile_fd = xopen(preq->tmpfile.buf, O_RDONLY);
 
 	argv_array_push(&ip.args, "index-pack");
@@ -2297,15 +2288,26 @@ int finish_http_pack_request(struct http_pack_request *preq)
 		goto cleanup;
 	}
 
-	install_packed_git(the_repository, p);
 cleanup:
 	close(tmpfile_fd);
 	unlink(preq->tmpfile.buf);
 	return ret;
 }
 
+void http_install_packfile(struct packed_git *p,
+			   struct packed_git **list_to_remove_from)
+{
+	struct packed_git **lst = list_to_remove_from;
+
+	while (*lst != p)
+		lst = &((*lst)->next);
+	*lst = (*lst)->next;
+
+	install_packed_git(the_repository, p);
+}
+
 struct http_pack_request *new_http_pack_request(
-	struct packed_git *target, const char *base_url)
+	const unsigned char *packed_git_hash, const char *base_url)
 {
 	off_t prev_posn = 0;
 	struct strbuf buf = STRBUF_INIT;
@@ -2313,14 +2315,13 @@ struct http_pack_request *new_http_pack_request(
 
 	preq = xcalloc(1, sizeof(*preq));
 	strbuf_init(&preq->tmpfile, 0);
-	preq->target = target;
 
 	end_url_with_slash(&buf, base_url);
 	strbuf_addf(&buf, "objects/pack/pack-%s.pack",
-		hash_to_hex(target->hash));
+		hash_to_hex(packed_git_hash));
 	preq->url = strbuf_detach(&buf, NULL);
 
-	strbuf_addf(&preq->tmpfile, "%s.temp", sha1_pack_name(target->hash));
+	strbuf_addf(&preq->tmpfile, "%s.temp", sha1_pack_name(packed_git_hash));
 	preq->packfile = fopen(preq->tmpfile.buf, "a");
 	if (!preq->packfile) {
 		error("Unable to open local file %s for pack",
@@ -2344,7 +2345,7 @@ struct http_pack_request *new_http_pack_request(
 		if (http_is_verbose)
 			fprintf(stderr,
 				"Resuming fetch of pack %s at byte %"PRIuMAX"\n",
-				hash_to_hex(target->hash),
+				hash_to_hex(packed_git_hash),
 				(uintmax_t)prev_posn);
 		http_opt_request_remainder(preq->slot->curl, prev_posn);
 	}
diff --git a/http.h b/http.h
index 5e0ad724f9..bbc6b070f1 100644
--- a/http.h
+++ b/http.h
@@ -216,18 +216,25 @@ int http_get_info_packs(const char *base_url,
 
 struct http_pack_request {
 	char *url;
-	struct packed_git *target;
-	struct packed_git **lst;
 	FILE *packfile;
 	struct strbuf tmpfile;
 	struct active_request_slot *slot;
 };
 
 struct http_pack_request *new_http_pack_request(
-	struct packed_git *target, const char *base_url);
+	const unsigned char *packed_git_hash, const char *base_url);
 int finish_http_pack_request(struct http_pack_request *preq);
 void release_http_pack_request(struct http_pack_request *preq);
 
+/*
+ * Remove p from the given list, and invoke install_packed_git() on it.
+ *
+ * This is a convenience function for users that have obtained a list of packs
+ * from http_get_info_packs() and have chosen a specific pack to fetch.
+ */
+void http_install_packfile(struct packed_git *p,
+			   struct packed_git **list_to_remove_from);
+
 /* Helpers for fetching object */
 struct http_object_request {
 	char *url;
-- 
2.27.0.278.ge193c7cf3a9-goog
Previous: Junio C HamanoNext: Jonathan Tan
Message 30 of 44 in “CDN offloading update”
  1. 0/8 CDN offloading updateJonathan Tan, May 29, 2020
  2. 1/8 http: use --stdin when getting dumb HTTP packJonathan Tan, May 29, 2020
  3. Junio C HamanoMay 29, 2020
  4. Jonathan TanJun 1, 2020
  5. 2/8 http: improve documentation of http_pack_requestJonathan Tan, May 29, 2020
  6. 4/8 Documentation: order protocol v2 sectionsJonathan Tan, May 29, 2020
  7. Junio C HamanoMay 29, 2020
  8. Jonathan TanJun 1, 2020
  9. 3/8 http-fetch: support fetching packfiles by URLJonathan Tan, May 29, 2020
  10. Junio C HamanoMay 29, 2020
  11. Jonathan TanJun 1, 2020
  12. 5/8 Documentation: add Packfile URIs design docJonathan Tan, May 29, 2020
  13. Junio C HamanoMay 30, 2020
  14. Junio C HamanoMay 30, 2020
  15. Jonathan TanJun 1, 2020
  16. Jonathan TanJun 1, 2020
  17. Jonathan TanJun 10, 2020
  18. Junio C HamanoJun 10, 2020
  19. Jonathan TanJun 10, 2020
  20. 6/8 upload-pack: refactor reading of pack-objects outJonathan Tan, May 29, 2020
  21. 8/8 upload-pack: send part of packfile response as uriJonathan Tan, May 29, 2020
  22. Junio C HamanoMay 31, 2020
  23. Junio C HamanoMay 31, 2020
  24. Jonathan TanJun 1, 2020
  25. Jonathan NiederJun 1, 2020
  26. 7/8 fetch-pack: support more than one pack lockfileJonathan Tan, May 29, 2020
  27. 0/9 CDN offloading updateJonathan Tan, Jun 10, 2020
  28. 1/9 http: use --stdin when indexing dumb HTTP packJonathan Tan, Jun 10, 2020
  29. Junio C HamanoJun 11, 2020
  30. 2/9 http: refactor finish_http_pack_request()Jonathan Tan, Jun 10, 2020
  31. 3/9 http-fetch: refactor into functionJonathan Tan, Jun 10, 2020
  32. 5/9 Documentation: order protocol v2 sectionsJonathan Tan, Jun 10, 2020
  33. 4/9 http-fetch: support fetching packfiles by URLJonathan Tan, Jun 10, 2020
  34. Junio C HamanoJun 11, 2020
  35. 6/9 Documentation: add Packfile URIs design docJonathan Tan, Jun 10, 2020
  36. Junio C HamanoJun 11, 2020
  37. Ævar Arnfjörð BjarmasonNov 25, 2020
  38. Jonathan TanNov 25, 2020
  39. Ævar Arnfjörð BjarmasonDec 1, 2020
  40. 7/9 upload-pack: refactor reading of pack-objects outJonathan Tan, Jun 10, 2020
  41. 8/9 fetch-pack: support more than one pack lockfileJonathan Tan, Jun 10, 2020
  42. Junio C HamanoJun 11, 2020
  43. 9/9 upload-pack: send part of packfile response as uriJonathan Tan, Jun 10, 2020
  44. Junio C HamanoJun 10, 2020

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.