]> asedeno.scripts.mit.edu Git - git.git/blobdiff - builtin-apply.c
Allow fetching references from any namespace
[git.git] / builtin-apply.c
index b84d747e3f3117570db5a2089c2abe67a611cb02..db5272245569f4080a07cdb3a2aacd2c0cbda38c 100644 (file)
@@ -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);
@@ -1912,11 +1912,11 @@ static int apply_binary(struct buffer_desc *desc, struct patch *patch)
 
        if (has_sha1_file(sha1)) {
                /* We already have the postimage */
-               char type[10];
+               enum object_type type;
                unsigned long size;
 
                free(desc->buffer);
-               desc->buffer = read_sha1_file(sha1, type, &size);
+               desc->buffer = read_sha1_file(sha1, &type, &size);
                if (!desc->buffer)
                        return error("the necessary postimage %s for "
                                     "'%s' cannot be read",
@@ -1972,8 +1972,8 @@ static int apply_data(struct patch *patch, struct stat *st, struct cache_entry *
        buf = NULL;
        if (cached) {
                if (ce) {
-                       char type[20];
-                       buf = read_sha1_file(ce->sha1, type, &size);
+                       enum object_type type;
+                       buf = read_sha1_file(ce->sha1, &type, &size);
                        if (!buf)
                                return error("read of %s failed",
                                             patch->old_name);
@@ -1981,7 +1981,7 @@ static int apply_data(struct patch *patch, struct stat *st, struct cache_entry *
                }
        }
        else if (patch->old_name) {
-               size = st->st_size;
+               size = xsize_t(st->st_size);
                alloc = size + 8192;
                buf = xmalloc(alloc);
                if (read_old_data(st, patch->old_name, &buf, &alloc, &size))
@@ -2355,26 +2355,27 @@ static void add_index_file(const char *path, unsigned mode, void *buf, unsigned
 
 static int try_create_file(const char *path, unsigned int mode, const char *buf, unsigned long size)
 {
-       int fd;
+       int fd, converted;
        char *nbuf;
        unsigned long nsize;
 
-       if (S_ISLNK(mode))
+       if (has_symlinks && S_ISLNK(mode))
                /* Although buf:size is counted string, it also is NUL
                 * terminated.
                 */
                return symlink(buf, path);
+
+       fd = open(path, O_CREAT | O_EXCL | O_WRONLY, (mode & 0100) ? 0777 : 0666);
+       if (fd < 0)
+               return -1;
+
        nsize = size;
        nbuf = (char *) buf;
-       if (convert_to_working_tree(path, &nbuf, &nsize)) {
-               free((char *) buf);
+       converted = convert_to_working_tree(path, &nbuf, &nsize);
+       if (converted) {
                buf = nbuf;
                size = nsize;
        }
-
-       fd = open(path, O_CREAT | O_EXCL | O_WRONLY, (mode & 0100) ? 0777 : 0666);
-       if (fd < 0)
-               return -1;
        while (size) {
                int written = xwrite(fd, buf, size);
                if (written < 0)
@@ -2386,6 +2387,8 @@ 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)
+               free(nbuf);
        return 0;
 }
 
@@ -2413,8 +2416,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;
        }