git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Patryk Obara <patryk.obara@gmail.com>
To: git@vger.kernel.org, Junio C Hamano <gitster@pobox.com>,
	Stefan Beller <sbeller@google.com>
Cc: "brian m. carlson" <sandals@crustytoothpaste.net>,
	Jeff King <peff@peff.net>
Subject: [PATCH v3 4/4] commit: rewrite read_graft_line
Date: Fri, 18 Aug 2017 03:59:38 +0200	[thread overview]
Message-ID: <cb98970b3f6c175321f52efb65deb48f9cfeabae.1503020338.git.patryk.obara@gmail.com> (raw)
In-Reply-To: <cover.1503020338.git.patryk.obara@gmail.com>
In-Reply-To: <cover.1503020338.git.patryk.obara@gmail.com>

Determine the number of object_id's to parse in a single graft line by
counting separators (whitespace characters) instead of dividing by
length of hash representation.

This way graft parsing code can support different sizes of hashes
without any further code adaptations.

Signed-off-by: Patryk Obara <patryk.obara@gmail.com>
---
 commit.c | 28 +++++++++++++---------------
 1 file changed, 13 insertions(+), 15 deletions(-)

diff --git a/commit.c b/commit.c
index 61528a5..46ee2db 100644
--- a/commit.c
+++ b/commit.c
@@ -137,29 +137,27 @@ int register_commit_graft(struct commit_graft *graft, int ignore_dups)
 struct commit_graft *read_graft_line(struct strbuf *line)
 {
 	/* The format is just "Commit Parent1 Parent2 ...\n" */
-	int i, len;
-	char *buf = line->buf;
+	int i, n;
+	const char *tail = NULL;
 	struct commit_graft *graft = NULL;
-	const int entry_size = GIT_SHA1_HEXSZ + 1;
 
 	strbuf_rtrim(line);
 	if (line->buf[0] == '#' || line->len == 0)
 		return NULL;
-	len = line->len;
-	if ((len + 1) % entry_size)
-		goto bad_graft_data;
-	i = (len + 1) / entry_size - 1;
+	/* count number of blanks to determine size of array to allocate */
+	for (i = 0, n = 0; i < line->len; i++)
+		if (isspace(line->buf[i]))
+			n++;
 	graft = xmalloc(st_add(sizeof(*graft),
-	                       st_mult(sizeof(struct object_id), i)));
-	graft->nr_parent = i;
-	if (get_oid_hex(buf, &graft->oid))
+	                       st_mult(sizeof(struct object_id), n)));
+	graft->nr_parent = n;
+	if (parse_oid_hex(line->buf, &graft->oid, &tail))
 		goto bad_graft_data;
-	for (i = GIT_SHA1_HEXSZ; i < len; i += entry_size) {
-		if (buf[i] != ' ')
-			goto bad_graft_data;
-		if (get_sha1_hex(buf + i + 1, graft->parent[i/entry_size].hash))
+	for (i = 0; i < graft->nr_parent; i++)
+		if (!isspace(*tail++) || parse_oid_hex(tail, &graft->parent[i], &tail))
 			goto bad_graft_data;
-	}
+	if (tail[0] != '\0')
+		goto bad_graft_data;
 	return graft;
 
 bad_graft_data:
-- 
2.9.5


  parent reply	other threads:[~2017-08-18  1:59 UTC|newest]

