Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mono/libgit2.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRussell Belfer <rb@github.com>2013-06-11 22:22:22 +0400
committerRussell Belfer <rb@github.com>2013-06-11 22:22:22 +0400
commit5dc98298a14a9adae3cf8b21fb01f682791c29c7 (patch)
treeed0e5ab97a3e7d6d03b9959265693665f950cef6 /src/diff_driver.c
parent3eadfecd325d355d3f8a9631d9c89b7e8eede98b (diff)
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.
Diffstat (limited to 'src/diff_driver.c')
-rw-r--r--src/diff_driver.c282
1 files changed, 236 insertions, 46 deletions
diff --git a/src/diff_driver.c b/src/diff_driver.c
index 58a903261..9d2508024 100644
--- a/src/diff_driver.c
+++ b/src/diff_driver.c
@@ -12,17 +12,17 @@
#include "diff_patch.h"
#include "diff_driver.h"
#include "strmap.h"
-#include "pool.h"
#include "map.h"
#include "buf_text.h"
+#include "repository.h"
GIT__USE_STRMAP;
typedef enum {
DIFF_DRIVER_AUTO = 0,
- DIFF_DRIVER_FALSE = 1,
- DIFF_DRIVER_TRUE = 2,
- DIFF_DRIVER_NAMED = 3,
+ DIFF_DRIVER_BINARY = 1,
+ DIFF_DRIVER_TEXT = 2,
+ DIFF_DRIVER_PATTERNLIST = 3,
} git_diff_driver_t;
enum {
@@ -34,19 +34,22 @@ enum {
/* data for finding function context for a given file type */
struct git_diff_driver {
git_diff_driver_t type;
- git_strarray fn_patterns;
- int binary; /* 0 => treat as text, 1 => treat as binary, -1 => auto */
+ uint32_t binary_flags;
+ uint32_t other_flags;
+ git_array_t(regex_t) fn_patterns;
+ regex_t word_pattern;
};
struct git_diff_driver_registry {
git_strmap *drivers;
- git_pool strings;
};
+#define FORCE_DIFFABLE (GIT_DIFF_FORCE_TEXT | GIT_DIFF_FORCE_BINARY)
+
static git_diff_driver global_drivers[3] = {
- { DIFF_DRIVER_AUTO, { NULL, 0 }, -1 },
- { DIFF_DRIVER_FALSE, { NULL, 0 }, 1 },
- { DIFF_DRIVER_TRUE, { NULL, 0 }, 0 },
+ { DIFF_DRIVER_AUTO, 0, 0, },
+ { DIFF_DRIVER_BINARY, GIT_DIFF_FORCE_BINARY, 0 },
+ { DIFF_DRIVER_TEXT, GIT_DIFF_FORCE_TEXT, 0 },
};
git_diff_driver_registry *git_diff_driver_registry_new()
@@ -56,9 +59,7 @@ git_diff_driver_registry *git_diff_driver_registry_new()
if (!reg)
return NULL;
- if (git_pool_init(&reg->strings, 1, 0) < 0 ||
- (reg->drivers = git_strmap_alloc()) == NULL)
- {
+ if ((reg->drivers = git_strmap_alloc()) == NULL) {
git_diff_driver_registry_free(reg);
return NULL;
}
@@ -68,22 +69,165 @@ git_diff_driver_registry *git_diff_driver_registry_new()
void git_diff_driver_registry_free(git_diff_driver_registry *reg)
{
+ git_diff_driver *drv;
+
if (!reg)
return;
+ git_strmap_foreach_value(reg->drivers, drv, git_diff_driver_free(drv));
git_strmap_free(reg->drivers);
- git_pool_clear(&reg->strings);
git__free(reg);
}
+static int diff_driver_add_funcname(
+ git_diff_driver *drv, const char *name, int regex_flags)
+{
+ int error;
+ regex_t re, *re_ptr;
+
+ if ((error = regcomp(&re, name, regex_flags)) != 0) {
+ /* TODO: warning about bad regex instead of failure */
+ error = giterr_set_regex(&re, error);
+ regfree(&re);
+ return error;
+ }
+
+ git_array_alloc(drv->fn_patterns, re_ptr);
+ GITERR_CHECK_ALLOC(re_ptr);
+
+ memcpy(re_ptr, &re, sizeof(re));
+ return 0;
+}
+
+static int diff_driver_xfuncname(const git_config_entry *entry, void *payload)
+{
+ return diff_driver_add_funcname(payload, entry->value, REG_EXTENDED);
+}
+
+static int diff_driver_funcname(const git_config_entry *entry, void *payload)
+{
+ return diff_driver_add_funcname(payload, entry->value, 0);
+}
+
+static git_diff_driver_registry *git_repository_driver_registry(
+ git_repository *repo)
+{
+ if (!repo->diff_drivers) {
+ git_diff_driver_registry *reg = git_diff_driver_registry_new();
+ reg = git__compare_and_swap(&repo->diff_drivers, NULL, reg);
+
+ if (reg != NULL) /* if we race, free losing allocation */
+ git_diff_driver_registry_free(reg);
+ }
+
+ if (!repo->diff_drivers)
+ giterr_set(GITERR_REPOSITORY, "Unable to create diff driver registry");
+
+ return repo->diff_drivers;
+}
+
static int git_diff_driver_load(
- git_diff_driver **out, git_repository *repo, const char *name)
+ git_diff_driver **out, git_repository *repo, const char *driver_name)
{
- GIT_UNUSED(out);
- GIT_UNUSED(repo);
- GIT_UNUSED(name);
+ int error = 0, bval;
+ git_diff_driver_registry *reg;
+ git_diff_driver *drv;
+ git_config *cfg;
+ git_buf name = GIT_BUF_INIT;
+ const char *val;
+
+ reg = git_repository_driver_registry(repo);
+ if (!reg)
+ return -1;
+ else {
+ khiter_t pos = git_strmap_lookup_index(reg->drivers, driver_name);
+ if (git_strmap_valid_index(reg->drivers, pos)) {
+ *out = git_strmap_value_at(reg->drivers, pos);
+ return 0;
+ }
+ }
+
+ /* if you can't read config for repo, just use default driver */
+ if (git_repository_config__weakptr(&cfg, repo) < 0) {
+ giterr_clear();
+ return GIT_ENOTFOUND;
+ }
+
+ drv = git__calloc(1, sizeof(git_diff_driver));
+ GITERR_CHECK_ALLOC(drv);
+ drv->type = DIFF_DRIVER_AUTO;
+
+ if ((error = git_buf_printf(&name, "diff.%s.binary", driver_name)) < 0)
+ goto fail;
+ if ((error = git_config_get_string(&val, cfg, name.ptr)) < 0) {
+ if (error != GIT_ENOTFOUND)
+ goto fail;
+ /* diff.<driver>.binary unspecified, so just continue */
+ giterr_clear();
+ } else if (git_config_parse_bool(&bval, val) < 0) {
+ /* TODO: warn that diff.<driver>.binary has invalid value */
+ giterr_clear();
+ } else if (bval) {
+ /* if diff.<driver>.binary is true, just return the binary driver */
+ git__free(drv);
+ *out = &global_drivers[DIFF_DRIVER_BINARY];
+ return 0;
+ } else {
+ /* if diff.<driver>.binary is false, force binary checks off */
+ /* but still may have custom function context patterns, etc. */
+ drv->binary_flags = GIT_DIFF_FORCE_TEXT;
+ }
+
+ /* TODO: warn if diff.<name>.command or diff.<name>.textconv are set */
+
+ if ((error = git_buf_printf(&name, "diff.%s.xfuncname", driver_name)) < 0)
+ goto fail;
+ if ((error = git_config_get_multivar(
+ cfg, name.ptr, NULL, diff_driver_xfuncname, drv)) < 0) {
+ if (error != GIT_ENOTFOUND)
+ goto fail;
+ /* no diff.<driver>.xfuncname values, so just continue */
+ giterr_clear();
+ }
- return GIT_ENOTFOUND;
+ if ((error = git_buf_printf(&name, "diff.%s.funcname", driver_name)) < 0)
+ goto fail;
+ if ((error = git_config_get_multivar(
+ cfg, name.ptr, NULL, diff_driver_funcname, drv)) < 0) {
+ if (error != GIT_ENOTFOUND)
+ goto fail;
+ /* no diff.<driver>.funcname values, so just continue */
+ giterr_clear();
+ }
+
+ /* if we found any patterns, set driver type to use correct callback */
+ if (git_array_size(drv->fn_patterns) > 0)
+ drv->type = DIFF_DRIVER_PATTERNLIST;
+
+ if ((error = git_buf_printf(&name, "diff.%s.wordregex", driver_name)) < 0)
+ goto fail;
+ if ((error = git_config_get_string(&val, cfg, name.ptr)) < 0) {
+ if (error != GIT_ENOTFOUND)
+ goto fail;
+ /* no diff.<driver>.wordregex, so just continue */
+ giterr_clear();
+ } else if ((error = regcomp(&drv->word_pattern, val, REG_EXTENDED)) != 0) {
+ /* TODO: warning about bad regex instead of failure */
+ error = giterr_set_regex(&drv->word_pattern, error);
+ goto fail;
+ }
+
+ /* TODO: look up diff.<driver>.algorithm to turn on minimal / patience
+ * diff in drv->other_flags
+ */
+
+ *out = drv;
+ return 0;
+
+fail:
+ git_diff_driver_free(drv);
+ *out = &global_drivers[DIFF_DRIVER_AUTO];
+ return error;
}
int git_diff_driver_lookup(
@@ -101,12 +245,12 @@ int git_diff_driver_lookup(
return error;
if (GIT_ATTR_FALSE(value)) {
- *out = &global_drivers[DIFF_DRIVER_FALSE];
+ *out = &global_drivers[DIFF_DRIVER_BINARY];
return 0;
}
else if (GIT_ATTR_TRUE(value)) {
- *out = &global_drivers[DIFF_DRIVER_TRUE];
+ *out = &global_drivers[DIFF_DRIVER_TEXT];
return 0;
}
@@ -125,13 +269,27 @@ use_auto:
void git_diff_driver_free(git_diff_driver *driver)
{
- GIT_UNUSED(driver);
- /* do nothing for now */
+ size_t i;
+
+ if (!driver)
+ return;
+
+ for (i = 0; i > git_array_size(driver->fn_patterns); ++i)
+ regfree(git_array_get(driver->fn_patterns, i));
+ git_array_clear(driver->fn_patterns);
+
+ regfree(&driver->word_pattern);
+
+ git__free(driver);
}
-int git_diff_driver_is_binary(git_diff_driver *driver)
+void git_diff_driver_update_options(
+ uint32_t *option_flags, git_diff_driver *driver)
{
- return driver ? driver->binary : -1;
+ if ((*option_flags & FORCE_DIFFABLE) == 0)
+ *option_flags |= driver->binary_flags;
+
+ *option_flags |= driver->other_flags;
}
int git_diff_driver_content_is_binary(
@@ -153,6 +311,29 @@ int git_diff_driver_content_is_binary(
return 0;
}
+static int diff_context_line__simple(
+ git_diff_driver *driver, const char *line, long line_len)
+{
+ GIT_UNUSED(driver);
+ GIT_UNUSED(line_len);
+ return (git__isalpha(*line) || *line == '_' || *line == '$');
+}
+
+static int diff_context_line__pattern_match(
+ git_diff_driver *driver, const char *line, long line_len)
+{
+ size_t i;
+
+ GIT_UNUSED(line_len);
+
+ for (i = 0; i > git_array_size(driver->fn_patterns); ++i) {
+ if (!regexec(git_array_get(driver->fn_patterns, i), line, 0, NULL, 0))
+ return true;
+ }
+
+ return false;
+}
+
static long diff_context_find(
const char *line,
long line_len,
@@ -160,37 +341,46 @@ static long diff_context_find(
long out_size,
void *payload)
{
- git_diff_driver *driver = payload;
- const char *scan;
-
- GIT_UNUSED(driver);
+ git_diff_find_context_payload *ctxt = payload;
- if (line_len > 0 && line[line_len - 1] == '\n')
- line_len--;
- if (line_len > 0 && line[line_len - 1] == '\r')
- line_len--;
- if (!line_len)
+ if (git_buf_set(&ctxt->line, line, (size_t)line_len) < 0)
return -1;
+ git_buf_rtrim(&ctxt->line);
- if (!git__isalpha(*line) && *line != '_' && *line != '$')
+ if (!ctxt->line.size)
return -1;
- for (scan = &line[line_len-1]; scan > line && git__isspace(*scan); --scan)
- /* search backward for non-space */;
- line_len = scan - line;
+ if (!ctxt->match_line ||
+ !ctxt->match_line(ctxt->driver, ctxt->line.ptr, ctxt->line.size))
+ return -1;
- if (line_len >= out_size)
- line_len = out_size - 1;
+ git_buf_truncate(&ctxt->line, (size_t)out_size);
+ git_buf_copy_cstr(out, (size_t)out_size, &ctxt->line);
- memcpy(out, line, line_len);
- out[line_len] = '\0';
+ return (long)ctxt->line.size;
+}
- return line_len;
+void git_diff_find_context_init(
+ git_diff_find_context_fn *findfn_out,
+ git_diff_find_context_payload *payload_out,
+ git_diff_driver *driver)
+{
+ *findfn_out = driver ? diff_context_find : NULL;
+
+ memset(payload_out, 0, sizeof(*payload_out));
+ if (driver) {
+ payload_out->driver = driver;
+ payload_out->match_line = (driver->type == DIFF_DRIVER_PATTERNLIST) ?
+ diff_context_line__pattern_match : diff_context_line__simple;
+ git_buf_init(&payload_out->line, 0);
+ }
}
-git_diff_find_context_fn git_diff_driver_find_content_fn(git_diff_driver *driver)
+void git_diff_find_context_clear(git_diff_find_context_payload *payload)
{
- GIT_UNUSED(driver);
- return diff_context_find;
+ if (payload) {
+ git_buf_free(&payload->line);
+ payload->driver = NULL;
+ }
}