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

[PATCH v3 3/4] Refactoring: move duplicated code from builtin-pack-objects.c and fast-import.c to sha1_file.c

From
Michael Lukashov <michael.lukashov@gmail.com>
Date
Feb 16, 2010, 23:42 UTC
Message-ID
<19aeabff19266007077131b437571b047ef4c9e3.1266360267.git.michael.lukashov@gmail.com>
In-Reply-To
<cover.1266360267.git.michael.lukashov@gmail.com>
The following function is duplicated:
  encode_header

Move this function to sha1_file.c and rename it 'encode_in_pack_object_header', as suggested by Junio C Hamano

Signed-off-by: Michael Lukashov <michael.lukashov@gmail.com>
---
 builtin-pack-objects.c |   31 ++-----------------------------
 cache.h                |    8 ++++++++
 fast-import.c          |   29 +++--------------------------
 sha1_file.c            |   20 ++++++++++++++++++++
 4 files changed, 33 insertions(+), 55 deletions(-)
diff --git a/builtin-pack-objects.c b/builtin-pack-objects.c
index e1d3adf..6b2f65c 100644
--- a/builtin-pack-objects.c
+++ b/builtin-pack-objects.c
@@ -155,33 +155,6 @@ static unsigned long do_compress(void **pptr, unsigned long size)
 }
 
 /*
- * The per-object header is a pretty dense thing, which is
- *  - first byte: low four bits are "size", then three bits of "type",
- *    and the high bit is "size continues".
- *  - each byte afterwards: low seven bits are size continuation,
- *    with the high bit being "size continues"
- */
-static int encode_header(enum object_type type, unsigned long size, unsigned char *hdr)
-{
-	int n = 1;
-	unsigned char c;
-
-	if (type < OBJ_COMMIT || type > OBJ_REF_DELTA)
-		die("bad type %d", type);
-
-	c = (type << 4) | (size & 15);
-	size >>= 4;
-	while (size) {
-		*hdr++ = c | 0x80;
-		c = size & 0x7f;
-		size >>= 7;
-		n++;
-	}
-	*hdr = c;
-	return n;
-}
-
-/*
  * we are going to reuse the existing object data as is.  make
  * sure it is not corrupt.
  */
