mark parsing in fast-import

Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
From: Jörg
Date: Sunday, April 20, 2008 - 4:44 pm

Hallo Shawn,

Shawn O. Pearce schrieb am Mon 14. Apr, 19:29 (-0400):
 of fast-import in that point
=3D :0 and =E2=80=9C:-12=E2=80=9D

What about this:

diff --git a/fast-import.c b/fast-import.c
index 73e5439..f60e4ab 100644
--- a/fast-import.c
+++ b/fast-import.c
@@ -1690,10 +1690,31 @@ static void skip_optional_lf(void)
 		ungetc(term_char, stdin);
 }
=20
+static inline int parse_mark(const const char *str, uintmax_t* mark,
+	char **after_mark)
+{
+	if (!str || str[0] !=3D ':' || !isdigit(str[1]))
+		return 1;
+
+	char *am;
+	const uintmax_t m =3D strtoumax(&str[1], &am, 10);
+	if (errno =3D=3D 0) {
+		*mark =3D m;
+		*after_mark =3D am;
+		return 0;
+	}
+	return 1;
+}
+
 static void cmd_mark(void)
 {
-	if (!prefixcmp(command_buf.buf, "mark :")) {
-		next_mark =3D strtoumax(command_buf.buf + 6, NULL, 10);
+	uintmax_t mark =3D 0;
+	char *after_mark =3D NULL;
+
+	if (!prefixcmp(command_buf.buf, "mark ") &&
+		parse_mark(&command_buf.buf[5], &mark, &after_mark) &&
+		*after_mark =3D=3D '\0') {
+		next_mark =3D mark;
 		read_next_command();
 	}
 	else
@@ -1878,7 +1899,10 @@ static void file_change_m(struct branch *b)
=20
 	if (*p =3D=3D ':') {
 		char *x;
-		oe =3D find_mark(strtoumax(p + 1, &x, 10));
+		uintmax_t m;
+		if (parse_mark(p, &m, &x))
+			die("Invalid mark: %s", p);
+		oe =3D find_mark(m);
 		hashcpy(sha1, oe->sha1);
 		p =3D x;
 	} else if (!prefixcmp(p, "inline")) {
@@ -2045,7 +2069,11 @@ static int cmd_from(struct branch *b)
 		hashcpy(b->branch_tree.versions[0].sha1, t);
 		hashcpy(b->branch_tree.versions[1].sha1, t);
 	} else if (*from =3D=3D ':') {
-		uintmax_t idnum =3D strtoumax(from + 1, NULL, 10);
+		char *after_mark;
+		uintmax_t idnum;
+		if (parse_mark(from, &idnum, &after_mark) ||
+			*after_mark !=3D '\0')
+			die("Not a valid mark: %s", from);
 		struct object_entry *oe =3D find_mark(idnum);
 		if (oe->type !=3D OBJ_COMMIT)
 			die("Mark :%" PRIuMAX " not a commit", idnum);
@@ -2080,7 +2108,11 @@ static struct hash_list *cmd_merge(unsigned int *cou=
nt)
 		if (s)
 			hashcpy(n->sha1, s->sha1);
 		else if (*from =3D=3D ':') {
-			uintmax_t idnum =3D strtoumax(from + 1, NULL, 10);
+			char *after_mark;
+			uintmax_t idnum;
+			if (parse_mark(from, &idnum, &after_mark) ||
+				*after_mark !=3D '\0')
+				die("Not a valid mark: %s", from);
 			struct object_entry *oe =3D find_mark(idnum);
 			if (oe->type !=3D OBJ_COMMIT)
 				die("Mark :%" PRIuMAX " not a commit", idnum);
@@ -2228,7 +2260,10 @@ static void cmd_new_tag(void)
 		hashcpy(sha1, s->sha1);
 	} else if (*from =3D=3D ':') {
 		struct object_entry *oe;
-		from_mark =3D strtoumax(from + 1, NULL, 10);
+		char *after_mark;
+		if (parse_mark(from, &from_mark, &after_mark) ||
+			*after_mark !=3D '\0')
+			die("Not a valid mark: %s", from);
 		oe =3D find_mark(from_mark);
 		if (oe->type !=3D OBJ_COMMIT)
 			die("Mark :%" PRIuMAX " not a commit", from_mark);
@@ -2333,9 +2368,8 @@ static void import_marks(const char *input_file)
 		if (line[0] !=3D ':' || !end)
 			die("corrupt mark line: %s", line);
 		*end =3D 0;
-		mark =3D strtoumax(line + 1, &end, 10);
-		if (!mark || end =3D=3D line + 1
-			|| *end !=3D ' ' || get_sha1(end + 1, sha1))
+		if (parse_mark(line, &mark, &end) || !mark ||
+			*end !=3D ' ' || get_sha1(end + 1, sha1))
 			die("corrupt mark line: %s", line);
 		e =3D find_object(sha1);
 		if (!e) {

Bye, J=C3=B6rg.
--=20
Wer eher stirbt ist l=C3=A4nger tot.
    	 	    	       			(Un B. Kant)
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

Messages in current thread:
[PATCH 1/4] Move redo merge code in a function, Jörg Sommer, (Sun Mar 23, 2:42 pm)
[PATCH 2/4] Rework redo_merge, Jörg Sommer, (Sun Mar 23, 2:42 pm)
Re: [PATCH 1/4] Move redo merge code in a function, Johannes Schindelin, (Sun Mar 23, 3:26 pm)
Re: [PATCH 2/4] Rework redo_merge, Johannes Schindelin, (Sun Mar 23, 3:29 pm)
Re: [PATCH 3/4] Add a function for get the parents of a commit, Johannes Schindelin, (Sun Mar 23, 3:33 pm)
Re: [PATCH 4/4] git-rebase -i: New option to support rebas ..., Johannes Schindelin, (Sun Mar 23, 3:41 pm)
Re: [PATCH 4/4] git-rebase -i: New option to support rebas ..., Johannes Schindelin, (Mon Mar 24, 6:08 am)
[PATCH/RFC 04/10] Move redo merge code in a function, Jörg Sommer, (Wed Apr 9, 4:58 pm)
[PATCH/RFC 05/10] Rework redo_merge, Jörg Sommer, (Wed Apr 9, 4:58 pm)
[PATCH/RFC 09/10] Select all lines with fake-editor, Jörg Sommer, (Wed Apr 9, 4:58 pm)
[PATCH v2 06/13] Move redo merge code in a function, Jörg Sommer, (Sun Apr 13, 5:21 pm)
[PATCH v2 09/13] Select all lines with fake-editor, Jörg Sommer, (Sun Apr 13, 5:21 pm)
[PATCH v2 11/13] Add option --first-parent, Jörg Sommer, (Sun Apr 13, 5:21 pm)
[PATCH v2 13/13] Add option --preserve-tags, Jörg Sommer, (Sun Apr 13, 5:21 pm)
[PATCH v2.1] Teach rebase interactive the mark command, Jörg Sommer, (Mon Apr 14, 3:39 am)
Re: [PATCH v2.1] Teach rebase interactive the mark command, Shawn O. Pearce, (Mon Apr 14, 4:29 pm)
mark parsing in fast-import, Jörg, (Sun Apr 20, 4:44 pm)
Re: mark parsing in fast-import, Shawn O. Pearce, (Sun Apr 20, 5:26 pm)
Re: mark parsing in fast-import, Jörg, (Mon Apr 21, 1:41 am)
Re: mark parsing in fast-import, Shawn O. Pearce, (Mon Apr 21, 4:59 pm)
Re: [PATCH v2 04/13] Teach rebase interactive the mark command, Johannes Schindelin, (Tue Apr 22, 1:52 am)
Re: mark parsing in fast-import, Jörg, (Tue Apr 22, 2:39 am)
Re: [PATCH v2 04/13] Teach rebase interactive the mark command, Johannes Schindelin, (Tue Apr 22, 3:31 am)
Re: mark parsing in fast-import, Shawn O. Pearce, (Tue Apr 22, 4:15 pm)
[PATCH v2] Make mark parsing much more restrictive, Jörg Sommer, (Fri Apr 25, 2:04 am)
[PATCH v2.2] Teach rebase interactive the mark command, Jörg Sommer, (Fri Apr 25, 2:44 am)
Re: [PATCH v2.2] Teach rebase interactive the mark command, Junio C Hamano, (Sat Apr 26, 11:13 pm)