4242
4343struct gitdiff_data {
4444 struct strbuf * root ;
45+ const char * patch_input_file ;
4546 int linenr ;
4647 int p_value ;
4748};
@@ -900,7 +901,8 @@ static int parse_traditional_patch(struct apply_state *state,
900901 }
901902 }
902903 if (!name )
903- return error (_ ("unable to find filename in patch at line %d" ), state -> linenr );
904+ return error (_ ("unable to find filename in patch at %s:%d" ),
905+ state -> patch_input_file , state -> linenr );
904906
905907 return 0 ;
906908}
@@ -937,20 +939,35 @@ static int gitdiff_verify_name(struct gitdiff_data *state,
937939
938940 if (* name ) {
939941 char * another ;
940- if (isnull )
942+ if (isnull ) {
943+ if (state -> patch_input_file )
944+ return error (_ ("git apply: bad git-diff - expected /dev/null, got %s at %s:%d" ),
945+ * name , state -> patch_input_file , state -> linenr );
941946 return error (_ ("git apply: bad git-diff - expected /dev/null, got %s on line %d" ),
942947 * name , state -> linenr );
948+ }
943949 another = find_name (state -> root , line , NULL , state -> p_value , TERM_TAB );
944950 if (!another || strcmp (another , * name )) {
945951 free (another );
952+ if (state -> patch_input_file )
953+ return error ((side == DIFF_NEW_NAME ) ?
954+ _ ("git apply: bad git-diff - inconsistent new filename at %s:%d" ) :
955+ _ ("git apply: bad git-diff - inconsistent old filename at %s:%d" ),
956+ state -> patch_input_file , state -> linenr );
946957 return error ((side == DIFF_NEW_NAME ) ?
947- _ ("git apply: bad git-diff - inconsistent new filename on line %d" ) :
948- _ ("git apply: bad git-diff - inconsistent old filename on line %d" ), state -> linenr );
958+ _ ("git apply: bad git-diff - inconsistent new filename on line %d" ) :
959+ _ ("git apply: bad git-diff - inconsistent old filename on line %d" ),
960+ state -> linenr );
949961 }
950962 free (another );
951963 } else {
952- if (!is_dev_null (line ))
953- return error (_ ("git apply: bad git-diff - expected /dev/null on line %d" ), state -> linenr );
964+ if (!is_dev_null (line )) {
965+ if (state -> patch_input_file )
966+ return error (_ ("git apply: bad git-diff - expected /dev/null at %s:%d" ),
967+ state -> patch_input_file , state -> linenr );
968+ return error (_ ("git apply: bad git-diff - expected /dev/null on line %d" ),
969+ state -> linenr );
970+ }
954971 }
955972
956973 return 0 ;
@@ -974,12 +991,19 @@ static int gitdiff_newname(struct gitdiff_data *state,
974991 DIFF_NEW_NAME );
975992}
976993
977- static int parse_mode_line (const char * line , int linenr , unsigned int * mode )
994+ static int parse_mode_line (const char * line ,
995+ const char * patch_input_file ,
996+ int linenr ,
997+ unsigned int * mode )
978998{
979999 char * end ;
9801000 * mode = strtoul (line , & end , 8 );
981- if (end == line || !isspace (* end ))
1001+ if (end == line || !isspace (* end )) {
1002+ if (patch_input_file )
1003+ return error (_ ("invalid mode at %s:%d: %s" ),
1004+ patch_input_file , linenr , line );
9821005 return error (_ ("invalid mode on line %d: %s" ), linenr , line );
1006+ }
9831007 * mode = canon_mode (* mode );
9841008 return 0 ;
9851009}
@@ -988,14 +1012,16 @@ static int gitdiff_oldmode(struct gitdiff_data *state,
9881012 const char * line ,
9891013 struct patch * patch )
9901014{
991- return parse_mode_line (line , state -> linenr , & patch -> old_mode );
1015+ return parse_mode_line (line , state -> patch_input_file , state -> linenr ,
1016+ & patch -> old_mode );
9921017}
9931018
9941019static int gitdiff_newmode (struct gitdiff_data * state ,
9951020 const char * line ,
9961021 struct patch * patch )
9971022{
998- return parse_mode_line (line , state -> linenr , & patch -> new_mode );
1023+ return parse_mode_line (line , state -> patch_input_file , state -> linenr ,
1024+ & patch -> new_mode );
9991025}
10001026
10011027static int gitdiff_delete (struct gitdiff_data * state ,
@@ -1314,6 +1340,7 @@ static int check_header_line(int linenr, struct patch *patch)
13141340}
13151341
13161342int parse_git_diff_header (struct strbuf * root ,
1343+ const char * patch_input_file ,
13171344 int * linenr ,
13181345 int p_value ,
13191346 const char * line ,
@@ -1345,6 +1372,7 @@ int parse_git_diff_header(struct strbuf *root,
13451372 size -= len ;
13461373 (* linenr )++ ;
13471374 parse_hdr_state .root = root ;
1375+ parse_hdr_state .patch_input_file = patch_input_file ;
13481376 parse_hdr_state .linenr = * linenr ;
13491377 parse_hdr_state .p_value = p_value ;
13501378
@@ -1382,6 +1410,7 @@ int parse_git_diff_header(struct strbuf *root,
13821410 int res ;
13831411 if (len < oplen || memcmp (p -> str , line , oplen ))
13841412 continue ;
1413+ parse_hdr_state .linenr = * linenr ;
13851414 res = p -> fn (& parse_hdr_state , line + oplen , patch );
13861415 if (res < 0 )
13871416 return -1 ;
@@ -1396,21 +1425,33 @@ int parse_git_diff_header(struct strbuf *root,
13961425done :
13971426 if (!patch -> old_name && !patch -> new_name ) {
13981427 if (!patch -> def_name ) {
1399- error (Q_ ("git diff header lacks filename information when removing "
1400- "%d leading pathname component (line %d)" ,
1401- "git diff header lacks filename information when removing "
1402- "%d leading pathname components (line %d)" ,
1403- parse_hdr_state .p_value ),
1404- parse_hdr_state .p_value , * linenr );
1428+ if (patch_input_file )
1429+ error (Q_ ("git diff header lacks filename information when removing "
1430+ "%d leading pathname component at %s:%d" ,
1431+ "git diff header lacks filename information when removing "
1432+ "%d leading pathname components at %s:%d" ,
1433+ parse_hdr_state .p_value ),
1434+ parse_hdr_state .p_value , patch_input_file , * linenr );
1435+ else
1436+ error (Q_ ("git diff header lacks filename information when removing "
1437+ "%d leading pathname component (line %d)" ,
1438+ "git diff header lacks filename information when removing "
1439+ "%d leading pathname components (line %d)" ,
1440+ parse_hdr_state .p_value ),
1441+ parse_hdr_state .p_value , * linenr );
14051442 return -128 ;
14061443 }
14071444 patch -> old_name = xstrdup (patch -> def_name );
14081445 patch -> new_name = xstrdup (patch -> def_name );
14091446 }
14101447 if ((!patch -> new_name && !patch -> is_delete ) ||
14111448 (!patch -> old_name && !patch -> is_new )) {
1412- error (_ ("git diff header lacks filename information "
1413- "(line %d)" ), * linenr );
1449+ if (patch_input_file )
1450+ error (_ ("git diff header lacks filename information at %s:%d" ),
1451+ patch_input_file , * linenr );
1452+ else
1453+ error (_ ("git diff header lacks filename information (line %d)" ),
1454+ * linenr );
14141455 return -128 ;
14151456 }
14161457 patch -> is_toplevel_relative = 1 ;
@@ -1577,8 +1618,9 @@ static int find_header(struct apply_state *state,
15771618 struct fragment dummy ;
15781619 if (parse_fragment_header (line , len , & dummy ) < 0 )
15791620 continue ;
1580- error (_ ("patch fragment without header at line %d: %.*s" ),
1581- state -> linenr , (int )len - 1 , line );
1621+ error (_ ("patch fragment without header at %s:%d: %.*s" ),
1622+ state -> patch_input_file , state -> linenr ,
1623+ (int )len - 1 , line );
15821624 return -128 ;
15831625 }
15841626
@@ -1590,7 +1632,9 @@ static int find_header(struct apply_state *state,
15901632 * or mode change, so we handle that specially
15911633 */
15921634 if (!memcmp ("diff --git " , line , 11 )) {
1593- int git_hdr_len = parse_git_diff_header (& state -> root , & state -> linenr ,
1635+ int git_hdr_len = parse_git_diff_header (& state -> root ,
1636+ state -> patch_input_file ,
1637+ & state -> linenr ,
15941638 state -> p_value , line , len ,
15951639 size , patch );
15961640 if (git_hdr_len < 0 )
@@ -1875,7 +1919,8 @@ static int parse_single_patch(struct apply_state *state,
18751919 len = parse_fragment (state , line , size , patch , fragment );
18761920 if (len <= 0 ) {
18771921 free (fragment );
1878- return error (_ ("corrupt patch at line %d" ), state -> linenr );
1922+ return error (_ ("corrupt patch at %s:%d" ),
1923+ state -> patch_input_file , state -> linenr );
18791924 }
18801925 fragment -> patch = line ;
18811926 fragment -> size = len ;
@@ -2065,8 +2110,8 @@ static struct fragment *parse_binary_hunk(struct apply_state *state,
20652110 corrupt :
20662111 free (data );
20672112 * status_p = -1 ;
2068- error (_ ("corrupt binary patch at line %d: %.*s" ),
2069- state -> linenr - 1 , llen - 1 , buffer );
2113+ error (_ ("corrupt binary patch at %s: %d: %.*s" ),
2114+ state -> patch_input_file , state -> linenr - 1 , llen - 1 , buffer );
20702115 return NULL ;
20712116}
20722117
@@ -2102,7 +2147,8 @@ static int parse_binary(struct apply_state *state,
21022147 forward = parse_binary_hunk (state , & buffer , & size , & status , & used );
21032148 if (!forward && !status )
21042149 /* there has to be one hunk (forward hunk) */
2105- return error (_ ("unrecognized binary patch at line %d" ), state -> linenr - 1 );
2150+ return error (_ ("unrecognized binary patch at %s:%d" ),
2151+ state -> patch_input_file , state -> linenr - 1 );
21062152 if (status )
21072153 /* otherwise we already gave an error message */
21082154 return status ;
@@ -2264,7 +2310,8 @@ static int parse_chunk(struct apply_state *state, char *buffer, unsigned long si
22642310 */
22652311 if ((state -> apply || state -> check ) &&
22662312 (!patch -> is_binary && !metadata_changes (patch ))) {
2267- error (_ ("patch with only garbage at line %d" ), state -> linenr );
2313+ error (_ ("patch with only garbage at %s:%d" ),
2314+ state -> patch_input_file , state -> linenr );
22682315 return -128 ;
22692316 }
22702317 }
@@ -4825,6 +4872,7 @@ static int apply_patch(struct apply_state *state,
48254872 int flush_attributes = 0 ;
48264873
48274874 state -> patch_input_file = filename ;
4875+ state -> linenr = 1 ;
48284876 if (read_patch_file (& buf , fd ) < 0 )
48294877 return -128 ;
48304878 offset = 0 ;
0 commit comments