Code

Backup the array passed to fetch_pack so we can free items
[git.git] / builtin-apply.c
index 27a182bfaa826711b2e6c4c775fbd6e83b243f7e..86d89a4a7e9240a75e1500bba524acdcec7f14d2 100644 (file)
@@ -30,7 +30,7 @@ static int unidiff_zero;
 static int p_value = 1;
 static int p_value_known;
 static int check_index;
-static int write_index;
+static int update_index;
 static int cached;
 static int diffstat;
 static int numstat;
@@ -55,7 +55,7 @@ static enum whitespace_eol {
 } new_whitespace = warn_on_whitespace;
 static int whitespace_error;
 static int squelch_whitespace_errors = 5;
-static int applied_after_stripping;
+static int applied_after_fixing_ws;
 static const char *patch_input_file;
 
 static void parse_whitespace_option(const char *option)
@@ -185,7 +185,7 @@ static void *read_patch_file(int fd, unsigned long *sizep)
        void *buffer = xmalloc(alloc);
 
        for (;;) {
-               int nr = alloc - size;
+               ssize_t nr = alloc - size;
                if (nr < 1024) {
                        alloc += CHUNKSIZE;
                        buffer = xrealloc(buffer, alloc);
@@ -254,7 +254,7 @@ static char *find_name(const char *line, char *def, int p_value, int terminate)
                if (name) {
                        char *cp = name;
                        while (p_value) {
-                               cp = strchr(name, '/');
+                               cp = strchr(cp, '/');
                                if (!cp)
                                        break;
                                cp++;
@@ -417,7 +417,7 @@ static int gitdiff_hdrend(const char *line, struct patch *patch)
 static char *gitdiff_verify_name(const char *line, int isnull, char *orig_name, const char *oldnew)
 {
        if (!orig_name && !isnull)
-               return find_name(line, NULL, 1, TERM_TAB);
+               return find_name(line, NULL, p_value, TERM_TAB);
 
        if (orig_name) {
                int len;
@@ -427,7 +427,7 @@ static char *gitdiff_verify_name(const char *line, int isnull, char *orig_name,
                len = strlen(name);
                if (isnull)
                        die("git-apply: bad git-diff - expected /dev/null, got %s on line %d", name, linenr);
-               another = find_name(line, NULL, 1, TERM_TAB);
+               another = find_name(line, NULL, p_value, TERM_TAB);
                if (!another || memcmp(another, name, len))
                        die("git-apply: bad git-diff - inconsistent %s filename on line %d", oldnew, linenr);
                free(another);
@@ -1003,12 +1003,16 @@ static int parse_fragment(char *line, unsigned long size, struct patch *patch, s
                        trailing++;
                        break;
                case '-':
+                       if (apply_in_reverse &&
+                                       new_whitespace != nowarn_whitespace)
+                               check_whitespace(line, len);
                        deleted++;
                        oldlines--;
                        trailing = 0;
                        break;
                case '+':
-                       if (new_whitespace != nowarn_whitespace)
+                       if (!apply_in_reverse &&
+                                       new_whitespace != nowarn_whitespace)
                                check_whitespace(line, len);
                        added++;
                        newlines--;
@@ -1468,15 +1472,15 @@ static int read_old_data(struct stat *st, const char *path, char **buf_p, unsign
                        return error("unable to open %s", path);
                got = 0;
                for (;;) {
-                       int ret = xread(fd, buf + got, size - got);
+                       ssize_t ret = xread(fd, buf + got, size - got);
                        if (ret <= 0)
                                break;
                        got += ret;
                }
                close(fd);
                nsize = got;
-               nbuf = buf;
-               if (convert_to_git(path, &nbuf, &nsize)) {
+               nbuf = convert_to_git(path, buf, &nsize);
+               if (nbuf) {
                        free(buf);
                        *buf_p = nbuf;
                        *alloc_p = nsize;
@@ -1510,7 +1514,8 @@ static int find_offset(const char *buf, unsigned long size, const char *fragment
        }
 
        /* Exact line number? */
-       if (!memcmp(buf + start, fragment, fragsize))
+       if ((start + fragsize <= size) &&
+           !memcmp(buf + start, fragment, fragsize))
                return start;
 
        /*
@@ -1637,15 +1642,22 @@ static int apply_line(char *output, const char *patch, int plen)
 
        buf = output;
        if (need_fix_leading_space) {
+               int consecutive_spaces = 0;
                /* between patch[1..last_tab_in_indent] strip the
                 * funny spaces, updating them to tab as needed.
                 */
                for (i = 1; i < last_tab_in_indent; i++, plen--) {
                        char ch = patch[i];
-                       if (ch != ' ')
+                       if (ch != ' ') {
+                               consecutive_spaces = 0;
                                *output++ = ch;
-                       else if ((i % 8) == 0)
-                               *output++ = '\t';
+                       } else {
+                               consecutive_spaces++;
+                               if (consecutive_spaces == 8) {
+                                       *output++ = '\t';
+                                       consecutive_spaces = 0;
+                               }
+                       }
                }
                fixed = 1;
                i = last_tab_in_indent;
@@ -1657,7 +1669,7 @@ static int apply_line(char *output, const char *patch, int plen)
        if (add_nl_to_tail)
                output[plen++] = '\n';
        if (fixed)
-               applied_after_stripping++;
+               applied_after_fixing_ws++;
        return output + plen - buf;
 }
 
@@ -1671,6 +1683,7 @@ static int apply_one_fragment(struct buffer_desc *desc, struct fragment *frag, i
        char *new = xmalloc(size);
        const char *oldlines, *newlines;
        int oldsize = 0, newsize = 0;
+       int new_blank_lines_at_end = 0;
        unsigned long leading, trailing;
        int pos, lines;
 
@@ -1678,6 +1691,7 @@ static int apply_one_fragment(struct buffer_desc *desc, struct fragment *frag, i
                char first;
                int len = linelen(patch, size);
                int plen;
+               int added_blank_line = 0;
 
                if (!len)
                        break;
@@ -1699,6 +1713,7 @@ static int apply_one_fragment(struct buffer_desc *desc, struct fragment *frag, i
                        else if (first == '+')
                                first = '-';
                }
+
                switch (first) {
                case '\n':
                        /* Newer GNU diff, empty context line */
@@ -1716,9 +1731,14 @@ static int apply_one_fragment(struct buffer_desc *desc, struct fragment *frag, i
                                break;
                /* Fall-through for ' ' */
                case '+':
-                       if (first != '+' || !no_add)
-                               newsize += apply_line(new + newsize, patch,
-                                                     plen);
+                       if (first != '+' || !no_add) {
+                               int added = apply_line(new + newsize, patch,
+                                                      plen);
+                               newsize += added;
+                               if (first == '+' &&
+                                   added == 1 && new[newsize-1] == '\n')
+                                       added_blank_line = 1;
+                       }
                        break;
                case '@': case '\\':
                        /* Ignore it, we already handled it */
@@ -1728,6 +1748,10 @@ static int apply_one_fragment(struct buffer_desc *desc, struct fragment *frag, i
                                error("invalid start of line: '%c'", first);
                        return -1;
                }
+               if (added_blank_line)
+                       new_blank_lines_at_end++;
+               else
+                       new_blank_lines_at_end = 0;
                patch += len;
                size -= len;
        }
@@ -1770,9 +1794,16 @@ static int apply_one_fragment(struct buffer_desc *desc, struct fragment *frag, i
                if (match_beginning && offset)
                        offset = -1;
                if (offset >= 0) {
-                       int diff = newsize - oldsize;
-                       unsigned long size = desc->size + diff;
-                       unsigned long alloc = desc->alloc;
+                       int diff;
+                       unsigned long size, alloc;
+
+                       if (new_whitespace == strip_whitespace &&
+                           (desc->size - oldsize - offset == 0)) /* end of file? */
+                               newsize -= new_blank_lines_at_end;
+
+                       diff = newsize - oldsize;
+                       size = desc->size + diff;
+                       alloc = desc->alloc;
 
                        /* Warn if it was necessary to reduce the number
                         * of context lines.
@@ -1961,6 +1992,25 @@ static int apply_fragments(struct buffer_desc *desc, struct patch *patch)
        return 0;
 }
 
+static int read_file_or_gitlink(struct cache_entry *ce, char **buf_p,
+                               unsigned long *size_p)
+{
+       if (!ce)
+               return 0;
+
+       if (S_ISGITLINK(ntohl(ce->ce_mode))) {
+               *buf_p = xmalloc(100);
+               *size_p = snprintf(*buf_p, 100,
+                       "Subproject commit %s\n", sha1_to_hex(ce->sha1));
+       } else {
+               enum object_type type;
+               *buf_p = read_sha1_file(ce->sha1, &type, size_p);
+               if (!*buf_p)
+                       return -1;
+       }
+       return 0;
+}
+
 static int apply_data(struct patch *patch, struct stat *st, struct cache_entry *ce)
 {
        char *buf;
@@ -1971,22 +2021,32 @@ static int apply_data(struct patch *patch, struct stat *st, struct cache_entry *
        alloc = 0;
        buf = NULL;
        if (cached) {
-               if (ce) {
-                       enum object_type type;
-                       buf = read_sha1_file(ce->sha1, &type, &size);
-                       if (!buf)
+               if (read_file_or_gitlink(ce, &buf, &size))
+                       return error("read of %s failed", patch->old_name);
+               alloc = size;
+       } else if (patch->old_name) {
+               if (S_ISGITLINK(patch->old_mode)) {
+                       if (ce)
+                               read_file_or_gitlink(ce, &buf, &size);
+                       else {
+                               /*
+                                * There is no way to apply subproject
+                                * patch without looking at the index.
+                                */
+                               patch->fragments = NULL;
+                               size = 0;
+                       }
+               }
+               else {
+                       size = xsize_t(st->st_size);
+                       alloc = size + 8192;
+                       buf = xmalloc(alloc);
+                       if (read_old_data(st, patch->old_name,
+                                         &buf, &alloc, &size))
                                return error("read of %s failed",
                                             patch->old_name);
-                       alloc = size;
                }
        }
-       else if (patch->old_name) {
-               size = xsize_t(st->st_size);
-               alloc = size + 8192;
-               buf = xmalloc(alloc);
-               if (read_old_data(st, patch->old_name, &buf, &alloc, &size))
-                       return error("read of %s failed", patch->old_name);
-       }
 
        desc.size = size;
        desc.alloc = alloc;
@@ -2009,6 +2069,39 @@ static int apply_data(struct patch *patch, struct stat *st, struct cache_entry *
        return 0;
 }
 
+static int check_to_create_blob(const char *new_name, int ok_if_exists)
+{
+       struct stat nst;
+       if (!lstat(new_name, &nst)) {
+               if (S_ISDIR(nst.st_mode) || ok_if_exists)
+                       return 0;
+               /*
+                * A leading component of new_name might be a symlink
+                * that is going to be removed with this patch, but
+                * still pointing at somewhere that has the path.
+                * In such a case, path "new_name" does not exist as
+                * far as git is concerned.
+                */
+               if (has_symlink_leading_path(new_name, NULL))
+                       return 0;
+
+               return error("%s: already exists in working directory", new_name);
+       }
+       else if ((errno != ENOENT) && (errno != ENOTDIR))
+               return error("%s: %s", new_name, strerror(errno));
+       return 0;
+}
+
+static int verify_index_match(struct cache_entry *ce, struct stat *st)
+{
+       if (S_ISGITLINK(ntohl(ce->ce_mode))) {
+               if (!S_ISDIR(st->st_mode))
+                       return -1;
+               return 0;
+       }
+       return ce_match_stat(ce, st, 1);
+}
+
 static int check_patch(struct patch *patch, struct patch *prev_patch)
 {
        struct stat st;
@@ -2019,8 +2112,14 @@ static int check_patch(struct patch *patch, struct patch *prev_patch)
        int ok_if_exists;
 
        patch->rejected = 1; /* we will drop this after we succeed */
+
+       /*
+        * Make sure that we do not have local modifications from the
+        * index when we are looking at the index.  Also make sure
+        * we have the preimage file to be patched in the work tree,
+        * unless --cached, which tells git to apply only in the index.
+        */
        if (old_name) {
-               int changed = 0;
                int stat_ret = 0;
                unsigned st_mode = 0;
 
@@ -2050,15 +2149,12 @@ static int check_patch(struct patch *patch, struct patch *prev_patch)
                                    lstat(old_name, &st))
                                        return -1;
                        }
-                       if (!cached)
-                               changed = ce_match_stat(ce, &st, 1);
-                       if (changed)
+                       if (!cached && verify_index_match(ce, &st))
                                return error("%s: does not match index",
                                             old_name);
                        if (cached)
                                st_mode = ntohl(ce->ce_mode);
-               }
-               else if (stat_ret < 0)
+               } else if (stat_ret < 0)
                        return error("%s: %s", old_name, strerror(errno));
 
                if (!cached)
@@ -2095,15 +2191,9 @@ static int check_patch(struct patch *patch, struct patch *prev_patch)
                    !ok_if_exists)
                        return error("%s: already exists in index", new_name);
                if (!cached) {
-                       struct stat nst;
-                       if (!lstat(new_name, &nst)) {
-                               if (S_ISDIR(nst.st_mode) || ok_if_exists)
-                                       ; /* ok */
-                               else
-                                       return error("%s: already exists in working directory", new_name);
-                       }
-                       else if ((errno != ENOENT) && (errno != ENOTDIR))
-                               return error("%s: %s", new_name, strerror(errno));
+                       int err = check_to_create_blob(new_name, ok_if_exists);
+                       if (err)
+                               return err;
                }
                if (!patch->new_mode) {
                        if (0 < patch->is_new)
@@ -2144,6 +2234,20 @@ static int check_patch_list(struct patch *patch)
        return err;
 }
 
+/* This function tries to read the sha1 from the current index */
+static int get_current_sha1(const char *path, unsigned char *sha1)
+{
+       int pos;
+
+       if (read_cache() < 0)
+               return -1;
+       pos = cache_name_pos(path, strlen(path));
+       if (pos < 0)
+               return -1;
+       hashcpy(sha1, active_cache[pos]->sha1);
+       return 0;
+}
+
 static void show_index_list(struct patch *list)
 {
        struct patch *patch;
@@ -2160,8 +2264,16 @@ static void show_index_list(struct patch *list)
                if (0 < patch->is_new)
                        sha1_ptr = null_sha1;
                else if (get_sha1(patch->old_sha1_prefix, sha1))
-                       die("sha1 information is lacking or useless (%s).",
-                           name);
+                       /* git diff has no index line for mode/type changes */
+                       if (!patch->lines_added && !patch->lines_deleted) {
+                               if (get_current_sha1(patch->new_name, sha1) ||
+                                   get_current_sha1(patch->old_name, sha1))
+                                       die("mode change for %s, which is not "
+                                               "in current HEAD", name);
+                               sha1_ptr = sha1;
+                       } else
+                               die("sha1 information is lacking or useless "
+                                       "(%s).", name);
                else
                        sha1_ptr = sha1;
 
@@ -2308,13 +2420,16 @@ static void patch_stats(struct patch *patch)
 
 static void remove_file(struct patch *patch, int rmdir_empty)
 {
-       if (write_index) {
+       if (update_index) {
                if (remove_file_from_cache(patch->old_name) < 0)
                        die("unable to remove %s from index", patch->old_name);
-               cache_tree_invalidate_path(active_cache_tree, patch->old_name);
        }
        if (!cached) {
-               if (!unlink(patch->old_name) && rmdir_empty) {
+               if (S_ISGITLINK(patch->old_mode)) {
+                       if (rmdir(patch->old_name))
+                               warning("unable to remove submodule %s",
+                                       patch->old_name);
+               } else if (!unlink(patch->old_name) && rmdir_empty) {
                        char *name = xstrdup(patch->old_name);
                        char *end = strrchr(name, '/');
                        while (end) {
@@ -2335,29 +2450,43 @@ static void add_index_file(const char *path, unsigned mode, void *buf, unsigned
        int namelen = strlen(path);
        unsigned ce_size = cache_entry_size(namelen);
 
-       if (!write_index)
+       if (!update_index)
                return;
 
        ce = xcalloc(1, ce_size);
        memcpy(ce->name, path, namelen);
        ce->ce_mode = create_ce_mode(mode);
        ce->ce_flags = htons(namelen);
-       if (!cached) {
-               if (lstat(path, &st) < 0)
-                       die("unable to stat newly created file %s", path);
-               fill_stat_cache_info(ce, &st);
+       if (S_ISGITLINK(mode)) {
+               const char *s = buf;
+
+               if (get_sha1_hex(s + strlen("Subproject commit "), ce->sha1))
+                       die("corrupt patch for subproject %s", path);
+       } else {
+               if (!cached) {
+                       if (lstat(path, &st) < 0)
+                               die("unable to stat newly created file %s",
+                                   path);
+                       fill_stat_cache_info(ce, &st);
+               }
+               if (write_sha1_file(buf, size, blob_type, ce->sha1) < 0)
+                       die("unable to create backing store for newly created file %s", path);
        }
-       if (write_sha1_file(buf, size, blob_type, ce->sha1) < 0)
-               die("unable to create backing store for newly created file %s", path);
        if (add_cache_entry(ce, ADD_CACHE_OK_TO_ADD) < 0)
                die("unable to add cache entry for %s", path);
 }
 
 static int try_create_file(const char *path, unsigned int mode, const char *buf, unsigned long size)
 {
-       int fd, converted;
+       int fd;
        char *nbuf;
-       unsigned long nsize;
+
+       if (S_ISGITLINK(mode)) {
+               struct stat st;
+               if (!lstat(path, &st) && S_ISDIR(st.st_mode))
+                       return 0;
+               return mkdir(path, 0777);
+       }
 
        if (has_symlinks && S_ISLNK(mode))
                /* Although buf:size is counted string, it also is NUL
@@ -2369,13 +2498,10 @@ static int try_create_file(const char *path, unsigned int mode, const char *buf,
        if (fd < 0)
                return -1;
 
-       nsize = size;
-       nbuf = (char *) buf;
-       converted = convert_to_working_tree(path, &nbuf, &nsize);
-       if (converted) {
+       nbuf = convert_to_working_tree(path, buf, &size);
+       if (nbuf)
                buf = nbuf;
-               size = nsize;
-       }
+
        while (size) {
                int written = xwrite(fd, buf, size);
                if (written < 0)
@@ -2387,7 +2513,7 @@ static int try_create_file(const char *path, unsigned int mode, const char *buf,
        }
        if (close(fd) < 0)
                die("closing file %s: %s", path, strerror(errno));
-       if (converted)
+       if (nbuf)
                free(nbuf);
        return 0;
 }
@@ -2416,8 +2542,7 @@ static void create_one_file(char *path, unsigned mode, const char *buf, unsigned
                 * used to be.
                 */
                struct stat st;
-               errno = 0;
-               if (!lstat(path, &st) && S_ISDIR(st.st_mode) && !rmdir(path))
+               if (!lstat(path, &st) && (!S_ISDIR(st.st_mode) || !rmdir(path)))
                        errno = EEXIST;
        }
 
@@ -2452,7 +2577,6 @@ static void create_file(struct patch *patch)
                mode = S_IFREG | 0644;
        create_one_file(path, mode, buf, size);
        add_index_file(path, mode, buf, size);
-       cache_tree_invalidate_path(active_cache_tree, path);
 }
 
 /* phase zero is to remove, phase one is to create */
@@ -2473,7 +2597,7 @@ static void write_out_one_result(struct patch *patch, int phase)
         * thing: remove the old, write the new
         */
        if (phase == 0)
-               remove_file(patch, 0);
+               remove_file(patch, patch->is_rename);
        if (phase == 1)
                create_file(patch);
 }
@@ -2662,10 +2786,10 @@ static int apply_patch(int fd, const char *filename, int inaccurate_eof)
        if (whitespace_error && (new_whitespace == error_on_whitespace))
                apply = 0;
 
-       write_index = check_index && apply;
-       if (write_index && newfd < 0)
-               newfd = hold_lock_file_for_update(&lock_file,
-                                                 get_index_file(), 1);
+       update_index = check_index && apply;
+       if (update_index && newfd < 0)
+               newfd = hold_locked_index(&lock_file, 1);
+
        if (check_index) {
                if (read_cache() < 0)
                        die("unable to read index file");
@@ -2853,26 +2977,25 @@ int cmd_apply(int argc, const char **argv, const char *unused_prefix)
                                squelched == 1 ? "" : "s");
                }
                if (new_whitespace == error_on_whitespace)
-                       die("%d line%s add%s trailing whitespaces.",
+                       die("%d line%s add%s whitespace errors.",
                            whitespace_error,
                            whitespace_error == 1 ? "" : "s",
                            whitespace_error == 1 ? "s" : "");
-               if (applied_after_stripping)
+               if (applied_after_fixing_ws)
                        fprintf(stderr, "warning: %d line%s applied after"
-                               " stripping trailing whitespaces.\n",
-                               applied_after_stripping,
-                               applied_after_stripping == 1 ? "" : "s");
+                               " fixing whitespace errors.\n",
+                               applied_after_fixing_ws,
+                               applied_after_fixing_ws == 1 ? "" : "s");
                else if (whitespace_error)
-                       fprintf(stderr, "warning: %d line%s add%s trailing"
-                               " whitespaces.\n",
+                       fprintf(stderr, "warning: %d line%s add%s whitespace errors.\n",
                                whitespace_error,
                                whitespace_error == 1 ? "" : "s",
                                whitespace_error == 1 ? "s" : "");
        }
 
-       if (write_index) {
+       if (update_index) {
                if (write_cache(newfd, active_cache, active_nr) ||
-                   close(newfd) || commit_lock_file(&lock_file))
+                   close(newfd) || commit_locked_index(&lock_file))
                        die("Unable to write new index file");
        }