* This file is part of libgit2, distributed under the GNU GPL v2 with
* a Linking Exception. For full terms see the included COPYING file.
*/
+
+#include "patch_parse.h"
+
#include "git2/patch.h"
#include "patch.h"
-#include "patch_parse.h"
#include "diff_parse.h"
-#include "path.h"
-
-#define parse_err(...) \
- ( giterr_set(GITERR_PATCH, __VA_ARGS__), -1 )
+#include "fs_path.h"
typedef struct {
git_patch base;
char *old_prefix, *new_prefix;
} git_patch_parsed;
-
-GIT_INLINE(bool) parse_ctx_contains(
- git_patch_parse_ctx *ctx, const char *str, size_t len)
-{
- return (ctx->line_len >= len && memcmp(ctx->line, str, len) == 0);
-}
-
-#define parse_ctx_contains_s(ctx, str) \
- parse_ctx_contains(ctx, str, sizeof(str) - 1)
-
-static void parse_advance_line(git_patch_parse_ctx *ctx)
+static int git_parse_err(const char *fmt, ...) GIT_FORMAT_PRINTF(1, 2);
+static int git_parse_err(const char *fmt, ...)
{
- ctx->line += ctx->line_len;
- ctx->remain_len -= ctx->line_len;
- ctx->line_len = git__linenlen(ctx->line, ctx->remain_len);
- ctx->line_num++;
-}
-
-static void parse_advance_chars(git_patch_parse_ctx *ctx, size_t char_cnt)
-{
- ctx->line += char_cnt;
- ctx->remain_len -= char_cnt;
- ctx->line_len -= char_cnt;
-}
+ va_list ap;
-static int parse_advance_expected(
- git_patch_parse_ctx *ctx,
- const char *expected,
- size_t expected_len)
-{
- if (ctx->line_len < expected_len)
- return -1;
-
- if (memcmp(ctx->line, expected, expected_len) != 0)
- return -1;
+ va_start(ap, fmt);
+ git_error_vset(GIT_ERROR_PATCH, fmt, ap);
+ va_end(ap);
- parse_advance_chars(ctx, expected_len);
- return 0;
-}
-
-#define parse_advance_expected_str(ctx, str) \
- parse_advance_expected(ctx, str, strlen(str))
-
-static int parse_advance_ws(git_patch_parse_ctx *ctx)
-{
- int ret = -1;
-
- while (ctx->line_len > 0 &&
- ctx->line[0] != '\n' &&
- git__isspace(ctx->line[0])) {
- ctx->line++;
- ctx->line_len--;
- ctx->remain_len--;
- ret = 0;
- }
-
- return ret;
-}
-
-static int parse_advance_nl(git_patch_parse_ctx *ctx)
-{
- if (ctx->line_len != 1 || ctx->line[0] != '\n')
- return -1;
-
- parse_advance_line(ctx);
- return 0;
+ return -1;
}
-static int header_path_len(git_patch_parse_ctx *ctx)
+static size_t header_path_len(git_patch_parse_ctx *ctx)
{
bool inquote = 0;
- bool quoted = (ctx->line_len > 0 && ctx->line[0] == '"');
+ bool quoted = git_parse_ctx_contains_s(&ctx->parse_ctx, "\"");
size_t len;
- for (len = quoted; len < ctx->line_len; len++) {
- if (!quoted && git__isspace(ctx->line[len]))
+ for (len = quoted; len < ctx->parse_ctx.line_len; len++) {
+ if (!quoted && git__isspace(ctx->parse_ctx.line[len]))
break;
- else if (quoted && !inquote && ctx->line[len] == '"') {
+ else if (quoted && !inquote && ctx->parse_ctx.line[len] == '"') {
len++;
break;
}
- inquote = (!inquote && ctx->line[len] == '\\');
+ inquote = (!inquote && ctx->parse_ctx.line[len] == '\\');
}
return len;
}
-static int parse_header_path_buf(git_buf *path, git_patch_parse_ctx *ctx)
+static int parse_header_path_buf(git_str *path, git_patch_parse_ctx *ctx, size_t path_len)
{
- int path_len, error = 0;
-
- path_len = header_path_len(ctx);
+ int error;
- if ((error = git_buf_put(path, ctx->line, path_len)) < 0)
- goto done;
+ if ((error = git_str_put(path, ctx->parse_ctx.line, path_len)) < 0)
+ return error;
- parse_advance_chars(ctx, path_len);
+ git_parse_advance_chars(&ctx->parse_ctx, path_len);
- git_buf_rtrim(path);
+ git_str_rtrim(path);
- if (path->size > 0 && path->ptr[0] == '"')
- error = git_buf_unquote(path);
+ if (path->size > 0 && path->ptr[0] == '"' &&
+ (error = git_str_unquote(path)) < 0)
+ return error;
- if (error < 0)
- goto done;
+ git_fs_path_squash_slashes(path);
- git_path_squash_slashes(path);
+ if (!path->size)
+ return git_parse_err("patch contains empty path at line %"PRIuZ,
+ ctx->parse_ctx.line_num);
-done:
- return error;
+ return 0;
}
static int parse_header_path(char **out, git_patch_parse_ctx *ctx)
{
- git_buf path = GIT_BUF_INIT;
- int error = parse_header_path_buf(&path, ctx);
+ git_str path = GIT_STR_INIT;
+ int error;
- *out = git_buf_detach(&path);
+ if ((error = parse_header_path_buf(&path, ctx, header_path_len(ctx))) < 0)
+ goto out;
+ *out = git_str_detach(&path);
+out:
+ git_str_dispose(&path);
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);
+ git_str old_path = GIT_STR_INIT;
+ int error;
+
+ if (patch->old_path) {
+ error = git_parse_err("patch contains duplicate old path at line %"PRIuZ,
+ ctx->parse_ctx.line_num);
+ goto out;
+ }
+
+ if ((error = parse_header_path_buf(&old_path, ctx, ctx->parse_ctx.line_len - 1)) < 0)
+ goto out;
+
+ patch->old_path = git_str_detach(&old_path);
+
+out:
+ git_str_dispose(&old_path);
+ return error;
}
static int parse_header_git_newpath(
git_patch_parsed *patch, git_patch_parse_ctx *ctx)
{
- return parse_header_path(&patch->new_path, ctx);
+ git_str new_path = GIT_STR_INIT;
+ int error;
+
+ if (patch->new_path) {
+ error = git_parse_err("patch contains duplicate new path at line %"PRIuZ,
+ ctx->parse_ctx.line_num);
+ goto out;
+ }
+
+ if ((error = parse_header_path_buf(&new_path, ctx, ctx->parse_ctx.line_len - 1)) < 0)
+ goto out;
+ patch->new_path = git_str_detach(&new_path);
+
+out:
+ git_str_dispose(&new_path);
+ return error;
}
static int parse_header_mode(uint16_t *mode, git_patch_parse_ctx *ctx)
{
- const char *end;
- int32_t m;
- int ret;
-
- if (ctx->line_len < 1 || !git__isdigit(ctx->line[0]))
- return parse_err("invalid file mode at line %"PRIuZ, ctx->line_num);
+ int64_t m;
- if ((ret = git__strntol32(&m, ctx->line, ctx->line_len, &end, 8)) < 0)
- return ret;
+ if ((git_parse_advance_digit(&m, &ctx->parse_ctx, 8)) < 0)
+ return git_parse_err("invalid file mode at line %"PRIuZ, ctx->parse_ctx.line_num);
if (m > UINT16_MAX)
return -1;
*mode = (uint16_t)m;
- parse_advance_chars(ctx, (end - ctx->line));
-
- return ret;
+ return 0;
}
static int parse_header_oid(
{
size_t len;
- for (len = 0; len < ctx->line_len && len < GIT_OID_HEXSZ; len++) {
- if (!git__isxdigit(ctx->line[len]))
+ for (len = 0; len < ctx->parse_ctx.line_len && len < GIT_OID_HEXSZ; len++) {
+ if (!git__isxdigit(ctx->parse_ctx.line[len]))
break;
}
if (len < GIT_OID_MINPREFIXLEN || len > GIT_OID_HEXSZ ||
- git_oid_fromstrn(oid, ctx->line, len) < 0)
- return parse_err("invalid hex formatted object id at line %"PRIuZ,
- ctx->line_num);
+ git_oid_fromstrn(oid, ctx->parse_ctx.line, len) < 0)
+ return git_parse_err("invalid hex formatted object id at line %"PRIuZ,
+ ctx->parse_ctx.line_num);
- parse_advance_chars(ctx, len);
+ git_parse_advance_chars(&ctx->parse_ctx, len);
*oid_len = (uint16_t)len;
static int parse_header_git_index(
git_patch_parsed *patch, git_patch_parse_ctx *ctx)
{
+ char c;
+
if (parse_header_oid(&patch->base.delta->old_file.id,
&patch->base.delta->old_file.id_abbrev, ctx) < 0 ||
- parse_advance_expected_str(ctx, "..") < 0 ||
+ git_parse_advance_expected_str(&ctx->parse_ctx, "..") < 0 ||
parse_header_oid(&patch->base.delta->new_file.id,
&patch->base.delta->new_file.id_abbrev, ctx) < 0)
return -1;
- if (ctx->line_len > 0 && ctx->line[0] == ' ') {
- uint16_t mode;
+ if (git_parse_peek(&c, &ctx->parse_ctx, 0) == 0 && c == ' ') {
+ uint16_t mode = 0;
- parse_advance_chars(ctx, 1);
+ git_parse_advance_chars(&ctx->parse_ctx, 1);
if (parse_header_mode(&mode, ctx) < 0)
return -1;
git_patch_parsed *patch,
git_patch_parse_ctx *ctx)
{
- git__free((char *)patch->base.delta->old_file.path);
+ git__free((char *)patch->base.delta->new_file.path);
- patch->base.delta->old_file.path = NULL;
+ patch->base.delta->new_file.path = NULL;
patch->base.delta->status = GIT_DELTA_DELETED;
patch->base.delta->nfiles = 1;
git_patch_parsed *patch,
git_patch_parse_ctx *ctx)
{
- git__free((char *)patch->base.delta->new_file.path);
+ git__free((char *)patch->base.delta->old_file.path);
- patch->base.delta->new_file.path = NULL;
+ patch->base.delta->old_file.path = NULL;
patch->base.delta->status = GIT_DELTA_ADDED;
patch->base.delta->nfiles = 1;
char **out,
git_patch_parse_ctx *ctx)
{
- git_buf path = GIT_BUF_INIT;
+ git_str path = GIT_STR_INIT;
- if (parse_header_path_buf(&path, ctx) < 0)
+ if (parse_header_path_buf(&path, ctx, header_path_len(ctx)) < 0)
return -1;
/* Note: the `rename from` and `rename to` lines include the literal
* filename. They do *not* include the prefix. (Who needs consistency?)
*/
- *out = git_buf_detach(&path);
+ *out = git_str_detach(&path);
return 0;
}
static int parse_header_percent(uint16_t *out, git_patch_parse_ctx *ctx)
{
- int32_t val;
- const char *end;
+ int64_t val;
- if (ctx->line_len < 1 || !git__isdigit(ctx->line[0]) ||
- git__strntol32(&val, ctx->line, ctx->line_len, &end, 10) < 0)
+ if (git_parse_advance_digit(&val, &ctx->parse_ctx, 10) < 0)
return -1;
- parse_advance_chars(ctx, (end - ctx->line));
-
- if (parse_advance_expected_str(ctx, "%") < 0)
+ if (git_parse_advance_expected_str(&ctx->parse_ctx, "%") < 0)
return -1;
- if (val > 100)
+ if (val < 0 || val > 100)
return -1;
- *out = val;
+ *out = (uint16_t)val;
return 0;
}
git_patch_parsed *patch, git_patch_parse_ctx *ctx)
{
if (parse_header_percent(&patch->base.delta->similarity, ctx) < 0)
- return parse_err("invalid similarity percentage at line %"PRIuZ,
- ctx->line_num);
+ return git_parse_err("invalid similarity percentage at line %"PRIuZ,
+ ctx->parse_ctx.line_num);
return 0;
}
uint16_t dissimilarity;
if (parse_header_percent(&dissimilarity, ctx) < 0)
- return parse_err("invalid similarity percentage at line %"PRIuZ,
- ctx->line_num);
+ return git_parse_err("invalid similarity percentage at line %"PRIuZ,
+ ctx->parse_ctx.line_num);
patch->base.delta->similarity = 100 - dissimilarity;
return 0;
}
+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
+ * proceed 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_ctx_contains(&ctx->parse_ctx, "\r\n", 2)) {
+ 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;
+}
+
+typedef enum {
+ STATE_START,
+
+ STATE_DIFF,
+ STATE_FILEMODE,
+ STATE_MODE,
+ STATE_INDEX,
+ STATE_PATH,
+
+ STATE_SIMILARITY,
+ STATE_RENAME,
+ STATE_COPY,
+
+ STATE_END
+} parse_header_state;
+
typedef struct {
const char *str;
+ parse_header_state expected_state;
+ parse_header_state next_state;
int(*fn)(git_patch_parsed *, git_patch_parse_ctx *);
-} header_git_op;
-
-static const header_git_op header_git_ops[] = {
- { "diff --git ", NULL },
- { "@@ -", NULL },
- { "GIT binary patch", NULL },
- { "Binary files ", NULL },
- { "--- ", parse_header_git_oldpath },
- { "+++ ", parse_header_git_newpath },
- { "index ", parse_header_git_index },
- { "old mode ", parse_header_git_oldmode },
- { "new mode ", parse_header_git_newmode },
- { "deleted file mode ", parse_header_git_deletedfilemode },
- { "new file mode ", parse_header_git_newfilemode },
- { "rename from ", parse_header_renamefrom },
- { "rename to ", parse_header_renameto },
- { "rename old ", parse_header_renamefrom },
- { "rename new ", parse_header_renameto },
- { "copy from ", parse_header_copyfrom },
- { "copy to ", parse_header_copyto },
- { "similarity index ", parse_header_similarity },
- { "dissimilarity index ", parse_header_dissimilarity },
+} parse_header_transition;
+
+static const parse_header_transition transitions[] = {
+ /* Start */
+ { "diff --git " , STATE_START, STATE_DIFF, parse_header_start },
+
+ { "deleted file mode " , STATE_DIFF, STATE_FILEMODE, parse_header_git_deletedfilemode },
+ { "new file mode " , STATE_DIFF, STATE_FILEMODE, parse_header_git_newfilemode },
+ { "old mode " , STATE_DIFF, STATE_MODE, parse_header_git_oldmode },
+ { "new mode " , STATE_MODE, STATE_END, parse_header_git_newmode },
+
+ { "index " , STATE_FILEMODE, STATE_INDEX, parse_header_git_index },
+ { "index " , STATE_DIFF, STATE_INDEX, parse_header_git_index },
+ { "index " , STATE_END, STATE_INDEX, parse_header_git_index },
+
+ { "--- " , STATE_DIFF, STATE_PATH, parse_header_git_oldpath },
+ { "--- " , STATE_INDEX, STATE_PATH, parse_header_git_oldpath },
+ { "--- " , STATE_FILEMODE, STATE_PATH, parse_header_git_oldpath },
+ { "+++ " , STATE_PATH, STATE_END, parse_header_git_newpath },
+ { "GIT binary patch" , STATE_INDEX, STATE_END, NULL },
+ { "Binary files " , STATE_INDEX, STATE_END, NULL },
+
+ { "similarity index " , STATE_END, STATE_SIMILARITY, parse_header_similarity },
+ { "similarity index " , STATE_DIFF, STATE_SIMILARITY, parse_header_similarity },
+ { "dissimilarity index ", STATE_DIFF, STATE_SIMILARITY, parse_header_dissimilarity },
+ { "rename from " , STATE_SIMILARITY, STATE_RENAME, parse_header_renamefrom },
+ { "rename old " , STATE_SIMILARITY, STATE_RENAME, parse_header_renamefrom },
+ { "copy from " , STATE_SIMILARITY, STATE_COPY, parse_header_copyfrom },
+ { "rename to " , STATE_RENAME, STATE_END, parse_header_renameto },
+ { "rename new " , STATE_RENAME, STATE_END, parse_header_renameto },
+ { "copy to " , STATE_COPY, STATE_END, parse_header_copyto },
+
+ /* Next patch */
+ { "diff --git " , STATE_END, 0, NULL },
+ { "@@ -" , STATE_END, 0, NULL },
+ { "-- " , STATE_INDEX, 0, NULL },
+ { "-- " , STATE_END, 0, NULL },
};
static int parse_header_git(
{
size_t i;
int error = 0;
-
- /* Parse the diff --git line */
- if (parse_advance_expected_str(ctx, "diff --git ") < 0)
- return parse_err("corrupt git diff header at line %"PRIuZ, 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 %"PRIuZ,
- 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 %"PRIuZ,
- ctx->line_num);
+ parse_header_state state = STATE_START;
/* Parse remaining header lines */
- for (parse_advance_line(ctx);
- ctx->remain_len > 0;
- parse_advance_line(ctx)) {
-
+ for (; ctx->parse_ctx.remain_len > 0; git_parse_advance_line(&ctx->parse_ctx)) {
bool found = false;
- if (ctx->line_len == 0 || ctx->line[ctx->line_len - 1] != '\n')
+ if (ctx->parse_ctx.line_len == 0 || ctx->parse_ctx.line[ctx->parse_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);
+ for (i = 0; i < ARRAY_SIZE(transitions); i++) {
+ const parse_header_transition *transition = &transitions[i];
+ size_t op_len = strlen(transition->str);
- if (memcmp(ctx->line, op->str, min(op_len, ctx->line_len)) != 0)
+ if (transition->expected_state != state ||
+ git__prefixcmp(ctx->parse_ctx.line, transition->str) != 0)
continue;
+ state = transition->next_state;
+
/* Do not advance if this is the patch separator */
- if (op->fn == NULL)
+ if (transition->fn == NULL)
goto done;
- parse_advance_chars(ctx, op_len);
+ git_parse_advance_chars(&ctx->parse_ctx, op_len);
- if ((error = op->fn(patch, ctx)) < 0)
+ if ((error = transition->fn(patch, ctx)) < 0)
goto done;
- parse_advance_ws(ctx);
+ git_parse_advance_ws(&ctx->parse_ctx);
- if (parse_advance_expected_str(ctx, "\n") < 0 ||
- ctx->line_len > 0) {
- error = parse_err("trailing data at line %"PRIuZ, ctx->line_num);
+ if (git_parse_advance_expected_str(&ctx->parse_ctx, "\n") < 0 ||
+ ctx->parse_ctx.line_len > 0) {
+ error = git_parse_err("trailing data at line %"PRIuZ, ctx->parse_ctx.line_num);
goto done;
}
found = true;
break;
}
-
+
if (!found) {
- error = parse_err("invalid patch header at line %"PRIuZ,
- ctx->line_num);
+ error = git_parse_err("invalid patch header at line %"PRIuZ,
+ ctx->parse_ctx.line_num);
goto done;
}
}
+ if (state != STATE_END) {
+ error = git_parse_err("unexpected header line %"PRIuZ, ctx->parse_ctx.line_num);
+ goto done;
+ }
+
done:
return error;
}
-static int parse_number(git_off_t *out, git_patch_parse_ctx *ctx)
-{
- const char *end;
- int64_t num;
-
- if (!git__isdigit(ctx->line[0]))
- return -1;
-
- if (git__strntol64(&num, ctx->line, ctx->line_len, &end, 10) < 0)
- return -1;
-
- if (num < 0)
- return -1;
-
- *out = num;
- parse_advance_chars(ctx, (end - ctx->line));
-
- return 0;
-}
-
static int parse_int(int *out, git_patch_parse_ctx *ctx)
{
- git_off_t num;
+ int64_t num;
- if (parse_number(&num, ctx) < 0 || !git__is_int(num))
+ if (git_parse_advance_digit(&num, &ctx->parse_ctx, 10) < 0 || !git__is_int(num))
return -1;
*out = (int)num;
git_patch_hunk *hunk,
git_patch_parse_ctx *ctx)
{
- const char *header_start = ctx->line;
+ const char *header_start = ctx->parse_ctx.line;
+ char c;
hunk->hunk.old_lines = 1;
hunk->hunk.new_lines = 1;
- if (parse_advance_expected_str(ctx, "@@ -") < 0 ||
+ if (git_parse_advance_expected_str(&ctx->parse_ctx, "@@ -") < 0 ||
parse_int(&hunk->hunk.old_start, ctx) < 0)
goto fail;
- if (ctx->line_len > 0 && ctx->line[0] == ',') {
- if (parse_advance_expected_str(ctx, ",") < 0 ||
+ if (git_parse_peek(&c, &ctx->parse_ctx, 0) == 0 && c == ',') {
+ if (git_parse_advance_expected_str(&ctx->parse_ctx, ",") < 0 ||
parse_int(&hunk->hunk.old_lines, ctx) < 0)
goto fail;
}
- if (parse_advance_expected_str(ctx, " +") < 0 ||
+ if (git_parse_advance_expected_str(&ctx->parse_ctx, " +") < 0 ||
parse_int(&hunk->hunk.new_start, ctx) < 0)
goto fail;
- if (ctx->line_len > 0 && ctx->line[0] == ',') {
- if (parse_advance_expected_str(ctx, ",") < 0 ||
+ if (git_parse_peek(&c, &ctx->parse_ctx, 0) == 0 && c == ',') {
+ if (git_parse_advance_expected_str(&ctx->parse_ctx, ",") < 0 ||
parse_int(&hunk->hunk.new_lines, ctx) < 0)
goto fail;
}
- if (parse_advance_expected_str(ctx, " @@") < 0)
+ if (git_parse_advance_expected_str(&ctx->parse_ctx, " @@") < 0)
goto fail;
- parse_advance_line(ctx);
+ git_parse_advance_line(&ctx->parse_ctx);
if (!hunk->hunk.old_lines && !hunk->hunk.new_lines)
goto fail;
- hunk->hunk.header_len = ctx->line - header_start;
+ hunk->hunk.header_len = ctx->parse_ctx.line - header_start;
if (hunk->hunk.header_len > (GIT_DIFF_HUNK_HEADER_SIZE - 1))
- return parse_err("oversized patch hunk header at line %"PRIuZ,
- ctx->line_num);
+ return git_parse_err("oversized patch hunk header at line %"PRIuZ,
+ ctx->parse_ctx.line_num);
memcpy(hunk->hunk.header, header_start, hunk->hunk.header_len);
hunk->hunk.header[hunk->hunk.header_len] = '\0';
return 0;
fail:
- giterr_set(GITERR_PATCH, "invalid patch hunk header at line %"PRIuZ,
- ctx->line_num);
+ git_error_set(GIT_ERROR_PATCH, "invalid patch hunk header at line %"PRIuZ,
+ ctx->parse_ctx.line_num);
return -1;
}
+static int eof_for_origin(int origin) {
+ if (origin == GIT_DIFF_LINE_ADDITION)
+ return GIT_DIFF_LINE_ADD_EOFNL;
+ if (origin == GIT_DIFF_LINE_DELETION)
+ return GIT_DIFF_LINE_DEL_EOFNL;
+ return GIT_DIFF_LINE_CONTEXT_EOFNL;
+}
+
static int parse_hunk_body(
git_patch_parsed *patch,
git_patch_hunk *hunk,
int oldlines = hunk->hunk.old_lines;
int newlines = hunk->hunk.new_lines;
+ int last_origin = 0;
for (;
- ctx->remain_len > 1 &&
+ ctx->parse_ctx.remain_len > 1 &&
(oldlines || newlines) &&
- (ctx->remain_len <= 4 || memcmp(ctx->line, "@@ -", 4) != 0);
- parse_advance_line(ctx)) {
-
- int origin;
- int prefix = 1;
+ !git_parse_ctx_contains_s(&ctx->parse_ctx, "@@ -");
+ git_parse_advance_line(&ctx->parse_ctx)) {
+
+ int old_lineno, new_lineno, origin, prefix = 1;
+ char c;
+
+ if (git__add_int_overflow(&old_lineno, hunk->hunk.old_start, hunk->hunk.old_lines) ||
+ git__sub_int_overflow(&old_lineno, old_lineno, oldlines) ||
+ git__add_int_overflow(&new_lineno, hunk->hunk.new_start, hunk->hunk.new_lines) ||
+ git__sub_int_overflow(&new_lineno, new_lineno, newlines)) {
+ error = git_parse_err("unrepresentable line count at line %"PRIuZ,
+ ctx->parse_ctx.line_num);
+ goto done;
+ }
- if (ctx->line_len == 0 || ctx->line[ctx->line_len - 1] != '\n') {
- error = parse_err("invalid patch instruction at line %"PRIuZ,
- ctx->line_num);
+ if (ctx->parse_ctx.line_len == 0 || ctx->parse_ctx.line[ctx->parse_ctx.line_len - 1] != '\n') {
+ error = git_parse_err("invalid patch instruction at line %"PRIuZ,
+ ctx->parse_ctx.line_num);
goto done;
}
- switch (ctx->line[0]) {
+ git_parse_peek(&c, &ctx->parse_ctx, 0);
+
+ switch (c) {
case '\n':
prefix = 0;
+ /* fall through */
case ' ':
origin = GIT_DIFF_LINE_CONTEXT;
case '-':
origin = GIT_DIFF_LINE_DELETION;
oldlines--;
+ new_lineno = -1;
break;
case '+':
origin = GIT_DIFF_LINE_ADDITION;
newlines--;
+ old_lineno = -1;
break;
+ case '\\':
+ /*
+ * If there are no oldlines left, then this is probably
+ * the "\ No newline at end of file" marker. Do not
+ * verify its format, as it may be localized.
+ */
+ if (!oldlines) {
+ prefix = 0;
+ origin = eof_for_origin(last_origin);
+ old_lineno = -1;
+ new_lineno = -1;
+ break;
+ }
+ /* fall through */
+
default:
- error = parse_err("invalid patch hunk at line %"PRIuZ, ctx->line_num);
+ error = git_parse_err("invalid patch hunk at line %"PRIuZ, ctx->parse_ctx.line_num);
goto done;
}
line = git_array_alloc(patch->base.lines);
- GITERR_CHECK_ALLOC(line);
+ GIT_ERROR_CHECK_ALLOC(line);
memset(line, 0x0, sizeof(git_diff_line));
- line->content = ctx->line + prefix;
- line->content_len = ctx->line_len - prefix;
- line->content_offset = ctx->content_len - ctx->remain_len;
+ line->content_len = ctx->parse_ctx.line_len - prefix;
+ line->content = git__strndup(ctx->parse_ctx.line + prefix, line->content_len);
+ GIT_ERROR_CHECK_ALLOC(line->content);
+ line->content_offset = ctx->parse_ctx.content_len - ctx->parse_ctx.remain_len;
line->origin = origin;
+ line->num_lines = 1;
+ line->old_lineno = old_lineno;
+ line->new_lineno = new_lineno;
hunk->line_count++;
+
+ last_origin = origin;
}
if (oldlines || newlines) {
- error = parse_err(
+ error = git_parse_err(
"invalid patch hunk, expected %d old lines and %d new lines",
hunk->hunk.old_lines, hunk->hunk.new_lines);
goto done;
}
- /* Handle "\ No newline at end of file". Only expect the leading
+ /*
+ * Handle "\ No newline at end of file". Only expect the leading
* backslash, though, because the rest of the string could be
* localized. Because `diff` optimizes for the case where you
* want to apply the patch by hand.
*/
- if (parse_ctx_contains_s(ctx, "\\ ") &&
+ if (git_parse_ctx_contains_s(&ctx->parse_ctx, "\\ ") &&
git_array_size(patch->base.lines) > 0) {
line = git_array_get(patch->base.lines, git_array_size(patch->base.lines) - 1);
if (line->content_len < 1) {
- error = parse_err("cannot trim trailing newline of empty line");
+ error = git_parse_err("last line has no trailing newline");
goto done;
}
- line->content_len--;
+ line = git_array_alloc(patch->base.lines);
+ GIT_ERROR_CHECK_ALLOC(line);
+
+ memset(line, 0x0, sizeof(git_diff_line));
+
+ line->content_len = ctx->parse_ctx.line_len;
+ line->content = git__strndup(ctx->parse_ctx.line, line->content_len);
+ GIT_ERROR_CHECK_ALLOC(line->content);
+ line->content_offset = ctx->parse_ctx.content_len - ctx->parse_ctx.remain_len;
+ line->origin = eof_for_origin(last_origin);
+ line->num_lines = 1;
+ line->old_lineno = -1;
+ line->new_lineno = -1;
- parse_advance_line(ctx);
+ hunk->line_count++;
+
+ git_parse_advance_line(&ctx->parse_ctx);
}
done:
{
int error = 0;
- for (ctx->line = ctx->remain;
- ctx->remain_len > 0;
- parse_advance_line(ctx)) {
-
+ for (; ctx->parse_ctx.remain_len > 0; git_parse_advance_line(&ctx->parse_ctx)) {
/* This line is too short to be a patch header. */
- if (ctx->line_len < 6)
+ if (ctx->parse_ctx.line_len < 6)
continue;
/* This might be a hunk header without a patch header, provide a
* sensible error message. */
- if (parse_ctx_contains_s(ctx, "@@ -")) {
- size_t line_num = ctx->line_num;
+ if (git_parse_ctx_contains_s(&ctx->parse_ctx, "@@ -")) {
+ size_t line_num = ctx->parse_ctx.line_num;
git_patch_hunk hunk;
/* If this cannot be parsed as a hunk header, it's just leading
* noise, continue.
*/
if (parse_hunk_header(&hunk, ctx) < 0) {
- giterr_clear();
+ git_error_clear();
continue;
}
- error = parse_err("invalid hunk header outside patch at line %"PRIuZ,
+ error = git_parse_err("invalid hunk header outside patch at line %"PRIuZ,
line_num);
goto done;
}
/* This buffer is too short to contain a patch. */
- if (ctx->remain_len < ctx->line_len + 6)
+ if (ctx->parse_ctx.remain_len < ctx->parse_ctx.line_len + 6)
break;
/* A proper git patch */
- if (parse_ctx_contains_s(ctx, "diff --git ")) {
+ if (git_parse_ctx_contains_s(&ctx->parse_ctx, "diff --git ")) {
error = parse_header_git(patch, ctx);
goto done;
}
continue;
}
- giterr_set(GITERR_PATCH, "no patch found");
+ git_error_set(GIT_ERROR_PATCH, "no patch found");
error = GIT_ENOTFOUND;
done:
git_patch_parse_ctx *ctx)
{
git_diff_binary_t type = GIT_DIFF_BINARY_NONE;
- git_buf base85 = GIT_BUF_INIT, decoded = GIT_BUF_INIT;
- git_off_t len;
+ git_str base85 = GIT_STR_INIT, decoded = GIT_STR_INIT;
+ int64_t len;
int error = 0;
- if (parse_ctx_contains_s(ctx, "literal ")) {
+ if (git_parse_ctx_contains_s(&ctx->parse_ctx, "literal ")) {
type = GIT_DIFF_BINARY_LITERAL;
- parse_advance_chars(ctx, 8);
- } else if (parse_ctx_contains_s(ctx, "delta ")) {
+ git_parse_advance_chars(&ctx->parse_ctx, 8);
+ } else if (git_parse_ctx_contains_s(&ctx->parse_ctx, "delta ")) {
type = GIT_DIFF_BINARY_DELTA;
- parse_advance_chars(ctx, 6);
+ git_parse_advance_chars(&ctx->parse_ctx, 6);
} else {
- error = parse_err(
- "unknown binary delta type at line %"PRIuZ, ctx->line_num);
+ error = git_parse_err(
+ "unknown binary delta type at line %"PRIuZ, ctx->parse_ctx.line_num);
goto done;
}
- if (parse_number(&len, ctx) < 0 || parse_advance_nl(ctx) < 0 || len < 0) {
- error = parse_err("invalid binary size at line %"PRIuZ, ctx->line_num);
+ if (git_parse_advance_digit(&len, &ctx->parse_ctx, 10) < 0 ||
+ git_parse_advance_nl(&ctx->parse_ctx) < 0 || len < 0) {
+ error = git_parse_err("invalid binary size at line %"PRIuZ, ctx->parse_ctx.line_num);
goto done;
}
- while (ctx->line_len) {
- char c = ctx->line[0];
+ while (ctx->parse_ctx.line_len) {
+ char c;
size_t encoded_len, decoded_len = 0, decoded_orig = decoded.size;
+ git_parse_peek(&c, &ctx->parse_ctx, 0);
+
if (c == '\n')
break;
else if (c >= 'A' && c <= 'Z')
decoded_len = c - 'a' + (('z' - 'a') + 1) + 1;
if (!decoded_len) {
- error = parse_err("invalid binary length at line %"PRIuZ, ctx->line_num);
+ error = git_parse_err("invalid binary length at line %"PRIuZ, ctx->parse_ctx.line_num);
goto done;
}
- parse_advance_chars(ctx, 1);
+ git_parse_advance_chars(&ctx->parse_ctx, 1);
encoded_len = ((decoded_len / 4) + !!(decoded_len % 4)) * 5;
- if (encoded_len > ctx->line_len - 1) {
- error = parse_err("truncated binary data at line %"PRIuZ, ctx->line_num);
+ if (!encoded_len || !ctx->parse_ctx.line_len || encoded_len > ctx->parse_ctx.line_len - 1) {
+ error = git_parse_err("truncated binary data at line %"PRIuZ, ctx->parse_ctx.line_num);
goto done;
}
- if ((error = git_buf_decode_base85(
- &decoded, ctx->line, encoded_len, decoded_len)) < 0)
+ if ((error = git_str_decode_base85(
+ &decoded, ctx->parse_ctx.line, encoded_len, decoded_len)) < 0)
goto done;
if (decoded.size - decoded_orig != decoded_len) {
- error = parse_err("truncated binary data at line %"PRIuZ, ctx->line_num);
+ error = git_parse_err("truncated binary data at line %"PRIuZ, ctx->parse_ctx.line_num);
goto done;
}
- parse_advance_chars(ctx, encoded_len);
+ git_parse_advance_chars(&ctx->parse_ctx, encoded_len);
- if (parse_advance_nl(ctx) < 0) {
- error = parse_err("trailing data at line %"PRIuZ, ctx->line_num);
+ if (git_parse_advance_nl(&ctx->parse_ctx) < 0) {
+ error = git_parse_err("trailing data at line %"PRIuZ, ctx->parse_ctx.line_num);
goto done;
}
}
binary->type = type;
binary->inflatedlen = (size_t)len;
binary->datalen = decoded.size;
- binary->data = git_buf_detach(&decoded);
+ binary->data = git_str_detach(&decoded);
done:
- git_buf_free(&base85);
- git_buf_free(&decoded);
+ git_str_dispose(&base85);
+ git_str_dispose(&decoded);
return error;
}
{
int error;
- if (parse_advance_expected_str(ctx, "GIT binary patch") < 0 ||
- parse_advance_nl(ctx) < 0)
- return parse_err("corrupt git binary header at line %"PRIuZ, ctx->line_num);
+ if (git_parse_advance_expected_str(&ctx->parse_ctx, "GIT binary patch") < 0 ||
+ git_parse_advance_nl(&ctx->parse_ctx) < 0)
+ return git_parse_err("corrupt git binary header at line %"PRIuZ, ctx->parse_ctx.line_num);
/* parse old->new binary diff */
if ((error = parse_patch_binary_side(
&patch->base.binary.new_file, ctx)) < 0)
return error;
- if (parse_advance_nl(ctx) < 0)
- return parse_err("corrupt git binary separator at line %"PRIuZ,
- ctx->line_num);
+ if (git_parse_advance_nl(&ctx->parse_ctx) < 0)
+ return git_parse_err("corrupt git binary separator at line %"PRIuZ,
+ ctx->parse_ctx.line_num);
/* parse new->old binary diff */
if ((error = parse_patch_binary_side(
&patch->base.binary.old_file, ctx)) < 0)
return error;
- if (parse_advance_nl(ctx) < 0)
- return parse_err("corrupt git binary patch separator at line %"PRIuZ,
- ctx->line_num);
+ if (git_parse_advance_nl(&ctx->parse_ctx) < 0)
+ return git_parse_err("corrupt git binary patch separator at line %"PRIuZ,
+ ctx->parse_ctx.line_num);
patch->base.binary.contains_data = 1;
patch->base.delta->flags |= GIT_DIFF_FLAG_BINARY;
git_patch_parsed *patch,
git_patch_parse_ctx *ctx)
{
- if (parse_advance_expected_str(ctx, "Binary files ") < 0 ||
- parse_advance_expected_str(ctx, patch->header_old_path) < 0 ||
- parse_advance_expected_str(ctx, " and ") < 0 ||
- parse_advance_expected_str(ctx, patch->header_new_path) < 0 ||
- parse_advance_expected_str(ctx, " differ") < 0 ||
- parse_advance_nl(ctx) < 0)
- return parse_err("corrupt git binary header at line %"PRIuZ, ctx->line_num);
+ const char *old = patch->old_path ? patch->old_path : patch->header_old_path;
+ const char *new = patch->new_path ? patch->new_path : patch->header_new_path;
+
+ if (!old || !new)
+ return git_parse_err("corrupt binary data without paths at line %"PRIuZ, ctx->parse_ctx.line_num);
+
+ if (patch->base.delta->status == GIT_DELTA_ADDED)
+ old = "/dev/null";
+ else if (patch->base.delta->status == GIT_DELTA_DELETED)
+ new = "/dev/null";
+
+ if (git_parse_advance_expected_str(&ctx->parse_ctx, "Binary files ") < 0 ||
+ git_parse_advance_expected_str(&ctx->parse_ctx, old) < 0 ||
+ git_parse_advance_expected_str(&ctx->parse_ctx, " and ") < 0 ||
+ git_parse_advance_expected_str(&ctx->parse_ctx, new) < 0 ||
+ git_parse_advance_expected_str(&ctx->parse_ctx, " differ") < 0 ||
+ git_parse_advance_nl(&ctx->parse_ctx) < 0)
+ return git_parse_err("corrupt git binary header at line %"PRIuZ, ctx->parse_ctx.line_num);
patch->base.binary.contains_data = 0;
patch->base.delta->flags |= GIT_DIFF_FLAG_BINARY;
git_patch_hunk *hunk;
int error = 0;
- while (parse_ctx_contains_s(ctx, "@@ -")) {
+ while (git_parse_ctx_contains_s(&ctx->parse_ctx, "@@ -")) {
hunk = git_array_alloc(patch->base.hunks);
- GITERR_CHECK_ALLOC(hunk);
+ GIT_ERROR_CHECK_ALLOC(hunk);
memset(hunk, 0, sizeof(git_patch_hunk));
static int parse_patch_body(
git_patch_parsed *patch, git_patch_parse_ctx *ctx)
{
- if (parse_ctx_contains_s(ctx, "GIT binary patch"))
+ if (git_parse_ctx_contains_s(&ctx->parse_ctx, "GIT binary patch"))
return parse_patch_binary(patch, ctx);
- else if (parse_ctx_contains_s(ctx, "Binary files "))
+ else if (git_parse_ctx_contains_s(&ctx->parse_ctx, "Binary files "))
return parse_patch_binary_nodata(patch, ctx);
else
return parse_patch_hunks(patch, ctx);
}
-int check_header_names(
+static int check_header_names(
const char *one,
const char *two,
const char *old_or_new,
return 0;
if (two_null && strcmp(two, "/dev/null") != 0)
- return parse_err("expected %s path of '/dev/null'", old_or_new);
+ return git_parse_err("expected %s path of '/dev/null'", old_or_new);
else if (!two_null && strcmp(one, two) != 0)
- return parse_err("mismatched %s path names", old_or_new);
+ return git_parse_err("mismatched %s path names", old_or_new);
return 0;
}
}
if (remain_len || !*path)
- return parse_err(
+ return git_parse_err(
"header filename does not contain %"PRIuZ" path components",
prefix_len);
bool deleted = (patch->base.delta->status == GIT_DELTA_DELETED);
if (patch->old_path && !patch->new_path)
- return parse_err("missing new path");
+ return git_parse_err("missing new path");
if (!patch->old_path && patch->new_path)
- return parse_err("missing old path");
+ return git_parse_err("missing old path");
/* Ensure (non-renamed) paths match */
- if (check_header_names(
- patch->header_old_path, patch->old_path, "old", added) < 0 ||
- check_header_names(
- patch->header_new_path, patch->new_path, "new", deleted) < 0)
+ if (check_header_names(patch->header_old_path, patch->old_path, "old", added) < 0 ||
+ check_header_names(patch->header_new_path, patch->new_path, "new", deleted) < 0)
return -1;
- prefixed_old = (!added && patch->old_path) ? patch->old_path :
- patch->header_old_path;
- prefixed_new = (!deleted && patch->new_path) ? patch->new_path :
- patch->header_new_path;
+ prefixed_old = (!added && patch->old_path) ? patch->old_path : patch->header_old_path;
+ prefixed_new = (!deleted && patch->new_path) ? patch->new_path : patch->header_new_path;
- if (check_prefix(
- &patch->old_prefix, &old_prefixlen, patch, prefixed_old) < 0 ||
- check_prefix(
- &patch->new_prefix, &new_prefixlen, patch, prefixed_new) < 0)
+ if ((prefixed_old && check_prefix(&patch->old_prefix, &old_prefixlen, patch, prefixed_old) < 0) ||
+ (prefixed_new && check_prefix(&patch->new_prefix, &new_prefixlen, patch, prefixed_new) < 0))
return -1;
/* Prefer the rename filenames as they are unambiguous and unprefixed */
if (patch->rename_old_path)
patch->base.delta->old_file.path = patch->rename_old_path;
- else
+ else if (prefixed_old)
patch->base.delta->old_file.path = prefixed_old + old_prefixlen;
+ else
+ patch->base.delta->old_file.path = NULL;
if (patch->rename_new_path)
patch->base.delta->new_file.path = patch->rename_new_path;
- else
+ else if (prefixed_new)
patch->base.delta->new_file.path = prefixed_new + new_prefixlen;
+ else
+ patch->base.delta->new_file.path = NULL;
if (!patch->base.delta->old_file.path &&
- !patch->base.delta->new_file.path)
- return parse_err("git diff header lacks old / new paths");
+ !patch->base.delta->new_file.path)
+ return git_parse_err("git diff header lacks old / new paths");
return 0;
}
return -1;
if (delta->old_file.path &&
- delta->status != GIT_DELTA_DELETED &&
- !delta->new_file.mode)
+ delta->status != GIT_DELTA_DELETED &&
+ !delta->new_file.mode)
delta->new_file.mode = delta->old_file.mode;
if (delta->status == GIT_DELTA_MODIFIED &&
- !(delta->flags & GIT_DIFF_FLAG_BINARY) &&
- delta->new_file.mode == delta->old_file.mode &&
- git_array_size(patch->base.hunks) == 0)
- return parse_err("patch with no hunks");
+ !(delta->flags & GIT_DIFF_FLAG_BINARY) &&
+ delta->new_file.mode == delta->old_file.mode &&
+ git_array_size(patch->base.hunks) == 0)
+ return git_parse_err("patch with no hunks");
if (delta->status == GIT_DELTA_ADDED) {
memset(&delta->old_file.id, 0x0, sizeof(git_oid));
if ((ctx = git__calloc(1, sizeof(git_patch_parse_ctx))) == NULL)
return NULL;
- if (content_len) {
- if ((ctx->content = git__malloc(content_len)) == NULL) {
- git__free(ctx);
- return NULL;
- }
-
- memcpy((char *)ctx->content, content, content_len);
+ if ((git_parse_ctx_init(&ctx->parse_ctx, content, content_len)) < 0) {
+ git__free(ctx);
+ return NULL;
}
- ctx->content_len = content_len;
- ctx->remain = ctx->content;
- ctx->remain_len = ctx->content_len;
-
if (opts)
memcpy(&ctx->opts, opts, sizeof(git_patch_options));
else
if (!ctx)
return;
- git__free((char *)ctx->content);
+ git_parse_ctx_clear(&ctx->parse_ctx);
git__free(ctx);
}
static void patch_parsed__free(git_patch *p)
{
git_patch_parsed *patch = (git_patch_parsed *)p;
+ git_diff_line *line;
+ size_t i;
if (!patch)
return;
git__free((char *)patch->base.binary.old_file.data);
git__free((char *)patch->base.binary.new_file.data);
git_array_clear(patch->base.hunks);
+ git_array_foreach(patch->base.lines, i, line)
+ git__free((char *) line->content);
git_array_clear(patch->base.lines);
git__free(patch->base.delta);
size_t start, used;
int error = 0;
- assert(out && ctx);
+ GIT_ASSERT_ARG(out);
+ GIT_ASSERT_ARG(ctx);
*out = NULL;
patch = git__calloc(1, sizeof(git_patch_parsed));
- GITERR_CHECK_ALLOC(patch);
+ GIT_ERROR_CHECK_ALLOC(patch);
patch->ctx = ctx;
GIT_REFCOUNT_INC(patch->ctx);
patch->base.free_fn = patch_parsed__free;
patch->base.delta = git__calloc(1, sizeof(git_diff_delta));
- GITERR_CHECK_ALLOC(patch->base.delta);
+ GIT_ERROR_CHECK_ALLOC(patch->base.delta);
patch->base.delta->status = GIT_DELTA_MODIFIED;
patch->base.delta->nfiles = 2;
- start = ctx->remain_len;
+ start = ctx->parse_ctx.remain_len;
if ((error = parse_patch_header(patch, ctx)) < 0 ||
(error = parse_patch_body(patch, ctx)) < 0 ||
(error = check_patch(patch)) < 0)
goto done;
- used = start - ctx->remain_len;
- ctx->remain += used;
+ used = start - ctx->parse_ctx.remain_len;
+ ctx->parse_ctx.remain += used;
patch->base.diff_opts.old_prefix = patch->old_prefix;
patch->base.diff_opts.new_prefix = patch->new_prefix;
patch->base.diff_opts.flags |= GIT_DIFF_SHOW_BINARY;
- GIT_REFCOUNT_INC(patch);
+ GIT_REFCOUNT_INC(&patch->base);
*out = &patch->base;
done:
int error;
ctx = git_patch_parse_ctx_init(content, content_len, opts);
- GITERR_CHECK_ALLOC(ctx);
+ GIT_ERROR_CHECK_ALLOC(ctx);
error = git_patch_parse(out, ctx);