Merge branch 'jk/maint-null-in-trees'
[gitweb.git] / builtin / apply.c
index 51b695bac8f16691bed381ede764b3faaec6fddb..3bf71dc4522564463945673aaba0501a51141bb2 100644 (file)
@@ -16,6 +16,9 @@
 #include "dir.h"
 #include "diff.h"
 #include "parse-options.h"
+#include "xdiff-interface.h"
+#include "ll-merge.h"
+#include "rerere.h"
 
 /*
  *  --check turns on checking that the working tree matches the
@@ -51,7 +54,7 @@ static const char *fake_ancestor;
 static int line_termination = '\n';
 static unsigned int p_context = UINT_MAX;
 static const char * const apply_usage[] = {
-       "git apply [options] [<patch>...]",
+       N_("git apply [options] [<patch>...]"),
        NULL
 };
 
@@ -185,7 +188,6 @@ struct patch {
        int is_new, is_delete;  /* -1 = unknown, 0 = false, 1 = true */
        int rejected;
        unsigned ws_rule;
-       unsigned long deflate_origlen;
        int lines_added, lines_deleted;
        int score;
        unsigned int is_toplevel_relative:1;
@@ -194,12 +196,17 @@ struct patch {
        unsigned int is_copy:1;
        unsigned int is_rename:1;
        unsigned int recount:1;
+       unsigned int conflicted_threeway:1;
+       unsigned int direct_to_threeway:1;
        struct fragment *fragments;
        char *result;
        size_t resultsize;
        char old_sha1_prefix[41];
        char new_sha1_prefix[41];
        struct patch *next;
+
+       /* three-way fallback result */
+       unsigned char threeway_stage[3][20];
 };
 
 static void free_fragment_list(struct fragment *list)
@@ -920,7 +927,10 @@ static int gitdiff_hdrend(const char *line, struct patch *patch)
  * their names against any previous information, just
  * to make sure..
  */
