Commit 71a3d27ea686845811f04314d02798b4f1745046

Russell Belfer 2013-02-08T10:06:47

Replace diff delta binary with flags Previously the git_diff_delta recorded if the delta was binary. This replaces that (with no net change in structure size) with a full set of flags. The flag values that were already in use for individual git_diff_file objects are reused for the delta flags, too (along with renaming those flags to make it clear that they are used more generally). This (a) makes things somewhat more consistent (because I was using a -1 value in the "boolean" binary field to indicate unset, whereas now I can just use the flags that are easier to understand), and (b) will make it easier for me to add some additional flags to the delta object in the future, such as marking the results of a copy/rename detection or other deltas that might want a special indicator. While making this change, I officially moved some of the flags that were internal only into the private diff header. This also allowed me to remove a gross hack in rename/copy detect code where I was overwriting the status field with an internal value.

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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
diff --git a/include/git2/diff.h b/include/git2/diff.h
index c0f4836..7f16504 100644
--- a/include/git2/diff.h
+++ b/include/git2/diff.h
@@ -88,10 +88,9 @@ typedef enum {
 	GIT_DIFF_INCLUDE_UNTRACKED = (1 << 8),
 	/** Include unmodified files in the diff list */
 	GIT_DIFF_INCLUDE_UNMODIFIED = (1 << 9),
-	/** Even with the GIT_DIFF_INCLUDE_UNTRACKED flag, when an untracked
-	 *  directory is found, only a single entry for the directory is added
-	 *  to the diff list; with this flag, all files under the directory will
-	 *  be included, too.
+	/** Even with GIT_DIFF_INCLUDE_UNTRACKED, an entire untracked directory
+	 *  will be marked with only a single entry in the diff list; this flag
+	 *  adds all files under the directory as UNTRACKED entries, too.
 	 */
 	GIT_DIFF_RECURSE_UNTRACKED_DIRS = (1 << 10),
 	/** If the pathspec is set in the diff options, this flags means to
@@ -120,6 +119,11 @@ typedef enum {
 	GIT_DIFF_INCLUDE_TYPECHANGE_TREES  = (1 << 16),
 	/** Ignore file mode changes */
 	GIT_DIFF_IGNORE_FILEMODE = (1 << 17),
+	/** Even with GIT_DIFF_INCLUDE_IGNORED, an entire ignored directory
+	 *  will be marked with only a single entry in the diff list; this flag
+	 *  adds all files under the directory as IGNORED entries, too.
+	 */
+	GIT_DIFF_RECURSE_IGNORED_DIRS = (1 << 10),
 } git_diff_option_t;
 
 /**
@@ -133,20 +137,18 @@ typedef enum {
 typedef struct git_diff_list git_diff_list;
 
 /**
- * Flags for the file object on each side of a diff.
+ * Flags for the delta object and the file objects on each side.
  *
- * Note: most of these flags are just for **internal** consumption by
- * libgit2, but some of them may be interesting to external users.
+ * These flags are used for both the `flags` value of the `git_diff_delta`
+ * and the flags for the `git_diff_file` objects representing the old and
+ * new sides of the delta.  Values outside of this public range should be
+ * considered reserved for internal or future use.
  */
 typedef enum {
-	GIT_DIFF_FILE_VALID_OID  = (1 << 0), /** `oid` value is known correct */
-	GIT_DIFF_FILE_FREE_PATH  = (1 << 1), /** `path` is allocated memory */
-	GIT_DIFF_FILE_BINARY     = (1 << 2), /** should be considered binary data */
-	GIT_DIFF_FILE_NOT_BINARY = (1 << 3), /** should be considered text data */
-	GIT_DIFF_FILE_FREE_DATA  = (1 << 4), /** internal file data is allocated */
-	GIT_DIFF_FILE_UNMAP_DATA = (1 << 5), /** internal file data is mmap'ed */
-	GIT_DIFF_FILE_NO_DATA    = (1 << 6), /** file data should not be loaded */
-} git_diff_file_flag_t;
+	GIT_DIFF_FLAG_BINARY     = (1 << 0), /** file(s) treated as binary data */
+	GIT_DIFF_FLAG_NOT_BINARY = (1 << 1), /** file(s) treated as text data */
+	GIT_DIFF_FLAG_VALID_OID  = (1 << 2), /** `oid` value is known correct */
+} git_diff_flag_t;
 
 /**
  * What type of change is described by a git_diff_delta?
@@ -186,18 +188,17 @@ typedef enum {
  *
  * `size` is the size of the entry in bytes.
  *
- * `flags` is a combination of the `git_diff_file_flag_t` types, but those
- * are largely internal values.
+ * `flags` is a combination of the `git_diff_flag_t` types
  *
  * `mode` is, roughly, the stat() `st_mode` value for the item.  This will
  * be restricted to one of the `git_filemode_t` values.
  */
 typedef struct {
-	git_oid oid;
+	git_oid     oid;
 	const char *path;
-	git_off_t size;
-	unsigned int flags;
-	uint16_t mode;
+	git_off_t   size;
+	uint32_t    flags;
+	uint16_t    mode;
 } git_diff_file;
 
 /**
@@ -219,16 +220,17 @@ typedef struct {
  *
  * Under some circumstances, in the name of efficiency, not all fields will
  * be filled in, but we generally try to fill in as much as possible.  One
- * example is that the "binary" field will not examine file contents if you
- * do not pass in hunk and/or line callbacks to the diff foreach iteration
- * function.  It will just use the git attributes for those files.
+ * example is that the "flags" field may not have either the `BINARY` or the
+ * `NOT_BINARY` flag set to avoid examining file contents if you do not pass
+ * in hunk and/or line callbacks to the diff foreach iteration function.  It
+ * will just use the git attributes for those files.
  */
 typedef struct {
 	git_diff_file old_file;
 	git_diff_file new_file;
 	git_delta_t   status;
-	unsigned int  similarity; /**< for RENAMED and COPIED, value 0-100 */
-	int           binary;
+	uint32_t      similarity; /**< for RENAMED and COPIED, value 0-100 */
+	uint32_t      flags;
 } git_diff_delta;
 
 /**
diff --git a/src/checkout.c b/src/checkout.c
index 59cd218..52a4a4e 100644
--- a/src/checkout.c
+++ b/src/checkout.c
@@ -78,7 +78,7 @@ static int checkout_notify(
 		git_oid_cpy(&wdfile.oid, &wditem->oid);
 		wdfile.path = wditem->path;
 		wdfile.size = wditem->file_size;
-		wdfile.flags = GIT_DIFF_FILE_VALID_OID;
+		wdfile.flags = GIT_DIFF_FLAG_VALID_OID;
 		wdfile.mode = wditem->mode;
 
 		workdir = &wdfile;
diff --git a/src/diff.c b/src/diff.c
index d9bc32a..0861b13 100644
--- a/src/diff.c
+++ b/src/diff.c
@@ -92,11 +92,11 @@ static int diff_delta__from_one(
 		git_oid_cpy(&delta->new_file.oid, &entry->oid);
 	}
 
-	delta->old_file.flags |= GIT_DIFF_FILE_VALID_OID;
+	delta->old_file.flags |= GIT_DIFF_FLAG_VALID_OID;
 
 	if (delta->status == GIT_DELTA_DELETED ||
 		!git_oid_iszero(&delta->new_file.oid))
-		delta->new_file.flags |= GIT_DIFF_FILE_VALID_OID;
+		delta->new_file.flags |= GIT_DIFF_FLAG_VALID_OID;
 
 	notify_res = diff_notify(diff, delta, matched_pathspec);
 
@@ -142,7 +142,7 @@ static int diff_delta__from_two(
 	git_oid_cpy(&delta->old_file.oid, &old_entry->oid);
 	delta->old_file.size = old_entry->file_size;
 	delta->old_file.mode = old_mode;
-	delta->old_file.flags |= GIT_DIFF_FILE_VALID_OID;
+	delta->old_file.flags |= GIT_DIFF_FLAG_VALID_OID;
 
 	git_oid_cpy(&delta->new_file.oid, &new_entry->oid);
 	delta->new_file.size = new_entry->file_size;
@@ -156,7 +156,7 @@ static int diff_delta__from_two(
 	}
 
 	if (new_oid || !git_oid_iszero(&new_entry->oid))
-		delta->new_file.flags |= GIT_DIFF_FILE_VALID_OID;
+		delta->new_file.flags |= GIT_DIFF_FLAG_VALID_OID;
 
 	notify_res = diff_notify(diff, delta, matched_pathspec);
 
diff --git a/src/diff.h b/src/diff.h
index 16fbf71..8e3cbcd 100644
--- a/src/diff.h
+++ b/src/diff.h
@@ -28,8 +28,14 @@ enum {
 	GIT_DIFFCAPS_USE_DEV          = (1 << 4), /* use st_dev? */
 };
 