Thread overview: 59+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-08-15 11:49 [PATCH 0/5] Modernize read_graft_line implementation Patryk Obara
2017-08-15 11:49 ` [PATCH 1/5] cache: extend object_id size to sha3-256 Patryk Obara
2017-08-15 11:49 ` [PATCH 2/5] sha1_file: fix hardcoded size in null_sha1 Patryk Obara
2017-08-15 18:23   ` Junio C Hamano
2017-08-15 18:29     ` Stefan Beller
2017-08-15 19:02       ` Junio C Hamano
2017-08-16 12:11         ` Patryk Obara
2017-08-16 19:32           ` Junio C Hamano
2017-08-15 11:49 ` [PATCH 3/5] commit: replace the raw buffer with strbuf in read_graft_line Patryk Obara
2017-08-15 17:02   ` Stefan Beller
2017-08-16 12:24     ` Patryk Obara
2017-08-16 22:59       ` brian m. carlson
2017-08-17  5:55         ` Jeff King
2017-08-17 21:17           ` Junio C Hamano
2017-08-17 21:38             ` Patryk Obara
2017-08-15 18:25   ` Junio C Hamano
2017-08-15 11:49 ` [PATCH 4/5] commit: implement free_commit_graft Patryk Obara
2017-08-15 17:04   ` Stefan Beller
2017-08-15 18:26   ` Junio C Hamano
2017-08-16 12:28     ` Patryk Obara
2017-08-15 11:49 ` [PATCH 5/5] commit: rewrite read_graft_line Patryk Obara
2017-08-15 17:11   ` Stefan Beller
2017-08-15 18:30   ` Junio C Hamano
2017-08-16 13:07     ` Patryk Obara
2017-08-16 19:39       ` Junio C Hamano
2017-08-15 17:19 ` [PATCH 0/5] Modernize read_graft_line implementation Stefan Beller
2017-08-16 17:58 ` [PATCH v2 0/4] " Patryk Obara
2017-08-16 17:58   ` [PATCH v2 1/4] sha1_file: fix hardcoded size in null_sha1 Patryk Obara
2017-08-16 19:46     ` Junio C Hamano
2017-08-16 17:58   ` [PATCH v2 2/4] commit: replace the raw buffer with strbuf in read_graft_line Patryk Obara
2017-08-16 17:58   ` [PATCH v2 3/4] commit: implement free_commit_graft Patryk Obara
2017-08-16 17:58   ` [PATCH v2 4/4] commit: rewrite read_graft_line Patryk Obara
2017-08-17 21:20     ` Junio C Hamano
2017-08-17 21:42       ` Patryk Obara
2017-08-18  1:59   ` [PATCH v3 0/4] Modernize read_graft_line implementation Patryk Obara
2017-08-18  1:59     ` [PATCH v3 1/4] sha1_file: fix definition of null_sha1 Patryk Obara
2017-08-18  1:59     ` [PATCH v3 2/4] commit: replace the raw buffer with strbuf in read_graft_line Patryk Obara
2017-08-18  6:29       ` Jeff King
2017-08-18 10:12         ` Patryk Obara
2017-08-18 11:50           ` Jeff King
2017-08-18  1:59     ` [PATCH v3 3/4] commit: allocate array using object_id size Patryk Obara
2017-08-18  1:59     ` Patryk Obara [this message]
2017-08-18  6:43       ` [PATCH v3 4/4] commit: rewrite read_graft_line Jeff King
2017-08-18  7:44         ` Junio C Hamano
2017-08-18 11:30           ` Patryk Obara
2017-08-18 11:45             ` Jeff King
2017-08-18 16:44             ` Junio C Hamano
2017-08-18 17:05               ` Patryk Obara
2017-08-18 18:29                 ` Junio C Hamano
2017-08-18  2:20     ` [PATCH v3 0/4] Modernize read_graft_line implementation Junio C Hamano
2017-08-18 18:33     ` [PATCH v4 " Patryk Obara
2017-08-18 18:33       ` [PATCH v4 1/4] sha1_file: fix definition of null_sha1 Patryk Obara
2017-08-18 18:33       ` [PATCH v4 2/4] commit: replace the raw buffer with strbuf in read_graft_line Patryk Obara
2017-08-18 18:33       ` [PATCH v4 3/4] commit: allocate array using object_id size Patryk Obara
2017-08-18 18:33       ` [PATCH v4 4/4] commit: rewrite read_graft_line Patryk Obara
2017-08-18 18:38         ` Patryk Obara
2017-08-18 19:12           ` Junio C Hamano
2017-08-18 19:33             ` Patryk Obara
2017-08-18 19:47           ` 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

  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=cb98970b3f6c175321f52efb65deb48f9cfeabae.1503020338.git.patryk.obara@gmail.com \
    --to=patryk.obara@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=peff@peff.net \
    --cc=sandals@crustytoothpaste.net \
    --cc=sbeller@google.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).