Skip to content

Commit e814c39

Browse files
peffgitster
authored andcommitted
fast-import: refactor parsing of spaces
When we see a file change in a commit, we expect one of: 1. A mark. 2. An "inline" keyword. 3. An object sha1. The handling of spaces is inconsistent between the three options. Option 1 calls a sub-function which checks for the space, but doesn't parse past it. Option 2 parses the space, then deliberately avoids moving the pointer past it. Option 3 detects the space locally but doesn't move past it. This is confusing, because it looks like option 1 forgets to check for the space (it's just buried). And option 2 checks for "inline ", but only moves strlen("inline") characters forward, which looks like a bug but isn't. We can make this more clear by just having each branch move past the space as it is checked (and we can replace the doubled use of "inline" with a call to skip_prefix). Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0539cc0 commit e814c39

File tree

1 file changed

+7
-13
lines changed

1 file changed

+7
-13
lines changed

fast-import.c

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2269,7 +2269,7 @@ static uintmax_t parse_mark_ref_space(const char **p)
22692269
char *end;
22702270

22712271
mark = parse_mark_ref(*p, &end);
2272-
if (*end != ' ')
2272+
if (*end++ != ' ')
22732273
die("Missing space after mark: %s", command_buf.buf);
22742274
*p = end;
22752275
return mark;
@@ -2304,20 +2304,17 @@ static void file_change_m(const char *p, struct branch *b)
23042304
if (*p == ':') {
23052305
oe = find_mark(parse_mark_ref_space(&p));
23062306
hashcpy(sha1, oe->idx.sha1);
2307-
} else if (starts_with(p, "inline ")) {
2307+
} else if (skip_prefix(p, "inline ", &p)) {
23082308
inline_data = 1;
23092309
oe = NULL; /* not used with inline_data, but makes gcc happy */
2310-
p += strlen("inline"); /* advance to space */
23112310
} else {
23122311
if (get_sha1_hex(p, sha1))
23132312
die("Invalid dataref: %s", command_buf.buf);
23142313
oe = find_object(sha1);
23152314
p += 40;
2316-
if (*p != ' ')
2315+
if (*p++ != ' ')
23172316
die("Missing space after SHA1: %s", command_buf.buf);
23182317
}
2319-
assert(*p == ' ');
2320-
p++; /* skip space */
23212318

23222319
strbuf_reset(&uq);
23232320
if (!unquote_c_style(&uq, p, &endp)) {
@@ -2474,20 +2471,17 @@ static void note_change_n(const char *p, struct branch *b, unsigned char *old_fa
24742471
if (*p == ':') {
24752472
oe = find_mark(parse_mark_ref_space(&p));
24762473
hashcpy(sha1, oe->idx.sha1);
2477-
} else if (starts_with(p, "inline ")) {
2474+
} else if (skip_prefix(p, "inline ", &p)) {
24782475
inline_data = 1;
24792476
oe = NULL; /* not used with inline_data, but makes gcc happy */
2480-
p += strlen("inline"); /* advance to space */
24812477
} else {
24822478
if (get_sha1_hex(p, sha1))
24832479
die("Invalid dataref: %s", command_buf.buf);
24842480
oe = find_object(sha1);
24852481
p += 40;
2486-
if (*p != ' ')
2482+
if (*p++ != ' ')
24872483
die("Missing space after SHA1: %s", command_buf.buf);
24882484
}
2489-
assert(*p == ' ');
2490-
p++; /* skip space */
24912485

24922486
/* <commit-ish> */
24932487
s = lookup_branch(p);
@@ -3003,6 +2997,8 @@ static struct object_entry *parse_treeish_dataref(const char **p)
30032997
die("Invalid dataref: %s", command_buf.buf);
30042998
e = find_object(sha1);
30052999
*p += 40;
3000+
if (*(*p)++ != ' ')
3001+
die("Missing space after tree-ish: %s", command_buf.buf);
30063002
}
30073003

30083004
while (!e || e->type != OBJ_TREE)
@@ -3054,8 +3050,6 @@ static void parse_ls(const char *p, struct branch *b)
30543050
if (!is_null_sha1(root->versions[1].sha1))
30553051
root->versions[1].mode = S_IFDIR;
30563052
load_tree(root);
3057-
if (*p++ != ' ')
3058-
die("Missing space after tree-ish: %s", command_buf.buf);
30593053
}
30603054
if (*p == '"') {
30613055
static struct strbuf uq = STRBUF_INIT;

0 commit comments

Comments
 (0)