Commit b85bd8ce by Edward Thomson Committed by Edward Thomson

patch: use delta's old_file/new_file members

No need to replicate the old_file/new_file members, or plumb them
strangely up.
parent 804d5fe9
......@@ -340,7 +340,7 @@ int git_apply__patch(
*mode_out = 0;
if (patch->delta->status != GIT_DELTA_DELETED) {
const git_diff_file *newfile = patch->newfile(patch);
const git_diff_file *newfile = &patch->delta->new_file;
filename = git__strdup(newfile->path);
mode = newfile->mode ?
......
......@@ -33,8 +33,6 @@ struct git_patch {
size_t content_size;
size_t context_size;
const git_diff_file *(*newfile)(git_patch *patch);
const git_diff_file *(*oldfile)(git_patch *patch);
void (*free_fn)(git_patch *patch);
};
......
......@@ -21,18 +21,6 @@ static void diff_output_init(
static void diff_output_to_patch(git_patch_diff_output *, git_patch_diff *);
static const git_diff_file *patch_diff_newfile(git_patch *p)
{
git_patch_diff *patch = (git_patch_diff *)p;
return patch->nfile.file;
}
static const git_diff_file *patch_diff_oldfile(git_patch *p)
{
git_patch_diff *patch = (git_patch_diff *)p;
return patch->ofile.file;
}
static void patch_diff_free(git_patch *p)
{
git_patch_diff *patch = (git_patch_diff *)p;
......@@ -72,8 +60,6 @@ static void patch_diff_update_binary(git_patch_diff *patch)
static void patch_diff_init_common(git_patch_diff *patch)
{
patch->base.newfile = patch_diff_newfile;
patch->base.oldfile = patch_diff_oldfile;
patch->base.free_fn = patch_diff_free;
patch_diff_update_binary(patch);
......
......@@ -5,11 +5,9 @@
#define parse_err(...) \
( giterr_set(GITERR_PATCH, __VA_ARGS__), -1 )
/* TODO: remove this, just use git_patch */
typedef struct {
git_patch base;
git_diff_file old_file;
git_diff_file new_file;
} git_patch_parsed;
typedef struct {
......@@ -141,13 +139,13 @@ static int parse_header_path(char **out, patch_parse_ctx *ctx)
static int parse_header_git_oldpath(
git_patch_parsed *patch, patch_parse_ctx *ctx)
{
return parse_header_path((char **)&patch->old_file.path, ctx);
return parse_header_path((char **)&patch->base.delta->old_file.path, ctx);
}
static int parse_header_git_newpath(
git_patch_parsed *patch, patch_parse_ctx *ctx)
{
return parse_header_path((char **)&patch->new_file.path, ctx);
return parse_header_path((char **)&patch->base.delta->new_file.path, ctx);
}
static int parse_header_mode(uint16_t *mode, patch_parse_ctx *ctx)
......@@ -231,37 +229,37 @@ static int parse_header_git_index(
static int parse_header_git_oldmode(
git_patch_parsed *patch, patch_parse_ctx *ctx)
{
return parse_header_mode(&patch->old_file.mode, ctx);
return parse_header_mode(&patch->base.delta->old_file.mode, ctx);
}
static int parse_header_git_newmode(
git_patch_parsed *patch, patch_parse_ctx *ctx)
{
return parse_header_mode(&patch->new_file.mode, ctx);
return parse_header_mode(&patch->base.delta->new_file.mode, ctx);
}
static int parse_header_git_deletedfilemode(
git_patch_parsed *patch,
patch_parse_ctx *ctx)
{
git__free((char *)patch->old_file.path);
git__free((char *)patch->base.delta->old_file.path);
patch->old_file.path = NULL;
patch->base.delta->old_file.path = NULL;
patch->base.delta->status = GIT_DELTA_DELETED;
return parse_header_mode(&patch->old_file.mode, ctx);
return parse_header_mode(&patch->base.delta->old_file.mode, ctx);
}
static int parse_header_git_newfilemode(
git_patch_parsed *patch,
patch_parse_ctx *ctx)
{
git__free((char *)patch->new_file.path);
git__free((char *)patch->base.delta->new_file.path);
patch->new_file.path = NULL;
patch->base.delta->new_file.path = NULL;
patch->base.delta->status = GIT_DELTA_ADDED;
return parse_header_mode(&patch->new_file.mode, ctx);
return parse_header_mode(&patch->base.delta->new_file.mode, ctx);
}
static int parse_header_rename(
......@@ -313,7 +311,7 @@ static int parse_header_renamefrom(
patch->base.delta->status |= GIT_DELTA_RENAMED;
return parse_header_rename(
(char **)&patch->old_file.path,
(char **)&patch->base.delta->old_file.path,
&ctx->header_old_path,
ctx);
}
......@@ -324,7 +322,7 @@ static int parse_header_renameto(
patch->base.delta->status |= GIT_DELTA_RENAMED;
return parse_header_rename(
(char **)&patch->new_file.path,
(char **)&patch->base.delta->new_file.path,
&ctx->header_new_path,
ctx);
}
......@@ -674,16 +672,18 @@ static int parsed_patch_header(
/* For modechange only patches, it does not include filenames;
* instead we need to use the paths in the diff --git header.
*/
if (!patch->old_file.path && !patch->new_file.path) {
if (!patch->base.delta->old_file.path &&
!patch->base.delta->new_file.path) {
if (!ctx->header_old_path || !ctx->header_new_path) {
error = parse_err("git diff header lacks old / new paths");
goto done;
}
patch->old_file.path = ctx->header_old_path;
patch->base.delta->old_file.path = ctx->header_old_path;
ctx->header_old_path = NULL;
patch->new_file.path = ctx->header_new_path;
patch->base.delta->new_file.path = ctx->header_new_path;
ctx->header_new_path = NULL;
}
......@@ -848,38 +848,28 @@ static int parsed_patch_body(
static int check_patch(git_patch_parsed *patch)
{
if (!patch->old_file.path && patch->base.delta->status != GIT_DELTA_ADDED)
if (!patch->base.delta->old_file.path &&
patch->base.delta->status != GIT_DELTA_ADDED)
return parse_err("missing old file path");
if (!patch->new_file.path && patch->base.delta->status != GIT_DELTA_DELETED)
if (!patch->base.delta->new_file.path &&
patch->base.delta->status != GIT_DELTA_DELETED)
return parse_err("missing new file path");
if (patch->old_file.path && patch->new_file.path) {
if (!patch->new_file.mode)
patch->new_file.mode = patch->old_file.mode;
if (patch->base.delta->old_file.path && patch->base.delta->new_file.path) {
if (!patch->base.delta->new_file.mode)
patch->base.delta->new_file.mode = patch->base.delta->old_file.mode;
}
if (patch->base.delta->status == GIT_DELTA_MODIFIED &&
!(patch->base.delta->flags & GIT_DIFF_FLAG_BINARY) &&
patch->new_file.mode == patch->old_file.mode &&
patch->base.delta->new_file.mode == patch->base.delta->old_file.mode &&
git_array_size(patch->base.hunks) == 0)
return parse_err("patch with no hunks");
return 0;
}
static const git_diff_file *parsed_patch_newfile(git_patch *p)
{
git_patch_parsed *patch = (git_patch_parsed *)p;
return &patch->new_file;
}
static const git_diff_file *parsed_patch_oldfile(git_patch *p)
{
git_patch_parsed *patch = (git_patch_parsed *)p;
return &patch->old_file;
}
int git_patch_from_patchfile(
git_patch **out,
const char *content,
......@@ -894,9 +884,6 @@ int git_patch_from_patchfile(
patch = git__calloc(1, sizeof(git_patch_parsed));
GITERR_CHECK_ALLOC(patch);
patch->base.newfile = parsed_patch_newfile;
patch->base.oldfile = parsed_patch_oldfile;
patch->base.delta = git__calloc(1, sizeof(git_diff_delta));
patch->base.delta->status = GIT_DELTA_MODIFIED;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment