@@ -27,6 +27,7 @@ static const char *prefix;
2727static int prefix_length = -1 ;
2828static int newfd = -1 ;
2929
30+ static int unidiff_zero ;
3031static int p_value = 1 ;
3132static int check_index ;
3233static int write_index ;
@@ -854,11 +855,10 @@ static int find_header(char *line, unsigned long size, int *hdrsize, struct patc
854855}
855856
856857/*
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..
862862 */
863863static int parse_fragment (char * line , unsigned long size , struct patch * patch , struct fragment * fragment )
864864{
@@ -875,31 +875,14 @@ static int parse_fragment(char *line, unsigned long size, struct patch *patch, s
875875 leading = 0 ;
876876 trailing = 0 ;
877877
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-
897878 /* Parse the thing.. */
898879 line += len ;
899880 size -= len ;
900881 linenr ++ ;
901882 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 ++ ) {
903886 if (!oldlines && !newlines )
904887 break ;
905888 len = linelen (line , size );
@@ -972,12 +955,18 @@ static int parse_fragment(char *line, unsigned long size, struct patch *patch, s
972955
973956 patch -> lines_added += added ;
974957 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" );
975963 return offset ;
976964}
977965
978966static int parse_single_patch (char * line , unsigned long size , struct patch * patch )
979967{
980968 unsigned long offset = 0 ;
969+ unsigned long oldlines = 0 , newlines = 0 , context = 0 ;
981970 struct fragment * * fragp = & patch -> fragments ;
982971
983972 while (size > 4 && !memcmp (line , "@@ -" , 4 )) {
@@ -988,9 +977,11 @@ static int parse_single_patch(char *line, unsigned long size, struct patch *patc
988977 len = parse_fragment (line , size , patch , fragment );
989978 if (len <= 0 )
990979 die ("corrupt patch at line %d" , linenr );
991-
992980 fragment -> patch = line ;
993981 fragment -> size = len ;
982+ oldlines += fragment -> oldlines ;
983+ newlines += fragment -> newlines ;
984+ context += fragment -> leading + fragment -> trailing ;
994985
995986 * fragp = fragment ;
996987 fragp = & fragment -> next ;
@@ -999,6 +990,46 @@ static int parse_single_patch(char *line, unsigned long size, struct patch *patc
999990 line += len ;
1000991 size -= len ;
1001992 }
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+
10021033 return offset ;
10031034}
10041035
@@ -1556,9 +1587,19 @@ static int apply_one_fragment(struct buffer_desc *desc, struct fragment *frag, i
15561587 /*
15571588 * If we don't have any leading/trailing data in the patch,
15581589 * 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.
15591594 */
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+ }
15621603
15631604 lines = 0 ;
15641605 pos = frag -> newpos ;
@@ -1804,7 +1845,7 @@ static int apply_data(struct patch *patch, struct stat *st, struct cache_entry *
18041845 patch -> result = desc .buffer ;
18051846 patch -> resultsize = desc .size ;
18061847
1807- if (patch -> is_delete && patch -> resultsize )
1848+ if (0 < patch -> is_delete && patch -> resultsize )
18081849 return error ("removal patch leaves file contents" );
18091850
18101851 return 0 ;
@@ -1876,7 +1917,7 @@ static int check_patch(struct patch *patch, struct patch *prev_patch)
18761917 old_name , st_mode , patch -> old_mode );
18771918 }
18781919
1879- if (new_name && prev_patch && prev_patch -> is_delete &&
1920+ if (new_name && prev_patch && 0 < prev_patch -> is_delete &&
18801921 !strcmp (prev_patch -> old_name , new_name ))
18811922 /* A type-change diff is always split into a patch to
18821923 * delete old, immediately followed by a patch to
@@ -1889,7 +1930,8 @@ static int check_patch(struct patch *patch, struct patch *prev_patch)
18891930 else
18901931 ok_if_exists = 0 ;
18911932
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 )) {
18931935 if (check_index &&
18941936 cache_name_pos (new_name , strlen (new_name )) >= 0 &&
18951937 !ok_if_exists )
@@ -1906,7 +1948,7 @@ static int check_patch(struct patch *patch, struct patch *prev_patch)
19061948 return error ("%s: %s" , new_name , strerror (errno ));
19071949 }
19081950 if (!patch -> new_mode ) {
1909- if (patch -> is_new )
1951+ if (0 < patch -> is_new )
19101952 patch -> new_mode = S_IFREG | 0644 ;
19111953 else
19121954 patch -> new_mode = patch -> old_mode ;
@@ -1957,7 +1999,7 @@ static void show_index_list(struct patch *list)
19571999 const char * name ;
19582000
19592001 name = patch -> old_name ? patch -> old_name : patch -> new_name ;
1960- if (patch -> is_new )
2002+ if (0 < patch -> is_new )
19612003 sha1_ptr = null_sha1 ;
19622004 else if (get_sha1 (patch -> old_sha1_prefix , sha1 ))
19632005 die ("sha1 information is lacking or useless (%s)." ,
@@ -2543,6 +2585,10 @@ int cmd_apply(int argc, const char **argv, const char *prefix)
25432585 apply_in_reverse = 1 ;
25442586 continue ;
25452587 }
2588+ if (!strcmp (arg , "--unidiff-zero" )) {
2589+ unidiff_zero = 1 ;
2590+ continue ;
2591+ }
25462592 if (!strcmp (arg , "--reject" )) {
25472593 apply = apply_with_reject = apply_verbosely = 1 ;
25482594 continue ;
0 commit comments