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

[PATCH 1/4] fetch-pack: save shallow file before fetching the pack

From
Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Date
Mar 31, 2013, 11:09 UTC
Message-ID
<1364728148-7537-2-git-send-email-pclouds@gmail.com>
In-Reply-To
<1364728148-7537-1-git-send-email-pclouds@gmail.com>

index-pack --strict looks up and follows parent commits. If shallow information is not ready by the time index-pack is run, index-pack may be lead to non-existent objects. Make fetch-pack save shallow file to disk before invoking index-pack.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 fetch-pack.c          | 70 ++++++++++++++++++++++++++++-----------------------
 t/t5500-fetch-pack.sh |  7 ++++++
 2 files changed, 45 insertions(+), 32 deletions(-)
diff --git a/fetch-pack.c b/fetch-pack.c
index cef8fde..1f9c5ba 100644
--- a/fetch-pack.c
+++ b/fetch-pack.c
@@ -779,11 +779,44 @@ static int cmp_ref_by_name(const void *a_, const void *b_)
 	return strcmp(a->name, b->name);
 }
 
+static void flush_shallow_to_disk(struct stat *st)
+{
+	static struct lock_file lock;
+	struct cache_time mtime;
+	struct strbuf sb = STRBUF_INIT;
+	char *shallow = git_path("shallow");
+	int fd;
+
+	mtime.sec = st->st_mtime;
+	mtime.nsec = ST_MTIME_NSEC(*st);
+	if (stat(shallow, st)) {
+		if (mtime.sec)
+			die("shallow file was removed during fetch");
+	} else if (st->st_mtime != mtime.sec
+#ifdef USE_NSEC
+		   || ST_MTIME_NSEC(*st) != mtime.nsec
+#endif
+		   )
+		die("shallow file was changed during fetch");
+
+	fd = hold_lock_file_for_update(&lock, shallow,
+				       LOCK_DIE_ON_ERROR);
+	if (!write_shallow_commits(&sb, 0)
+	    || write_in_full(fd, sb.buf, sb.len) != sb.len) {
+		unlink_or_warn(shallow);
+		rollback_lock_file(&lock);
+	} else {
+		commit_lock_file(&lock);
+	}
+	strbuf_release(&sb);
+}
+
 static struct ref *do_fetch_pack(struct fetch_pack_args *args,
 				 int fd[2],
 				 const struct ref *orig_ref,
 				 struct ref **sought, int nr_sought,
-				 char **pack_lockfile)
+				 char **pack_lockfile,
+				 struct stat *shallow_st)
 {
 	struct ref *ref = copy_ref_list(orig_ref);
 	unsigned char sha1[20];
@@ -858,6 +891,8 @@ static struct ref *do_fetch_pack(struct fetch_pack_args *args,
 
 	if (args->stateless_rpc)
 		packet_flush(fd[1]);
+	if (args->depth > 0)
+		flush_shallow_to_disk(shallow_st);
 	if (get_pack(args, fd, pack_lockfile))
 		die("git fetch-pack: fetch failed.");
 
@@ -952,38 +987,9 @@ struct ref *fetch_pack(struct fetch_pack_args *args,
 		packet_flush(fd[1]);
 		die("no matching remote head");
 	}
-	ref_cpy = do_fetch_pack(args, fd, ref, sought, nr_sought, pack_lockfile);
 
-	if (args->depth > 0) {
-		static struct lock_file lock;
-		struct cache_time mtime;
-		struct strbuf sb = STRBUF_INIT;
-		char *shallow = git_path("shallow");
-		int fd;
-
-		mtime.sec = st.st_mtime;
-		mtime.nsec = ST_MTIME_NSEC(st);
-		if (stat(shallow, &st)) {
-			if (mtime.sec)
-				die("shallow file was removed during fetch");
-		} else if (st.st_mtime != mtime.sec
-#ifdef USE_NSEC
-				|| ST_MTIME_NSEC(st) != mtime.nsec
-#endif
-			  )
-			die("shallow file was changed during fetch");
-
-		fd = hold_lock_file_for_update(&lock, shallow,
-					       LOCK_DIE_ON_ERROR);
-		if (!write_shallow_commits(&sb, 0)
-		 || write_in_full(fd, sb.buf, sb.len) != sb.len) {
-			unlink_or_warn(shallow);
-			rollback_lock_file(&lock);
-		} else {
-			commit_lock_file(&lock);
-		}
-		strbuf_release(&sb);
-	}
+	ref_cpy = do_fetch_pack(args, fd, ref, sought, nr_sought,
+				pack_lockfile, &st);
 
 	reprepare_packed_git();
 	return ref_cpy;
diff --git a/t/t5500-fetch-pack.sh b/t/t5500-fetch-pack.sh
index d574085..557b073 100755
--- a/t/t5500-fetch-pack.sh
+++ b/t/t5500-fetch-pack.sh
@@ -135,6 +135,13 @@ test_expect_success 'clone shallow depth 1' '
 	test "`git --git-dir=shallow0/.git rev-list --count HEAD`" = 1
 '
 
+test_expect_success 'clone shallow depth 1 with fsck' '
+	git config --global fetch.fsckobjects true &&
+	git clone --no-single-branch --depth 1 "file://$(pwd)/." shallow0fsck &&
+	test "`git --git-dir=shallow0fsck/.git rev-list --count HEAD`" = 1 &&
+	git config --global --unset fetch.fsckobjects
+'
+
 test_expect_success 'clone shallow' '
 	git clone --no-single-branch --depth 2 "file://$(pwd)/." shallow
 '
-- 
1.8.2.83.gc99314b
Previous: Nguyễn Thái Ngọc DuyNext: Junio C Hamano
Message 2 of 35 in “check_everything_connected replacement”
  1. 0/4 check_everything_connected replacementNguyễn Thái Ngọc Duy, Mar 31, 2013
  2. 1/4 fetch-pack: save shallow file before fetching the packNguyễn Thái Ngọc Duy, Mar 31, 2013
  3. Junio C HamanoApr 1, 2013
  4. Duy NguyenApr 5, 2013
  5. 2/4 index-pack: remove dead code (it should never happen)Nguyễn Thái Ngọc Duy, Mar 31, 2013
  6. 3/4 index-pack, unpack-objects: add --not-so-strict for connectivity checkNguyễn Thái Ngọc Duy, Mar 31, 2013
  7. 4/4 Use --not-so-strict on all pack transfer for connectivity checkNguyễn Thái Ngọc Duy, Mar 31, 2013
  8. Junio C HamanoApr 1, 2013
  9. 0/5 check_everything_connected replacementNguyễn Thái Ngọc Duy, May 1, 2013
  10. 1/5 clone: let the user know when check_everything_connected is runNguyễn Thái Ngọc Duy, May 1, 2013
  11. 2/5 fetch-pack: prepare updated shallow file before fetching the packNguyễn Thái Ngọc Duy, May 1, 2013
  12. Junio C HamanoMay 1, 2013
  13. Duy NguyenMay 2, 2013
  14. 3/5 index-pack: remove dead code (it should never happen)Nguyễn Thái Ngọc Duy, May 1, 2013
  15. 4/5 index-pack, unpack-objects: add --not-so-strict for connectivity checkNguyễn Thái Ngọc Duy, May 1, 2013
  16. Junio C HamanoMay 1, 2013
  17. Duy NguyenMay 2, 2013
  18. Junio C HamanoMay 2, 2013
  19. Duy NguyenMay 3, 2013
  20. Junio C HamanoMay 3, 2013
  21. Junio C HamanoMay 3, 2013
  22. Duy NguyenMay 3, 2013
  23. Eric SunshineMay 3, 2013
  24. 5/5 Use --not-so-strict on all pack transfer for connectivity checkNguyễn Thái Ngọc Duy, May 1, 2013
  25. 0/4 check_everything_connected replacementNguyễn Thái Ngọc Duy, May 3, 2013
  26. 1/4 clone: let the user know when check_everything_connected is runNguyễn Thái Ngọc Duy, May 3, 2013
  27. 2/4 fetch-pack: prepare updated shallow file before fetching the packNguyễn Thái Ngọc Duy, May 3, 2013
  28. Eric SunshineMay 3, 2013
  29. Junio C HamanoMay 7, 2013
  30. Duy NguyenMay 26, 2013
  31. 3/4 index-pack: remove dead code (it should never happen)Nguyễn Thái Ngọc Duy, May 3, 2013
  32. 4/4 clone: open a shortcut for connectivity checkNguyễn Thái Ngọc Duy, May 3, 2013
  33. Eric SunshineMay 3, 2013
  34. Junio C HamanoMay 3, 2013
  35. Duy NguyenMay 4, 2013

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.