@@ -321,7 +294,7 @@ static unsigned long write_object(struct sha1file *f,
 		 * The object header is a byte of 'type' followed by zero or
 		 * more bytes of length.
 		 */
-		hdrlen = encode_header(type, size, header);
+		hdrlen = encode_in_pack_object_header(type, size, header);
 
 		if (type == OBJ_OFS_DELTA) {
 			/*
@@ -372,7 +345,7 @@ static unsigned long write_object(struct sha1file *f,
 		if (entry->delta)
 			type = (allow_ofs_delta && entry->delta->idx.offset) ?
 				OBJ_OFS_DELTA : OBJ_REF_DELTA;
-		hdrlen = encode_header(type, entry->size, header);
+		hdrlen = encode_in_pack_object_header(type, entry->size, header);
 
 		offset = entry->in_pack_offset;
 		revidx = find_pack_revindex(p, offset);
diff --git a/cache.h b/cache.h
index d478eff..2654b60 100644
--- a/cache.h
+++ b/cache.h
@@ -911,6 +911,14 @@ extern void *unpack_entry(struct packed_git *, off_t, enum object_type *, unsign
 extern unsigned long unpack_object_header_buffer(const unsigned char *buf, unsigned long len, enum object_type *type, unsigned long *sizep);
 extern unsigned long get_size_from_delta(struct packed_git *, struct pack_window **, off_t);
 extern const char *packed_object_info_detail(struct packed_git *, off_t, unsigned long *, unsigned long *, unsigned int *, unsigned char *);
+/*
+ * The per-object header is a pretty dense thing, which is
+ *  - first byte: low four bits are "size", then three bits of "type",
+ *    and the high bit is "size continues".
+ *  - each byte afterwards: low seven bits are size continuation,
+ *    with the high bit being "size continues"
+ */
+int encode_in_pack_object_header(enum object_type type, uintmax_t size, unsigned char *hdr);
 
 /* Dumb servers support */
 extern int update_server_info(int);
diff --git a/fast-import.c b/fast-import.c
index b477dc6..776782a 100644
--- a/fast-import.c
+++ b/fast-import.c
@@ -1013,29 +1013,6 @@ static void cycle_packfile(void)
 	start_packfile();
 }
 
-static size_t encode_header(
-	enum object_type type,
-	uintmax_t size,
-	unsigned char *hdr)
-{
-	int n = 1;
-	unsigned char c;
-
-	if (type < OBJ_COMMIT || type > OBJ_REF_DELTA)
-		die("bad type %d", type);
-
-	c = (type << 4) | (size & 15);
-	size >>= 4;
-	while (size) {
-		*hdr++ = c | 0x80;
-		c = size & 0x7f;
-		size >>= 7;
-		n++;
-	}
-	*hdr = c;
-	return n;
-}
-
 static int store_object(
 	enum object_type type,
 	struct strbuf *dat,
@@ -1138,7 +1115,7 @@ static int store_object(
 		delta_count_by_type[type]++;
 		e->depth = last->depth + 1;
 
-		hdrlen = encode_header(OBJ_OFS_DELTA, deltalen, hdr);
+		hdrlen = encode_in_pack_object_header(OBJ_OFS_DELTA, deltalen, hdr);
 		write_or_die(pack_data->pack_fd, hdr, hdrlen);
 		pack_size += hdrlen;
 
@@ -1149,7 +1126,7 @@ static int store_object(
 		pack_size += sizeof(hdr) - pos;
 	} else {
 		e->depth = 0;
-		hdrlen = encode_header(type, dat->len, hdr);
+		hdrlen = encode_in_pack_object_header(type, dat->len, hdr);
 		write_or_die(pack_data->pack_fd, hdr, hdrlen);
 		pack_size += hdrlen;
 	}
@@ -1209,7 +1186,7 @@ static void stream_blob(uintmax_t len, unsigned char *sha1out, uintmax_t mark)
 	memset(&s, 0, sizeof(s));
 	deflateInit(&s, pack_compression_level);
 
-	hdrlen = encode_header(OBJ_BLOB, len, out_buf);
+	hdrlen = encode_in_pack_object_header(OBJ_BLOB, len, out_buf);
 	if (out_sz <= hdrlen)
 		die("impossibly large object header");
 
diff --git a/sha1_file.c b/sha1_file.c
index 657825e..f3c9823 100644
--- a/sha1_file.c
+++ b/sha1_file.c
@@ -1475,6 +1475,26 @@ const char *packed_object_info_detail(struct packed_git *p,
 	}
 }
 
+int encode_in_pack_object_header(enum object_type type, uintmax_t size, unsigned char *hdr)
+{
+	int n = 1;
+	unsigned char c;
+
+	if (type < OBJ_COMMIT || type > OBJ_REF_DELTA)
+		die("bad type %d", type);
+
+	c = (type << 4) | (size & 15);
+	size >>= 4;
+	while (size) {
+		*hdr++ = c | 0x80;
+		c = size & 0x7f;
+		size >>= 7;
+		n++;
+	}
+	*hdr = c;
+	return n;
+}
+
 static int packed_object_info(struct packed_git *p, off_t obj_offset,
 			      unsigned long *sizep)
 {
-- 
1.7.0.1571.g856c2
Previous: Junio C HamanoNext: Michael Lukashov
Message 9 of 10 in “Refactoring: remove duplicated code”
  1. 0/4 Refactoring: remove duplicated codeMichael Lukashov, Feb 16, 2010
  2. 1/4 Refactoring: remove duplicated code from builtin-send-pack.c and transport.cMichael Lukashov, Feb 16, 2010
  3. Tay Ray ChuanFeb 17, 2010
  4. 2/4 Refactoring: connect.c: move duplicated code to a new function 'get_host_and_port'Michael Lukashov, Feb 16, 2010
  5. Larry D'AnnaFeb 17, 2010
  6. Michael LukashovFeb 17, 2010
  7. 2/4 Refactoring: connect.c: move duplicated code to a new function 'get_host_and_port'Michael Lukashov, Feb 17, 2010
  8. Junio C HamanoFeb 17, 2010
  9. 3/4 Refactoring: move duplicated code from builtin-pack-objects.c and fast-import.c to sha1_file.cMichael Lukashov, Feb 16, 2010
  10. 4/4 Refactoring: move duplicated code from builtin-checkout.c and merge-recursive.c to xdiff-interface.cMichael Lukashov, Feb 16, 2010

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.