-static char *gitdiff_verify_name(const char *line, int isnull, char *orig_name, const char *oldnew)
+#define DIFF_OLD_NAME 0
+#define DIFF_NEW_NAME 1
+
+static char *gitdiff_verify_name(const char *line, int isnull, char *orig_name, int side)
 {
        if (!orig_name && !isnull)
                return find_name(line, NULL, p_value, TERM_TAB);
@@ -935,7 +945,9 @@ static char *gitdiff_verify_name(const char *line, int isnull, char *orig_name,
                        die(_("git apply: bad git-diff - expected /dev/null, got %s on line %d"), name, linenr);
                another = find_name(line, NULL, p_value, TERM_TAB);
                if (!another || memcmp(another, name, len + 1))
-                       die(_("git apply: bad git-diff - inconsistent %s filename on line %d"), oldnew, linenr);
+                       die((side == DIFF_NEW_NAME) ?
+                           _("git apply: bad git-diff - inconsistent new filename on line %d") :
+                           _("git apply: bad git-diff - inconsistent old filename on line %d"), linenr);
                free(another);
                return orig_name;
        }
@@ -950,7 +962,8 @@ static char *gitdiff_verify_name(const char *line, int isnull, char *orig_name,
 static int gitdiff_oldname(const char *line, struct patch *patch)
 {
        char *orig = patch->old_name;
-       patch->old_name = gitdiff_verify_name(line, patch->is_new, patch->old_name, "old");
+       patch->old_name = gitdiff_verify_name(line, patch->is_new, patch->old_name,
+                                             DIFF_OLD_NAME);
        if (orig != patch->old_name)
                free(orig);
        return 0;
@@ -959,7 +972,8 @@ static int gitdiff_oldname(const char *line, struct patch *patch)
 static int gitdiff_newname(const char *line, struct patch *patch)
 {
        char *orig = patch->new_name;
-       patch->new_name = gitdiff_verify_name(line, patch->is_delete, patch->new_name, "new");
+       patch->new_name = gitdiff_verify_name(line, patch->is_delete, patch->new_name,
+                                             DIFF_NEW_NAME);
        if (orig != patch->new_name)
                free(orig);
        return 0;
@@ -3068,6 +3082,16 @@ static struct patch *previous_patch(struct patch *patch, int *gone)
        return previous;
 }
 
+static int verify_index_match(struct cache_entry *ce, struct stat *st)
+{
+       if (S_ISGITLINK(ce->ce_mode)) {
+               if (!S_ISDIR(st->st_mode))
+                       return -1;
+               return 0;
+       }
+       return ce_match_stat(ce, st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE);
+}
+
 #define SUBMODULE_PATCH_WITHOUT_INDEX 1
 
 static int load_patch_target(struct strbuf *buf,
@@ -3140,10 +3164,148 @@ static int load_preimage(struct image *image,
        return 0;
 }
 
+static int three_way_merge(struct image *image,
+                          char *path,
+                          const unsigned char *base,
+                          const unsigned char *ours,
+                          const unsigned char *theirs)
+{
+       mmfile_t base_file, our_file, their_file;
+       mmbuffer_t result = { NULL };
+       int status;
+
+       read_mmblob(&base_file, base);
+       read_mmblob(&our_file, ours);
+       read_mmblob(&their_file, theirs);
+       status = ll_merge(&result, path,
+                         &base_file, "base",
+                         &our_file, "ours",
+                         &their_file, "theirs", NULL);
+       free(base_file.ptr);
+       free(our_file.ptr);
+       free(their_file.ptr);
+       if (status < 0 || !result.ptr) {
+               free(result.ptr);
+               return -1;
+       }
+       clear_image(image);
+       image->buf = result.ptr;
+       image->len = result.size;
+
+       return status;
+}
+
+/*
+ * When directly falling back to add/add three-way merge, we read from
+ * the current contents of the new_name.  In no cases other than that
+ * this function will be called.
+ */
+static int load_current(struct image *image, struct patch *patch)
+{
+       struct strbuf buf = STRBUF_INIT;
+       int status, pos;
+       size_t len;
+       char *img;
+       struct stat st;
+       struct cache_entry *ce;
+       char *name = patch->new_name;
+       unsigned mode = patch->new_mode;
+
+       if (!patch->is_new)
+               die("BUG: patch to %s is not a creation", patch->old_name);
+
+       pos = cache_name_pos(name, strlen(name));
+       if (pos < 0)
+               return error(_("%s: does not exist in index"), name);
+       ce = active_cache[pos];
+       if (lstat(name, &st)) {
+               if (errno != ENOENT)
+                       return error(_("%s: %s"), name, strerror(errno));
+               if (checkout_target(ce, &st))
+                       return -1;
+       }
+       if (verify_index_match(ce, &st))
+               return error(_("%s: does not match index"), name);
+
+       status = load_patch_target(&buf, ce, &st, name, mode);
+       if (status < 0)
+               return status;
+       else if (status)
+               return -1;
+       img = strbuf_detach(&buf, &len);
+       prepare_image(image, img, len, !patch->is_binary);
+       return 0;
+}
+
 static int try_threeway(struct image *image, struct patch *patch,
                        struct stat *st, struct cache_entry *ce)
 {
-       return -1; /* for now */
+       unsigned char pre_sha1[20], post_sha1[20], our_sha1[20];
+       struct strbuf buf = STRBUF_INIT;
+       size_t len;
+       int status;
+       char *img;
+       struct image tmp_image;
+
+       /* No point falling back to 3-way merge in these cases */
+       if (patch->is_delete ||
+           S_ISGITLINK(patch->old_mode) || S_ISGITLINK(patch->new_mode))
+               return -1;
+
+       /* Preimage the patch was prepared for */
+       if (patch->is_new)
+               write_sha1_file("", 0, blob_type, pre_sha1);
+       else if (get_sha1(patch->old_sha1_prefix, pre_sha1) ||
+                read_blob_object(&buf, pre_sha1, patch->old_mode))
+               return error("repository lacks the necessary blob to fall back on 3-way merge.");
+
+       fprintf(stderr, "Falling back to three-way merge...\n");
+
+       img = strbuf_detach(&buf, &len);
+       prepare_image(&tmp_image, img, len, 1);
+       /* Apply the patch to get the post image */
+       if (apply_fragments(&tmp_image, patch) < 0) {
+               clear_image(&tmp_image);
+               return -1;
+       }
+       /* post_sha1[] is theirs */
+       write_sha1_file(tmp_image.buf, tmp_image.len, blob_type, post_sha1);
+       clear_image(&tmp_image);
+
+       /* our_sha1[] is ours */
+       if (patch->is_new) {
+               if (load_current(&tmp_image, patch))
+                       return error("cannot read the current contents of '%s'",
+                                    patch->new_name);
+       } else {
+               if (load_preimage(&tmp_image, patch, st, ce))
+                       return error("cannot read the current contents of '%s'",
+                                    patch->old_name);
+       }
+       write_sha1_file(tmp_image.buf, tmp_image.len, blob_type, our_sha1);
+       clear_image(&tmp_image);
+
+       /* in-core three-way merge between post and our using pre as base */
+       status = three_way_merge(image, patch->new_name,
+                                pre_sha1, our_sha1, post_sha1);
+       if (status < 0) {
+               fprintf(stderr, "Failed to fall back on three-way merge...\n");
+               return status;
+       }
+
+       if (status) {
+               patch->conflicted_threeway = 1;
+               if (patch->is_new)
+                       hashclr(patch->threeway_stage[0]);
+               else
+                       hashcpy(patch->threeway_stage[0], pre_sha1);
+               hashcpy(patch->threeway_stage[1], our_sha1);
+               hashcpy(patch->threeway_stage[2], post_sha1);
+               fprintf(stderr, "Applied patch to '%s' with conflicts.\n", patch->new_name);
+       } else {
+               fprintf(stderr, "Applied patch to '%s' cleanly.\n", patch->new_name);
+       }
+       return 0;
 }
 
 static int apply_data(struct patch *patch, struct stat *st, struct cache_entry *ce)
@@ -3153,7 +3315,8 @@ static int apply_data(struct patch *patch, struct stat *st, struct cache_entry *
        if (load_preimage(&image, patch, st, ce) < 0)
                return -1;
 
-       if (apply_fragments(&image, patch) < 0) {
+       if (patch->direct_to_threeway ||
+           apply_fragments(&image, patch) < 0) {
                /* Note: with --reject, apply_fragments() returns 0 */
                if (!threeway || try_threeway(&image, patch, st, ce) < 0)
                        return -1;
@@ -3169,16 +3332,6 @@ static int apply_data(struct patch *patch, struct stat *st, struct cache_entry *
        return 0;
 }
 
-static int verify_index_match(struct cache_entry *ce, struct stat *st)
-{
-       if (S_ISGITLINK(ce->ce_mode)) {
-               if (!S_ISDIR(st->st_mode))
-                       return -1;
-               return 0;
-       }
-       return ce_match_stat(ce, st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE);
-}
-
 /*
  * If "patch" that we are looking at modifies or deletes what we have,
  * we would want it not to lose any local modification we have, either
@@ -3340,7 +3493,9 @@ static int check_patch(struct patch *patch)
            ((0 < patch->is_new) | (0 < patch->is_rename) | patch->is_copy)) {
                int err = check_to_create(new_name, ok_if_exists);
 
-               switch (err) {
+               if (err && threeway) {
+                       patch->direct_to_threeway = 1;
+               } else switch (err) {
                case 0:
                        break; /* happy */
                case EXISTS_IN_INDEX:
@@ -3365,10 +3520,18 @@ static int check_patch(struct patch *patch)
                int same = !strcmp(old_name, new_name);
                if (!patch->new_mode)
                        patch->new_mode = patch->old_mode;
-               if ((patch->old_mode ^ patch->new_mode) & S_IFMT)
-                       return error(_("new mode (%o) of %s does not match old mode (%o)%s%s"),
-                               patch->new_mode, new_name, patch->old_mode,
-                               same ? "" : " of ", same ? "" : old_name);
+               if ((patch->old_mode ^ patch->new_mode) & S_IFMT) {
+                       if (same)
+                               return error(_("new mode (%o) of %s does not "
+                                              "match old mode (%o)"),
+                                       patch->new_mode, new_name,
+                                       patch->old_mode);
+                       else
+                               return error(_("new mode (%o) of %s does not "
+                                              "match old mode (%o) of %s"),
+                                       patch->new_mode, new_name,
+                                       patch->old_mode, old_name);
+               }
        }
 
        if (apply_data(patch, &st, ce) < 0)
@@ -3425,7 +3588,7 @@ static void build_fake_ancestor(struct patch *list, const char *filename)
                name = patch->old_name ? patch->old_name : patch->new_name;
                if (0 < patch->is_new)
                        continue;
-               else if (get_sha1(patch->old_sha1_prefix, sha1))
+               else if (get_sha1_blob(patch->old_sha1_prefix, sha1))
                        /* git diff has no index line for mode/type changes */
                        if (!patch->lines_added && !patch->lines_deleted) {
                                if (get_current_sha1(patch->old_name, sha1))
@@ -3605,7 +3768,8 @@ static void add_index_file(const char *path, unsigned mode, void *buf, unsigned
        ce = xcalloc(1, ce_size);
        memcpy(ce->name, path, namelen);
        ce->ce_mode = create_ce_mode(mode);
-       ce->ce_flags = namelen;
+       ce->ce_flags = create_ce_flags(0);
+       ce->ce_namelen = namelen;
        if (S_ISGITLINK(mode)) {
                const char *s = buf;
 
@@ -3707,6 +3871,33 @@ static void create_one_file(char *path, unsigned mode, const char *buf, unsigned
        die_errno(_("unable to write file '%s' mode %o"), path, mode);
 }
 
+static void add_conflicted_stages_file(struct patch *patch)
+{
+       int stage, namelen;
+       unsigned ce_size, mode;
+       struct cache_entry *ce;
+
+       if (!update_index)
+               return;
+       namelen = strlen(patch->new_name);
+       ce_size = cache_entry_size(namelen);
+       mode = patch->new_mode ? patch->new_mode : (S_IFREG | 0644);
+
+       remove_file_from_cache(patch->new_name);
+       for (stage = 1; stage < 4; stage++) {
+               if (is_null_sha1(patch->threeway_stage[stage - 1]))
+                       continue;
+               ce = xcalloc(1, ce_size);
+               memcpy(ce->name, patch->new_name, namelen);
+               ce->ce_mode = create_ce_mode(mode);
+               ce->ce_flags = create_ce_flags(stage);
+               ce->ce_namelen = namelen;
+               hashcpy(ce->sha1, patch->threeway_stage[stage - 1]);
+               if (add_cache_entry(ce, ADD_CACHE_OK_TO_ADD) < 0)
+                       die(_("unable to add cache entry for %s"), patch->new_name);
+       }
+}
+
 static void create_file(struct patch *patch)
 {
        char *path = patch->new_name;
@@ -3717,7 +3908,11 @@ static void create_file(struct patch *patch)
        if (!mode)
                mode = S_IFREG | 0644;
        create_one_file(path, mode, buf, size);
-       add_index_file(path, mode, buf, size);
+
+       if (patch->conflicted_threeway)
+               add_conflicted_stages_file(patch);
+       else
+               add_index_file(path, mode, buf, size);
 }
 
 /* phase zero is to remove, phase one is to create */
@@ -3819,6 +4014,7 @@ static int write_out_results(struct patch *list)
        int phase;
        int errs = 0;
        struct patch *l;
+       struct string_list cpath = STRING_LIST_INIT_DUP;
 
        for (phase = 0; phase < 2; phase++) {
                l = list;
@@ -3827,12 +4023,30 @@ static int write_out_results(struct patch *list)
                                errs = 1;
                        else {
                                write_out_one_result(l, phase);
-                               if (phase == 1 && write_out_one_reject(l))
-                                       errs = 1;
+                               if (phase == 1) {
+                                       if (write_out_one_reject(l))
+                                               errs = 1;
+                                       if (l->conflicted_threeway) {
+                                               string_list_append(&cpath, l->new_name);
+                                               errs = 1;
+                                       }
+                               }
                        }
                        l = l->next;
                }
        }
+
+       if (cpath.nr) {
+               struct string_list_item *item;
+
+               sort_string_list(&cpath);
+               for_each_string_list_item(item, &cpath)
+                       fprintf(stderr, "U %s\n", item->string);
+               string_list_clear(&cpath, 0);
+
+               rerere(0);
+       }
+
        return errs;
 }
 
@@ -3955,8 +4169,12 @@ static int apply_patch(int fd, const char *filename, int options)
            !apply_with_reject)
                exit(1);
 
-       if (apply && write_out_results(list))
-               exit(1);
+       if (apply && write_out_results(list)) {
+               if (apply_with_reject)
+                       exit(1);
+               /* with --3way, we still need to write the index out */
+               return 1;
+       }
 
        if (fake_ancestor)
                build_fake_ancestor(list, fake_ancestor);
@@ -4062,68 +4280,68 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
        const char *whitespace_option = NULL;
 
        struct option builtin_apply_options[] = {
-               { OPTION_CALLBACK, 0, "exclude", NULL, "path",
-                       "don't apply changes matching the given path",
+               { OPTION_CALLBACK, 0, "exclude", NULL, N_("path"),
+                       N_("don't apply changes matching the given path"),
                        0, option_parse_exclude },
-               { OPTION_CALLBACK, 0, "include", NULL, "path",
-                       "apply changes matching the given path",
+               { OPTION_CALLBACK, 0, "include", NULL, N_("path"),
+                       N_("apply changes matching the given path"),
                        0, option_parse_include },
-               { OPTION_CALLBACK, 'p', NULL, NULL, "num",
-                       "remove <num> leading slashes from traditional diff paths",
+               { OPTION_CALLBACK, 'p', NULL, NULL, N_("num"),
+                       N_("remove <num> leading slashes from traditional diff paths"),
                        0, option_parse_p },
                OPT_BOOLEAN(0, "no-add", &no_add,
-                       "ignore additions made by the patch"),
+                       N_("ignore additions made by the patch")),
                OPT_BOOLEAN(0, "stat", &diffstat,
-                       "instead of applying the patch, output diffstat for the input"),
+                       N_("instead of applying the patch, output diffstat for the input")),
                OPT_NOOP_NOARG(0, "allow-binary-replacement"),
                OPT_NOOP_NOARG(0, "binary"),
                OPT_BOOLEAN(0, "numstat", &numstat,
-                       "shows number of added and deleted lines in decimal notation"),
+                       N_("shows number of added and deleted lines in decimal notation")),
                OPT_BOOLEAN(0, "summary", &summary,
-                       "instead of applying the patch, output a summary for the input"),
+                       N_("instead of applying the patch, output a summary for the input")),
                OPT_BOOLEAN(0, "check", &check,
-                       "instead of applying the patch, see if the patch is applicable"),
+                       N_("instead of applying the patch, see if the patch is applicable")),
                OPT_BOOLEAN(0, "index", &check_index,
-                       "make sure the patch is applicable to the current index"),
+                       N_("make sure the patch is applicable to the current index")),
                OPT_BOOLEAN(0, "cached", &cached,
-                       "apply a patch without touching the working tree"),
+                       N_("apply a patch without touching the working tree")),
                OPT_BOOLEAN(0, "apply", &force_apply,
-                       "also apply the patch (use with --stat/--summary/--check)"),
+                       N_("also apply the patch (use with --stat/--summary/--check)")),
                OPT_BOOL('3', "3way", &threeway,
-                        "attempt three-way merge if a patch does not apply"),
+                        N_( "attempt three-way merge if a patch does not apply")),
                OPT_FILENAME(0, "build-fake-ancestor", &fake_ancestor,
-                       "build a temporary index based on embedded index information"),
+                       N_("build a temporary index based on embedded index information")),
                { OPTION_CALLBACK, 'z', NULL, NULL, NULL,
-                       "paths are separated with NUL character",
+                       N_("paths are separated with NUL character"),
                        PARSE_OPT_NOARG, option_parse_z },
                OPT_INTEGER('C', NULL, &p_context,
-                               "ensure at least <n> lines of context match"),
-               { OPTION_CALLBACK, 0, "whitespace", &whitespace_option, "action",
-                       "detect new or modified lines that have whitespace errors",
+                               N_("ensure at least <n> lines of context match")),
+               { OPTION_CALLBACK, 0, "whitespace", &whitespace_option, N_("action"),
+                       N_("detect new or modified lines that have whitespace errors"),
                        0, option_parse_whitespace },
                { OPTION_CALLBACK, 0, "ignore-space-change", NULL, NULL,
-                       "ignore changes in whitespace when finding context",
+                       N_("ignore changes in whitespace when finding context"),
                        PARSE_OPT_NOARG, option_parse_space_change },
                { OPTION_CALLBACK, 0, "ignore-whitespace", NULL, NULL,
-                       "ignore changes in whitespace when finding context",
+                       N_("ignore changes in whitespace when finding context"),
                        PARSE_OPT_NOARG, option_parse_space_change },
                OPT_BOOLEAN('R', "reverse", &apply_in_reverse,
-                       "apply the patch in reverse"),
+                       N_("apply the patch in reverse")),
                OPT_BOOLEAN(0, "unidiff-zero", &unidiff_zero,
-                       "don't expect at least one line of context"),
+                       N_("don't expect at least one line of context")),
                OPT_BOOLEAN(0, "reject", &apply_with_reject,
-                       "leave the rejected hunks in corresponding *.rej files"),
+                       N_("leave the rejected hunks in corresponding *.rej files")),
                OPT_BOOLEAN(0, "allow-overlap", &allow_overlap,
-                       "allow overlapping hunks"),
-               OPT__VERBOSE(&apply_verbosely, "be verbose"),
+                       N_("allow overlapping hunks")),
+               OPT__VERBOSE(&apply_verbosely, N_("be verbose")),
                OPT_BIT(0, "inaccurate-eof", &options,
-                       "tolerate incorrectly detected missing new-line at the end of file",
+                       N_("tolerate incorrectly detected missing new-line at the end of file"),
                        INACCURATE_EOF),
                OPT_BIT(0, "recount", &options,
-                       "do not trust the line counts in the hunk headers",
+                       N_("do not trust the line counts in the hunk headers"),
                        RECOUNT),
-               { OPTION_CALLBACK, 0, "directory", NULL, "root",
-                       "prepend <root> to all filenames",
+               { OPTION_CALLBACK, 0, "directory", NULL, N_("root"),
+                       N_("prepend <root> to all filenames"),
                        0, option_parse_directory },
                OPT_END()
        };