-#define GIT_DELTA__TO_DELETE 10
-#define GIT_DELTA__TO_SPLIT  11
+enum {
+	GIT_DIFF_FLAG__FREE_PATH  = (1 << 7),  /* `path` is allocated memory */
+	GIT_DIFF_FLAG__FREE_DATA  = (1 << 8),  /* internal file data is allocated */
+	GIT_DIFF_FLAG__UNMAP_DATA = (1 << 9),  /* internal file data is mmap'ed */
+	GIT_DIFF_FLAG__NO_DATA    = (1 << 10), /* file data should not be loaded */
+	GIT_DIFF_FLAG__TO_DELETE  = (1 << 11), /* delete entry during rename det. */
+	GIT_DIFF_FLAG__TO_SPLIT   = (1 << 12), /* split entry during rename det. */
+};
 
 struct git_diff_list {
 	git_refcount     rc;
diff --git a/src/diff_output.c b/src/diff_output.c
index 88ccc9d..5e9d5fe 100644
--- a/src/diff_output.c
+++ b/src/diff_output.c
@@ -52,8 +52,8 @@ static int parse_hunk_header(git_diff_range *range, const char *header)
 	return 0;
 }
 
-#define KNOWN_BINARY_FLAGS (GIT_DIFF_FILE_BINARY|GIT_DIFF_FILE_NOT_BINARY)
-#define NOT_BINARY_FLAGS   (GIT_DIFF_FILE_NOT_BINARY|GIT_DIFF_FILE_NO_DATA)
+#define KNOWN_BINARY_FLAGS (GIT_DIFF_FLAG_BINARY|GIT_DIFF_FLAG_NOT_BINARY)
+#define NOT_BINARY_FLAGS   (GIT_DIFF_FLAG_NOT_BINARY|GIT_DIFF_FLAG__NO_DATA)
 
 static int update_file_is_binary_by_attr(
 	git_repository *repo, git_diff_file *file)
