Fix diff header naming issues

This makes the git_diff_patch definition private to diff_patch.c
and fixes a number of other header file naming inconsistencies to
use `git_` prefixes on functions and structures that are shared
between files.
This commit is contained in:
Russell Belfer 2013-06-12 14:18:09 -07:00
parent ef3374a8a8
commit 360f42f4b3
6 changed files with 154 additions and 95 deletions

View File

@ -85,7 +85,7 @@ static int diff_file_content_init_common(
return 0; return 0;
} }
int diff_file_content_init_from_diff( int git_diff_file_content__init_from_diff(
git_diff_file_content *fc, git_diff_file_content *fc,
git_diff_list *diff, git_diff_list *diff,
size_t delta_index, size_t delta_index,
@ -127,7 +127,7 @@ int diff_file_content_init_from_diff(
return diff_file_content_init_common(fc, &diff->opts); return diff_file_content_init_common(fc, &diff->opts);
} }
int diff_file_content_init_from_blob( int git_diff_file_content__init_from_blob(
git_diff_file_content *fc, git_diff_file_content *fc,
git_repository *repo, git_repository *repo,
const git_diff_options *opts, const git_diff_options *opts,
@ -152,7 +152,7 @@ int diff_file_content_init_from_blob(
return diff_file_content_init_common(fc, opts); return diff_file_content_init_common(fc, opts);
} }
int diff_file_content_init_from_raw( int git_diff_file_content__init_from_raw(
git_diff_file_content *fc, git_diff_file_content *fc,
git_repository *repo, git_repository *repo,
const git_diff_options *opts, const git_diff_options *opts,
@ -385,7 +385,7 @@ static int diff_file_content_load_workdir(git_diff_file_content *fc)
return error; return error;
} }
int diff_file_content_load(git_diff_file_content *fc) int git_diff_file_content__load(git_diff_file_content *fc)
{ {
int error = 0; int error = 0;
@ -409,7 +409,7 @@ int diff_file_content_load(git_diff_file_content *fc)
return 0; return 0;
} }
void diff_file_content_unload(git_diff_file_content *fc) void git_diff_file_content__unload(git_diff_file_content *fc)
{ {
if (fc->file.flags & GIT_DIFF_FLAG__FREE_DATA) { if (fc->file.flags & GIT_DIFF_FLAG__FREE_DATA) {
git__free(fc->map.data); git__free(fc->map.data);
@ -433,9 +433,9 @@ void diff_file_content_unload(git_diff_file_content *fc)
fc->file.flags &= ~GIT_DIFF_FLAG__LOADED; fc->file.flags &= ~GIT_DIFF_FLAG__LOADED;
} }
void diff_file_content_clear(git_diff_file_content *fc) void git_diff_file_content__clear(git_diff_file_content *fc)
{ {
diff_file_content_unload(fc); git_diff_file_content__unload(fc);
/* for now, nothing else to do */ /* for now, nothing else to do */
} }

View File

@ -24,19 +24,19 @@ typedef struct {
git_map map; git_map map;
} git_diff_file_content; } git_diff_file_content;
extern int diff_file_content_init_from_diff( extern int git_diff_file_content__init_from_diff(
git_diff_file_content *fc, git_diff_file_content *fc,
git_diff_list *diff, git_diff_list *diff,
size_t delta_index, size_t delta_index,
bool use_old); bool use_old);
extern int diff_file_content_init_from_blob( extern int git_diff_file_content__init_from_blob(
git_diff_file_content *fc, git_diff_file_content *fc,
git_repository *repo, git_repository *repo,
const git_diff_options *opts, const git_diff_options *opts,
const git_blob *blob); const git_blob *blob);
extern int diff_file_content_init_from_raw( extern int git_diff_file_content__init_from_raw(
git_diff_file_content *fc, git_diff_file_content *fc,
git_repository *repo, git_repository *repo,
const git_diff_options *opts, const git_diff_options *opts,
@ -44,12 +44,12 @@ extern int diff_file_content_init_from_raw(
size_t buflen); size_t buflen);
/* this loads the blob/file-on-disk as needed */ /* this loads the blob/file-on-disk as needed */
extern int diff_file_content_load(git_diff_file_content *fc); extern int git_diff_file_content__load(git_diff_file_content *fc);
/* this releases the blob/file-in-memory */ /* this releases the blob/file-in-memory */
extern void diff_file_content_unload(git_diff_file_content *fc); extern void git_diff_file_content__unload(git_diff_file_content *fc);
/* this unloads and also releases any other resources */ /* this unloads and also releases any other resources */
extern void diff_file_content_clear(git_diff_file_content *fc); extern void git_diff_file_content__clear(git_diff_file_content *fc);
#endif #endif

View File

@ -11,6 +11,49 @@
#include "diff_patch.h" #include "diff_patch.h"
#include "diff_xdiff.h" #include "diff_xdiff.h"
/* cached information about a single span in a diff */
typedef struct diff_patch_line diff_patch_line;
struct diff_patch_line {
const char *ptr;
size_t len;
size_t lines, oldno, newno;
char origin;
};
/* cached information about a hunk in a diff */
typedef struct diff_patch_hunk diff_patch_hunk;
struct diff_patch_hunk {
git_diff_range range;
char header[128];
size_t header_len;
size_t line_start;
size_t line_count;
};
struct git_diff_patch {
git_refcount rc;
git_diff_list *diff; /* for refcount purposes, maybe NULL for blob diffs */
git_diff_delta *delta;
size_t delta_index;
git_diff_file_content ofile;
git_diff_file_content nfile;
uint32_t flags;
git_array_t(diff_patch_hunk) hunks;
git_array_t(diff_patch_line) lines;
size_t oldno, newno;
size_t content_size;
git_pool flattened;
};
enum {
GIT_DIFF_PATCH_ALLOCATED = (1 << 0),
GIT_DIFF_PATCH_INITIALIZED = (1 << 1),
GIT_DIFF_PATCH_LOADED = (1 << 2),
GIT_DIFF_PATCH_DIFFABLE = (1 << 3),
GIT_DIFF_PATCH_DIFFED = (1 << 4),
GIT_DIFF_PATCH_FLATTENED = (1 << 5),
};
static void diff_output_init(git_diff_output*, const git_diff_options*, static void diff_output_init(git_diff_output*, const git_diff_options*,
git_diff_file_cb, git_diff_hunk_cb, git_diff_data_cb, void*); git_diff_file_cb, git_diff_hunk_cb, git_diff_data_cb, void*);
@ -53,9 +96,9 @@ static int diff_patch_init_from_diff(
patch->delta = git_vector_get(&diff->deltas, delta_index); patch->delta = git_vector_get(&diff->deltas, delta_index);
patch->delta_index = delta_index; patch->delta_index = delta_index;
if ((error = diff_file_content_init_from_diff( if ((error = git_diff_file_content__init_from_diff(
&patch->ofile, diff, delta_index, true)) < 0 || &patch->ofile, diff, delta_index, true)) < 0 ||
(error = diff_file_content_init_from_diff( (error = git_diff_file_content__init_from_diff(
&patch->nfile, diff, delta_index, false)) < 0) &patch->nfile, diff, delta_index, false)) < 0)
return error; return error;
@ -110,24 +153,24 @@ static int diff_patch_load(git_diff_patch *patch, git_diff_output *output)
* need 2x data size and this minimizes peak memory footprint * need 2x data size and this minimizes peak memory footprint
*/ */
if (patch->ofile.src == GIT_ITERATOR_TYPE_WORKDIR) { if (patch->ofile.src == GIT_ITERATOR_TYPE_WORKDIR) {
if ((error = diff_file_content_load(&patch->ofile)) < 0 || if ((error = git_diff_file_content__load(&patch->ofile)) < 0 ||
(patch->ofile.file.flags & GIT_DIFF_FLAG_BINARY) != 0) (patch->ofile.file.flags & GIT_DIFF_FLAG_BINARY) != 0)
goto cleanup; goto cleanup;
} }
if (patch->nfile.src == GIT_ITERATOR_TYPE_WORKDIR) { if (patch->nfile.src == GIT_ITERATOR_TYPE_WORKDIR) {
if ((error = diff_file_content_load(&patch->nfile)) < 0 || if ((error = git_diff_file_content__load(&patch->nfile)) < 0 ||
(patch->nfile.file.flags & GIT_DIFF_FLAG_BINARY) != 0) (patch->nfile.file.flags & GIT_DIFF_FLAG_BINARY) != 0)
goto cleanup; goto cleanup;
} }
/* once workdir has been tried, load other data as needed */ /* once workdir has been tried, load other data as needed */
if (patch->ofile.src != GIT_ITERATOR_TYPE_WORKDIR) { if (patch->ofile.src != GIT_ITERATOR_TYPE_WORKDIR) {
if ((error = diff_file_content_load(&patch->ofile)) < 0 || if ((error = git_diff_file_content__load(&patch->ofile)) < 0 ||
(patch->ofile.file.flags & GIT_DIFF_FLAG_BINARY) != 0) (patch->ofile.file.flags & GIT_DIFF_FLAG_BINARY) != 0)
goto cleanup; goto cleanup;
} }
if (patch->nfile.src != GIT_ITERATOR_TYPE_WORKDIR) { if (patch->nfile.src != GIT_ITERATOR_TYPE_WORKDIR) {
if ((error = diff_file_content_load(&patch->nfile)) < 0 || if ((error = git_diff_file_content__load(&patch->nfile)) < 0 ||
(patch->nfile.file.flags & GIT_DIFF_FLAG_BINARY) != 0) (patch->nfile.file.flags & GIT_DIFF_FLAG_BINARY) != 0)
goto cleanup; goto cleanup;
} }
@ -198,8 +241,8 @@ static int diff_patch_generate(git_diff_patch *patch, git_diff_output *output)
static void diff_patch_free(git_diff_patch *patch) static void diff_patch_free(git_diff_patch *patch)
{ {
diff_file_content_clear(&patch->ofile); git_diff_file_content__clear(&patch->ofile);
diff_file_content_clear(&patch->nfile); git_diff_file_content__clear(&patch->nfile);
git_array_clear(patch->lines); git_array_clear(patch->lines);
git_array_clear(patch->hunks); git_array_clear(patch->hunks);
@ -323,9 +366,9 @@ static int diff_patch_from_blobs(
new_blob = swap; new_blob = swap;
} }
if ((error = diff_file_content_init_from_blob( if ((error = git_diff_file_content__init_from_blob(
&pd->patch.ofile, repo, opts, old_blob)) < 0 || &pd->patch.ofile, repo, opts, old_blob)) < 0 ||
(error = diff_file_content_init_from_blob( (error = git_diff_file_content__init_from_blob(
&pd->patch.nfile, repo, opts, new_blob)) < 0) &pd->patch.nfile, repo, opts, new_blob)) < 0)
return error; return error;
@ -409,14 +452,14 @@ static int diff_patch_from_blob_and_buffer(
return 0; return 0;
if (opts && (opts->flags & GIT_DIFF_REVERSE) != 0) { if (opts && (opts->flags & GIT_DIFF_REVERSE) != 0) {
if (!(error = diff_file_content_init_from_raw( if (!(error = git_diff_file_content__init_from_raw(
&pd->patch.ofile, repo, opts, buf, buflen))) &pd->patch.ofile, repo, opts, buf, buflen)))
error = diff_file_content_init_from_blob( error = git_diff_file_content__init_from_blob(
&pd->patch.nfile, repo, opts, old_blob); &pd->patch.nfile, repo, opts, old_blob);
} else { } else {
if (!(error = diff_file_content_init_from_blob( if (!(error = git_diff_file_content__init_from_blob(
&pd->patch.ofile, repo, opts, old_blob))) &pd->patch.ofile, repo, opts, old_blob)))
error = diff_file_content_init_from_raw( error = git_diff_file_content__init_from_raw(
&pd->patch.nfile, repo, opts, buf, buflen); &pd->patch.nfile, repo, opts, buf, buflen);
} }
@ -690,6 +733,68 @@ notfound:
return diff_error_outofrange(thing); return diff_error_outofrange(thing);
} }
git_diff_list *git_diff_patch__diff(git_diff_patch *patch)
{
return patch->diff;
}
git_diff_driver *git_diff_patch__driver(git_diff_patch *patch)
{
/* ofile driver is representative for whole patch */
return patch->ofile.driver;
}
void git_diff_patch__old_data(
char **ptr, size_t *len, git_diff_patch *patch)
{
*ptr = patch->ofile.map.data;
*len = patch->ofile.map.len;
}
void git_diff_patch__new_data(
char **ptr, size_t *len, git_diff_patch *patch)
{
*ptr = patch->nfile.map.data;
*len = patch->nfile.map.len;
}
int git_diff_patch__invoke_callbacks(
git_diff_patch *patch,
git_diff_file_cb file_cb,
git_diff_hunk_cb hunk_cb,
git_diff_data_cb line_cb,
void *payload)
{
int error = 0;
uint32_t i, j;
if (file_cb)
error = file_cb(patch->delta, 0, payload);
if (!hunk_cb && !line_cb)
return error;
for (i = 0; !error && i < git_array_size(patch->hunks); ++i) {
diff_patch_hunk *h = git_array_get(patch->hunks, i);
error = hunk_cb(
patch->delta, &h->range, h->header, h->header_len, payload);
if (!line_cb)
continue;
for (j = 0; !error && j < h->line_count; ++j) {
diff_patch_line *l =
git_array_get(patch->lines, h->line_start + j);
error = line_cb(
patch->delta, &h->range, l->origin, l->ptr, l->len, payload);
}
}
return error;
}
static int diff_patch_file_cb( static int diff_patch_file_cb(
const git_diff_delta *delta, const git_diff_delta *delta,

View File

@ -12,48 +12,19 @@
#include "diff_file.h" #include "diff_file.h"
#include "array.h" #include "array.h"
/* cached information about a single span in a diff */ extern git_diff_list *git_diff_patch__diff(git_diff_patch *);
typedef struct diff_patch_line diff_patch_line;
struct diff_patch_line {
const char *ptr;
size_t len;
size_t lines, oldno, newno;
char origin;
};
/* cached information about a hunk in a diff */ extern git_diff_driver *git_diff_patch__driver(git_diff_patch *);
typedef struct diff_patch_hunk diff_patch_hunk;
struct diff_patch_hunk {
git_diff_range range;
char header[128];
size_t header_len;
size_t line_start;
size_t line_count;
};
struct git_diff_patch { extern void git_diff_patch__old_data(char **, size_t *, git_diff_patch *);
git_refcount rc; extern void git_diff_patch__new_data(char **, size_t *, git_diff_patch *);
git_diff_list *diff; /* for refcount purposes, maybe NULL for blob diffs */
git_diff_delta *delta;
size_t delta_index;
git_diff_file_content ofile;
git_diff_file_content nfile;
uint32_t flags;
git_array_t(diff_patch_hunk) hunks;
git_array_t(diff_patch_line) lines;
size_t oldno, newno;
size_t content_size;
git_pool flattened;
};
enum { extern int git_diff_patch__invoke_callbacks(
GIT_DIFF_PATCH_ALLOCATED = (1 << 0), git_diff_patch *patch,
GIT_DIFF_PATCH_INITIALIZED = (1 << 1), git_diff_file_cb file_cb,
GIT_DIFF_PATCH_LOADED = (1 << 2), git_diff_hunk_cb hunk_cb,
GIT_DIFF_PATCH_DIFFABLE = (1 << 3), git_diff_data_cb line_cb,
GIT_DIFF_PATCH_DIFFED = (1 << 4), void *payload);
GIT_DIFF_PATCH_FLATTENED = (1 << 5),
};
typedef struct git_diff_output git_diff_output; typedef struct git_diff_output git_diff_output;
struct git_diff_output { struct git_diff_output {

View File

@ -383,29 +383,13 @@ int git_diff_patch_print(
int error; int error;
git_buf temp = GIT_BUF_INIT; git_buf temp = GIT_BUF_INIT;
diff_print_info pi; diff_print_info pi;
size_t h, l;
assert(patch && print_cb); assert(patch && print_cb);
if (!(error = diff_print_info_init( if (!(error = diff_print_info_init(
&pi, &temp, patch->diff, print_cb, payload))) &pi, &temp, git_diff_patch__diff(patch), print_cb, payload)))
error = print_patch_file(patch->delta, 0, &pi); error = git_diff_patch__invoke_callbacks(
patch, print_patch_file, print_patch_hunk, print_patch_line, &pi);
for (h = 0; h < git_array_size(patch->hunks) && !error; ++h) {
diff_patch_hunk *hunk = git_array_get(patch->hunks, h);
error = print_patch_hunk(
patch->delta, &hunk->range, hunk->header, hunk->header_len, &pi);
for (l = 0; l < hunk->line_count && !error; ++l) {
diff_patch_line *line =
git_array_get(patch->lines, hunk->line_start + l);
error = print_patch_line(
patch->delta, &hunk->range,
line->origin, line->ptr, line->len, &pi);
}
}
git_buf_free(&temp); git_buf_free(&temp);

View File

@ -59,6 +59,7 @@ static int git_xdiff_cb(void *priv, mmbuffer_t *bufs, int len)
{ {
git_xdiff_info *info = priv; git_xdiff_info *info = priv;
git_diff_patch *patch = info->patch; git_diff_patch *patch = info->patch;
const git_diff_delta *delta = git_diff_patch_delta(patch);
git_diff_output *output = &info->xo->output; git_diff_output *output = &info->xo->output;
if (len == 1) { if (len == 1) {
@ -67,7 +68,7 @@ static int git_xdiff_cb(void *priv, mmbuffer_t *bufs, int len)
return output->error; return output->error;
if (output->hunk_cb != NULL && if (output->hunk_cb != NULL &&
output->hunk_cb(patch->delta, &info->range, output->hunk_cb(delta, &info->range,
bufs[0].ptr, bufs[0].size, output->payload)) bufs[0].ptr, bufs[0].size, output->payload))
output->error = GIT_EUSER; output->error = GIT_EUSER;
} }
@ -80,7 +81,7 @@ static int git_xdiff_cb(void *priv, mmbuffer_t *bufs, int len)
GIT_DIFF_LINE_CONTEXT; GIT_DIFF_LINE_CONTEXT;
if (output->data_cb != NULL && if (output->data_cb != NULL &&
output->data_cb(patch->delta, &info->range, output->data_cb(delta, &info->range,
origin, bufs[1].ptr, bufs[1].size, output->payload)) origin, bufs[1].ptr, bufs[1].size, output->payload))
output->error = GIT_EUSER; output->error = GIT_EUSER;
} }
@ -97,7 +98,7 @@ static int git_xdiff_cb(void *priv, mmbuffer_t *bufs, int len)
GIT_DIFF_LINE_CONTEXT_EOFNL; GIT_DIFF_LINE_CONTEXT_EOFNL;
if (output->data_cb != NULL && if (output->data_cb != NULL &&
output->data_cb(patch->delta, &info->range, output->data_cb(delta, &info->range,
origin, bufs[2].ptr, bufs[2].size, output->payload)) origin, bufs[2].ptr, bufs[2].size, output->payload))
output->error = GIT_EUSER; output->error = GIT_EUSER;
} }
@ -110,7 +111,7 @@ static int git_xdiff(git_diff_output *output, git_diff_patch *patch)
git_xdiff_output *xo = (git_xdiff_output *)output; git_xdiff_output *xo = (git_xdiff_output *)output;
git_xdiff_info info; git_xdiff_info info;
git_diff_find_context_payload findctxt; git_diff_find_context_payload findctxt;
mmfile_t old_xdiff_data, new_xdiff_data; mmfile_t xd_old_data, xd_new_data;
memset(&info, 0, sizeof(info)); memset(&info, 0, sizeof(info));
info.patch = patch; info.patch = patch;
@ -119,7 +120,7 @@ static int git_xdiff(git_diff_output *output, git_diff_patch *patch)
xo->callback.priv = &info; xo->callback.priv = &info;
git_diff_find_context_init( git_diff_find_context_init(
&xo->config.find_func, &findctxt, patch->ofile.driver); &xo->config.find_func, &findctxt, git_diff_patch__driver(patch));
xo->config.find_func_priv = &findctxt; xo->config.find_func_priv = &findctxt;
if (xo->config.find_func != NULL) if (xo->config.find_func != NULL)
@ -131,12 +132,10 @@ static int git_xdiff(git_diff_output *output, git_diff_patch *patch)
* updates are needed to xo->params.flags * updates are needed to xo->params.flags
*/ */
old_xdiff_data.ptr = patch->ofile.map.data; git_diff_patch__old_data(&xd_old_data.ptr, &xd_old_data.size, patch);
old_xdiff_data.size = patch->ofile.map.len; git_diff_patch__new_data(&xd_new_data.ptr, &xd_new_data.size, patch);
new_xdiff_data.ptr = patch->nfile.map.data;
new_xdiff_data.size = patch->nfile.map.len;
xdl_diff(&old_xdiff_data, &new_xdiff_data, xdl_diff(&xd_old_data, &xd_new_data,
&xo->params, &xo->config, &xo->callback); &xo->params, &xo->config, &xo->callback);
git_diff_find_context_clear(&findctxt); git_diff_find_context_clear(&findctxt);