Blame src/patch_parse.c

Packit Service 20376f
/*
Packit Service 20376f
 * Copyright (C) the libgit2 contributors. All rights reserved.
Packit Service 20376f
 *
Packit Service 20376f
 * This file is part of libgit2, distributed under the GNU GPL v2 with
Packit Service 20376f
 * a Linking Exception. For full terms see the included COPYING file.
Packit Service 20376f
 */
Packit Service 20376f
#include "git2/patch.h"
Packit Service 20376f
#include "patch.h"
Packit Service 20376f
#include "patch_parse.h"
Packit Service 20376f
#include "diff_parse.h"
Packit Service 20376f
#include "path.h"
Packit Service 20376f
Packit Service 20376f
#define parse_err(...) \
Packit Service 20376f
	( giterr_set(GITERR_PATCH, __VA_ARGS__), -1 )
Packit Service 20376f
Packit Service 20376f
typedef struct {
Packit Service 20376f
	git_patch base;
Packit Service 20376f
Packit Service 20376f
	git_patch_parse_ctx *ctx;
Packit Service 20376f
Packit Service 20376f
	/* the paths from the `diff --git` header, these will be used if this is not
Packit Service 20376f
	 * a rename (and rename paths are specified) or if no `+++`/`---` line specify
Packit Service 20376f
	 * the paths.
Packit Service 20376f
	 */
Packit Service 20376f
	char *header_old_path, *header_new_path;
Packit Service 20376f
Packit Service 20376f
	/* renamed paths are precise and are not prefixed */
Packit Service 20376f
	char *rename_old_path, *rename_new_path;
Packit Service 20376f
Packit Service 20376f
	/* the paths given in `---` and `+++` lines */
Packit Service 20376f
	char *old_path, *new_path;
Packit Service 20376f
Packit Service 20376f
	/* the prefixes from the old/new paths */
Packit Service 20376f
	char *old_prefix, *new_prefix;
Packit Service 20376f
} git_patch_parsed;
Packit Service 20376f
Packit Service 20376f
Packit Service 20376f
GIT_INLINE(bool) parse_ctx_contains(
Packit Service 20376f
	git_patch_parse_ctx *ctx, const char *str, size_t len)
Packit Service 20376f
{
Packit Service 20376f
	return (ctx->line_len >= len && memcmp(ctx->line, str, len) == 0);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
#define parse_ctx_contains_s(ctx, str) \
Packit Service 20376f
	parse_ctx_contains(ctx, str, sizeof(str) - 1)
Packit Service 20376f
Packit Service 20376f
static void parse_advance_line(git_patch_parse_ctx *ctx)
Packit Service 20376f
{
Packit Service 20376f
	ctx->line += ctx->line_len;
Packit Service 20376f
	ctx->remain_len -= ctx->line_len;
Packit Service 20376f
	ctx->line_len = git__linenlen(ctx->line, ctx->remain_len);
Packit Service 20376f
	ctx->line_num++;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static void parse_advance_chars(git_patch_parse_ctx *ctx, size_t char_cnt)
Packit Service 20376f
{
Packit Service 20376f
	ctx->line += char_cnt;
Packit Service 20376f
	ctx->remain_len -= char_cnt;
Packit Service 20376f
	ctx->line_len -= char_cnt;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int parse_advance_expected(
Packit Service 20376f
	git_patch_parse_ctx *ctx,
Packit Service 20376f
	const char *expected,
Packit Service 20376f
	size_t expected_len)
Packit Service 20376f
{
Packit Service 20376f
	if (ctx->line_len < expected_len)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	if (memcmp(ctx->line, expected, expected_len) != 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	parse_advance_chars(ctx, expected_len);
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
#define parse_advance_expected_str(ctx, str) \
Packit Service 20376f
	parse_advance_expected(ctx, str, strlen(str))
Packit Service 20376f
Packit Service 20376f
static int parse_advance_ws(git_patch_parse_ctx *ctx)
Packit Service 20376f
{
Packit Service 20376f
	int ret = -1;
Packit Service 20376f
Packit Service 20376f
	while (ctx->line_len > 0 &&
Packit Service 20376f
		ctx->line[0] != '\n' &&
Packit Service 20376f
		git__isspace(ctx->line[0])) {
Packit Service 20376f
		ctx->line++;
Packit Service 20376f
		ctx->line_len--;
Packit Service 20376f
		ctx->remain_len--;
Packit Service 20376f
		ret = 0;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return ret;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int parse_advance_nl(git_patch_parse_ctx *ctx)
Packit Service 20376f
{
Packit Service 20376f
	if (ctx->line_len != 1 || ctx->line[0] != '\n')
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	parse_advance_line(ctx);
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int header_path_len(git_patch_parse_ctx *ctx)
Packit Service 20376f
{
Packit Service 20376f
	bool inquote = 0;
Packit Service 20376f
	bool quoted = (ctx->line_len > 0 && ctx->line[0] == '"');
Packit Service 20376f
	size_t len;
Packit Service 20376f
Packit Service 20376f
	for (len = quoted; len < ctx->line_len; len++) {
Packit Service 20376f
		if (!quoted && git__isspace(ctx->line[len]))
Packit Service 20376f
			break;
Packit Service 20376f
		else if (quoted && !inquote && ctx->line[len] == '"') {
Packit Service 20376f
			len++;
Packit Service 20376f
			break;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		inquote = (!inquote && ctx->line[len] == '\\');
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return len;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int parse_header_path_buf(git_buf *path, git_patch_parse_ctx *ctx)
Packit Service 20376f
{
Packit Service 20376f
	int path_len, error = 0;
Packit Service 20376f
Packit Service 20376f
	path_len = header_path_len(ctx);
Packit Service 20376f
Packit Service 20376f
	if ((error = git_buf_put(path, ctx->line, path_len)) < 0)
Packit Service 20376f
		goto done;
Packit Service 20376f
Packit Service 20376f
	parse_advance_chars(ctx, path_len);
Packit Service 20376f
Packit Service 20376f
	git_buf_rtrim(path);
Packit Service 20376f
Packit Service 20376f
	if (path->size > 0 && path->ptr[0] == '"')
Packit Service 20376f
		error = git_buf_unquote(path);
Packit Service 20376f
Packit Service 20376f
	if (error < 0)
Packit Service 20376f
		goto done;
Packit Service 20376f
Packit Service 20376f
	git_path_squash_slashes(path);
Packit Service 20376f
Packit Service 20376f
done:
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int parse_header_path(char **out, git_patch_parse_ctx *ctx)
Packit Service 20376f
{
Packit Service 20376f
	git_buf path = GIT_BUF_INIT;
Packit Service 20376f
	int error = parse_header_path_buf(&path, ctx);
Packit Service 20376f
Packit Service 20376f
	*out = git_buf_detach(&path);
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int parse_header_git_oldpath(
Packit Service 20376f
	git_patch_parsed *patch, git_patch_parse_ctx *ctx)
Packit Service 20376f
{
Packit Service 20376f
	return parse_header_path(&patch->old_path, ctx);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int parse_header_git_newpath(
Packit Service 20376f
	git_patch_parsed *patch, git_patch_parse_ctx *ctx)
Packit Service 20376f
{
Packit Service 20376f
	return parse_header_path(&patch->new_path, ctx);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int parse_header_mode(uint16_t *mode, git_patch_parse_ctx *ctx)
Packit Service 20376f
{
Packit Service 20376f
	const char *end;
Packit Service 20376f
	int32_t m;
Packit Service 20376f
	int ret;
Packit Service 20376f
Packit Service 20376f
	if (ctx->line_len < 1 || !git__isdigit(ctx->line[0]))
Packit Service 20376f
		return parse_err("invalid file mode at line %"PRIuZ, ctx->line_num);
Packit Service 20376f
Packit Service 20376f
	if ((ret = git__strntol32(&m, ctx->line, ctx->line_len, &end, 8)) < 0)
Packit Service 20376f
		return ret;
Packit Service 20376f
Packit Service 20376f
	if (m > UINT16_MAX)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	*mode = (uint16_t)m;
Packit Service 20376f
Packit Service 20376f
	parse_advance_chars(ctx, (end - ctx->line));
Packit Service 20376f
Packit Service 20376f
	return ret;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int parse_header_oid(
Packit Service 20376f
	git_oid *oid,
Packit Service 20376f
	uint16_t *oid_len,
Packit Service 20376f
	git_patch_parse_ctx *ctx)
Packit Service 20376f
{
Packit Service 20376f
	size_t len;
Packit Service 20376f
Packit Service 20376f
	for (len = 0; len < ctx->line_len && len < GIT_OID_HEXSZ; len++) {
Packit Service 20376f
		if (!git__isxdigit(ctx->line[len]))
Packit Service 20376f
			break;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (len < GIT_OID_MINPREFIXLEN || len > GIT_OID_HEXSZ ||
Packit Service 20376f
		git_oid_fromstrn(oid, ctx->line, len) < 0)
Packit Service 20376f
		return parse_err("invalid hex formatted object id at line %"PRIuZ,
Packit Service 20376f
			ctx->line_num);
Packit Service 20376f
Packit Service 20376f
	parse_advance_chars(ctx, len);
Packit Service 20376f
Packit Service 20376f
	*oid_len = (uint16_t)len;
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int parse_header_git_index(
Packit Service 20376f
	git_patch_parsed *patch, git_patch_parse_ctx *ctx)
Packit Service 20376f
{
Packit Service 20376f
	if (parse_header_oid(&patch->base.delta->old_file.id,
Packit Service 20376f
			&patch->base.delta->old_file.id_abbrev, ctx) < 0 ||
Packit Service 20376f
		parse_advance_expected_str(ctx, "..") < 0 ||
Packit Service 20376f
		parse_header_oid(&patch->base.delta->new_file.id,
Packit Service 20376f
			&patch->base.delta->new_file.id_abbrev, ctx) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	if (ctx->line_len > 0 && ctx->line[0] == ' ') {
Packit Service 20376f
		uint16_t mode;
Packit Service 20376f
Packit Service 20376f
		parse_advance_chars(ctx, 1);
Packit Service 20376f
Packit Service 20376f
		if (parse_header_mode(&mode, ctx) < 0)
Packit Service 20376f
			return -1;
Packit Service 20376f
Packit Service 20376f
		if (!patch->base.delta->new_file.mode)
Packit Service 20376f
			patch->base.delta->new_file.mode = mode;
Packit Service 20376f
Packit Service 20376f
		if (!patch->base.delta->old_file.mode)
Packit Service 20376f
			patch->base.delta->old_file.mode = mode;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int parse_header_git_oldmode(
Packit Service 20376f
	git_patch_parsed *patch, git_patch_parse_ctx *ctx)
Packit Service 20376f
{
Packit Service 20376f
	return parse_header_mode(&patch->base.delta->old_file.mode, ctx);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int parse_header_git_newmode(
Packit Service 20376f
	git_patch_parsed *patch, git_patch_parse_ctx *ctx)
Packit Service 20376f
{
Packit Service 20376f
	return parse_header_mode(&patch->base.delta->new_file.mode, ctx);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int parse_header_git_deletedfilemode(
Packit Service 20376f
	git_patch_parsed *patch,
Packit Service 20376f
	git_patch_parse_ctx *ctx)
Packit Service 20376f
{
Packit Service 20376f
	git__free((char *)patch->base.delta->old_file.path);
Packit Service 20376f
Packit Service 20376f
	patch->base.delta->old_file.path = NULL;
Packit Service 20376f
	patch->base.delta->status = GIT_DELTA_DELETED;
Packit Service 20376f
	patch->base.delta->nfiles = 1;
Packit Service 20376f
Packit Service 20376f
	return parse_header_mode(&patch->base.delta->old_file.mode, ctx);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int parse_header_git_newfilemode(
Packit Service 20376f
	git_patch_parsed *patch,
Packit Service 20376f
	git_patch_parse_ctx *ctx)
Packit Service 20376f
{
Packit Service 20376f
	git__free((char *)patch->base.delta->new_file.path);
Packit Service 20376f
Packit Service 20376f
	patch->base.delta->new_file.path = NULL;
Packit Service 20376f
	patch->base.delta->status = GIT_DELTA_ADDED;
Packit Service 20376f
	patch->base.delta->nfiles = 1;
Packit Service 20376f
Packit Service 20376f
	return parse_header_mode(&patch->base.delta->new_file.mode, ctx);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int parse_header_rename(
Packit Service 20376f
	char **out,
Packit Service 20376f
	git_patch_parse_ctx *ctx)
Packit Service 20376f
{
Packit Service 20376f
	git_buf path = GIT_BUF_INIT;
Packit Service 20376f
Packit Service 20376f
	if (parse_header_path_buf(&path, ctx) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	/* Note: the `rename from` and `rename to` lines include the literal
Packit Service 20376f
	 * filename.  They do *not* include the prefix.  (Who needs consistency?)
Packit Service 20376f
	 */
Packit Service 20376f
	*out = git_buf_detach(&path);
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int parse_header_renamefrom(
Packit Service 20376f
	git_patch_parsed *patch, git_patch_parse_ctx *ctx)
Packit Service 20376f
{
Packit Service 20376f
	patch->base.delta->status = GIT_DELTA_RENAMED;
Packit Service 20376f
	return parse_header_rename(&patch->rename_old_path, ctx);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int parse_header_renameto(
Packit Service 20376f
	git_patch_parsed *patch, git_patch_parse_ctx *ctx)
Packit Service 20376f
{
Packit Service 20376f
	patch->base.delta->status = GIT_DELTA_RENAMED;
Packit Service 20376f
	return parse_header_rename(&patch->rename_new_path, ctx);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int parse_header_copyfrom(
Packit Service 20376f
	git_patch_parsed *patch, git_patch_parse_ctx *ctx)
Packit Service 20376f
{
Packit Service 20376f
	patch->base.delta->status = GIT_DELTA_COPIED;
Packit Service 20376f
	return parse_header_rename(&patch->rename_old_path, ctx);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int parse_header_copyto(
Packit Service 20376f
	git_patch_parsed *patch, git_patch_parse_ctx *ctx)
Packit Service 20376f
{
Packit Service 20376f
	patch->base.delta->status = GIT_DELTA_COPIED;
Packit Service 20376f
	return parse_header_rename(&patch->rename_new_path, ctx);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int parse_header_percent(uint16_t *out, git_patch_parse_ctx *ctx)
Packit Service 20376f
{
Packit Service 20376f
	int32_t val;
Packit Service 20376f
	const char *end;
Packit Service 20376f
Packit Service 20376f
	if (ctx->line_len < 1 || !git__isdigit(ctx->line[0]) ||
Packit Service 20376f
		git__strntol32(&val, ctx->line, ctx->line_len, &end, 10) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	parse_advance_chars(ctx, (end - ctx->line));
Packit Service 20376f
Packit Service 20376f
	if (parse_advance_expected_str(ctx, "%") < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	if (val > 100)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	*out = val;
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int parse_header_similarity(
Packit Service 20376f
	git_patch_parsed *patch, git_patch_parse_ctx *ctx)
Packit Service 20376f
{
Packit Service 20376f
	if (parse_header_percent(&patch->base.delta->similarity, ctx) < 0)
Packit Service 20376f
		return parse_err("invalid similarity percentage at line %"PRIuZ,
Packit Service 20376f
			ctx->line_num);
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int parse_header_dissimilarity(
Packit Service 20376f
	git_patch_parsed *patch, git_patch_parse_ctx *ctx)
Packit Service 20376f
{
Packit Service 20376f
	uint16_t dissimilarity;
Packit Service 20376f
Packit Service 20376f
	if (parse_header_percent(&dissimilarity, ctx) < 0)
Packit Service 20376f
		return parse_err("invalid similarity percentage at line %"PRIuZ,
Packit Service 20376f
			ctx->line_num);
Packit Service 20376f
Packit Service 20376f
	patch->base.delta->similarity = 100 - dissimilarity;
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
typedef struct {
Packit Service 20376f
	const char *str;
Packit Service 20376f
	int(*fn)(git_patch_parsed *, git_patch_parse_ctx *);
Packit Service 20376f
} header_git_op;
Packit Service 20376f
Packit Service 20376f
static const header_git_op header_git_ops[] = {
Packit Service 20376f
	{ "diff --git ", NULL },
Packit Service 20376f
	{ "@@ -", NULL },
Packit Service 20376f
	{ "GIT binary patch", NULL },
Packit Service 20376f
	{ "Binary files ", NULL },
Packit Service 20376f
	{ "--- ", parse_header_git_oldpath },
Packit Service 20376f
	{ "+++ ", parse_header_git_newpath },
Packit Service 20376f
	{ "index ", parse_header_git_index },
Packit Service 20376f
	{ "old mode ", parse_header_git_oldmode },
Packit Service 20376f
	{ "new mode ", parse_header_git_newmode },
Packit Service 20376f
	{ "deleted file mode ", parse_header_git_deletedfilemode },
Packit Service 20376f
	{ "new file mode ", parse_header_git_newfilemode },
Packit Service 20376f
	{ "rename from ", parse_header_renamefrom },
Packit Service 20376f
	{ "rename to ", parse_header_renameto },
Packit Service 20376f
	{ "rename old ", parse_header_renamefrom },
Packit Service 20376f
	{ "rename new ", parse_header_renameto },
Packit Service 20376f
	{ "copy from ", parse_header_copyfrom },
Packit Service 20376f
	{ "copy to ", parse_header_copyto },
Packit Service 20376f
	{ "similarity index ", parse_header_similarity },
Packit Service 20376f
	{ "dissimilarity index ", parse_header_dissimilarity },
Packit Service 20376f
};
Packit Service 20376f
Packit Service 20376f
static int parse_header_git(
Packit Service 20376f
	git_patch_parsed *patch,
Packit Service 20376f
	git_patch_parse_ctx *ctx)
Packit Service 20376f
{
Packit Service 20376f
	size_t i;
Packit Service 20376f
	int error = 0;
Packit Service 20376f
Packit Service 20376f
	/* Parse the diff --git line */
Packit Service 20376f
	if (parse_advance_expected_str(ctx, "diff --git ") < 0)
Packit Service 20376f
		return parse_err("corrupt git diff header at line %"PRIuZ, ctx->line_num);
Packit Service 20376f
Packit Service 20376f
	if (parse_header_path(&patch->header_old_path, ctx) < 0)
Packit Service 20376f
		return parse_err("corrupt old path in git diff header at line %"PRIuZ,
Packit Service 20376f
			ctx->line_num);
Packit Service 20376f
Packit Service 20376f
	if (parse_advance_ws(ctx) < 0 ||
Packit Service 20376f
		parse_header_path(&patch->header_new_path, ctx) < 0)
Packit Service 20376f
		return parse_err("corrupt new path in git diff header at line %"PRIuZ,
Packit Service 20376f
			ctx->line_num);
Packit Service 20376f
Packit Service 20376f
	/* Parse remaining header lines */
Packit Service 20376f
	for (parse_advance_line(ctx);
Packit Service 20376f
		ctx->remain_len > 0;
Packit Service 20376f
		parse_advance_line(ctx)) {
Packit Service 20376f
Packit Service 20376f
		bool found = false;
Packit Service 20376f
Packit Service 20376f
		if (ctx->line_len == 0 || ctx->line[ctx->line_len - 1] != '\n')
Packit Service 20376f
			break;
Packit Service 20376f
Packit Service 20376f
		for (i = 0; i < ARRAY_SIZE(header_git_ops); i++) {
Packit Service 20376f
			const header_git_op *op = &header_git_ops[i];
Packit Service 20376f
			size_t op_len = strlen(op->str);
Packit Service 20376f
Packit Service 20376f
			if (memcmp(ctx->line, op->str, min(op_len, ctx->line_len)) != 0)
Packit Service 20376f
				continue;
Packit Service 20376f
Packit Service 20376f
			/* Do not advance if this is the patch separator */
Packit Service 20376f
			if (op->fn == NULL)
Packit Service 20376f
				goto done;
Packit Service 20376f
Packit Service 20376f
			parse_advance_chars(ctx, op_len);
Packit Service 20376f
Packit Service 20376f
			if ((error = op->fn(patch, ctx)) < 0)
Packit Service 20376f
				goto done;
Packit Service 20376f
Packit Service 20376f
			parse_advance_ws(ctx);
Packit Service 20376f
Packit Service 20376f
			if (parse_advance_expected_str(ctx, "\n") < 0 ||
Packit Service 20376f
			    ctx->line_len > 0) {
Packit Service 20376f
				error = parse_err("trailing data at line %"PRIuZ, ctx->line_num);
Packit Service 20376f
				goto done;
Packit Service 20376f
			}
Packit Service 20376f
Packit Service 20376f
			found = true;
Packit Service 20376f
			break;
Packit Service 20376f
		}
Packit Service 20376f
		
Packit Service 20376f
		if (!found) {
Packit Service 20376f
			error = parse_err("invalid patch header at line %"PRIuZ,
Packit Service 20376f
				ctx->line_num);
Packit Service 20376f
			goto done;
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
done:
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int parse_number(git_off_t *out, git_patch_parse_ctx *ctx)
Packit Service 20376f
{
Packit Service 20376f
	const char *end;
Packit Service 20376f
	int64_t num;
Packit Service 20376f
Packit Service 20376f
	if (!git__isdigit(ctx->line[0]))
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	if (git__strntol64(&num, ctx->line, ctx->line_len, &end, 10) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	if (num < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	*out = num;
Packit Service 20376f
	parse_advance_chars(ctx, (end - ctx->line));
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int parse_int(int *out, git_patch_parse_ctx *ctx)
Packit Service 20376f
{
Packit Service 20376f
	git_off_t num;
Packit Service 20376f
Packit Service 20376f
	if (parse_number(&num, ctx) < 0 || !git__is_int(num))
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	*out = (int)num;
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int parse_hunk_header(
Packit Service 20376f
	git_patch_hunk *hunk,
Packit Service 20376f
	git_patch_parse_ctx *ctx)
Packit Service 20376f
{
Packit Service 20376f
	const char *header_start = ctx->line;
Packit Service 20376f
Packit Service 20376f
	hunk->hunk.old_lines = 1;
Packit Service 20376f
	hunk->hunk.new_lines = 1;
Packit Service 20376f
Packit Service 20376f
	if (parse_advance_expected_str(ctx, "@@ -") < 0 ||
Packit Service 20376f
		parse_int(&hunk->hunk.old_start, ctx) < 0)
Packit Service 20376f
		goto fail;
Packit Service 20376f
Packit Service 20376f
	if (ctx->line_len > 0 && ctx->line[0] == ',') {
Packit Service 20376f
		if (parse_advance_expected_str(ctx, ",") < 0 ||
Packit Service 20376f
			parse_int(&hunk->hunk.old_lines, ctx) < 0)
Packit Service 20376f
			goto fail;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (parse_advance_expected_str(ctx, " +") < 0 ||
Packit Service 20376f
		parse_int(&hunk->hunk.new_start, ctx) < 0)
Packit Service 20376f
		goto fail;
Packit Service 20376f
Packit Service 20376f
	if (ctx->line_len > 0 && ctx->line[0] == ',') {
Packit Service 20376f
		if (parse_advance_expected_str(ctx, ",") < 0 ||
Packit Service 20376f
			parse_int(&hunk->hunk.new_lines, ctx) < 0)
Packit Service 20376f
			goto fail;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (parse_advance_expected_str(ctx, " @@") < 0)
Packit Service 20376f
		goto fail;
Packit Service 20376f
Packit Service 20376f
	parse_advance_line(ctx);
Packit Service 20376f
Packit Service 20376f
	if (!hunk->hunk.old_lines && !hunk->hunk.new_lines)
Packit Service 20376f
		goto fail;
Packit Service 20376f
Packit Service 20376f
	hunk->hunk.header_len = ctx->line - header_start;
Packit Service 20376f
	if (hunk->hunk.header_len > (GIT_DIFF_HUNK_HEADER_SIZE - 1))
Packit Service 20376f
		return parse_err("oversized patch hunk header at line %"PRIuZ,
Packit Service 20376f
			ctx->line_num);
Packit Service 20376f
Packit Service 20376f
	memcpy(hunk->hunk.header, header_start, hunk->hunk.header_len);
Packit Service 20376f
	hunk->hunk.header[hunk->hunk.header_len] = '\0';
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
Packit Service 20376f
fail:
Packit Service 20376f
	giterr_set(GITERR_PATCH, "invalid patch hunk header at line %"PRIuZ,
Packit Service 20376f
		ctx->line_num);
Packit Service 20376f
	return -1;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int parse_hunk_body(
Packit Service 20376f
	git_patch_parsed *patch,
Packit Service 20376f
	git_patch_hunk *hunk,
Packit Service 20376f
	git_patch_parse_ctx *ctx)
Packit Service 20376f
{
Packit Service 20376f
	git_diff_line *line;
Packit Service 20376f
	int error = 0;
Packit Service 20376f
Packit Service 20376f
	int oldlines = hunk->hunk.old_lines;
Packit Service 20376f
	int newlines = hunk->hunk.new_lines;
Packit Service 20376f
Packit Service 20376f
	for (;
Packit Service 20376f
		ctx->remain_len > 1 &&
Packit Service 20376f
		(oldlines || newlines) &&
Packit Service 20376f
		(ctx->remain_len <= 4 || memcmp(ctx->line, "@@ -", 4) != 0);
Packit Service 20376f
		parse_advance_line(ctx)) {
Packit Service 20376f
Packit Service 20376f
		int origin;
Packit Service 20376f
		int prefix = 1;
Packit Service 20376f
Packit Service 20376f
		if (ctx->line_len == 0 || ctx->line[ctx->line_len - 1] != '\n') {
Packit Service 20376f
			error = parse_err("invalid patch instruction at line %"PRIuZ,
Packit Service 20376f
				ctx->line_num);
Packit Service 20376f
			goto done;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		switch (ctx->line[0]) {
Packit Service 20376f
		case '\n':
Packit Service 20376f
			prefix = 0;
Packit Service 20376f
Packit Service 20376f
		case ' ':
Packit Service 20376f
			origin = GIT_DIFF_LINE_CONTEXT;
Packit Service 20376f
			oldlines--;
Packit Service 20376f
			newlines--;
Packit Service 20376f
			break;
Packit Service 20376f
Packit Service 20376f
		case '-':
Packit Service 20376f
			origin = GIT_DIFF_LINE_DELETION;
Packit Service 20376f
			oldlines--;
Packit Service 20376f
			break;
Packit Service 20376f
Packit Service 20376f
		case '+':
Packit Service 20376f
			origin = GIT_DIFF_LINE_ADDITION;
Packit Service 20376f
			newlines--;
Packit Service 20376f
			break;
Packit Service 20376f
Packit Service 20376f
		default:
Packit Service 20376f
			error = parse_err("invalid patch hunk at line %"PRIuZ, ctx->line_num);
Packit Service 20376f
			goto done;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		line = git_array_alloc(patch->base.lines);
Packit Service 20376f
		GITERR_CHECK_ALLOC(line);
Packit Service 20376f
Packit Service 20376f
		memset(line, 0x0, sizeof(git_diff_line));
Packit Service 20376f
Packit Service 20376f
		line->content = ctx->line + prefix;
Packit Service 20376f
		line->content_len = ctx->line_len - prefix;
Packit Service 20376f
		line->content_offset = ctx->content_len - ctx->remain_len;
Packit Service 20376f
		line->origin = origin;
Packit Service 20376f
Packit Service 20376f
		hunk->line_count++;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (oldlines || newlines) {
Packit Service 20376f
		error = parse_err(
Packit Service 20376f
			"invalid patch hunk, expected %d old lines and %d new lines",
Packit Service 20376f
			hunk->hunk.old_lines, hunk->hunk.new_lines);
Packit Service 20376f
		goto done;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/* Handle "\ No newline at end of file".  Only expect the leading
Packit Service 20376f
	 * backslash, though, because the rest of the string could be
Packit Service 20376f
	 * localized.  Because `diff` optimizes for the case where you
Packit Service 20376f
	 * want to apply the patch by hand.
Packit Service 20376f
	 */
Packit Service 20376f
	if (parse_ctx_contains_s(ctx, "\\ ") &&
Packit Service 20376f
		git_array_size(patch->base.lines) > 0) {
Packit Service 20376f
Packit Service 20376f
		line = git_array_get(patch->base.lines, git_array_size(patch->base.lines) - 1);
Packit Service 20376f
Packit Service 20376f
		if (line->content_len < 1) {
Packit Service 20376f
			error = parse_err("cannot trim trailing newline of empty line");
Packit Service 20376f
			goto done;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		line->content_len--;
Packit Service 20376f
Packit Service 20376f
		parse_advance_line(ctx);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
done:
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int parse_patch_header(
Packit Service 20376f
	git_patch_parsed *patch,
Packit Service 20376f
	git_patch_parse_ctx *ctx)
Packit Service 20376f
{
Packit Service 20376f
	int error = 0;
Packit Service 20376f
Packit Service 20376f
	for (ctx->line = ctx->remain;
Packit Service 20376f
		ctx->remain_len > 0;
Packit Service 20376f
		parse_advance_line(ctx)) {
Packit Service 20376f
Packit Service 20376f
		/* This line is too short to be a patch header. */
Packit Service 20376f
		if (ctx->line_len < 6)
Packit Service 20376f
			continue;
Packit Service 20376f
Packit Service 20376f
		/* This might be a hunk header without a patch header, provide a
Packit Service 20376f
		 * sensible error message. */
Packit Service 20376f
		if (parse_ctx_contains_s(ctx, "@@ -")) {
Packit Service 20376f
			size_t line_num = ctx->line_num;
Packit Service 20376f
			git_patch_hunk hunk;
Packit Service 20376f
Packit Service 20376f
			/* If this cannot be parsed as a hunk header, it's just leading
Packit Service 20376f
			* noise, continue.
Packit Service 20376f
			*/
Packit Service 20376f
			if (parse_hunk_header(&hunk, ctx) < 0) {
Packit Service 20376f
				giterr_clear();
Packit Service 20376f
				continue;
Packit Service 20376f
			}
Packit Service 20376f
Packit Service 20376f
			error = parse_err("invalid hunk header outside patch at line %"PRIuZ,
Packit Service 20376f
				line_num);
Packit Service 20376f
			goto done;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		/* This buffer is too short to contain a patch. */
Packit Service 20376f
		if (ctx->remain_len < ctx->line_len + 6)
Packit Service 20376f
			break;
Packit Service 20376f
Packit Service 20376f
		/* A proper git patch */
Packit Service 20376f
		if (parse_ctx_contains_s(ctx, "diff --git ")) {
Packit Service 20376f
			error = parse_header_git(patch, ctx);
Packit Service 20376f
			goto done;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		error = 0;
Packit Service 20376f
		continue;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	giterr_set(GITERR_PATCH, "no patch found");
Packit Service 20376f
	error = GIT_ENOTFOUND;
Packit Service 20376f
Packit Service 20376f
done:
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int parse_patch_binary_side(
Packit Service 20376f
	git_diff_binary_file *binary,
Packit Service 20376f
	git_patch_parse_ctx *ctx)
Packit Service 20376f
{
Packit Service 20376f
	git_diff_binary_t type = GIT_DIFF_BINARY_NONE;
Packit Service 20376f
	git_buf base85 = GIT_BUF_INIT, decoded = GIT_BUF_INIT;
Packit Service 20376f
	git_off_t len;
Packit Service 20376f
	int error = 0;
Packit Service 20376f
Packit Service 20376f
	if (parse_ctx_contains_s(ctx, "literal ")) {
Packit Service 20376f
		type = GIT_DIFF_BINARY_LITERAL;
Packit Service 20376f
		parse_advance_chars(ctx, 8);
Packit Service 20376f
	} else if (parse_ctx_contains_s(ctx, "delta ")) {
Packit Service 20376f
		type = GIT_DIFF_BINARY_DELTA;
Packit Service 20376f
		parse_advance_chars(ctx, 6);
Packit Service 20376f
	} else {
Packit Service 20376f
		error = parse_err(
Packit Service 20376f
			"unknown binary delta type at line %"PRIuZ, ctx->line_num);
Packit Service 20376f
		goto done;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (parse_number(&len, ctx) < 0 || parse_advance_nl(ctx) < 0 || len < 0) {
Packit Service 20376f
		error = parse_err("invalid binary size at line %"PRIuZ, ctx->line_num);
Packit Service 20376f
		goto done;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	while (ctx->line_len) {
Packit Service 20376f
		char c = ctx->line[0];
Packit Service 20376f
		size_t encoded_len, decoded_len = 0, decoded_orig = decoded.size;
Packit Service 20376f
Packit Service 20376f
		if (c == '\n')
Packit Service 20376f
			break;
Packit Service 20376f
		else if (c >= 'A' && c <= 'Z')
Packit Service 20376f
			decoded_len = c - 'A' + 1;
Packit Service 20376f
		else if (c >= 'a' && c <= 'z')
Packit Service 20376f
			decoded_len = c - 'a' + (('z' - 'a') + 1) + 1;
Packit Service 20376f
Packit Service 20376f
		if (!decoded_len) {
Packit Service 20376f
			error = parse_err("invalid binary length at line %"PRIuZ, ctx->line_num);
Packit Service 20376f
			goto done;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		parse_advance_chars(ctx, 1);
Packit Service 20376f
Packit Service 20376f
		encoded_len = ((decoded_len / 4) + !!(decoded_len % 4)) * 5;
Packit Service 20376f
Packit Service 20376f
		if (encoded_len > ctx->line_len - 1) {
Packit Service 20376f
			error = parse_err("truncated binary data at line %"PRIuZ, ctx->line_num);
Packit Service 20376f
			goto done;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		if ((error = git_buf_decode_base85(
Packit Service 20376f
			&decoded, ctx->line, encoded_len, decoded_len)) < 0)
Packit Service 20376f
			goto done;
Packit Service 20376f
Packit Service 20376f
		if (decoded.size - decoded_orig != decoded_len) {
Packit Service 20376f
			error = parse_err("truncated binary data at line %"PRIuZ, ctx->line_num);
Packit Service 20376f
			goto done;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		parse_advance_chars(ctx, encoded_len);
Packit Service 20376f
Packit Service 20376f
		if (parse_advance_nl(ctx) < 0) {
Packit Service 20376f
			error = parse_err("trailing data at line %"PRIuZ, ctx->line_num);
Packit Service 20376f
			goto done;
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	binary->type = type;
Packit Service 20376f
	binary->inflatedlen = (size_t)len;
Packit Service 20376f
	binary->datalen = decoded.size;
Packit Service 20376f
	binary->data = git_buf_detach(&decoded);
Packit Service 20376f
Packit Service 20376f
done:
Packit Service 20376f
	git_buf_free(&base85);
Packit Service 20376f
	git_buf_free(&decoded);
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int parse_patch_binary(
Packit Service 20376f
	git_patch_parsed *patch,
Packit Service 20376f
	git_patch_parse_ctx *ctx)
Packit Service 20376f
{
Packit Service 20376f
	int error;
Packit Service 20376f
Packit Service 20376f
	if (parse_advance_expected_str(ctx, "GIT binary patch") < 0 ||
Packit Service 20376f
		parse_advance_nl(ctx) < 0)
Packit Service 20376f
		return parse_err("corrupt git binary header at line %"PRIuZ, ctx->line_num);
Packit Service 20376f
Packit Service 20376f
	/* parse old->new binary diff */
Packit Service 20376f
	if ((error = parse_patch_binary_side(
Packit Service 20376f
			&patch->base.binary.new_file, ctx)) < 0)
Packit Service 20376f
		return error;
Packit Service 20376f
Packit Service 20376f
	if (parse_advance_nl(ctx) < 0)
Packit Service 20376f
		return parse_err("corrupt git binary separator at line %"PRIuZ,
Packit Service 20376f
			ctx->line_num);
Packit Service 20376f
Packit Service 20376f
	/* parse new->old binary diff */
Packit Service 20376f
	if ((error = parse_patch_binary_side(
Packit Service 20376f
			&patch->base.binary.old_file, ctx)) < 0)
Packit Service 20376f
		return error;
Packit Service 20376f
Packit Service 20376f
	if (parse_advance_nl(ctx) < 0)
Packit Service 20376f
		return parse_err("corrupt git binary patch separator at line %"PRIuZ,
Packit Service 20376f
			ctx->line_num);
Packit Service 20376f
Packit Service 20376f
	patch->base.binary.contains_data = 1;
Packit Service 20376f
	patch->base.delta->flags |= GIT_DIFF_FLAG_BINARY;
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int parse_patch_binary_nodata(
Packit Service 20376f
	git_patch_parsed *patch,
Packit Service 20376f
	git_patch_parse_ctx *ctx)
Packit Service 20376f
{
Packit Service 20376f
	if (parse_advance_expected_str(ctx, "Binary files ") < 0 ||
Packit Service 20376f
		parse_advance_expected_str(ctx, patch->header_old_path) < 0 ||
Packit Service 20376f
		parse_advance_expected_str(ctx, " and ") < 0 ||
Packit Service 20376f
		parse_advance_expected_str(ctx, patch->header_new_path) < 0 ||
Packit Service 20376f
		parse_advance_expected_str(ctx, " differ") < 0 ||
Packit Service 20376f
		parse_advance_nl(ctx) < 0)
Packit Service 20376f
		return parse_err("corrupt git binary header at line %"PRIuZ, ctx->line_num);
Packit Service 20376f
Packit Service 20376f
	patch->base.binary.contains_data = 0;
Packit Service 20376f
	patch->base.delta->flags |= GIT_DIFF_FLAG_BINARY;
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int parse_patch_hunks(
Packit Service 20376f
	git_patch_parsed *patch,
Packit Service 20376f
	git_patch_parse_ctx *ctx)
Packit Service 20376f
{
Packit Service 20376f
	git_patch_hunk *hunk;
Packit Service 20376f
	int error = 0;
Packit Service 20376f
Packit Service 20376f
	while (parse_ctx_contains_s(ctx, "@@ -")) {
Packit Service 20376f
		hunk = git_array_alloc(patch->base.hunks);
Packit Service 20376f
		GITERR_CHECK_ALLOC(hunk);
Packit Service 20376f
Packit Service 20376f
		memset(hunk, 0, sizeof(git_patch_hunk));
Packit Service 20376f
Packit Service 20376f
		hunk->line_start = git_array_size(patch->base.lines);
Packit Service 20376f
		hunk->line_count = 0;
Packit Service 20376f
Packit Service 20376f
		if ((error = parse_hunk_header(hunk, ctx)) < 0 ||
Packit Service 20376f
			(error = parse_hunk_body(patch, hunk, ctx)) < 0)
Packit Service 20376f
			goto done;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	patch->base.delta->flags |= GIT_DIFF_FLAG_NOT_BINARY;
Packit Service 20376f
Packit Service 20376f
done:
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int parse_patch_body(
Packit Service 20376f
	git_patch_parsed *patch, git_patch_parse_ctx *ctx)
Packit Service 20376f
{
Packit Service 20376f
	if (parse_ctx_contains_s(ctx, "GIT binary patch"))
Packit Service 20376f
		return parse_patch_binary(patch, ctx);
Packit Service 20376f
	else if (parse_ctx_contains_s(ctx, "Binary files "))
Packit Service 20376f
		return parse_patch_binary_nodata(patch, ctx);
Packit Service 20376f
	else
Packit Service 20376f
		return parse_patch_hunks(patch, ctx);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int check_header_names(
Packit Service 20376f
	const char *one,
Packit Service 20376f
	const char *two,
Packit Service 20376f
	const char *old_or_new,
Packit Service 20376f
	bool two_null)
Packit Service 20376f
{
Packit Service 20376f
	if (!one || !two)
Packit Service 20376f
		return 0;
Packit Service 20376f
Packit Service 20376f
	if (two_null && strcmp(two, "/dev/null") != 0)
Packit Service 20376f
		return parse_err("expected %s path of '/dev/null'", old_or_new);
Packit Service 20376f
Packit Service 20376f
	else if (!two_null && strcmp(one, two) != 0)
Packit Service 20376f
		return parse_err("mismatched %s path names", old_or_new);
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int check_prefix(
Packit Service 20376f
	char **out,
Packit Service 20376f
	size_t *out_len,
Packit Service 20376f
	git_patch_parsed *patch,
Packit Service 20376f
	const char *path_start)
Packit Service 20376f
{
Packit Service 20376f
	const char *path = path_start;
Packit Service 20376f
	size_t prefix_len = patch->ctx->opts.prefix_len;
Packit Service 20376f
	size_t remain_len = prefix_len;
Packit Service 20376f
Packit Service 20376f
	*out = NULL;
Packit Service 20376f
	*out_len = 0;
Packit Service 20376f
Packit Service 20376f
	if (prefix_len == 0)
Packit Service 20376f
		goto done;
Packit Service 20376f
Packit Service 20376f
	/* leading slashes do not count as part of the prefix in git apply */
Packit Service 20376f
	while (*path == '/')
Packit Service 20376f
		path++;
Packit Service 20376f
Packit Service 20376f
	while (*path && remain_len) {
Packit Service 20376f
		if (*path == '/')
Packit Service 20376f
			remain_len--;
Packit Service 20376f
Packit Service 20376f
		path++;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (remain_len || !*path)
Packit Service 20376f
		return parse_err(
Packit Service 20376f
			"header filename does not contain %"PRIuZ" path components",
Packit Service 20376f
			prefix_len);
Packit Service 20376f
Packit Service 20376f
done:
Packit Service 20376f
	*out_len = (path - path_start);
Packit Service 20376f
	*out = git__strndup(path_start, *out_len);
Packit Service 20376f
Packit Service 20376f
	return (*out == NULL) ? -1 : 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int check_filenames(git_patch_parsed *patch)
Packit Service 20376f
{
Packit Service 20376f
	const char *prefixed_new, *prefixed_old;
Packit Service 20376f
	size_t old_prefixlen = 0, new_prefixlen = 0;
Packit Service 20376f
	bool added = (patch->base.delta->status == GIT_DELTA_ADDED);
Packit Service 20376f
	bool deleted = (patch->base.delta->status == GIT_DELTA_DELETED);
Packit Service 20376f
Packit Service 20376f
	if (patch->old_path && !patch->new_path)
Packit Service 20376f
		return parse_err("missing new path");
Packit Service 20376f
Packit Service 20376f
	if (!patch->old_path && patch->new_path)
Packit Service 20376f
		return parse_err("missing old path");
Packit Service 20376f
Packit Service 20376f
	/* Ensure (non-renamed) paths match */
Packit Service 20376f
	if (check_header_names(
Packit Service 20376f
			patch->header_old_path, patch->old_path, "old", added) < 0 ||
Packit Service 20376f
		check_header_names(
Packit Service 20376f
			patch->header_new_path, patch->new_path, "new", deleted) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	prefixed_old = (!added && patch->old_path) ? patch->old_path :
Packit Service 20376f
		patch->header_old_path;
Packit Service 20376f
	prefixed_new = (!deleted && patch->new_path) ? patch->new_path :
Packit Service 20376f
		patch->header_new_path;
Packit Service 20376f
Packit Service 20376f
	if (check_prefix(
Packit Service 20376f
			&patch->old_prefix, &old_prefixlen, patch, prefixed_old) < 0 ||
Packit Service 20376f
		check_prefix(
Packit Service 20376f
			&patch->new_prefix, &new_prefixlen, patch, prefixed_new) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	/* Prefer the rename filenames as they are unambiguous and unprefixed */
Packit Service 20376f
	if (patch->rename_old_path)
Packit Service 20376f
		patch->base.delta->old_file.path = patch->rename_old_path;
Packit Service 20376f
	else
Packit Service 20376f
		patch->base.delta->old_file.path = prefixed_old + old_prefixlen;
Packit Service 20376f
Packit Service 20376f
	if (patch->rename_new_path)
Packit Service 20376f
		patch->base.delta->new_file.path = patch->rename_new_path;
Packit Service 20376f
	else
Packit Service 20376f
		patch->base.delta->new_file.path = prefixed_new + new_prefixlen;
Packit Service 20376f
Packit Service 20376f
	if (!patch->base.delta->old_file.path &&
Packit Service 20376f
		!patch->base.delta->new_file.path)
Packit Service 20376f
		return parse_err("git diff header lacks old / new paths");
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int check_patch(git_patch_parsed *patch)
Packit Service 20376f
{
Packit Service 20376f
	git_diff_delta *delta = patch->base.delta;
Packit Service 20376f
Packit Service 20376f
	if (check_filenames(patch) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	if (delta->old_file.path &&
Packit Service 20376f
			delta->status != GIT_DELTA_DELETED &&
Packit Service 20376f
			!delta->new_file.mode)
Packit Service 20376f
		delta->new_file.mode = delta->old_file.mode;
Packit Service 20376f
Packit Service 20376f
	if (delta->status == GIT_DELTA_MODIFIED &&
Packit Service 20376f
			!(delta->flags & GIT_DIFF_FLAG_BINARY) &&
Packit Service 20376f
			delta->new_file.mode == delta->old_file.mode &&
Packit Service 20376f
			git_array_size(patch->base.hunks) == 0)
Packit Service 20376f
		return parse_err("patch with no hunks");
Packit Service 20376f
Packit Service 20376f
	if (delta->status == GIT_DELTA_ADDED) {
Packit Service 20376f
		memset(&delta->old_file.id, 0x0, sizeof(git_oid));
Packit Service 20376f
		delta->old_file.id_abbrev = 0;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (delta->status == GIT_DELTA_DELETED) {
Packit Service 20376f
		memset(&delta->new_file.id, 0x0, sizeof(git_oid));
Packit Service 20376f
		delta->new_file.id_abbrev = 0;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
git_patch_parse_ctx *git_patch_parse_ctx_init(
Packit Service 20376f
	const char *content,
Packit Service 20376f
	size_t content_len,
Packit Service 20376f
	const git_patch_options *opts)
Packit Service 20376f
{
Packit Service 20376f
	git_patch_parse_ctx *ctx;
Packit Service 20376f
	git_patch_options default_opts = GIT_PATCH_OPTIONS_INIT;
Packit Service 20376f
Packit Service 20376f
	if ((ctx = git__calloc(1, sizeof(git_patch_parse_ctx))) == NULL)
Packit Service 20376f
		return NULL;
Packit Service 20376f
Packit Service 20376f
	if (content_len) {
Packit Service 20376f
		if ((ctx->content = git__malloc(content_len)) == NULL) {
Packit Service 20376f
			git__free(ctx);
Packit Service 20376f
			return NULL;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		memcpy((char *)ctx->content, content, content_len);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	ctx->content_len = content_len;
Packit Service 20376f
	ctx->remain = ctx->content;
Packit Service 20376f
	ctx->remain_len = ctx->content_len;
Packit Service 20376f
Packit Service 20376f
	if (opts)
Packit Service 20376f
		memcpy(&ctx->opts, opts, sizeof(git_patch_options));
Packit Service 20376f
	else
Packit Service 20376f
		memcpy(&ctx->opts, &default_opts, sizeof(git_patch_options));
Packit Service 20376f
Packit Service 20376f
	GIT_REFCOUNT_INC(ctx);
Packit Service 20376f
	return ctx;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static void patch_parse_ctx_free(git_patch_parse_ctx *ctx)
Packit Service 20376f
{
Packit Service 20376f
	if (!ctx)
Packit Service 20376f
		return;
Packit Service 20376f
Packit Service 20376f
	git__free((char *)ctx->content);
Packit Service 20376f
	git__free(ctx);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void git_patch_parse_ctx_free(git_patch_parse_ctx *ctx)
Packit Service 20376f
{
Packit Service 20376f
	GIT_REFCOUNT_DEC(ctx, patch_parse_ctx_free);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_patch_parsed_from_diff(git_patch **out, git_diff *d, size_t idx)
Packit Service 20376f
{
Packit Service 20376f
	git_diff_parsed *diff = (git_diff_parsed *)d;
Packit Service 20376f
	git_patch *p;
Packit Service 20376f
Packit Service 20376f
	if ((p = git_vector_get(&diff->patches, idx)) == NULL)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	GIT_REFCOUNT_INC(p);
Packit Service 20376f
	*out = p;
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static void patch_parsed__free(git_patch *p)
Packit Service 20376f
{
Packit Service 20376f
	git_patch_parsed *patch = (git_patch_parsed *)p;
Packit Service 20376f
Packit Service 20376f
	if (!patch)
Packit Service 20376f
		return;
Packit Service 20376f
Packit Service 20376f
	git_patch_parse_ctx_free(patch->ctx);
Packit Service 20376f
Packit Service 20376f
	git__free((char *)patch->base.binary.old_file.data);
Packit Service 20376f
	git__free((char *)patch->base.binary.new_file.data);
Packit Service 20376f
	git_array_clear(patch->base.hunks);
Packit Service 20376f
	git_array_clear(patch->base.lines);
Packit Service 20376f
	git__free(patch->base.delta);
Packit Service 20376f
Packit Service 20376f
	git__free(patch->old_prefix);
Packit Service 20376f
	git__free(patch->new_prefix);
Packit Service 20376f
	git__free(patch->header_old_path);
Packit Service 20376f
	git__free(patch->header_new_path);
Packit Service 20376f
	git__free(patch->rename_old_path);
Packit Service 20376f
	git__free(patch->rename_new_path);
Packit Service 20376f
	git__free(patch->old_path);
Packit Service 20376f
	git__free(patch->new_path);
Packit Service 20376f
	git__free(patch);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_patch_parse(
Packit Service 20376f
	git_patch **out,
Packit Service 20376f
	git_patch_parse_ctx *ctx)
Packit Service 20376f
{
Packit Service 20376f
	git_patch_parsed *patch;
Packit Service 20376f
	size_t start, used;
Packit Service 20376f
	int error = 0;
Packit Service 20376f
Packit Service 20376f
	assert(out && ctx);
Packit Service 20376f
Packit Service 20376f
	*out = NULL;
Packit Service 20376f
Packit Service 20376f
	patch = git__calloc(1, sizeof(git_patch_parsed));
Packit Service 20376f
	GITERR_CHECK_ALLOC(patch);
Packit Service 20376f
Packit Service 20376f
	patch->ctx = ctx;
Packit Service 20376f
	GIT_REFCOUNT_INC(patch->ctx);
Packit Service 20376f
Packit Service 20376f
	patch->base.free_fn = patch_parsed__free;
Packit Service 20376f
Packit Service 20376f
	patch->base.delta = git__calloc(1, sizeof(git_diff_delta));
Packit Service 20376f
	GITERR_CHECK_ALLOC(patch->base.delta);
Packit Service 20376f
Packit Service 20376f
	patch->base.delta->status = GIT_DELTA_MODIFIED;
Packit Service 20376f
	patch->base.delta->nfiles = 2;
Packit Service 20376f
Packit Service 20376f
	start = ctx->remain_len;
Packit Service 20376f
Packit Service 20376f
	if ((error = parse_patch_header(patch, ctx)) < 0 ||
Packit Service 20376f
		(error = parse_patch_body(patch, ctx)) < 0 ||
Packit Service 20376f
		(error = check_patch(patch)) < 0)
Packit Service 20376f
		goto done;
Packit Service 20376f
Packit Service 20376f
	used = start - ctx->remain_len;
Packit Service 20376f
	ctx->remain += used;
Packit Service 20376f
Packit Service 20376f
	patch->base.diff_opts.old_prefix = patch->old_prefix;
Packit Service 20376f
	patch->base.diff_opts.new_prefix = patch->new_prefix;
Packit Service 20376f
	patch->base.diff_opts.flags |= GIT_DIFF_SHOW_BINARY;
Packit Service 20376f
Packit Service 20376f
	GIT_REFCOUNT_INC(patch);
Packit Service 20376f
	*out = &patch->base;
Packit Service 20376f
Packit Service 20376f
done:
Packit Service 20376f
	if (error < 0)
Packit Service 20376f
		patch_parsed__free(&patch->base);
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_patch_from_buffer(
Packit Service 20376f
	git_patch **out,
Packit Service 20376f
	const char *content,
Packit Service 20376f
	size_t content_len,
Packit Service 20376f
	const git_patch_options *opts)
Packit Service 20376f
{
Packit Service 20376f
	git_patch_parse_ctx *ctx;
Packit Service 20376f
	int error;
Packit Service 20376f
Packit Service 20376f
	ctx = git_patch_parse_ctx_init(content, content_len, opts);
Packit Service 20376f
	GITERR_CHECK_ALLOC(ctx);
Packit Service 20376f
Packit Service 20376f
	error = git_patch_parse(out, ctx);
Packit Service 20376f
Packit Service 20376f
	git_patch_parse_ctx_free(ctx);
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f