git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: git@vger.kernel.org
Subject: [PATCH 3/6] builtin-mktree.c: use a helper function to handle one line of input
Date: Sun, 10 May 2009 11:49:48 -0700	[thread overview]
Message-ID: <1241981391-19639-4-git-send-email-gitster@pobox.com> (raw)
In-Reply-To: <1241981391-19639-3-git-send-email-gitster@pobox.com>

The main() function used to do the whole thing; this moves the handling of
a single input line to a separate function to make it easier to read.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 builtin-mktree.c |   82 +++++++++++++++++++++++++++--------------------------
 1 files changed, 42 insertions(+), 40 deletions(-)

diff --git a/builtin-mktree.c b/builtin-mktree.c
index 2b3145b..133ab4b 100644
--- a/builtin-mktree.c
+++ b/builtin-mktree.c
@@ -67,10 +67,48 @@ static const char *mktree_usage[] = {
 	NULL
 };
 
+static void mktree_line(char *buf, size_t len, int line_termination)
+{
+	char *ptr, *ntr;
+	unsigned mode;
+	enum object_type type;
+	char *path;
+	unsigned char sha1[20];
+
+	ptr = buf;
+	/*
+	 * Read non-recursive ls-tree output format:
+	 *     mode SP type SP sha1 TAB name
+	 */
+	mode = strtoul(ptr, &ntr, 8);
+	if (ptr == ntr || !ntr || *ntr != ' ')
+		die("input format error: %s", buf);
+	ptr = ntr + 1; /* type */
+	ntr = strchr(ptr, ' ');
+	if (!ntr || buf + len <= ntr + 40 ||
+	    ntr[41] != '\t' ||
+	    get_sha1_hex(ntr + 1, sha1))
+		die("input format error: %s", buf);
+	type = sha1_object_info(sha1, NULL);
+	if (type < 0)
+		die("object %s unavailable", sha1_to_hex(sha1));
+	*ntr++ = 0; /* now at the beginning of SHA1 */
+	if (type != type_from_string(ptr))
+		die("object type %s mismatch (%s)", ptr, typename(type));
+
+	path = ntr + 41;  /* at the beginning of name */
+	if (line_termination && path[0] == '"') {
+		struct strbuf p_uq = STRBUF_INIT;
+		if (unquote_c_style(&p_uq, path, NULL))
+			die("invalid quoting");
+		path = strbuf_detach(&p_uq, NULL);
+	}
+	append_to_tree(mode, sha1, path);
+}
+
 int cmd_mktree(int ac, const char **av, const char *prefix)
 {
 	struct strbuf sb = STRBUF_INIT;
-	struct strbuf p_uq = STRBUF_INIT;
 	unsigned char sha1[20];
 	int line_termination = '\n';
 	const struct option option[] = {
@@ -80,45 +118,9 @@ int cmd_mktree(int ac, const char **av, const char *prefix)
 
 	ac = parse_options(ac, av, option, mktree_usage, 0);
 
-	while (strbuf_getline(&sb, stdin, line_termination) != EOF) {
-		char *ptr, *ntr;
-		unsigned mode;
-		enum object_type type;
-		char *path;
-
-		ptr = sb.buf;
-		/*
-		 * Read non-recursive ls-tree output format:
-		 *     mode SP type SP sha1 TAB name
-		 */
-		mode = strtoul(ptr, &ntr, 8);
-		if (ptr == ntr || !ntr || *ntr != ' ')
-			die("input format error: %s", sb.buf);
-		ptr = ntr + 1; /* type */
-		ntr = strchr(ptr, ' ');
-		if (!ntr || sb.buf + sb.len <= ntr + 40 ||
-		    ntr[41] != '\t' ||
-		    get_sha1_hex(ntr + 1, sha1))
-			die("input format error: %s", sb.buf);
-		type = sha1_object_info(sha1, NULL);
-		if (type < 0)
-			die("object %s unavailable", sha1_to_hex(sha1));
-		*ntr++ = 0; /* now at the beginning of SHA1 */
-		if (type != type_from_string(ptr))
-			die("object type %s mismatch (%s)", ptr, typename(type));
-
-		path = ntr + 41;  /* at the beginning of name */
-		if (line_termination && path[0] == '"') {
-			strbuf_reset(&p_uq);
-			if (unquote_c_style(&p_uq, path, NULL)) {
-				die("invalid quoting");
-			}
-			path = p_uq.buf;
-		}
-
-		append_to_tree(mode, sha1, path);
-	}
-	strbuf_release(&p_uq);
+	while (strbuf_getline(&sb, stdin, line_termination) != EOF)
+		mktree_line(sb.buf, sb.len, line_termination);
+
 	strbuf_release(&sb);
 
 	write_tree(sha1);
-- 
1.6.3.9.g6345d

  reply	other threads:[~2009-05-10 18:50 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-05-10 13:41 questions about git-mktree Jon Seymour
2009-05-10 15:12 ` Uwe Kleine-König
2009-05-10 15:27   ` A Large Angry SCM
2009-05-10 15:31   ` Jakub Narebski
2009-05-10 16:39 ` Junio C Hamano
2009-05-10 17:10   ` Junio C Hamano
2009-05-10 18:49     ` [PATCH 0/6] Modernize mktree somewhat Junio C Hamano
2009-05-10 18:49       ` [PATCH 1/6] build-in git-mktree Junio C Hamano
2009-05-10 18:49         ` [PATCH 2/6] mktree: use parse-options Junio C Hamano
2009-05-10 18:49           ` Junio C Hamano [this message]
2009-05-10 18:49             ` [PATCH 4/6] mktree: do not barf on a submodule commit Junio C Hamano
2009-05-10 18:49               ` [PATCH 5/6] t1010: add mktree test Junio C Hamano
2009-05-10 18:49                 ` [PATCH 6/6] mktree --missing: allow missing objects Junio C Hamano
2009-05-10 22:13                   ` René Scharfe
2009-05-11  0:29                     ` Junio C Hamano
2009-05-10 18:54     ` questions about git-mktree - [PATCH] proposed '--batch' option Josh Micich

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=1241981391-19639-4-git-send-email-gitster@pobox.com \
    --to=gitster@pobox.com \
    --cc=git@vger.kernel.org \
    /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 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 NNTP newsgroup(s).