git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Jeff King <peff@peff.net>
To: "René Scharfe" <l.s.r@web.de>
Cc: Git List <git@vger.kernel.org>,
	Junio C Hamano <gitster@pobox.com>,
	"brian m. carlson" <sandals@crustytoothpaste.net>,
	Michael Haggerty <mhagger@alum.mit.edu>,
	Thomas Gummerer <t.gummerer@gmail.com>
Subject: Re: [PATCH 3/3] sha1_file: use hex_to_bytes()
Date: Wed, 1 Nov 2017 15:58:53 -0400	[thread overview]
Message-ID: <20171101195853.tu7wfpzo5cqoqwbo@sigill.intra.peff.net> (raw)
In-Reply-To: <d8f73744-b3f5-0fca-d58c-1f60e79214e0@web.de>

On Tue, Oct 31, 2017 at 02:50:06PM +0100, René Scharfe wrote:

> The path of a loose object contains its hash value encoded into two
> substrings of 2 and 38 hexadecimal digits separated by a slash.  The
> first part is handed to for_each_file_in_obj_subdir() in decoded form as
> subdir_nr.  The current code builds a full hexadecimal representation of
> the hash in a temporary buffer, then uses get_oid_hex() to decode it.
> 
> Avoid the intermediate step by taking subdir_nr as-is and using
> hex_to_bytes() directly on the second substring.  That's shorter and
> easier.

This raises some of the same questions as the previous one on whether
hex_to_bytes() is the ideal abstraction. But as before, I'm on the
fence.

> @@ -1908,20 +1911,15 @@ int for_each_file_in_obj_subdir(unsigned int subdir_nr,
>  		strbuf_setlen(path, baselen);
>  		strbuf_addf(path, "/%s", de->d_name);
>  
> -		if (strlen(de->d_name) == GIT_SHA1_HEXSZ - 2)  {
> -			char hex[GIT_MAX_HEXSZ+1];
> -			struct object_id oid;
> -
> -			xsnprintf(hex, sizeof(hex), "%02x%s",
> -				  subdir_nr, de->d_name);
> -			if (!get_oid_hex(hex, &oid)) {
> -				if (obj_cb) {
> -					r = obj_cb(&oid, path->buf, data);
> -					if (r)
> -						break;
> -				}
> -				continue;
> +		if (strlen(de->d_name) == GIT_SHA1_HEXSZ - 2 &&
> +		    !hex_to_bytes(oid.hash + 1, de->d_name,
> +				  GIT_SHA1_RAWSZ - 1)) {
> +			if (obj_cb) {
> +				r = obj_cb(&oid, path->buf, data);
> +				if (r)
> +					break;
>  			}
> +			continue;
>  		}
>  
>  		if (cruft_cb) {

Now that this is one big conditional for "is this a valid object
filename", I think we could get rid of the "continue" in favor of:

  if (...looks like an object...)
          ...call obj_cb...
  else if (cruft_cb)
          ...call cruft_cb...

Not a big deal, but it may make the flow more clear (the original had to
use a continue because there were multiple independent steps to
determining it was an object file, so we had to "break out" from the
inner conditional).

-Peff

  reply	other threads:[~2017-11-01 19:58 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-10-31 13:46 [PATCH 1/3] notes: move hex_to_bytes() to hex.c and export it René Scharfe
2017-10-31 13:49 ` [PATCH 2/3] http-push: use hex_to_bytes() René Scharfe
2017-11-01 19:55   ` Jeff King
2017-11-01 21:59     ` René Scharfe
2017-11-01 22:15       ` Jeff King
2017-11-04  9:05         ` René Scharfe
2017-10-31 13:50 ` [PATCH 3/3] sha1_file: " René Scharfe
2017-11-01 19:58   ` Jeff King [this message]
2017-11-05  2:56 ` [PATCH 1/3] notes: move hex_to_bytes() to hex.c and export it Kevin Daudt
2017-11-05 16:47   ` René Scharfe
2017-11-05 19:57     ` Kevin Daudt

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

  List information: http://vger.kernel.org/majordomo-info.html

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

  git send-email \
    --in-reply-to=20171101195853.tu7wfpzo5cqoqwbo@sigill.intra.peff.net \
    --to=peff@peff.net \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=l.s.r@web.de \
    --cc=mhagger@alum.mit.edu \
    --cc=sandals@crustytoothpaste.net \
    --cc=t.gummerer@gmail.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.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).