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

[PATCH 3/8] index-pack: add --append-pack=<path>

From
Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Date
Feb 5, 2016, 08:57 UTC
Message-ID
<1454662677-15137-4-git-send-email-pclouds@gmail.com>
In-Reply-To
<1454662677-15137-1-git-send-email-pclouds@gmail.com>

In this mode (--append-pack --stdin), index-pack consumes current content in <path> first without producing anything else, then continues to read from stdin and append to <path>. This is the consumer end of "pack-objects --skip".

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 builtin/index-pack.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 48 insertions(+), 2 deletions(-)
diff --git a/builtin/index-pack.c b/builtin/index-pack.c
index 6a01509..f099ac2 100644
--- a/builtin/index-pack.c
+++ b/builtin/index-pack.c
@@ -73,6 +73,7 @@ static int nr_resolved_deltas;
 static int nr_threads;
 
 static int from_stdin;
+static int skip_mode;
 static int strict;
 static int do_fsck_object;
 static struct fsck_options fsck_options = FSCK_OPTIONS_STRICT;
@@ -272,7 +273,29 @@ static void *fill(int min)
 	do {
 		ssize_t ret = xread(input_fd, input_buffer + input_len,
 				sizeof(input_buffer) - input_len);
-		if (ret <= 0) {
+
+		if (ret == 0 && skip_mode) {
+			output_fd = input_fd;
+			input_fd = 0;
+			skip_mode = 0;
+			/*
+			 * At this point we have 'input_len' bytes in
+			 * input_buffer, which is from the existing pack.
+			 * Assuming that we still need to read more, the
+			 * next loop will read from stdin instead.
+			 *
+			 * What's read from stdin must be written down. The
+			 * next flush() will write from &input_buffer[0],
+			 * which appends the 'input_len' bytes from existing
+			 * pack to the pack again.
+			 *
+			 * Seek back to make this an overwrite (of same
+			 * content) instead of an append.
+			 */
+			if (input_len && lseek(output_fd, -input_len, SEEK_CUR))
+				die_errno(_("cannot seek back %d bytes"), input_len);
+		}
+		else if (ret <= 0) {
 			if (!ret)
 				die(_("early EOF"));
 			die_errno(_("read error on input"));
@@ -323,6 +346,26 @@ static const char *open_pack_file(const char *pack_name)
 	return pack_name;
 }
 
+static const char *open_pack_for_append(const char *path)
+{
+	if (!from_stdin)
+		die(_("--append-pack must be used with --stdin"));
+
+	input_fd = open(path, O_CREAT|O_RDWR, 0600);
+	if (input_fd < 0)
+		die_errno(_("cannot open packfile '%s'"), path);
+	output_fd = -1;
+	nothread_data.pack_fd = input_fd;
+	git_SHA1_Init(&input_ctx);
+
+	/*
+	 * fill() will eventually turn this flag off and set output_fd
+	 * after reading everything
+	 */
+	skip_mode = 1;
+	return xstrdup(path);
+}
+
 static void parse_pack_header(void)
 {
 	struct pack_header *hdr = fill(sizeof(struct pack_header));
@@ -1692,6 +1735,8 @@ int cmd_index_pack(int argc, const char **argv, const char *prefix)
 					opts.off32_limit = strtoul(c+1, &c, 0);
 				if (*c || opts.off32_limit & 0x80000000)
 					die(_("bad %s"), arg);
+			} else if (skip_prefix(arg, "--append-pack=", &arg)) {
+				curr_pack = open_pack_for_append(arg);
 			} else
 				usage(index_pack_usage);
 			continue;
@@ -1742,7 +1787,8 @@ int cmd_index_pack(int argc, const char **argv, const char *prefix)
 	}
 #endif
 
-	curr_pack = open_pack_file(pack_name);
+	if (!curr_pack)
+		curr_pack = open_pack_file(pack_name);
 	parse_pack_header();
 	objects = xcalloc(nr_objects + 1, sizeof(struct object_entry));
 	if (show_stat)
-- 
2.7.0.377.g4cd97dd
Previous: Duy NguyenNext: Nguyễn Thái Ngọc Duy
Message 9 of 22 in “Resumable clone revisited, proof of concept”
  1. 0/8 Resumable clone revisited, proof of conceptNguyễn Thái Ngọc Duy, Feb 5, 2016
  2. 1/8 pack-objects: add --skip and --skip-hashNguyễn Thái Ngọc Duy, Feb 5, 2016
  3. Johannes SchindelinFeb 5, 2016
  4. 2/8 pack-objects: produce a stable pack when --skip is givenNguyễn Thái Ngọc Duy, Feb 5, 2016
  5. Junio C HamanoFeb 5, 2016
  6. Duy NguyenFeb 5, 2016
  7. Junio C HamanoFeb 6, 2016
  8. Duy NguyenFeb 8, 2016
  9. 3/8 index-pack: add --append-pack=<path>Nguyễn Thái Ngọc Duy, Feb 5, 2016
  10. 4/8 upload-pack: new capability to pass --skip* to pack-objectsNguyễn Thái Ngọc Duy, Feb 5, 2016
  11. Johannes SchindelinFeb 5, 2016
  12. 5/8 fetch-pack.c: send "skip" line to pack-objectsNguyễn Thái Ngọc Duy, Feb 5, 2016
  13. Eric SunshineFeb 7, 2016
  14. 6/8 fetch: add --resume-pack=<path>Nguyễn Thái Ngọc Duy, Feb 5, 2016
  15. 7/8 index-pack: --append-pack implies --strictNguyễn Thái Ngọc Duy, Feb 5, 2016
  16. Eric SunshineFeb 7, 2016
  17. 8/8 one ugly test to verify basic functionalityNguyễn Thái Ngọc Duy, Feb 5, 2016
  18. Elia PintoFeb 5, 2016
  19. Duy NguyenFeb 5, 2016
  20. Elia PintoFeb 5, 2016
  21. Johannes SchindelinFeb 5, 2016
  22. Elia PintoFeb 5, 2016

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.