Commit dd088d9503d8e7cebbb30c535d8e4d11556c8d30

Stefan Sperling 2021-10-06T19:49:06

let 'got fetch' send all references to the server to avoid redundant downloads Problem reported by naddy. ok naddy

diff --git a/lib/fetch.c b/lib/fetch.c
index 3b8efe3..06d450c 100644
--- a/lib/fetch.c
+++ b/lib/fetch.c
@@ -298,6 +298,9 @@ got_fetch_pack(struct got_object_id **pack_hash, struct got_pathlist_head *refs,
 		    &packfile_size_cur, (*pack_hash)->sha1, &fetchibuf);
 		if (err != NULL)
 			goto done;
+		/* Don't report size progress for an empty pack file. */
+		if (packfile_size_cur <= ssizeof(pack_hdr) + SHA1_DIGEST_LENGTH)
+			packfile_size_cur = 0;
 		if (!done && refname && id) {
 			err = got_pathlist_insert(NULL, refs, refname, id);
 			if (err)
@@ -404,8 +407,11 @@ got_fetch_pack(struct got_object_id **pack_hash, struct got_pathlist_head *refs,
 	 * If the pack file contains no objects, we may only need to update
 	 * references in our repository. The caller will take care of that.
 	 */
-	if (nobj == 0)
+	if (nobj == 0) {
+		free(*pack_hash);
+		*pack_hash = NULL;
 		goto done;
+	}
 
 	if (lseek(packfd, 0, SEEK_SET) == -1) {
 		err = got_error_from_errno("lseek");
diff --git a/libexec/got-fetch-pack/got-fetch-pack.c b/libexec/got-fetch-pack/got-fetch-pack.c
index 9630831..171700f 100644
--- a/libexec/got-fetch-pack/got-fetch-pack.c
+++ b/libexec/got-fetch-pack/got-fetch-pack.c
@@ -57,7 +57,6 @@
 
 struct got_object *indexed;
 static int chattygot;
-static struct got_object_id zhash = {.sha1={0}};
 
 static const struct got_capability got_capabilities[] = {
 	{ GOT_CAPA_AGENT, "got/" GOT_VERSION_STR },
@@ -516,10 +515,9 @@ fetch_pack(int fd, int packfd, uint8_t *pack_sha1,
 	if (nwant == 0)
 		goto done;
 
-	for (i = 0; i < nref; i++) {
-		if (got_object_id_cmp(&have[i], &zhash) == 0)
-			continue;
-		got_sha1_digest_to_str(have[i].sha1, hashstr, sizeof(hashstr));
+	TAILQ_FOREACH(pe, have_refs, entry) {
+		struct got_object_id *id = pe->data;
+		got_sha1_digest_to_str(id->sha1, hashstr, sizeof(hashstr));
 		n = snprintf(buf, sizeof(buf), "have %s\n", hashstr);
 		if (n >= sizeof(buf)) {
 			err = got_error(GOT_ERR_NO_SPACE);