All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jeff King <peff@peff.net>
To: "Kyle J. McKay" <mackyle@gmail.com>
Cc: Jonathon Mah <me@jonathonmah.com>,
	Junio C Hamano <gitster@pobox.com>,
	Git mailing list <git@vger.kernel.org>
Subject: [PATCH 1/2] for_each_loose_file_in_objdir: take an optional strbuf path
Date: Sun, 8 Feb 2015 20:13:22 -0500	[thread overview]
Message-ID: <20150209011321.GA21123@peff.net> (raw)
In-Reply-To: <20150209011159.GA21072@peff.net>

We feed a root "objdir" path to this iterator function,
which then copies the result into a strbuf, so that it can
repeatedly append the object sub-directories to it. Let's
make it easy for callers to just pass us a strbuf in the
first place.

We leave the original interface as a convenience for callers
who want to just pass a const string like the result of
get_object_directory().

Signed-off-by: Jeff King <peff@peff.net>
---
 cache.h     |  9 +++++++++
 sha1_file.c | 31 +++++++++++++++++++++----------
 2 files changed, 30 insertions(+), 10 deletions(-)

diff --git a/cache.h b/cache.h
index 51ee856..4743f7e 100644
--- a/cache.h
+++ b/cache.h
@@ -1256,6 +1256,10 @@ extern int unpack_object_header(struct packed_git *, struct pack_window **, off_
  *
  * Any callback that is NULL will be ignored. Callbacks returning non-zero
  * will end the iteration.
+ *
+ * In the "buf" variant, "path" is a strbuf which will also be used as a
+ * scratch buffer, but restored to its original contents before
+ * the function returns.
  */
 typedef int each_loose_object_fn(const unsigned char *sha1,
 				 const char *path,
@@ -1271,6 +1275,11 @@ int for_each_loose_file_in_objdir(const char *path,
 				  each_loose_cruft_fn cruft_cb,
 				  each_loose_subdir_fn subdir_cb,
 				  void *data);
+int for_each_loose_file_in_objdir_buf(struct strbuf *path,
+				      each_loose_object_fn obj_cb,
+				      each_loose_cruft_fn cruft_cb,
+				      each_loose_subdir_fn subdir_cb,
+				      void *data);
 
 /*
  * Iterate over loose and packed objects in both the local
diff --git a/sha1_file.c b/sha1_file.c
index c632641..725de7f 100644
--- a/sha1_file.c
+++ b/sha1_file.c
@@ -3358,31 +3358,42 @@ static int for_each_file_in_obj_subdir(int subdir_nr,
 	return r;
 }
 
-int for_each_loose_file_in_objdir(const char *path,
+int for_each_loose_file_in_objdir_buf(struct strbuf *path,
 			    each_loose_object_fn obj_cb,
 			    each_loose_cruft_fn cruft_cb,
 			    each_loose_subdir_fn subdir_cb,
 			    void *data)
 {
-	struct strbuf buf = STRBUF_INIT;
-	size_t baselen;
+	size_t baselen = path->len;
 	int r = 0;
 	int i;
 
-	strbuf_addstr(&buf, path);
-	strbuf_addch(&buf, '/');
-	baselen = buf.len;
-
 	for (i = 0; i < 256; i++) {
-		strbuf_addf(&buf, "%02x", i);
-		r = for_each_file_in_obj_subdir(i, &buf, obj_cb, cruft_cb,
+		strbuf_addf(path, "/%02x", i);
+		r = for_each_file_in_obj_subdir(i, path, obj_cb, cruft_cb,
 						subdir_cb, data);
-		strbuf_setlen(&buf, baselen);
+		strbuf_setlen(path, baselen);
 		if (r)
 			break;
 	}
 
+	return r;
+}
+
+int for_each_loose_file_in_objdir(const char *path,
+				  each_loose_object_fn obj_cb,
+				  each_loose_cruft_fn cruft_cb,
+				  each_loose_subdir_fn subdir_cb,
+				  void *data)
+{
+	struct strbuf buf = STRBUF_INIT;
+	int r;
+
+	strbuf_addstr(&buf, path);
+	r = for_each_loose_file_in_objdir_buf(&buf, obj_cb, cruft_cb,
+					      subdir_cb, data);
 	strbuf_release(&buf);
+
 	return r;
 }
 
-- 
2.3.0.rc1.287.g761fd19

  reply	other threads:[~2015-02-09  1:13 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-02-08 23:05 [PATCH] sha1_file.c: make sure open_sha1_file does not open a directory Kyle J. McKay
2015-02-09  0:54 ` Jeff King
2015-02-09  1:12   ` Jeff King
2015-02-09  1:13     ` Jeff King [this message]
2015-02-09  1:15     ` [PATCH 2/2] sha1_file: fix iterating loose alternate objects Jeff King
2015-02-09  9:44       ` Kyle J. McKay
2015-02-09 18:48   ` [PATCH] sha1_file.c: make sure open_sha1_file does not open a directory Junio C Hamano

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=20150209011321.GA21123@peff.net \
    --to=peff@peff.net \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=mackyle@gmail.com \
    --cc=me@jonathonmah.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.