allow for configuring a custom file atomizer with got_diff_get_config()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
diff --git a/lib/blame.c b/lib/blame.c
index 0d4400c..6d0ab27 100644
--- a/lib/blame.c
+++ b/lib/blame.c
@@ -53,7 +53,7 @@ struct got_blame_line {
struct got_blame {
FILE *f;
off_t size;
- const struct diff_config *cfg;
+ struct diff_config *cfg;
size_t filesize;
int nlines;
int nannotated;
@@ -273,6 +273,7 @@ blame_close(struct got_blame *blame)
err = got_error_from_errno("fclose");
free(blame->lines);
free(blame->linemap2);
+ free(blame->cfg);
free(blame);
return err;
}
@@ -327,7 +328,10 @@ blame_open(struct got_blame **blamep, const char *path,
if (err || blame->nlines == 0)
goto done;
- blame->cfg = got_diff_get_config(GOT_DIFF_ALGORITHM_PATIENCE);
+ err = got_diff_get_config(&blame->cfg, GOT_DIFF_ALGORITHM_PATIENCE,
+ NULL, NULL);
+ if (err)
+ goto done;
/* Don't include \n at EOF in the blame line count. */
if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
diff --git a/lib/diffreg.c b/lib/diffreg.c
index 8dbab6f..e22e81b 100644
--- a/lib/diffreg.c
+++ b/lib/diffreg.c
@@ -105,16 +105,35 @@ got_diffreg_close(FILE *f1, char *p1, size_t size1,
return err;
}
-const struct diff_config *
-got_diff_get_config(enum got_diff_algorithm algorithm)
+const struct got_error *
+got_diff_get_config(struct diff_config **cfg,
+ enum got_diff_algorithm algorithm,
+ diff_atomize_func_t atomize_func, void *atomize_func_data)
{
+ *cfg = calloc(1, sizeof(**cfg));
+ if (*cfg == NULL)
+ return got_error_from_errno("calloc");
+
switch (algorithm) {
case GOT_DIFF_ALGORITHM_PATIENCE:
- return &diff_config_patience;
+ (*cfg)->algo = &patience;
+ break;
case GOT_DIFF_ALGORITHM_MYERS:
- return &diff_config_myers_then_myers_divide;
+ (*cfg)->algo = &myers_then_myers_divide;
+ break;
+ default:
+ return got_error_msg(GOT_ERR_NOT_IMPL, "bad diff algorithm");
}
- return NULL; /* should not happen */
+
+ if (atomize_func) {
+ (*cfg)->atomize_func = atomize_func;
+ (*cfg)->atomize_func_data = atomize_func_data;
+ } else
+ (*cfg)->atomize_func = diff_atomize_text_by_line;
+
+ (*cfg)->max_recursion_depth = 0; /* use default recursion depth */
+
+ return NULL;
}
const struct got_error *
@@ -161,7 +180,7 @@ got_diffreg(struct got_diffreg_result **diffreg_result, FILE *f1, FILE *f2,
enum got_diff_algorithm algorithm, int ignore_whitespace)
{
const struct got_error *err = NULL;
- const struct diff_config *cfg;
+ struct diff_config *cfg = NULL;
char *p1 = NULL, *p2 = NULL;
int f1_created = 0, f2_created = 0;
size_t size1, size2;
@@ -182,11 +201,9 @@ got_diffreg(struct got_diffreg_result **diffreg_result, FILE *f1, FILE *f2,
right = &d_right;
}
- cfg = got_diff_get_config(algorithm);
- if (cfg == NULL) {
- err = got_error(GOT_ERR_NOT_IMPL);
+ err = got_diff_get_config(&cfg, algorithm, NULL, NULL);
+ if (err)
goto done;
- }
if (f1 == NULL) {
f1_created = 1;
@@ -237,6 +254,7 @@ got_diffreg(struct got_diffreg_result **diffreg_result, FILE *f1, FILE *f2,
(*diffreg_result)->size2 = size2;
}
done:
+ free(cfg);
if (diffreg_result == NULL) {
diff_data_free(left);
diff_data_free(right);
diff --git a/lib/got_lib_diff.h b/lib/got_lib_diff.h
index 569cb20..3a995c4 100644
--- a/lib/got_lib_diff.h
+++ b/lib/got_lib_diff.h
@@ -45,7 +45,8 @@ struct got_diffreg_result {
#define GOT_DIFF_CONFLICT_MARKER_SEP "======="
#define GOT_DIFF_CONFLICT_MARKER_END ">>>>>>>"
-const struct diff_config *got_diff_get_config(enum got_diff_algorithm);
+const struct got_error *got_diff_get_config(struct diff_config **,
+ enum got_diff_algorithm, diff_atomize_func_t, void *);
const struct got_error *got_diff_prepare_file(FILE *, char **, size_t *,
struct diff_data *, const struct diff_config *, int);
const struct got_error *got_diffreg(struct got_diffreg_result **, FILE *, FILE *,