@@ -68,9 +68,9 @@ static int update_file_is_binary_by_attr(
 		return -1;
 
 	if (GIT_ATTR_FALSE(value))
-		file->flags |= GIT_DIFF_FILE_BINARY;
+		file->flags |= GIT_DIFF_FLAG_BINARY;
 	else if (GIT_ATTR_TRUE(value))
-		file->flags |= GIT_DIFF_FILE_NOT_BINARY;
+		file->flags |= GIT_DIFF_FLAG_NOT_BINARY;
 	/* otherwise leave file->flags alone */
 
 	return 0;
@@ -78,15 +78,15 @@ static int update_file_is_binary_by_attr(
 
 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;
+	if ((delta->old_file.flags & GIT_DIFF_FLAG_BINARY) != 0 ||
+		(delta->new_file.flags & GIT_DIFF_FLAG_BINARY) != 0)
+		delta->flags |= GIT_DIFF_FLAG_BINARY;
 
 	else if ((delta->old_file.flags & NOT_BINARY_FLAGS) != 0 &&
 			 (delta->new_file.flags & NOT_BINARY_FLAGS) != 0)
-		delta->binary = 0;
+		delta->flags |= GIT_DIFF_FLAG_NOT_BINARY;
 
-	/* otherwise leave delta->binary value untouched */
+	/* otherwise leave delta->flags binary value untouched */
 }
 
 /* returns if we forced binary setting (and no further checks needed) */
@@ -95,24 +95,24 @@ static bool diff_delta_is_binary_forced(
 	git_diff_delta *delta)
 {
 	/* return true if binary-ness has already been settled */
-	if (delta->binary != -1)
+	if ((delta->flags & KNOWN_BINARY_FLAGS) != 0)
 		return true;
 
 	/* make sure files are conceivably mmap-able */
 	if ((git_off_t)((size_t)delta->old_file.size) != delta->old_file.size ||
 		(git_off_t)((size_t)delta->new_file.size) != delta->new_file.size)
 	{
-		delta->old_file.flags |= GIT_DIFF_FILE_BINARY;
-		delta->new_file.flags |= GIT_DIFF_FILE_BINARY;
-		delta->binary = 1;
+		delta->old_file.flags |= GIT_DIFF_FLAG_BINARY;
+		delta->new_file.flags |= GIT_DIFF_FLAG_BINARY;
+		delta->flags |= GIT_DIFF_FLAG_BINARY;
 		return true;
 	}
 
 	/* check if user is forcing us to text diff these files */
 	if (ctxt->opts && (ctxt->opts->flags & GIT_DIFF_FORCE_TEXT) != 0) {
-		delta->old_file.flags |= GIT_DIFF_FILE_NOT_BINARY;
-		delta->new_file.flags |= GIT_DIFF_FILE_NOT_BINARY;
-		delta->binary = 0;
+		delta->old_file.flags |= GIT_DIFF_FLAG_NOT_BINARY;
+		delta->new_file.flags |= GIT_DIFF_FLAG_NOT_BINARY;
+		delta->flags |= GIT_DIFF_FLAG_NOT_BINARY;
 		return true;
 	}
 
@@ -125,8 +125,6 @@ static int diff_delta_is_binary_by_attr(
 	int error = 0, mirror_new;
 	git_diff_delta *delta = patch->delta;
 
-	delta->binary = -1;
-
 	if (diff_delta_is_binary_forced(ctxt, delta))
 		return 0;
 
@@ -152,23 +150,21 @@ static int diff_delta_is_binary_by_content(
 	git_diff_file *file,
 	const git_map *map)
 {
+	const git_buf search = { map->data, 0, min(map->len, 4000) };
+
 	if (diff_delta_is_binary_forced(ctxt, delta))
 		return 0;
 
-	if ((file->flags & KNOWN_BINARY_FLAGS) == 0) {
-		const git_buf search = { map->data, 0, min(map->len, 4000) };
-
-		/* TODO: provide encoding / binary detection callbacks that can
-		 * be UTF-8 aware, etc.  For now, instead of trying to be smart,
-		 * let's just use the simple NUL-byte detection that core git uses.
-		 */
+	/* TODO: provide encoding / binary detection callbacks that can
+	 * be UTF-8 aware, etc.  For now, instead of trying to be smart,
+	 * let's just use the simple NUL-byte detection that core git uses.
+	 */
 
-		/* previously was: if (git_buf_text_is_binary(&search)) */
-		if (git_buf_text_contains_nul(&search))
-			file->flags |= GIT_DIFF_FILE_BINARY;
-		else
-			file->flags |= GIT_DIFF_FILE_NOT_BINARY;
-	}
+	/* previously was: if (git_buf_text_is_binary(&search)) */
+	if (git_buf_text_contains_nul(&search))
+		file->flags |= GIT_DIFF_FLAG_BINARY;
+	else
+		file->flags |= GIT_DIFF_FLAG_NOT_BINARY;
 
 	update_delta_is_binary(delta);
 
@@ -192,7 +188,7 @@ static int diff_delta_is_binary_by_size(
 	}
 
 	if (file->size > threshold)
-		file->flags |= GIT_DIFF_FILE_BINARY;
+		file->flags |= GIT_DIFF_FLAG_BINARY;
 
 	update_delta_is_binary(delta);
 
@@ -247,7 +243,7 @@ static int get_blob_content(
 		map->data = git_buf_detach(&content);
 		map->len = strlen(map->data);
 
-		file->flags |= GIT_DIFF_FILE_FREE_DATA;
+		file->flags |= GIT_DIFF_FLAG__FREE_DATA;
 		return 0;
 	}
 
@@ -270,7 +266,7 @@ static int get_blob_content(
 	/* if blob is too large to diff, mark as binary */
 	if ((error = diff_delta_is_binary_by_size(ctxt, delta, file)) < 0)
 		return error;
-	if (delta->binary == 1)
+	if ((delta->flags & GIT_DIFF_FLAG_BINARY) != 0)
 		return 0;
 
 	if (odb_obj != NULL) {
@@ -306,14 +302,14 @@ static int get_workdir_sm_content(
 		return error;
 
 	/* update OID if we didn't have it previously */
-	if ((file->flags & GIT_DIFF_FILE_VALID_OID) == 0) {
+	if ((file->flags & GIT_DIFF_FLAG_VALID_OID) == 0) {
 		const git_oid* sm_head;
 
 		if ((sm_head = git_submodule_wd_id(sm)) != NULL ||
 			(sm_head = git_submodule_head_id(sm)) != NULL)
 		{
 			git_oid_cpy(&file->oid, sm_head);
-			file->flags |= GIT_DIFF_FILE_VALID_OID;
+			file->flags |= GIT_DIFF_FLAG_VALID_OID;
 		}
 	}
 
@@ -329,7 +325,7 @@ static int get_workdir_sm_content(
 	map->data = git_buf_detach(&content);
 	map->len = strlen(map->data);
 
-	file->flags |= GIT_DIFF_FILE_FREE_DATA;
+	file->flags |= GIT_DIFF_FLAG__FREE_DATA;
 
 	return 0;
 }
@@ -356,8 +352,8 @@ static int get_workdir_content(
 	if (S_ISLNK(file->mode)) {
 		ssize_t alloc_len, read_len;
 
-		file->flags |= GIT_DIFF_FILE_FREE_DATA;
-		file->flags |= GIT_DIFF_FILE_BINARY;
+		file->flags |= GIT_DIFF_FLAG__FREE_DATA;
+		file->flags |= GIT_DIFF_FLAG_BINARY;
 
 		/* link path on disk could be UTF-16, so prepare a buffer that is
 		 * big enough to handle some UTF-8 data expansion
@@ -389,7 +385,7 @@ static int get_workdir_content(
 			file->size = git_futils_filesize(fd);
 
 		if ((error = diff_delta_is_binary_by_size(ctxt, delta, file)) < 0 ||
-			delta->binary == 1)
+			(delta->flags & GIT_DIFF_FLAG_BINARY) != 0)
 			goto close_and_cleanup;
 
 		error = git_filters_load(
@@ -402,7 +398,7 @@ static int get_workdir_content(
 				goto close_and_cleanup;
 
 			error = git_futils_mmap_ro(map, fd, 0, (size_t)file->size);
-			file->flags |= GIT_DIFF_FILE_UNMAP_DATA;
+			file->flags |= GIT_DIFF_FLAG__UNMAP_DATA;
 		} else {
 			git_buf raw = GIT_BUF_INIT, filtered = GIT_BUF_INIT;
 
@@ -412,7 +408,7 @@ static int get_workdir_content(
 				map->len  = git_buf_len(&filtered);
 				map->data = git_buf_detach(&filtered);
 
-				file->flags |= GIT_DIFF_FILE_FREE_DATA;
+				file->flags |= GIT_DIFF_FLAG__FREE_DATA;
 			}
 
 			git_buf_free(&raw);
@@ -425,11 +421,11 @@ close_and_cleanup:
 	}
 
 	/* once data is loaded, update OID if we didn't have it previously */
-	if (!error && (file->flags & GIT_DIFF_FILE_VALID_OID) == 0) {
+	if (!error && (file->flags & GIT_DIFF_FLAG_VALID_OID) == 0) {
 		error = git_odb_hash(
 			&file->oid, map->data, map->len, GIT_OBJ_BLOB);
 		if (!error)
-			file->flags |= GIT_DIFF_FILE_VALID_OID;
+			file->flags |= GIT_DIFF_FLAG_VALID_OID;
 	}
 
 	if (!error)
@@ -445,17 +441,17 @@ static void release_content(git_diff_file *file, git_map *map, git_blob *blob)
 	if (blob != NULL)
 		git_blob_free(blob);
 
-	if (file->flags & GIT_DIFF_FILE_FREE_DATA) {
+	if (file->flags & GIT_DIFF_FLAG__FREE_DATA) {
 		git__free(map->data);
 		map->data = "";
 		map->len  = 0;
-		file->flags &= ~GIT_DIFF_FILE_FREE_DATA;
+		file->flags &= ~GIT_DIFF_FLAG__FREE_DATA;
 	}
-	else if (file->flags & GIT_DIFF_FILE_UNMAP_DATA) {
+	else if (file->flags & GIT_DIFF_FLAG__UNMAP_DATA) {
 		git_futils_mmap_free(map);
 		map->data = "";
 		map->len  = 0;
-		file->flags &= ~GIT_DIFF_FILE_UNMAP_DATA;
+		file->flags &= ~GIT_DIFF_FLAG__UNMAP_DATA;
 	}
 }
 
@@ -555,7 +551,7 @@ static int diff_patch_load(
 	patch->new_data.len  = 0;
 	patch->new_blob      = NULL;
 
-	if (delta->binary == 1)
+	if ((delta->flags & GIT_DIFF_FLAG_BINARY) != 0)
 		goto cleanup;
 
 	if (!ctxt->hunk_cb &&
@@ -565,25 +561,25 @@ static int diff_patch_load(
 
 	switch (delta->status) {
 	case GIT_DELTA_ADDED:
-		delta->old_file.flags |= GIT_DIFF_FILE_NO_DATA;
+		delta->old_file.flags |= GIT_DIFF_FLAG__NO_DATA;
 		break;
 	case GIT_DELTA_DELETED:
-		delta->new_file.flags |= GIT_DIFF_FILE_NO_DATA;
+		delta->new_file.flags |= GIT_DIFF_FLAG__NO_DATA;
 		break;
 	case GIT_DELTA_MODIFIED:
 		break;
 	case GIT_DELTA_UNTRACKED:
-		delta->old_file.flags |= GIT_DIFF_FILE_NO_DATA;
+		delta->old_file.flags |= GIT_DIFF_FLAG__NO_DATA;
 		if ((ctxt->opts->flags & GIT_DIFF_INCLUDE_UNTRACKED_CONTENT) == 0)
-			delta->new_file.flags |= GIT_DIFF_FILE_NO_DATA;
+			delta->new_file.flags |= GIT_DIFF_FLAG__NO_DATA;
 		break;
 	default:
-		delta->new_file.flags |= GIT_DIFF_FILE_NO_DATA;
-		delta->old_file.flags |= GIT_DIFF_FILE_NO_DATA;
+		delta->new_file.flags |= GIT_DIFF_FLAG__NO_DATA;
+		delta->old_file.flags |= GIT_DIFF_FLAG__NO_DATA;
 		break;
 	}
 
-#define CHECK_UNMODIFIED (GIT_DIFF_FILE_NO_DATA | GIT_DIFF_FILE_VALID_OID)
+#define CHECK_UNMODIFIED (GIT_DIFF_FLAG__NO_DATA | GIT_DIFF_FLAG_VALID_OID)
 
 	check_if_unmodified =
 		(delta->old_file.flags & CHECK_UNMODIFIED) == 0 &&
@@ -594,41 +590,41 @@ static int diff_patch_load(
 	 * memory footprint during diff.
 	 */
 
-	if ((delta->old_file.flags & GIT_DIFF_FILE_NO_DATA) == 0 &&
+	if ((delta->old_file.flags & GIT_DIFF_FLAG__NO_DATA) == 0 &&
 		patch->old_src == GIT_ITERATOR_TYPE_WORKDIR) {
 		if ((error = get_workdir_content(
 				ctxt, delta, &delta->old_file, &patch->old_data)) < 0)
 			goto cleanup;
-		if (delta->binary == 1)
+		if ((delta->flags & GIT_DIFF_FLAG_BINARY) != 0)
 			goto cleanup;
 	}
 
-	if ((delta->new_file.flags & GIT_DIFF_FILE_NO_DATA) == 0 &&
+	if ((delta->new_file.flags & GIT_DIFF_FLAG__NO_DATA) == 0 &&
 		patch->new_src == GIT_ITERATOR_TYPE_WORKDIR) {
 		if ((error = get_workdir_content(
 				ctxt, delta, &delta->new_file, &patch->new_data)) < 0)
 			goto cleanup;
-		if (delta->binary == 1)
+		if ((delta->flags & GIT_DIFF_FLAG_BINARY) != 0)
 			goto cleanup;
 	}
 
-	if ((delta->old_file.flags & GIT_DIFF_FILE_NO_DATA) == 0 &&
+	if ((delta->old_file.flags & GIT_DIFF_FLAG__NO_DATA) == 0 &&
 		patch->old_src != GIT_ITERATOR_TYPE_WORKDIR) {
 		if ((error = get_blob_content(
 				ctxt, delta, &delta->old_file,
 				&patch->old_data, &patch->old_blob)) < 0)
 			goto cleanup;
-		if (delta->binary == 1)
+		if ((delta->flags & GIT_DIFF_FLAG_BINARY) != 0)
 			goto cleanup;
 	}
 
-	if ((delta->new_file.flags & GIT_DIFF_FILE_NO_DATA) == 0 &&
+	if ((delta->new_file.flags & GIT_DIFF_FLAG__NO_DATA) == 0 &&
 		patch->new_src != GIT_ITERATOR_TYPE_WORKDIR) {
 		if ((error = get_blob_content(
 				ctxt, delta, &delta->new_file,
 				&patch->new_data, &patch->new_blob)) < 0)
 			goto cleanup;
-		if (delta->binary == 1)
+		if ((delta->flags & GIT_DIFF_FLAG_BINARY) != 0)
 			goto cleanup;
 	}
 
@@ -646,13 +642,13 @@ static int diff_patch_load(
 	}
 
 cleanup:
-	if (delta->binary == -1)
+	if ((delta->flags & KNOWN_BINARY_FLAGS) == 0)
 		update_delta_is_binary(delta);
 
 	if (!error) {
 		patch->flags |= GIT_DIFF_PATCH_LOADED;
 
-		if (delta->binary != 1 &&
+		if ((delta->flags & GIT_DIFF_FLAG_BINARY) == 0 &&
 			delta->status != GIT_DELTA_UNMODIFIED &&
 			(patch->old_data.len || patch->new_data.len) &&
 			!git_oid_equal(&delta->old_file.oid, &delta->new_file.oid))
@@ -1138,7 +1134,7 @@ static int print_patch_file(
 		newpath = "/dev/null";
 	}
 
-	if (delta->binary != 1) {
+	if ((delta->flags & GIT_DIFF_FLAG_BINARY) == 0) {
 		git_buf_printf(pi->buf, "--- %s%s\n", oldpfx, oldpath);
 		git_buf_printf(pi->buf, "+++ %s%s\n", newpfx, newpath);
 	}
@@ -1153,7 +1149,7 @@ static int print_patch_file(
 		return GIT_EUSER;
 	}
 
-	if (delta->binary != 1)
+	if ((delta->flags & GIT_DIFF_FLAG_BINARY) == 0)
 		return 0;
 
 	git_buf_clear(pi->buf);
@@ -1268,7 +1264,7 @@ static void set_data_from_blob(
 		map->data  = (char *)git_blob_rawcontent(blob);
 	} else {
 		file->size = 0;
-		file->flags |= GIT_DIFF_FILE_NO_DATA;
+		file->flags |= GIT_DIFF_FLAG__NO_DATA;
 
 		map->len   = 0;
 		map->data  = "";
@@ -1283,7 +1279,7 @@ static void set_data_from_buffer(
 	map->len   = buffer_len;
 
 	if (!buffer) {
-		file->flags |= GIT_DIFF_FILE_NO_DATA;
+		file->flags |= GIT_DIFF_FLAG__NO_DATA;
 		map->data = NULL;
 	} else {
 		map->data = (char *)buffer;
@@ -1322,13 +1318,13 @@ static int diff_single_apply(diff_single_data *data)
 {
 	int error;
 	git_diff_delta *delta = &data->delta;
-	bool has_old = ((delta->old_file.flags & GIT_DIFF_FILE_NO_DATA) == 0);
-	bool has_new = ((delta->new_file.flags & GIT_DIFF_FILE_NO_DATA) == 0);
+	bool has_old = ((delta->old_file.flags & GIT_DIFF_FLAG__NO_DATA) == 0);
+	bool has_new = ((delta->new_file.flags & GIT_DIFF_FLAG__NO_DATA) == 0);
 
 	/* finish setting up fake git_diff_delta record and loaded data */
 
 	data->patch.delta = delta;
-	delta->binary = -1;
+	delta->flags = delta->flags & ~KNOWN_BINARY_FLAGS;
 
 	delta->status = has_new ?
 		(has_old ? GIT_DELTA_MODIFIED : GIT_DELTA_ADDED) :
@@ -1345,7 +1341,8 @@ static int diff_single_apply(diff_single_data *data)
 
 	data->patch.flags |= GIT_DIFF_PATCH_LOADED;
 
-	if (delta->binary != 1 && delta->status != GIT_DELTA_UNMODIFIED)
+	if ((delta->flags & GIT_DIFF_FLAG_BINARY) == 0 &&
+		delta->status != GIT_DELTA_UNMODIFIED)
 		data->patch.flags |= GIT_DIFF_PATCH_DIFFABLE;
 
 	/* do diffs */
@@ -1469,7 +1466,7 @@ int git_diff_get_patch(
 		*delta_ptr = delta;
 
 	if (!patch_ptr &&
-		(delta->binary != -1 ||
+		((delta->flags & KNOWN_BINARY_FLAGS) != 0 ||
 		 (diff->opts.flags & GIT_DIFF_SKIP_BINARY_CHECK) != 0))
 		return 0;
 
diff --git a/src/diff_tform.c b/src/diff_tform.c
index 61cf5b3..e051732 100644
--- a/src/diff_tform.c
+++ b/src/diff_tform.c
@@ -315,10 +315,10 @@ static int apply_splits_and_deletes(git_diff_list *diff, size_t expected_size)
 
 	/* build new delta list without TO_DELETE and splitting TO_SPLIT */
 	git_vector_foreach(&diff->deltas, i, delta) {
-		if (delta->status == GIT_DELTA__TO_DELETE)
+		if ((delta->flags & GIT_DIFF_FLAG__TO_DELETE) != 0)
 			continue;
 
-		if (delta->status == GIT_DELTA__TO_SPLIT) {
+		if ((delta->flags & GIT_DIFF_FLAG__TO_SPLIT) != 0) {
 			git_diff_delta *deleted = diff_delta__dup(delta, &diff->pool);
 			if (!deleted)
 				goto on_error;
@@ -326,7 +326,7 @@ static int apply_splits_and_deletes(git_diff_list *diff, size_t expected_size)
 			deleted->status = GIT_DELTA_DELETED;
 			memset(&deleted->new_file, 0, sizeof(deleted->new_file));
 			deleted->new_file.path = deleted->old_file.path;
-			deleted->new_file.flags |= GIT_DIFF_FILE_VALID_OID;
+			deleted->new_file.flags |= GIT_DIFF_FLAG_VALID_OID;
 
 			if (git_vector_insert(&onto, deleted) < 0)
 				goto on_error;
@@ -334,7 +334,7 @@ static int apply_splits_and_deletes(git_diff_list *diff, size_t expected_size)
 			delta->status = GIT_DELTA_ADDED;
 			memset(&delta->old_file, 0, sizeof(delta->old_file));
 			delta->old_file.path = delta->new_file.path;
-			delta->old_file.flags |= GIT_DIFF_FILE_VALID_OID;
+			delta->old_file.flags |= GIT_DIFF_FLAG_VALID_OID;
 		}
 
 		if (git_vector_insert(&onto, delta) < 0)
@@ -343,7 +343,7 @@ static int apply_splits_and_deletes(git_diff_list *diff, size_t expected_size)
 
 	/* cannot return an error past this point */
 	git_vector_foreach(&diff->deltas, i, delta)
-		if (delta->status == GIT_DELTA__TO_DELETE)
+		if ((delta->flags & GIT_DIFF_FLAG__TO_DELETE) != 0)
 			git__free(delta);
 
 	/* swap new delta list into place */
@@ -411,7 +411,7 @@ int git_diff_find_similar(
 			/* calc_similarity(NULL, &from->old_file, from->new_file); */
 
 			if (similarity < opts.break_rewrite_threshold) {
-				from->status = GIT_DELTA__TO_SPLIT;
+				from->flags |= GIT_DIFF_FLAG__TO_SPLIT;
 				num_changes++;
 			}
 		}
@@ -502,7 +502,7 @@ int git_diff_find_similar(
 			to->status = GIT_DELTA_RENAMED;
 			memcpy(&to->old_file, &from->old_file, sizeof(to->old_file));
 
-			from->status = GIT_DELTA__TO_DELETE;
+			from->flags |= GIT_DIFF_FLAG__TO_DELETE;
 			num_changes++;
 
 			continue;
@@ -522,7 +522,7 @@ int git_diff_find_similar(
 				from->status = GIT_DELTA_ADDED;
 				memset(&from->old_file, 0, sizeof(from->old_file));
 				from->old_file.path = to->old_file.path;
-				from->old_file.flags |= GIT_DIFF_FILE_VALID_OID;
+				from->old_file.flags |= GIT_DIFF_FLAG_VALID_OID;
 
 				continue;
 			}
diff --git a/tests-clar/diff/diff_helpers.c b/tests-clar/diff/diff_helpers.c
index 1436ada..a1f75ce 100644
--- a/tests-clar/diff/diff_helpers.c
+++ b/tests-clar/diff/diff_helpers.c
@@ -32,7 +32,7 @@ int diff_file_cb(
 
 	e->files++;
 
-	if (delta->binary)
+	if ((delta->flags & GIT_DIFF_FLAG_BINARY) != 0)
 		e->files_binary++;
 
 	cl_assert(delta->status <= GIT_DELTA_TYPECHANGE);
@@ -126,7 +126,8 @@ int diff_foreach_via_iterator(
 
 		/* if there are no changes, then the patch will be NULL */
 		if (!patch) {
-			cl_assert(delta->status == GIT_DELTA_UNMODIFIED || delta->binary == 1);
+			cl_assert(delta->status == GIT_DELTA_UNMODIFIED ||
+					  (delta->flags & GIT_DIFF_FLAG_BINARY) != 0);
 			continue;
 		}
 
diff --git a/tests-clar/diff/diffiter.c b/tests-clar/diff/diffiter.c
index 8d550ec..932d720 100644
--- a/tests-clar/diff/diffiter.c
+++ b/tests-clar/diff/diffiter.c
@@ -152,8 +152,8 @@ void test_diff_diffiter__max_size_threshold(void)
 		file_count++;
 		hunk_count += (int)git_diff_patch_num_hunks(patch);
 
-		assert(delta->binary == 0 || delta->binary == 1);
-		binary_count += delta->binary;
+		assert((delta->flags & (GIT_DIFF_FLAG_BINARY|GIT_DIFF_FLAG_NOT_BINARY)) != 0);
+		binary_count += ((delta->flags & GIT_DIFF_FLAG_BINARY) != 0);
 
 		git_diff_patch_free(patch);
 	}
@@ -185,8 +185,8 @@ void test_diff_diffiter__max_size_threshold(void)
 		file_count++;
 		hunk_count += (int)git_diff_patch_num_hunks(patch);
 
-		assert(delta->binary == 0 || delta->binary == 1);
-		binary_count += delta->binary;
+		assert((delta->flags & (GIT_DIFF_FLAG_BINARY|GIT_DIFF_FLAG_NOT_BINARY)) != 0);
+		binary_count += ((delta->flags & GIT_DIFF_FLAG_BINARY) != 0);
 
 		git_diff_patch_free(patch);
 	}