All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jiang Xin <worldhello.net@gmail.com>
To: "Ævar Arnfjörð Bjarmason" <avarab@gmail.com>
Cc: "Jiang Xin" <zhiyou.jx@alibaba-inc.com>,
	git@vger.kernel.org, "Junio C Hamano" <gitster@pobox.com>,
	"Han Xin" <chiyutianyi@gmail.com>,
	"Jiang Xin" <worldhello.net@gmail.com>,
	"René Scharfe" <l.s.r@web.de>,
	"Derrick Stolee" <stolee@gmail.com>
Subject: RE: [PATCH v3 03/12] object-file API: add a format_object_header() function
Date: Thu, 17 Feb 2022 12:59:43 +0800	[thread overview]
Message-ID: <20220217045943.30223-1-worldhello.net@gmail.com> (raw)
In-Reply-To: <patch-v3-03.12-92fd020d199-20220204T234435Z-avarab@gmail.com>

From: Jiang Xin <zhiyou.jx@alibaba-inc.com>

On Sat, Feb 5, 2022 Ævar Arnfjörð Bjarmason wrote:

> diff --git a/builtin/fast-import.c b/builtin/fast-import.c
> index 2b2e28bad79..123df7d9a53 100644
> --- a/builtin/fast-import.c
> +++ b/builtin/fast-import.c
> @@ -937,8 +937,8 @@ static int store_object(
>  	git_hash_ctx c;
>  	git_zstream s;
>  
> -	hdrlen = xsnprintf((char *)hdr, sizeof(hdr), "%s %lu",
> -			   type_name(type), (unsigned long)dat->len) + 1;
> +	hdrlen = format_object_header((char *)hdr, sizeof(hdr), type,
> +				      dat->len);

Type casting can be avoid if we use "void *" as the first parameter of
"format_object_header", and do type casting inside the helper function.

> diff --git a/object-file.c b/object-file.c
> index eeb6814780a..3fcd46cf9ed 100644
> --- a/object-file.c
> +++ b/object-file.c
> @@ -1049,6 +1049,23 @@ void *xmmap(void *start, size_t length,
>  	return ret;
>  }
>  
> +static int format_object_header_literally(char *str, size_t size,
> +					  const char *type, size_t objsize)
> +{
> +	return xsnprintf(str, size, "%s %"PRIuMAX, type, (uintmax_t)objsize) + 1;
> +}
> +
> +int format_object_header(char *str, size_t size, enum object_type type,
> +			 size_t objsize)
> +{
> +	const char *name = type_name(type);
> +
> +	if (!name)
> +		BUG("could not get a type name for 'enum object_type' value %d", type);
> +

The return value of `type_name(type)` has not been checked for the original
implement, how about write a online inline-function in a header file like this:

	static inline int format_object_header(void *str, size_t size,
					       const char *type_name,
					       size_t objsize)
	{
		return xsnprintf((char *)str, size, "%s %"PRIuMAX, type_name,
				 (uintmax_t)objsize) + 1;
	}
 
> +	return format_object_header_literally(str, size, name, objsize);
> +}


--- >8 ---
From: Jiang Xin <zhiyou.jx@alibaba-inc.com>
Date: Thu, 17 Feb 2022 11:35:27 +0800
Subject: [PATCH] fixup! object-file API: add a format_object_header() function

Signed-off-by: Jiang Xin <zhiyou.jx@alibaba-inc.com>
---
 builtin/fast-import.c |  4 ++--
 builtin/index-pack.c  |  3 ++-
 bulk-checkin.c        |  4 ++--
 http-push.c           |  2 +-
 object-file.c         | 23 +++--------------------
 object-store.h        |  9 +++++++--
 6 files changed, 17 insertions(+), 28 deletions(-)

diff --git a/builtin/fast-import.c b/builtin/fast-import.c
index 123df7d9a5..f5ec40fc1b 100644
--- a/builtin/fast-import.c
+++ b/builtin/fast-import.c
@@ -937,7 +937,7 @@ static int store_object(
 	git_hash_ctx c;
 	git_zstream s;
 
-	hdrlen = format_object_header((char *)hdr, sizeof(hdr), type,
+	hdrlen = format_object_header(hdr, sizeof(hdr), type_name(type),
 				      dat->len);
 	the_hash_algo->init_fn(&c);
 	the_hash_algo->update_fn(&c, hdr, hdrlen);
@@ -1091,7 +1091,7 @@ static void stream_blob(uintmax_t len, struct object_id *oidout, uintmax_t mark)
 	hashfile_checkpoint(pack_file, &checkpoint);
 	offset = checkpoint.offset;
 
-	hdrlen = format_object_header((char *)out_buf, out_sz, OBJ_BLOB, len);
+	hdrlen = format_object_header(out_buf, out_sz, "blob", len);
 
 	the_hash_algo->init_fn(&c);
 	the_hash_algo->update_fn(&c, out_buf, hdrlen);
diff --git a/builtin/index-pack.c b/builtin/index-pack.c
index 01574378ce..a37533333e 100644
--- a/builtin/index-pack.c
+++ b/builtin/index-pack.c
@@ -449,7 +449,8 @@ static void *unpack_entry_data(off_t offset, unsigned long size,
 	int hdrlen;
 
 	if (!is_delta_type(type)) {
-		hdrlen = format_object_header(hdr, sizeof(hdr), type, size);
+		hdrlen = format_object_header(hdr, sizeof(hdr), type_name(type),
+					      size);
 		the_hash_algo->init_fn(&c);
 		the_hash_algo->update_fn(&c, hdr, hdrlen);
 	} else
diff --git a/bulk-checkin.c b/bulk-checkin.c
index 85b3ebaf97..58fb59990d 100644
--- a/bulk-checkin.c
+++ b/bulk-checkin.c
@@ -220,8 +220,8 @@ static int deflate_to_pack(struct bulk_checkin_state *state,
 	if (seekback == (off_t) -1)
 		return error("cannot find the current offset");
 
-	header_len = format_object_header((char *)obuf, sizeof(obuf),
-					  type, size);
+	header_len = format_object_header(obuf, sizeof(obuf), type_name(type),
+					  size);
 	the_hash_algo->init_fn(&ctx);
 	the_hash_algo->update_fn(&ctx, obuf, header_len);
 
diff --git a/http-push.c b/http-push.c
index f0c044dcf7..eb2b46b2fc 100644
--- a/http-push.c
+++ b/http-push.c
@@ -363,7 +363,7 @@ static void start_put(struct transfer_request *request)
 	git_zstream stream;
 
 	unpacked = read_object_file(&request->obj->oid, &type, &len);
-	hdrlen = format_object_header(hdr, sizeof(hdr), type, len);
+	hdrlen = format_object_header(hdr, sizeof(hdr), type_name(type), len);
 
 	/* Set it up */
 	git_deflate_init(&stream, zlib_compression_level);
diff --git a/object-file.c b/object-file.c
index 3fcd46cf9e..f396e78909 100644
--- a/object-file.c
+++ b/object-file.c
@@ -1049,23 +1049,6 @@ void *xmmap(void *start, size_t length,
 	return ret;
 }
 
-static int format_object_header_literally(char *str, size_t size,
-					  const char *type, size_t objsize)
-{
-	return xsnprintf(str, size, "%s %"PRIuMAX, type, (uintmax_t)objsize) + 1;
-}
-
-int format_object_header(char *str, size_t size, enum object_type type,
-			 size_t objsize)
-{
-	const char *name = type_name(type);
-
-	if (!name)
-		BUG("could not get a type name for 'enum object_type' value %d", type);
-
-	return format_object_header_literally(str, size, name, objsize);
-}
-
 /*
  * With an in-core object data in "map", rehash it to make sure the
  * object name actually matches "oid" to detect object corruption.
@@ -1094,7 +1077,7 @@ int check_object_signature(struct repository *r, const struct object_id *oid,
 		return -1;
 
 	/* Generate the header */
-	hdrlen = format_object_header(hdr, sizeof(hdr), obj_type, size);
+	hdrlen = format_object_header(hdr, sizeof(hdr), type_name(obj_type), size);
 
 	/* Sha1.. */
 	r->hash_algo->init_fn(&c);
@@ -1794,7 +1777,7 @@ static void write_object_file_prepare(const struct git_hash_algo *algo,
 	git_hash_ctx c;
 
 	/* Generate the header */
-	*hdrlen = format_object_header_literally(hdr, *hdrlen, type, len);
+	*hdrlen = format_object_header(hdr, *hdrlen, type, len);
 
 	/* Sha1.. */
 	algo->init_fn(&c);
@@ -2068,7 +2051,7 @@ int force_object_loose(const struct object_id *oid, time_t mtime)
 	buf = read_object(the_repository, oid, &type, &len);
 	if (!buf)
 		return error(_("cannot read object for %s"), oid_to_hex(oid));
-	hdrlen = format_object_header(hdr, sizeof(hdr), type, len);
+	hdrlen = format_object_header(hdr, sizeof(hdr), type_name(type), len);
 	ret = write_loose_object(oid, hdr, hdrlen, buf, len, mtime, 0);
 	free(buf);
 
diff --git a/object-store.h b/object-store.h
index 8b4413d0ce..33b5add7ab 100644
--- a/object-store.h
+++ b/object-store.h
@@ -336,8 +336,13 @@ int has_loose_object_nonlocal(const struct object_id *);
  * writes the initial "<type> <obj-len>" part of the loose object
  * header. It returns the size that snprintf() returns + 1.
  */
-int format_object_header(char *str, size_t size, enum object_type type,
-			 size_t objsize);
+static inline int format_object_header(void *str, size_t size,
+				       const char *type_name,
+				       size_t objsize)
+{
+	return xsnprintf((char *)str, size, "%s %"PRIuMAX, type_name,
+			 (uintmax_t)objsize) + 1;
+}
 
 void assert_oid_type(const struct object_id *oid, enum object_type expect);
 
-- 
2.35.0.rc2


  reply	other threads:[~2022-02-17  5:00 UTC|newest]

Thread overview: 46+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-02-01 14:53 [PATCH 00/10] object-file API: pass object enums, tidy up streaming interface Ævar Arnfjörð Bjarmason
2022-02-01 14:53 ` [PATCH 01/10] object-file.c: split up declaration of unrelated variables Ævar Arnfjörð Bjarmason
2022-02-01 14:53 ` [PATCH 02/10] object-file API: return "void", not "int" from hash_object_file() Ævar Arnfjörð Bjarmason
2022-02-01 14:53 ` [PATCH 03/10] object-file API: add a format_object_header() function Ævar Arnfjörð Bjarmason
2022-02-01 14:53 ` [PATCH 04/10] object-file API: have write_object_file() take "enum object_type" Ævar Arnfjörð Bjarmason
2022-02-01 18:58   ` Junio C Hamano
2022-02-01 14:53 ` [PATCH 05/10] object-file API: provide a hash_object_file_oideq() Ævar Arnfjörð Bjarmason
2022-02-01 19:08   ` Junio C Hamano
2022-02-01 20:56     ` Ævar Arnfjörð Bjarmason
2022-02-01 14:53 ` [PATCH 06/10] object-file API: replace some use of check_object_signature() Ævar Arnfjörð Bjarmason
2022-02-01 19:16   ` Junio C Hamano
2022-02-01 14:53 ` [PATCH 07/10] object-file API: have hash_object_file() take "enum object_type" Ævar Arnfjörð Bjarmason
2022-02-01 14:53 ` [PATCH 08/10] object-file API: replace check_object_signature() with stream_* Ævar Arnfjörð Bjarmason
2022-02-01 14:53 ` [PATCH 09/10] object-file.c: add a literal version of write_object_file_prepare() Ævar Arnfjörð Bjarmason
2022-02-01 14:53 ` [PATCH 10/10] object-file API: pass an enum to read_object_with_reference() Ævar Arnfjörð Bjarmason
2022-02-04 13:51 ` [PATCH v2 00/11] object-file API: pass object enums, tidy up streaming interface Ævar Arnfjörð Bjarmason
2022-02-04 13:51   ` [PATCH v2 01/11] object-file.c: split up declaration of unrelated variables Ævar Arnfjörð Bjarmason
2022-02-04 13:51   ` [PATCH v2 02/11] object-file API: return "void", not "int" from hash_object_file() Ævar Arnfjörð Bjarmason
2022-02-04 13:51   ` [PATCH v2 03/11] object-file API: add a format_object_header() function Ævar Arnfjörð Bjarmason
2022-02-04 13:51   ` [PATCH v2 04/11] object-file API: have write_object_file() take "enum object_type" Ævar Arnfjörð Bjarmason
2022-02-04 20:52     ` Junio C Hamano
2022-02-04 13:51   ` [PATCH v2 05/11] object API: correct "buf" v.s. "map" mismatch in *.c and *.h Ævar Arnfjörð Bjarmason
2022-02-04 20:54     ` Junio C Hamano
2022-02-04 13:51   ` [PATCH v2 06/11] object API: make check_object_signature() oideq()-like, move docs Ævar Arnfjörð Bjarmason
2022-02-04 21:15     ` Junio C Hamano
2022-02-04 13:51   ` [PATCH v2 07/11] object-file API: split up and simplify check_object_signature() Ævar Arnfjörð Bjarmason
2022-02-04 13:51   ` [PATCH v2 08/11] object API: rename hash_object_file_literally() to write_*() Ævar Arnfjörð Bjarmason
2022-02-04 13:51   ` [PATCH v2 09/11] object-file API: have hash_object_file() take "enum object_type" Ævar Arnfjörð Bjarmason
2022-02-04 13:51   ` [PATCH v2 10/11] object-file.c: add a literal version of write_object_file_prepare() Ævar Arnfjörð Bjarmason
2022-02-04 13:51   ` [PATCH v2 11/11] object-file API: pass an enum to read_object_with_reference() Ævar Arnfjörð Bjarmason
2022-02-04 23:48   ` [PATCH v3 00/12] object-file API: pass object enums, tidy up streaming interface Ævar Arnfjörð Bjarmason
2022-02-04 23:48     ` [PATCH v3 01/12] object-file.c: split up declaration of unrelated variables Ævar Arnfjörð Bjarmason
2022-02-04 23:48     ` [PATCH v3 02/12] object-file API: return "void", not "int" from hash_object_file() Ævar Arnfjörð Bjarmason
2022-02-04 23:48     ` [PATCH v3 03/12] object-file API: add a format_object_header() function Ævar Arnfjörð Bjarmason
2022-02-17  4:59       ` Jiang Xin [this message]
2022-02-17  9:21         ` Ævar Arnfjörð Bjarmason
2022-03-01  3:09           ` Jiang Xin
2022-02-04 23:48     ` [PATCH v3 04/12] object-file API: have write_object_file() take "enum object_type" Ævar Arnfjörð Bjarmason
2022-02-04 23:48     ` [PATCH v3 05/12] object API: correct "buf" v.s. "map" mismatch in *.c and *.h Ævar Arnfjörð Bjarmason
2022-02-04 23:48     ` [PATCH v3 06/12] object API docs: move check_object_signature() docs to cache.h Ævar Arnfjörð Bjarmason
2022-02-04 23:48     ` [PATCH v3 07/12] object API users + docs: check <0, not !0 with check_object_signature() Ævar Arnfjörð Bjarmason
2022-02-04 23:48     ` [PATCH v3 08/12] object-file API: split up and simplify check_object_signature() Ævar Arnfjörð Bjarmason
2022-02-04 23:48     ` [PATCH v3 09/12] object API: rename hash_object_file_literally() to write_*() Ævar Arnfjörð Bjarmason
2022-02-04 23:48     ` [PATCH v3 10/12] object-file API: have hash_object_file() take "enum object_type" Ævar Arnfjörð Bjarmason
2022-02-04 23:48     ` [PATCH v3 11/12] object-file.c: add a literal version of write_object_file_prepare() Ævar Arnfjörð Bjarmason
2022-02-04 23:48     ` [PATCH v3 12/12] object-file API: pass an enum to read_object_with_reference() Ævar Arnfjörð Bjarmason

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220217045943.30223-1-worldhello.net@gmail.com \
    --to=worldhello.net@gmail.com \
    --cc=avarab@gmail.com \
    --cc=chiyutianyi@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=l.s.r@web.de \
    --cc=stolee@gmail.com \
    --cc=zhiyou.jx@alibaba-inc.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.