git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v3] commit.c: Replace starts_with() with skip_prefix()

From
Karthik Nayak <karthik.188@gmail.com>
Date
Mar 6, 2014, 17:05 UTC
Message-ID
<1394125521-9341-1-git-send-email-karthik.188@gmail.com>

Replace all instances of starts_with() by skip_prefix(), which can not only be used to check presence of a prefix, but also used further on as it returns the string after the prefix, if the prefix is present. And also manages to do, what the current code does in two steps.

Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
---
Hello Eric,
In this patch, I have:
1. Fixed the improper placement of buf_date , initialised to a NULL string.
2. Fixed whitespace.
3. Better naming as per your suggestion.
4. Fixed the initilisation before the if statement.
Thanks for your suggestion.
---
 commit.c | 22 +++++++++++-----------
 1 file changed, 11 insertions(+), 11 deletions(-)
diff --git a/commit.c b/commit.c
index 6bf4fe0..4144e00 100644
--- a/commit.c
+++ b/commit.c
@@ -553,6 +553,7 @@ static void record_author_date(struct author_date_slab *author_date,
 	struct ident_split ident;
 	char *date_end;
 	unsigned long date;
+	const char *buf_date;
 
 	if (!commit->buffer) {
 		unsigned long size;
@@ -565,15 +566,15 @@ static void record_author_date(struct author_date_slab *author_date,
 	for (buf = commit->buffer ? commit->buffer : buffer;
 	     buf;
 	     buf = line_end + 1) {
+		buf_date = skip_prefix(buf, "author ");
 		line_end = strchrnul(buf, '\n');
-		if (!starts_with(buf, "author ")) {
+		if (!buf_date) {
 			if (!line_end[0] || line_end[1] == '\n')
 				return; /* end of header */
 			continue;
 		}
-		if (split_ident_line(&ident,
-				     buf + strlen("author "),
-				     line_end - (buf + strlen("author "))) ||
+		if (split_ident_line(&ident, buf_date,
+				     line_end - buf_date) ||
 		    !ident.date_begin || !ident.date_end)
 			goto fail_exit; /* malformed "author" line */
 		break;
@@ -1098,6 +1099,7 @@ int parse_signed_commit(const unsigned char *sha1,
 	char *buffer = read_sha1_file(sha1, &type, &size);
 	int in_signature, saw_signature = -1;
 	char *line, *tail;
+	const char *gpg_sig;
 
 	if (!buffer || type != OBJ_COMMIT)
 		goto cleanup;
@@ -1113,9 +1115,9 @@ int parse_signed_commit(const unsigned char *sha1,
 		next = next ? next + 1 : tail;
 		if (in_signature && line[0] == ' ')
 			sig = line + 1;
-		else if (starts_with(line, gpg_sig_header) &&
-			 line[gpg_sig_header_len] == ' ')
-			sig = line + gpg_sig_header_len + 1;
+		else if ((gpg_sig = skip_prefix(line, gpg_sig_header))
+			  && gpg_sig[0] == ' ')
+			sig = gpg_sig + 1;
 		if (sig) {
 			strbuf_add(signature, sig, next - sig);
 			saw_signature = 1;
@@ -1193,10 +1195,8 @@ static void parse_gpg_output(struct signature_check *sigc)
 	for (i = 0; i < ARRAY_SIZE(sigcheck_gpg_status); i++) {
 		const char *found, *next;
 
-		if (starts_with(buf, sigcheck_gpg_status[i].check + 1)) {
-			/* At the very beginning of the buffer */
-			found = buf + strlen(sigcheck_gpg_status[i].check + 1);
-		} else {
+		found = skip_prefix(buf, sigcheck_gpg_status[i].check + 1);
+		if (!found) {
 			found = strstr(buf, sigcheck_gpg_status[i].check);
 			if (!found)
 				continue;
-- 
1.9.0.138.g2de3478
Next: Junio C Hamano
Message 1 of 7 in “commit.c: Replace starts_with() with skip_prefix()”
  1. commit.c: Replace starts_with() with skip_prefix()Karthik Nayak, Mar 6, 2014
  2. Junio C HamanoMar 6, 2014
  3. Junio C HamanoMar 6, 2014
  4. Eric SunshineMar 7, 2014
  5. karthik nayakMar 7, 2014
  6. Eric SunshineMar 9, 2014
  7. karthik nayakMar 9, 2014

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.