static int parse_header_start(git_patch_parsed *patch, git_patch_parse_ctx *ctx) { if (parse_header_path(&patch->header_old_path, ctx) < 0) return git_parse_err("corrupt old path in git diff header at line %"PRIuZ, ctx->parse_ctx.line_num); if (git_parse_advance_ws(&ctx->parse_ctx) < 0 || parse_header_path(&patch->header_new_path, ctx) < 0) return git_parse_err("corrupt new path in git diff header at line %"PRIuZ, ctx->parse_ctx.line_num); /* * We cannot expect to be able to always parse paths correctly at this * point. Due to the possibility of unquoted names, whitespaces in * filenames and custom prefixes we have to allow that, though, and just * proceeed here. We then hope for the "---" and "+++" lines to fix that * for us. */ if (!git_parse_ctx_contains(&ctx->parse_ctx, "\n", 1)) { git_parse_advance_chars(&ctx->parse_ctx, ctx->parse_ctx.line_len - 1); git__free(patch->header_old_path); patch->header_old_path = NULL; git__free(patch->header_new_path); patch->header_new_path = NULL; } return 0; }
static int parse_header_git_newpath( git_patch_parsed *patch, git_patch_parse_ctx *ctx) { return parse_header_path(&patch->new_path, ctx); }
static int parse_header_git( git_patch_parsed *patch, git_patch_parse_ctx *ctx) { size_t i; int error = 0; /* Parse the diff --git line */ if (parse_advance_expected_s(ctx, "diff --git ") < 0) return parse_err("corrupt git diff header at line %d", ctx->line_num); if (parse_header_path(&patch->header_old_path, ctx) < 0) return parse_err("corrupt old path in git diff header at line %d", ctx->line_num); if (parse_advance_ws(ctx) < 0 || parse_header_path(&patch->header_new_path, ctx) < 0) return parse_err("corrupt new path in git diff header at line %d", ctx->line_num); /* Parse remaining header lines */ for (parse_advance_line(ctx); ctx->remain_len > 0; parse_advance_line(ctx)) { bool found = false; if (ctx->line_len == 0 || ctx->line[ctx->line_len - 1] != '\n') break; for (i = 0; i < ARRAY_SIZE(header_git_ops); i++) { const header_git_op *op = &header_git_ops[i]; size_t op_len = strlen(op->str); if (memcmp(ctx->line, op->str, min(op_len, ctx->line_len)) != 0) continue; /* Do not advance if this is the patch separator */ if (op->fn == NULL) goto done; parse_advance_chars(ctx, op_len); if ((error = op->fn(patch, ctx)) < 0) goto done; parse_advance_ws(ctx); parse_advance_expected_s(ctx, "\n"); if (ctx->line_len > 0) { error = parse_err("trailing data at line %d", ctx->line_num); goto done; } found = true; break; } if (!found) { error = parse_err("invalid patch header at line %d", ctx->line_num); goto done; } } done: return error; }
static int parse_header_git_oldpath( git_patch_parsed *patch, git_patch_parse_ctx *ctx) { return parse_header_path(&patch->old_path, ctx); }