Code

apply: Remove the quick rejection test
[git.git] / builtin-apply.c
index 8ca86f66c8e9bf55f99d5bb7e4d56913e1f74023..9641a6479a5325d5d1680d5e313a23e5d536f822 100644 (file)
@@ -404,6 +404,9 @@ static char *squash_slash(char *name)
 {
        int i = 0, j = 0;
 
+       if (!name)
+               return NULL;
+
        while (name[i]) {
                if ((name[j++] = name[i++]) == '/')
                        while (name[i] == '/')
@@ -416,7 +419,10 @@ static char *squash_slash(char *name)
 static char *find_name(const char *line, char *def, int p_value, int terminate)
 {
        int len;
-       const char *start = line;
+       const char *start = NULL;
+
+       if (p_value == 0)
+               start = line;
 
        if (*line == '"') {
                struct strbuf name = STRBUF_INIT;
@@ -1199,7 +1205,8 @@ static int find_header(char *line, unsigned long size, int *hdrsize, struct patc
                                continue;
                        if (!patch->old_name && !patch->new_name) {
                                if (!patch->def_name)
-                                       die("git diff header lacks filename information (line %d)", linenr);
+                                       die("git diff header lacks filename information when removing "
+                                           "%d leading pathname components (line %d)" , p_value, linenr);
                                patch->old_name = patch->new_name = patch->def_name;
                        }
                        patch->is_toplevel_relative = 1;
@@ -1898,20 +1905,15 @@ static int match_fragment(struct image *img,
                }
 
                /*
-                * Ok, the preimage matches with whitespace fuzz. Update it and
-                * the common postimage lines to use the same whitespace as the
-                * target. imgoff now holds the true length of the target that
-                * matches the preimage, and we need to update the line lengths
-                * of the preimage to match the target ones.
+                * Ok, the preimage matches with whitespace fuzz.
+                *
+                * imgoff now holds the true length of the target that
+                * matches the preimage.  Update the preimage and
+                * the common postimage context lines to use the same
+                * whitespace as the target.
                 */
                fixed_buf = xmalloc(imgoff);
                memcpy(fixed_buf, img->buf + try, imgoff);
-               for (i = 0; i < preimage->nr; i++)
-                       preimage->line[i].len = img->line[try_lno+i].len;
-
-               /*
-                * Update the preimage buffer and the postimage context lines.
-                */
                update_pre_post_images(preimage, postimage,
                                fixed_buf, imgoff, postlen);
                return 1;
@@ -1995,11 +1997,8 @@ static int find_pos(struct image *img,
        unsigned long backwards, forwards, try;
        int backwards_lno, forwards_lno, try_lno;
 
-       if (preimage->nr > img->nr)
-               return -1;
-
        /*
-        * If match_begining or match_end is specified, there is no
+        * If match_beginning or match_end is specified, there is no
         * point starting from a wrong line that will never match and
         * wander around and wait for a match at the specified end.
         */
@@ -2008,7 +2007,12 @@ static int find_pos(struct image *img,
        else if (match_end)
                line = img->nr - preimage->nr;
 
-       if (line > img->nr)
+       /*
+        * Because the comparison is unsigned, the following test
+        * will also take care of a negative line number that can
+        * result when match_end and preimage is larger than the target.
+        */
+       if ((size_t) line > img->nr)
                line = img->nr;
 
        try = 0;
@@ -2666,7 +2670,7 @@ static int verify_index_match(struct cache_entry *ce, struct stat *st)
                        return -1;
                return 0;
        }
-       return ce_match_stat(ce, st, CE_MATCH_IGNORE_VALID);
+       return ce_match_stat(ce, st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE);
 }
 
 static int check_preimage(struct patch *patch, struct cache_entry **ce, struct stat *st)