Move diff max_size to public API This commit adds a max_size value in the public `git_diff_options` structure so that the user can automatically flag blobs over a certain size as binary regardless of other properties. Also, and perhaps more importantly, this moves binary detection to be as early as possible in the diff traversal inner loop and makes sure that we stop loading objects as soon as we decide that they are binary.
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 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
diff --git a/include/git2/diff.h b/include/git2/diff.h
index 7a86d24..4b4591a 100644
--- a/include/git2/diff.h
+++ b/include/git2/diff.h
@@ -56,7 +56,13 @@ enum {
* values. Similarly, passing NULL for the options structure will
* give the defaults. The default values are marked below.
*
- * @todo Most of the parameters here are not actually supported at this time.
+ * - flags: a combination of the GIT_DIFF_... values above
+ * - context_lines: number of lines of context to show around diffs
+ * - interhunk_lines: min lines between diff hunks to merge them
+ * - old_prefix: "directory" to prefix to old file names (default "a")
+ * - new_prefix: "directory" to prefix to new file names (default "b")
+ * - pathspec: array of paths / patterns to constrain diff
+ * - max_size: maximum blob size to diff, above this treated as binary
*/
typedef struct {
uint32_t flags; /**< defaults to GIT_DIFF_NORMAL */
@@ -65,6 +71,7 @@ typedef struct {
char *old_prefix; /**< defaults to "a" */
char *new_prefix; /**< defaults to "b" */
git_strarray pathspec; /**< defaults to show all paths */
+ git_off_t max_size; /**< defaults to 512Mb */
} git_diff_options;
/**
diff --git a/src/diff_output.c b/src/diff_output.c
index f65d005..8873a4d 100644
--- a/src/diff_output.c
+++ b/src/diff_output.c
@@ -172,7 +172,7 @@ static void update_delta_is_binary(git_diff_delta *delta)
if ((delta->old_file.flags & GIT_DIFF_FILE_BINARY) != 0 ||
(delta->new_file.flags & GIT_DIFF_FILE_BINARY) != 0)
delta->binary = 1;
- else if ((delta->old_file.flags & GIT_DIFF_FILE_NOT_BINARY) != 0 ||
+ else if ((delta->old_file.flags & GIT_DIFF_FILE_NOT_BINARY) != 0 &&
(delta->new_file.flags & GIT_DIFF_FILE_NOT_BINARY) != 0)
delta->binary = 0;
/* otherwise leave delta->binary value untouched */
@@ -219,34 +219,46 @@ static int diff_delta_is_binary_by_attr(diff_delta_context *ctxt)
return error;
}
-static int diff_delta_is_binary_by_content(diff_delta_context *ctxt)
+static int diff_delta_is_binary_by_content(
+ diff_delta_context *ctxt, git_diff_file *file, git_map *map)
{
- git_diff_delta *delta = ctxt->delta;
git_buf search;
- if ((delta->old_file.flags & BINARY_DIFF_FLAGS) == 0) {
- search.ptr = ctxt->old_data.data;
- search.size = min(ctxt->old_data.len, 4000);
+ if ((file->flags & BINARY_DIFF_FLAGS) == 0) {
+ search.ptr = map->data;
+ search.size = min(map->len, 4000);
if (git_buf_is_binary(&search))
- delta->old_file.flags |= GIT_DIFF_FILE_BINARY;
+ file->flags |= GIT_DIFF_FILE_BINARY;
else
- delta->old_file.flags |= GIT_DIFF_FILE_NOT_BINARY;
+ file->flags |= GIT_DIFF_FILE_NOT_BINARY;
}
- if ((delta->new_file.flags & BINARY_DIFF_FLAGS) == 0) {
- search.ptr = ctxt->new_data.data;
- search.size = min(ctxt->new_data.len, 4000);
+ update_delta_is_binary(ctxt->delta);
- if (git_buf_is_binary(&search))
- delta->new_file.flags |= GIT_DIFF_FILE_BINARY;
- else
- delta->new_file.flags |= GIT_DIFF_FILE_NOT_BINARY;
+ return 0;
+}
+
+static int diff_delta_is_binary_by_size(
+ diff_delta_context *ctxt, git_diff_file *file)
+{
+ git_off_t threshold = MAX_DIFF_FILESIZE;
+
+ if ((file->flags & BINARY_DIFF_FLAGS) != 0)
+ return 0;
+
+ if (ctxt && ctxt->opts) {
+ if (ctxt->opts->max_size < 0)
+ return 0;
+
+ if (ctxt->opts->max_size > 0)
+ threshold = ctxt->opts->max_size;
}
- update_delta_is_binary(delta);
+ if (file->size > threshold)
+ file->flags |= GIT_DIFF_FILE_BINARY;
- /* TODO: if value != NULL, implement diff drivers */
+ update_delta_is_binary(ctxt->delta);
return 0;
}
@@ -274,53 +286,56 @@ static void setup_xdiff_options(
}
static int get_blob_content(
- git_repository *repo,
+ diff_delta_context *ctxt,
git_diff_file *file,
git_map *map,
git_blob **blob)
{
int error;
- git_odb *odb;
- size_t len;
- git_otype type;
if (git_oid_iszero(&file->oid))
return 0;
- /* peek at object header to avoid loading if too large */
- if ((error = git_repository_odb__weakptr(&odb, repo)) < 0 ||
- (error = git_odb_read_header(&len, &type, odb, &file->oid)) < 0)
- return error;
+ if (!file->size) {
+ git_odb *odb;
+ size_t len;
+ git_otype type;
- assert(type == GIT_OBJ_BLOB);
+ /* peek at object header to avoid loading if too large */
+ if ((error = git_repository_odb__weakptr(&odb, ctxt->repo)) < 0 ||
+ (error = git_odb_read_header(&len, &type, odb, &file->oid)) < 0)
+ return error;
- /* if blob is too large to diff, mark as binary */
- if (len > MAX_DIFF_FILESIZE) {
- file->flags |= GIT_DIFF_FILE_BINARY;
- return 0;
- }
+ assert(type == GIT_OBJ_BLOB);
- if (!file->size)
file->size = len;
+ }
- if ((error = git_blob_lookup(blob, repo, &file->oid)) < 0)
+ /* if blob is too large to diff, mark as binary */
+ if ((error = diff_delta_is_binary_by_size(ctxt, file)) < 0)
+ return error;
+ if (ctxt->delta->binary == 1)
+ return 0;
+
+ if ((error = git_blob_lookup(blob, ctxt->repo, &file->oid)) < 0)
return error;
map->data = (void *)git_blob_rawcontent(*blob);
map->len = git_blob_rawsize(*blob);
- return 0;
+ return diff_delta_is_binary_by_content(ctxt, file, map);
}
static int get_workdir_content(
- git_repository *repo,
+ diff_delta_context *ctxt,
git_diff_file *file,
git_map *map)
{
int error = 0;
git_buf path = GIT_BUF_INIT;
+ const char *wd = git_repository_workdir(ctxt->repo);
- if (git_buf_joinpath(&path, git_repository_workdir(repo), file->path) < 0)
+ if (git_buf_joinpath(&path, wd, file->path) < 0)
return -1;
if (S_ISLNK(file->mode)) {
@@ -358,13 +373,12 @@ static int get_workdir_content(
if (!file->size)
file->size = git_futils_filesize(fd);
- /* if file is too large to diff, mark as binary */
- if (file->size > MAX_DIFF_FILESIZE) {
- file->flags |= GIT_DIFF_FILE_BINARY;
+ if ((error = diff_delta_is_binary_by_size(ctxt, file)) < 0 ||
+ ctxt->delta->binary == 1)
goto close_and_cleanup;
- }
- error = git_filters_load(&filters, repo, file->path, GIT_FILTER_TO_ODB);
+ error = git_filters_load(
+ &filters, ctxt->repo, file->path, GIT_FILTER_TO_ODB);
if (error < 0)
goto close_and_cleanup;
@@ -400,6 +414,9 @@ close_and_cleanup:
file->flags |= GIT_DIFF_FILE_VALID_OID;
}
+ if (!error)
+ error = diff_delta_is_binary_by_content(ctxt, file, map);
+
cleanup:
git_buf_free(&path);
return error;
@@ -480,7 +497,6 @@ static int diff_delta_prep(diff_delta_context *ctxt)
static int diff_delta_load(diff_delta_context *ctxt)
{
int error = 0;
- git_repository *repo = ctxt->repo;
git_diff_delta *delta = ctxt->delta;
bool load_old = false, load_new = false, check_if_unmodified = false;
@@ -519,31 +535,35 @@ static int diff_delta_load(diff_delta_context *ctxt)
if (load_old && ctxt->old_src == GIT_ITERATOR_WORKDIR) {
if ((error = get_workdir_content(
- repo, &delta->old_file, &ctxt->old_data)) < 0)
+ ctxt, &delta->old_file, &ctxt->old_data)) < 0)
goto cleanup;
-
- if ((delta->old_file.flags & GIT_DIFF_FILE_BINARY) != 0)
+ if (delta->binary == 1)
goto cleanup;
}
if (load_new && ctxt->new_src == GIT_ITERATOR_WORKDIR) {
if ((error = get_workdir_content(
- repo, &delta->new_file, &ctxt->new_data)) < 0)
+ ctxt, &delta->new_file, &ctxt->new_data)) < 0)
goto cleanup;
-
- if ((delta->new_file.flags & GIT_DIFF_FILE_BINARY) != 0)
+ if (delta->binary == 1)
goto cleanup;
}
- if (load_old && ctxt->old_src != GIT_ITERATOR_WORKDIR &&
- (error = get_blob_content(
- repo, &delta->old_file, &ctxt->old_data, &ctxt->old_blob)) < 0)
- goto cleanup;
+ if (load_old && ctxt->old_src != GIT_ITERATOR_WORKDIR) {
+ if ((error = get_blob_content(
+ ctxt, &delta->old_file, &ctxt->old_data, &ctxt->old_blob)) < 0)
+ goto cleanup;
+ if (delta->binary == 1)
+ goto cleanup;
+ }
- if (load_new && ctxt->new_src != GIT_ITERATOR_WORKDIR &&
- (error = get_blob_content(
- repo, &delta->new_file, &ctxt->new_data, &ctxt->new_blob)) < 0)
- goto cleanup;
+ if (load_new && ctxt->new_src != GIT_ITERATOR_WORKDIR) {
+ if ((error = get_blob_content(
+ ctxt, &delta->new_file, &ctxt->new_data, &ctxt->new_blob)) < 0)
+ goto cleanup;
+ if (delta->binary == 1)
+ goto cleanup;
+ }
/* if we did not previously have the definitive oid, we may have
* incorrect status and need to switch this to UNMODIFIED.
@@ -559,12 +579,6 @@ static int diff_delta_load(diff_delta_context *ctxt)
}
cleanup:
- /* if we have not already decided whether file is binary,
- * check the first 4K for nul bytes to decide...
- */
- if (!error && delta->binary == -1)
- error = diff_delta_is_binary_by_content(ctxt);
-
ctxt->loaded = !error;
/* flag if we would want to diff the contents of these files */
@@ -1069,9 +1083,13 @@ int git_diff_blobs(
if ((error = diff_delta_prep(&ctxt)) < 0)
goto cleanup;
- if (delta.binary == -1 &&
- (error = diff_delta_is_binary_by_content(&ctxt)) < 0)
- goto cleanup;
+ if (delta.binary == -1) {
+ if ((error = diff_delta_is_binary_by_content(
+ &ctxt, &delta.old_file, &ctxt.old_data)) < 0 ||
+ (error = diff_delta_is_binary_by_content(
+ &ctxt, &delta.new_file, &ctxt.new_data)) < 0)
+ goto cleanup;
+ }
ctxt.loaded = 1;
ctxt.diffable = (delta.binary != 1 && delta.status != GIT_DELTA_UNMODIFIED);