Commit 5dc98298 by Russell Belfer

Implement regex pattern diff driver

This implements the loading of regular expression pattern lists
for diff drivers that search for function context in that way.
This also changes the way that diff drivers update options and
interface with xdiff APIs to make them a little more flexible.
parent 3eadfecd
......@@ -86,4 +86,3 @@ Internal Objects
for hunk headers
** At some point, the logic for getting a filtered version of file content
or calculating the OID of a file may be moved into the driver.
......@@ -148,6 +148,9 @@ typedef enum {
* Of course, ignore rules are still checked for the directory itself.
*/
GIT_DIFF_FAST_UNTRACKED_DIRS = (1 << 19),
/** Treat all files as binary, disabling text diffs */
GIT_DIFF_FORCE_BINARY = (1 << 20),
} git_diff_option_t;
/**
......
......@@ -8,6 +8,7 @@
#define INCLUDE_diff_driver_h__
#include "common.h"
#include "buffer.h"
typedef struct git_diff_driver_registry git_diff_driver_registry;
......@@ -19,8 +20,8 @@ typedef struct git_diff_driver git_diff_driver;
int git_diff_driver_lookup(git_diff_driver **, git_repository *, const char *);
void git_diff_driver_free(git_diff_driver *);
/* returns -1 meaning "unknown", 0 meaning not binary, 1 meaning binary */
int git_diff_driver_is_binary(git_diff_driver *);
/* diff option flags to force off and on for this driver */
void git_diff_driver_update_options(uint32_t *option_flags, git_diff_driver *);
/* returns -1 meaning "unknown", 0 meaning not binary, 1 meaning binary */
int git_diff_driver_content_is_binary(
......@@ -29,6 +30,20 @@ int git_diff_driver_content_is_binary(
typedef long (*git_diff_find_context_fn)(
const char *, long, char *, long, void *);
git_diff_find_context_fn git_diff_driver_find_content_fn(git_diff_driver *);
typedef int (*git_diff_find_context_line)(
git_diff_driver *, const char *, long);
typedef struct {
git_diff_driver *driver;
git_diff_find_context_line match_line;
git_buf line;
} git_diff_find_context_payload;
void git_diff_find_context_init(
git_diff_find_context_fn *findfn_out,
git_diff_find_context_payload *payload_out,
git_diff_driver *driver);
void git_diff_find_context_clear(git_diff_find_context_payload *);
#endif
......@@ -19,14 +19,9 @@ static bool diff_file_content_binary_by_size(git_diff_file_content *fc)
{
/* if we have diff opts, check max_size vs file size */
if ((fc->file.flags & DIFF_FLAGS_KNOWN_BINARY) == 0 &&
fc->opts && fc->opts->max_size >= 0)
{
git_off_t threshold = DIFF_MAX_FILESIZE;
if (fc->opts->max_size > 0)
threshold = fc->opts->max_size;
if (fc->file.size > threshold)
fc->opts_max_size > 0 &&
fc->file.size > fc->opts_max_size)
fc->file.flags |= GIT_DIFF_FLAG_BINARY;
}
return ((fc->file.flags & GIT_DIFF_FLAG_BINARY) != 0);
}
......@@ -44,9 +39,14 @@ static void diff_file_content_binary_by_content(git_diff_file_content *fc)
}
}
static int diff_file_content_init_common(git_diff_file_content *fc)
static int diff_file_content_init_common(
git_diff_file_content *fc, const git_diff_options *opts)
{
uint32_t flags = fc->opts ? fc->opts->flags : GIT_DIFF_NORMAL;
fc->opts_flags = opts ? opts->flags : GIT_DIFF_NORMAL;
if (opts && opts->max_size >= 0)
fc->opts_max_size = opts->max_size ?
opts->max_size : DIFF_MAX_FILESIZE;
if (!fc->driver) {
if (git_diff_driver_lookup(&fc->driver, fc->repo, "") < 0)
......@@ -54,19 +54,21 @@ static int diff_file_content_init_common(git_diff_file_content *fc)
fc->src = GIT_ITERATOR_TYPE_TREE;
}
/* give driver a chance to modify options */
git_diff_driver_update_options(&fc->opts_flags, fc->driver);
/* make sure file is conceivable mmap-able */
if ((git_off_t)((size_t)fc->file.size) != fc->file.size)
fc->file.flags |= GIT_DIFF_FLAG_BINARY;
/* check if user is forcing is to text diff the file */
else if (flags & GIT_DIFF_FORCE_TEXT)
/* check if user is forcing text diff the file */
else if (fc->opts_flags & GIT_DIFF_FORCE_TEXT) {
fc->file.flags &= ~GIT_DIFF_FLAG_BINARY;
fc->file.flags |= GIT_DIFF_FLAG_NOT_BINARY;
/* otherwise see if diff driver forces a behavior */
else switch (git_diff_driver_is_binary(fc->driver)) {
case 0: fc->file.flags |= GIT_DIFF_FLAG_NOT_BINARY; break;
case 1: fc->file.flags |= GIT_DIFF_FLAG_BINARY; break;
default: break;
}
/* check if user is forcing binary diff the file */
else if (fc->opts_flags & GIT_DIFF_FORCE_BINARY) {
fc->file.flags &= ~GIT_DIFF_FLAG_NOT_BINARY;
fc->file.flags |= GIT_DIFF_FLAG_BINARY;
}
diff_file_content_binary_by_size(fc);
......@@ -95,7 +97,6 @@ int diff_file_content_init_from_diff(
memset(fc, 0, sizeof(*fc));
fc->repo = diff->repo;
fc->opts = &diff->opts;
fc->src = use_old ? diff->old_src : diff->new_src;
memcpy(&fc->file, file, sizeof(fc->file));
......@@ -123,7 +124,7 @@ int diff_file_content_init_from_diff(
if (!has_data)
fc->file.flags |= GIT_DIFF_FLAG__NO_DATA;
return diff_file_content_init_common(fc);
return diff_file_content_init_common(fc, &diff->opts);
}
int diff_file_content_init_from_blob(
......@@ -134,7 +135,6 @@ int diff_file_content_init_from_blob(
{
memset(fc, 0, sizeof(*fc));
fc->repo = repo;
fc->opts = opts;
fc->blob = blob;
if (!blob) {
......@@ -149,7 +149,7 @@ int diff_file_content_init_from_blob(
fc->map.data = (char *)git_blob_rawcontent(blob);
}
return diff_file_content_init_common(fc);
return diff_file_content_init_common(fc, opts);
}
int diff_file_content_init_from_raw(
......@@ -161,7 +161,6 @@ int diff_file_content_init_from_raw(
{
memset(fc, 0, sizeof(*fc));
fc->repo = repo;
fc->opts = opts;
if (!buf) {
fc->file.flags |= GIT_DIFF_FLAG__NO_DATA;
......@@ -175,7 +174,7 @@ int diff_file_content_init_from_raw(
fc->map.data = (char *)buf;
}
return diff_file_content_init_common(fc);
return diff_file_content_init_common(fc, opts);
}
static int diff_file_content_commit_to_str(
......
......@@ -15,9 +15,10 @@
/* expanded information for one side of a delta */
typedef struct {
git_repository *repo;
const git_diff_options *opts;
git_diff_file file;
git_diff_driver *driver;
uint32_t opts_flags;
git_off_t opts_max_size;
git_iterator_type_t src;
const git_blob *blob;
git_map map;
......
......@@ -96,8 +96,7 @@ static int diff_patch_load(git_diff_patch *patch, git_diff_output *output)
/* if no hunk and data callbacks and user doesn't care if data looks
* binary, then there is no need to actually load the data
*/
if (patch->ofile.opts &&
(patch->ofile.opts->flags & GIT_DIFF_SKIP_BINARY_CHECK) != 0 &&
if ((patch->ofile.opts_flags & GIT_DIFF_SKIP_BINARY_CHECK) != 0 &&
output && !output->hunk_cb && !output->data_cb)
return 0;
......@@ -718,6 +717,6 @@ static void diff_output_init(
static void diff_output_to_patch(git_diff_output *out, git_diff_patch *patch)
{
diff_output_init(
out, patch->ofile.opts,
out, NULL,
diff_patch_file_cb, diff_patch_hunk_cb, diff_patch_line_cb, patch);
}
......@@ -109,6 +109,7 @@ static int git_xdiff(git_diff_output *output, git_diff_patch *patch)
{
git_xdiff_output *xo = (git_xdiff_output *)output;
git_xdiff_info info;
git_diff_find_context_payload findctxt;
mmfile_t old_xdiff_data, new_xdiff_data;
memset(&info, 0, sizeof(info));
......@@ -117,15 +118,18 @@ static int git_xdiff(git_diff_output *output, git_diff_patch *patch)
xo->callback.priv = &info;
xo->config.find_func_priv = patch->ofile.driver;
xo->config.find_func = patch->ofile.driver ?
git_diff_driver_find_content_fn(patch->ofile.driver) : NULL;
git_diff_find_context_init(
&xo->config.find_func, &findctxt, patch->ofile.driver);
xo->config.find_func_priv = &findctxt;
if (xo->config.find_func != NULL)
xo->config.flags |= XDL_EMIT_FUNCNAMES;
else
xo->config.flags &= ~XDL_EMIT_FUNCNAMES;
/* TODO: check ofile.opts_flags to see if driver-specific per-file
* updates are needed to xo->params.flags
*/
old_xdiff_data.ptr = patch->ofile.map.data;
old_xdiff_data.size = patch->ofile.map.len;
......@@ -135,6 +139,8 @@ static int git_xdiff(git_diff_output *output, git_diff_patch *patch)
xdl_diff(&old_xdiff_data, &new_xdiff_data,
&xo->params, &xo->config, &xo->callback);
git_diff_find_context_clear(&findctxt);
return xo->output.error;
}
......
......@@ -543,7 +543,7 @@ void test_diff_patch__line_counts_with_eofnl(void)
"index 378a7d9..3d0154e 100644\n"
"--- a/songof7cities.txt\n"
"+++ b/songof7cities.txt\n"
"@@ -42,7 +42,7 @@ With peoples undefeated of the dark, enduring blood\n"
"@@ -42,7 +42,7 @@ With peoples undefeated of the dark, enduring blood.\n"
" \n"
" To the sound of trumpets shall their seed restore my Cities\n"
" Wealthy and well-weaponed, that once more may I behold\n"
......
......@@ -558,7 +558,7 @@ void test_diff_rename__patch(void)
git_diff_patch *patch;
const git_diff_delta *delta;
char *text;
const char *expected = "diff --git a/sixserving.txt b/ikeepsix.txt\nindex ad0a8e5..36020db 100644\n--- a/sixserving.txt\n+++ b/ikeepsix.txt\n@@ -1,3 +1,6 @@\n+I Keep Six Honest Serving-Men\n+=============================\n+\n I KEEP six honest serving-men\n (They taught me all I knew);\n Their names are What and Why and When\n@@ -21,4 +24,4 @@ She sends'em abroad on her own affairs\n One million Hows, two million Wheres,\n And seven million Whys!\n \n- -- Rudyard Kipling\n+ -- Rudyard Kipling\n";
const char *expected = "diff --git a/sixserving.txt b/ikeepsix.txt\nindex ad0a8e5..36020db 100644\n--- a/sixserving.txt\n+++ b/ikeepsix.txt\n@@ -1,3 +1,6 @@\n+I Keep Six Honest Serving-Men\n+=============================\n+\n I KEEP six honest serving-men\n (They taught me all I knew);\n Their names are What and Why and When\n@@ -21,4 +24,4 @@ She sends'em abroad on her own affairs,\n One million Hows, two million Wheres,\n And seven million Whys!\n \n- -- Rudyard Kipling\n+ -- Rudyard Kipling\n";
old_tree = resolve_commit_oid_to_tree(g_repo, sha0);
new_tree = resolve_commit_oid_to_tree(g_repo, sha1);
......
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