27
static int prefix_length = -1;
28
static int newfd = -1;
29
30
+static int unidiff_zero;
31
static int p_value = 1;
32
static int check_index;
33
static int write_index;
855
}
856
857
/*
857
- * Parse a unified diff. Note that this really needs
858
- * to parse each fragment separately, since the only
859
- * way to know the difference between a "---" that is
860
- * part of a patch, and a "---" that starts the next
861
- * patch is to look at the line counts..
858
+ * Parse a unified diff. Note that this really needs to parse each
859
+ * fragment separately, since the only way to know the difference
860
+ * between a "---" that is part of a patch, and a "---" that starts
861
+ * the next patch is to look at the line counts..
862
*/
863
static int parse_fragment(char *line, unsigned long size, struct patch *patch, struct fragment *fragment)
864
{
875
leading = 0;
876
trailing = 0;
877
878
- if (patch->is_new < 0) {
879
- patch->is_new = !oldlines;
880
- if (!oldlines)
881
- patch->old_name = NULL;
882
- }
883
- if (patch->is_delete < 0) {
884
- patch->is_delete = !newlines;
885
- if (!newlines)
886
- patch->new_name = NULL;
887
- }
888
-
889
- if (patch->is_new && oldlines)
890
- return error("new file depends on old contents");
891
- if (patch->is_delete != !newlines) {
892
- if (newlines)
893
- return error("deleted file still has contents");
894
- fprintf(stderr, "** warning: file %s becomes empty but is not deleted\n", patch->new_name);
895
- }
896
-
878
/* Parse the thing.. */
879
line += len;
880
size -= len;
881
linenr++;
882
added = deleted = 0;
902
- for (offset = len; size > 0; offset += len, size -= len, line += len, linenr++) {
883
+ for (offset = len;
884
+ 0 < size;
885
+ offset += len, size -= len, line += len, linenr++) {
886
if (!oldlines && !newlines)
887
break;
888
len = linelen(line, size);
955
956
patch->lines_added += added;
957
patch->lines_deleted += deleted;
958
+
959
+ if (0 < patch->is_new && oldlines)
960
+ return error("new file depends on old contents");
961
+ if (0 < patch->is_delete && newlines)
962
+ return error("deleted file still has contents");
963
return offset;
964
}
965
966
static int parse_single_patch(char *line, unsigned long size, struct patch *patch)
967
{
968
unsigned long offset = 0;
969
+ unsigned long oldlines = 0, newlines = 0, context = 0;
970
struct fragment **fragp = &patch->fragments;
971
972
while (size > 4 && !memcmp(line, "@@ -", 4)) {
977
len = parse_fragment(line, size, patch, fragment);
978
if (len <= 0)
979
die("corrupt patch at line %d", linenr);
991
-
980
fragment->patch = line;
981
fragment->size = len;
982
+ oldlines += fragment->oldlines;
983
+ newlines += fragment->newlines;
984
+ context += fragment->leading + fragment->trailing;
985
986
*fragp = fragment;
987
fragp = &fragment->next;
990
line += len;
991
size -= len;
992
}
993
+
994
+ /*
995
+ * If something was removed (i.e. we have old-lines) it cannot
996
+ * be creation, and if something was added it cannot be
997
+ * deletion. However, the reverse is not true; --unified=0
998
+ * patches that only add are not necessarily creation even
999
+ * though they do not have any old lines, and ones that only
1000
+ * delete are not necessarily deletion.
1001
+ *
1002
+ * Unfortunately, a real creation/deletion patch do _not_ have
1003
+ * any context line by definition, so we cannot safely tell it
1004
+ * apart with --unified=0 insanity. At least if the patch has
1005
+ * more than one hunk it is not creation or deletion.
1006
+ */
1007
+ if (patch->is_new < 0 &&
1008
+ (oldlines || (patch->fragments && patch->fragments->next)))
1009
+ patch->is_new = 0;
1010
+ if (patch->is_delete < 0 &&
1011
+ (newlines || (patch->fragments && patch->fragments->next)))
1012
+ patch->is_delete = 0;
1013
+ if (!unidiff_zero || context) {
1014
+ /* If the user says the patch is not generated with
1015
+ * --unified=0, or if we have seen context lines,
1016
+ * then not having oldlines means the patch is creation,
1017
+ * and not having newlines means the patch is deletion.
1018
+ */
1019
+ if (patch->is_new < 0 && !oldlines)
1020
+ patch->is_new = 1;
1021
+ if (patch->is_delete < 0 && !newlines)
1022
+ patch->is_delete = 1;
1023
+ }
1024
+
1025
+ if (0 < patch->is_new && oldlines)
1026
+ die("new file %s depends on old contents", patch->new_name);
1027
+ if (0 < patch->is_delete && newlines)
1028
+ die("deleted file %s still has contents", patch->old_name);
1029
+ if (!patch->is_delete && !newlines && context)
1030
+ fprintf(stderr, "** warning: file %s becomes empty but "
1031
+ "is not deleted\n", patch->new_name);
1032
+
1033
return offset;
1034
}
1035
1587
/*
1588
* If we don't have any leading/trailing data in the patch,
1589
* we want it to match at the beginning/end of the file.
1590
+ *
1591
+ * But that would break if the patch is generated with
1592
+ * --unified=0; sane people wouldn't do that to cause us
1593
+ * trouble, but we try to please not so sane ones as well.
1594
*/
1560
- match_beginning = !leading && (frag->oldpos == 1);
1561
- match_end = !trailing;
1595
+ if (unidiff_zero) {
1596
+ match_beginning = (!leading && !frag->oldpos);
1597
+ match_end = 0;
1598
+ }
1599
+ else {
1600
+ match_beginning = !leading && (frag->oldpos == 1);
1601
+ match_end = !trailing;
1602
+ }
1603
1604
lines = 0;
1605
pos = frag->newpos;
1845
patch->result = desc.buffer;
1846
patch->resultsize = desc.size;
1847
1807
- if (patch->is_delete && patch->resultsize)
1848
+ if (0 < patch->is_delete && patch->resultsize)
1849
return error("removal patch leaves file contents");
1850
1851
return 0;
1917
old_name, st_mode, patch->old_mode);
1918
}
1919
1879
- if (new_name && prev_patch && prev_patch->is_delete &&
1920
+ if (new_name && prev_patch && 0 < prev_patch->is_delete &&
1921
!strcmp(prev_patch->old_name, new_name))
1922
/* A type-change diff is always split into a patch to
1923
* delete old, immediately followed by a patch to
1930
else
1931
ok_if_exists = 0;
1932
1892
- if (new_name && (patch->is_new | patch->is_rename | patch->is_copy)) {
1933
+ if (new_name &&
1934
+ ((0 < patch->is_new) | (0 < patch->is_rename) | patch->is_copy)) {
1935
if (check_index &&
1936
cache_name_pos(new_name, strlen(new_name)) >= 0 &&
1937
!ok_if_exists)
1948
return error("%s: %s", new_name, strerror(errno));
1949
}
1950
if (!patch->new_mode) {
1909
- if (patch->is_new)
1951
+ if (0 < patch->is_new)
1952
patch->new_mode = S_IFREG | 0644;
1953
else
1954
patch->new_mode = patch->old_mode;
1999
const char *name;
2000
2001
name = patch->old_name ? patch->old_name : patch->new_name;
1960
- if (patch->is_new)
2002
+ if (0 < patch->is_new)
2003
sha1_ptr = null_sha1;
2004
else if (get_sha1(patch->old_sha1_prefix, sha1))
2005
die("sha1 information is lacking or useless (%s).",
2585
apply_in_reverse = 1;
2586
continue;
2587
}
2588
+ if (!strcmp(arg, "--unidiff-zero")) {
2589
+ unidiff_zero = 1;
2590
+ continue;
2591
+ }
2592
if (!strcmp(arg, "--reject")) {
2593
apply = apply_with_reject = apply_verbosely = 1;
2594
continue;