pathspec: small readability changes
[gitweb.git] / apply.c
diff --git a/apply.c b/apply.c
index 74b450e1e4cbf191aaf397a526fd5447c104a2eb..705cf562f07098aafcd9f6e27105d2105714751f 100644 (file)
--- a/apply.c
+++ b/apply.c
@@ -112,14 +112,19 @@ void clear_apply_state(struct apply_state *state)
        /* &state->fn_table is cleared at the end of apply_patch() */
 }
 
+static void mute_routine(const char *msg, va_list params)
+{
+       /* do nothing */
+}
+
 int check_apply_state(struct apply_state *state, int force_apply)
 {
        int is_not_gitdir = !startup_info->have_repository;
 
        if (state->apply_with_reject && state->threeway)
-               return error("--reject and --3way cannot be used together.");
+               return error(_("--reject and --3way cannot be used together."));
        if (state->cached && state->threeway)
-               return error("--cached and --3way cannot be used together.");
+               return error(_("--cached and --3way cannot be used together."));
        if (state->threeway) {
                if (is_not_gitdir)
                        return error(_("--3way outside a repository"));
@@ -144,6 +149,13 @@ int check_apply_state(struct apply_state *state, int force_apply)
        if (!state->lock_file)
                return error("BUG: state->lock_file should not be NULL");
 
+       if (state->apply_verbosity <= verbosity_silent) {
+               state->saved_error_routine = get_error_routine();
+               state->saved_warn_routine = get_warn_routine();
+               set_error_routine(mute_routine);
+               set_warn_routine(mute_routine);
+       }
+
        return 0;
 }
 
@@ -1574,8 +1586,8 @@ static int find_header(struct apply_state *state,
                                patch->new_name = xstrdup(patch->def_name);
                        }
                        if (!patch->is_delete && !patch->new_name) {
-                               error("git diff header lacks filename information "
-                                            "(line %d)", state->linenr);
+                               error(_("git diff header lacks filename information "
+                                            "(line %d)"), state->linenr);
                                return -128;
                        }
                        patch->is_toplevel_relative = 1;
@@ -3083,8 +3095,8 @@ static int apply_binary_fragment(struct apply_state *state,
        /* Binary patch is irreversible without the optional second hunk */
        if (state->apply_in_reverse) {
                if (!fragment->next)
-                       return error("cannot reverse-apply a binary patch "
-                                    "without the reverse hunk to '%s'",
+                       return error(_("cannot reverse-apply a binary patch "
+                                      "without the reverse hunk to '%s'"),
                                     patch->new_name
                                     ? patch->new_name : patch->old_name);
                fragment = fragment->next;
@@ -3119,7 +3131,7 @@ static int apply_binary(struct apply_state *state,
                        struct patch *patch)
 {
        const char *name = patch->old_name ? patch->old_name : patch->new_name;
-       unsigned char sha1[20];
+       struct object_id oid;
 
        /*
         * For safety, we require patch index line to contain
@@ -3127,46 +3139,46 @@ static int apply_binary(struct apply_state *state,
         */
        if (strlen(patch->old_sha1_prefix) != 40 ||
            strlen(patch->new_sha1_prefix) != 40 ||
-           get_sha1_hex(patch->old_sha1_prefix, sha1) ||
-           get_sha1_hex(patch->new_sha1_prefix, sha1))
-               return error("cannot apply binary patch to '%s' "
-                            "without full index line", name);
+           get_oid_hex(patch->old_sha1_prefix, &oid) ||
+           get_oid_hex(patch->new_sha1_prefix, &oid))
+               return error(_("cannot apply binary patch to '%s' "
+                              "without full index line"), name);
 
        if (patch->old_name) {
                /*
                 * See if the old one matches what the patch
                 * applies to.
                 */
-               hash_sha1_file(img->buf, img->len, blob_type, sha1);
-               if (strcmp(sha1_to_hex(sha1), patch->old_sha1_prefix))
-                       return error("the patch applies to '%s' (%s), "
-                                    "which does not match the "
-                                    "current contents.",
-                                    name, sha1_to_hex(sha1));
+               hash_sha1_file(img->buf, img->len, blob_type, oid.hash);
+               if (strcmp(oid_to_hex(&oid), patch->old_sha1_prefix))
+                       return error(_("the patch applies to '%s' (%s), "
+                                      "which does not match the "
+                                      "current contents."),
+                                    name, oid_to_hex(&oid));
        }
        else {
                /* Otherwise, the old one must be empty. */
                if (img->len)
-                       return error("the patch applies to an empty "
-                                    "'%s' but it is not empty", name);
+                       return error(_("the patch applies to an empty "
+                                      "'%s' but it is not empty"), name);
        }
 
-       get_sha1_hex(patch->new_sha1_prefix, sha1);
-       if (is_null_sha1(sha1)) {
+       get_oid_hex(patch->new_sha1_prefix, &oid);
+       if (is_null_oid(&oid)) {
                clear_image(img);
                return 0; /* deletion patch */
        }
 
-       if (has_sha1_file(sha1)) {
+       if (has_sha1_file(oid.hash)) {
                /* We already have the postimage */
                enum object_type type;
                unsigned long size;
                char *result;
 
-               result = read_sha1_file(sha1, &type, &size);
+               result = read_sha1_file(oid.hash, &type, &size);
                if (!result)
-                       return error("the necessary postimage %s for "
-                                    "'%s' cannot be read",
+                       return error(_("the necessary postimage %s for "
+                                      "'%s' cannot be read"),
                                     patch->new_sha1_prefix, name);
                clear_image(img);
                img->buf = result;
@@ -3182,10 +3194,10 @@ static int apply_binary(struct apply_state *state,
                                     name);
 
                /* verify that the result matches */
-               hash_sha1_file(img->buf, img->len, blob_type, sha1);
-               if (strcmp(sha1_to_hex(sha1), patch->new_sha1_prefix))
+               hash_sha1_file(img->buf, img->len, blob_type, oid.hash);
+               if (strcmp(oid_to_hex(&oid), patch->new_sha1_prefix))
                        return error(_("binary patch to '%s' creates incorrect result (expecting %s, got %s)"),
-                               name, patch->new_sha1_prefix, sha1_to_hex(sha1));
+                               name, patch->new_sha1_prefix, oid_to_hex(&oid));
        }
 
        return 0;
@@ -3215,17 +3227,17 @@ static int apply_fragments(struct apply_state *state, struct image *img, struct
        return 0;
 }
 
-static int read_blob_object(struct strbuf *buf, const unsigned char *sha1, unsigned mode)
+static int read_blob_object(struct strbuf *buf, const struct object_id *oid, unsigned mode)
 {
        if (S_ISGITLINK(mode)) {
                strbuf_grow(buf, 100);
-               strbuf_addf(buf, "Subproject commit %s\n", sha1_to_hex(sha1));
+               strbuf_addf(buf, "Subproject commit %s\n", oid_to_hex(oid));
        } else {
                enum object_type type;
                unsigned long sz;
                char *result;
 
-               result = read_sha1_file(sha1, &type, &sz);
+               result = read_sha1_file(oid->hash, &type, &sz);
                if (!result)
                        return -1;
                /* XXX read_sha1_file NUL-terminates */
@@ -3238,7 +3250,7 @@ static int read_file_or_gitlink(const struct cache_entry *ce, struct strbuf *buf
 {
        if (!ce)
                return 0;
-       return read_blob_object(buf, ce->sha1, ce->ce_mode);
+       return read_blob_object(buf, &ce->oid, ce->ce_mode);
 }
 
 static struct patch *in_fn_table(struct apply_state *state, const char *name)
@@ -3322,10 +3334,8 @@ static void prepare_fn_table(struct apply_state *state, struct patch *patch)
 static int checkout_target(struct index_state *istate,
                           struct cache_entry *ce, struct stat *st)
 {
-       struct checkout costate;
+       struct checkout costate = CHECKOUT_INIT;
 
-       memset(&costate, 0, sizeof(costate));
-       costate.base_dir = "";
        costate.refresh_cache = 1;
        costate.istate = istate;
        if (checkout_entry(ce, &costate, NULL) || lstat(ce->name, st))
@@ -3445,9 +3455,9 @@ static int load_preimage(struct apply_state *state,
 
 static int three_way_merge(struct image *image,
                           char *path,
-                          const unsigned char *base,
-                          const unsigned char *ours,
-                          const unsigned char *theirs)
+                          const struct object_id *base,
+                          const struct object_id *ours,
+                          const struct object_id *theirs)
 {
        mmfile_t base_file, our_file, their_file;
        mmbuffer_t result = { NULL };
@@ -3524,7 +3534,7 @@ static int try_threeway(struct apply_state *state,
                        struct stat *st,
                        const struct cache_entry *ce)
 {
-       unsigned char pre_sha1[20], post_sha1[20], our_sha1[20];
+       struct object_id pre_oid, post_oid, our_oid;
        struct strbuf buf = STRBUF_INIT;
        size_t len;
        int status;
@@ -3538,13 +3548,13 @@ static int try_threeway(struct apply_state *state,
 
        /* 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.");
+               write_sha1_file("", 0, blob_type, pre_oid.hash);
+       else if (get_sha1(patch->old_sha1_prefix, pre_oid.hash) ||
+                read_blob_object(&buf, &pre_oid, patch->old_mode))
+               return error(_("repository lacks the necessary blob to fall back on 3-way merge."));
 
        if (state->apply_verbosity > verbosity_silent)
-               fprintf(stderr, "Falling back to three-way merge...\n");
+               fprintf(stderr, _("Falling back to three-way merge...\n"));
 
        img = strbuf_detach(&buf, &len);
        prepare_image(&tmp_image, img, len, 1);
@@ -3553,30 +3563,30 @@ static int try_threeway(struct apply_state *state,
                clear_image(&tmp_image);
                return -1;
        }
-       /* post_sha1[] is theirs */
-       write_sha1_file(tmp_image.buf, tmp_image.len, blob_type, post_sha1);
+       /* post_oid is theirs */
+       write_sha1_file(tmp_image.buf, tmp_image.len, blob_type, post_oid.hash);
        clear_image(&tmp_image);
 
-       /* our_sha1[] is ours */
+       /* our_oid is ours */
        if (patch->is_new) {
                if (load_current(state, &tmp_image, patch))
-                       return error("cannot read the current contents of '%s'",
+                       return error(_("cannot read the current contents of '%s'"),
                                     patch->new_name);
        } else {
                if (load_preimage(state, &tmp_image, patch, st, ce))
-                       return error("cannot read the current contents of '%s'",
+                       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);
+       write_sha1_file(tmp_image.buf, tmp_image.len, blob_type, our_oid.hash);
        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);
+                                &pre_oid, &our_oid, &post_oid);
        if (status < 0) {
                if (state->apply_verbosity > verbosity_silent)
                        fprintf(stderr,
-                               "Failed to fall back on three-way merge...\n");
+                               _("Failed to fall back on three-way merge...\n"));
                return status;
        }
 
@@ -3585,17 +3595,17 @@ static int try_threeway(struct apply_state *state,
                if (patch->is_new)
                        oidclr(&patch->threeway_stage[0]);
                else
-                       hashcpy(patch->threeway_stage[0].hash, pre_sha1);
-               hashcpy(patch->threeway_stage[1].hash, our_sha1);
-               hashcpy(patch->threeway_stage[2].hash, post_sha1);
+                       oidcpy(&patch->threeway_stage[0], &pre_oid);
+               oidcpy(&patch->threeway_stage[1], &our_oid);
+               oidcpy(&patch->threeway_stage[2], &post_oid);
                if (state->apply_verbosity > verbosity_silent)
                        fprintf(stderr,
-                               "Applied patch to '%s' with conflicts.\n",
+                               _("Applied patch to '%s' with conflicts.\n"),
                                patch->new_name);
        } else {
                if (state->apply_verbosity > verbosity_silent)
                        fprintf(stderr,
-                               "Applied patch to '%s' cleanly.\n",
+                               _("Applied patch to '%s' cleanly.\n"),
                                patch->new_name);
        }
        return 0;
@@ -3981,21 +3991,30 @@ static int check_patch_list(struct apply_state *state, 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)
+static int read_apply_cache(struct apply_state *state)
+{
+       if (state->index_file)
+               return read_cache_from(state->index_file);
+       else
+               return read_cache();
+}
+
+/* This function tries to read the object name from the current index */
+static int get_current_oid(struct apply_state *state, const char *path,
+                          struct object_id *oid)
 {
        int pos;
 
-       if (read_cache() < 0)
+       if (read_apply_cache(state) < 0)
                return -1;
        pos = cache_name_pos(path, strlen(path));
        if (pos < 0)
                return -1;
-       hashcpy(sha1, active_cache[pos]->sha1);
+       oidcpy(oid, &active_cache[pos]->oid);
        return 0;
 }
 
-static int preimage_sha1_in_gitlink_patch(struct patch *p, unsigned char sha1[20])
+static int preimage_oid_in_gitlink_patch(struct patch *p, struct object_id *oid)
 {
        /*
         * A usable gitlink patch has only one fragment (hunk) that looks like:
@@ -4019,18 +4038,18 @@ static int preimage_sha1_in_gitlink_patch(struct patch *p, unsigned char sha1[20
            (preimage = memchr(hunk->patch, '\n', hunk->size)) != NULL &&
            starts_with(++preimage, heading) &&
            /* does it record full SHA-1? */
-           !get_sha1_hex(preimage + sizeof(heading) - 1, sha1) &&
-           preimage[sizeof(heading) + 40 - 1] == '\n' &&
+           !get_oid_hex(preimage + sizeof(heading) - 1, oid) &&
+           preimage[sizeof(heading) + GIT_SHA1_HEXSZ - 1] == '\n' &&
            /* does the abbreviated name on the index line agree with it? */
            starts_with(preimage + sizeof(heading) - 1, p->old_sha1_prefix))
                return 0; /* it all looks fine */
 
        /* we may have full object name on the index line */
-       return get_sha1_hex(p->old_sha1_prefix, sha1);
+       return get_oid_hex(p->old_sha1_prefix, oid);
 }
 
 /* Build an index that contains the just the files needed for a 3way merge */
-static int build_fake_ancestor(struct patch *list, const char *filename)
+static int build_fake_ancestor(struct apply_state *state, struct patch *list)
 {
        struct patch *patch;
        struct index_state result = { NULL };
@@ -4041,7 +4060,7 @@ static int build_fake_ancestor(struct patch *list, const char *filename)
         * worth showing the new sha1 prefix, but until then...
         */
        for (patch = list; patch; patch = patch->next) {
-               unsigned char sha1[20];
+               struct object_id oid;
                struct cache_entry *ce;
                const char *name;
 
@@ -4050,209 +4069,210 @@ static int build_fake_ancestor(struct patch *list, const char *filename)
                        continue;
 
                if (S_ISGITLINK(patch->old_mode)) {
-                       if (!preimage_sha1_in_gitlink_patch(patch, sha1))
+                       if (!preimage_oid_in_gitlink_patch(patch, &oid))
                                ; /* ok, the textual part looks sane */
                        else
-                               return error("sha1 information is lacking or "
-                                            "useless for submodule %s", name);
-               } else if (!get_sha1_blob(patch->old_sha1_prefix, sha1)) {
+                               return error(_("sha1 information is lacking or "
+                                              "useless for submodule %s"), name);
+               } else if (!get_sha1_blob(patch->old_sha1_prefix, oid.hash)) {
                        ; /* ok */
                } else if (!patch->lines_added && !patch->lines_deleted) {
                        /* mode-only change: update the current */
-                       if (get_current_sha1(patch->old_name, sha1))
-                               return error("mode change for %s, which is not "
-                                            "in current HEAD", name);
+                       if (get_current_oid(state, patch->old_name, &oid))
+                               return error(_("mode change for %s, which is not "
+                                              "in current HEAD"), name);
                } else
-                       return error("sha1 information is lacking or useless "
-                                    "(%s).", name);
+                       return error(_("sha1 information is lacking or useless "
+                                      "(%s)."), name);
 
-               ce = make_cache_entry(patch->old_mode, sha1, name, 0, 0);
+               ce = make_cache_entry(patch->old_mode, oid.hash, name, 0, 0);
                if (!ce)
                        return error(_("make_cache_entry failed for path '%s'"),
                                     name);
                if (add_index_entry(&result, ce, ADD_CACHE_OK_TO_ADD)) {
                        free(ce);
-                       return error("Could not add %s to temporary index",
+                       return error(_("could not add %s to temporary index"),
                                     name);
                }
        }
 
-       hold_lock_file_for_update(&lock, filename, LOCK_DIE_ON_ERROR);
+       hold_lock_file_for_update(&lock, state->fake_ancestor, LOCK_DIE_ON_ERROR);
        res = write_locked_index(&result, &lock, COMMIT_LOCK);
        discard_index(&result);
 
-       if (res)
-               return error("Could not write temporary index to %s", filename);
-
-       return 0;
-}
+        if (res)
+                return error(_("could not write temporary index to %s"),
+                             state->fake_ancestor);
 
-static void stat_patch_list(struct apply_state *state, struct patch *patch)
-{
-       int files, adds, dels;
+        return 0;
+ }
 
-       for (files = adds = dels = 0 ; patch ; patch = patch->next) {
-               files++;
-               adds += patch->lines_added;
-               dels += patch->lines_deleted;
-               show_stats(state, patch);
-       }
+ static void stat_patch_list(struct apply_state *state, struct patch *patch)
+ {
+        int files, adds, dels;
 
-       print_stat_summary(stdout, files, adds, dels);
-}
+        for (files = adds = dels = 0 ; patch ; patch = patch->next) {
+                files++;
+                adds += patch->lines_added;
+                dels += patch->lines_deleted;
+                show_stats(state, patch);
+        }
 
-static void numstat_patch_list(struct apply_state *state,
-                              struct patch *patch)
-{
-       for ( ; patch; patch = patch->next) {
-               const char *name;
-               name = patch->new_name ? patch->new_name : patch->old_name;
-               if (patch->is_binary)
-                       printf("-\t-\t");
-               else
-                       printf("%d\t%d\t", patch->lines_added, patch->lines_deleted);
-               write_name_quoted(name, stdout, state->line_termination);
-       }
-}
+        print_stat_summary(stdout, files, adds, dels);
+ }
 
-static void show_file_mode_name(const char *newdelete, unsigned int mode, const char *name)
-{
-       if (mode)
-               printf(" %s mode %06o %s\n", newdelete, mode, name);
-       else
-               printf(" %s %s\n", newdelete, name);
-}
-
-static void show_mode_change(struct patch *p, int show_name)
-{
-       if (p->old_mode && p->new_mode && p->old_mode != p->new_mode) {
-               if (show_name)
-                       printf(" mode change %06o => %06o %s\n",
-                              p->old_mode, p->new_mode, p->new_name);
-               else
-                       printf(" mode change %06o => %06o\n",
-                              p->old_mode, p->new_mode);
-       }
-}
-
-static void show_rename_copy(struct patch *p)
-{
-       const char *renamecopy = p->is_rename ? "rename" : "copy";
-       const char *old, *new;
-
-       /* Find common prefix */
-       old = p->old_name;
-       new = p->new_name;
-       while (1) {
-               const char *slash_old, *slash_new;
-               slash_old = strchr(old, '/');
-               slash_new = strchr(new, '/');
-               if (!slash_old ||
-                   !slash_new ||
-                   slash_old - old != slash_new - new ||
-                   memcmp(old, new, slash_new - new))
-                       break;
-               old = slash_old + 1;
-               new = slash_new + 1;
-       }
-       /* p->old_name thru old is the common prefix, and old and new
-        * through the end of names are renames
-        */
-       if (old != p->old_name)
-               printf(" %s %.*s{%s => %s} (%d%%)\n", renamecopy,
-                      (int)(old - p->old_name), p->old_name,
-                      old, new, p->score);
-       else
-               printf(" %s %s => %s (%d%%)\n", renamecopy,
-                      p->old_name, p->new_name, p->score);
-       show_mode_change(p, 0);
-}
-
-static void summary_patch_list(struct patch *patch)
-{
-       struct patch *p;
-
-       for (p = patch; p; p = p->next) {
-               if (p->is_new)
-                       show_file_mode_name("create", p->new_mode, p->new_name);
-               else if (p->is_delete)
-                       show_file_mode_name("delete", p->old_mode, p->old_name);
-               else {
-                       if (p->is_rename || p->is_copy)
-                               show_rename_copy(p);
-                       else {
-                               if (p->score) {
-                                       printf(" rewrite %s (%d%%)\n",
-                                              p->new_name, p->score);
-                                       show_mode_change(p, 0);
-                               }
-                               else
-                                       show_mode_change(p, 1);
-                       }
-               }
-       }
-}
-
-static void patch_stats(struct apply_state *state, struct patch *patch)
-{
-       int lines = patch->lines_added + patch->lines_deleted;
-
-       if (lines > state->max_change)
-               state->max_change = lines;
-       if (patch->old_name) {
-               int len = quote_c_style(patch->old_name, NULL, NULL, 0);
-               if (!len)
-                       len = strlen(patch->old_name);
-               if (len > state->max_len)
-                       state->max_len = len;
-       }
-       if (patch->new_name) {
-               int len = quote_c_style(patch->new_name, NULL, NULL, 0);
-               if (!len)
-                       len = strlen(patch->new_name);
-               if (len > state->max_len)
-                       state->max_len = len;
-       }
-}
-
-static int remove_file(struct apply_state *state, struct patch *patch, int rmdir_empty)
-{
-       if (state->update_index) {
-               if (remove_file_from_cache(patch->old_name) < 0)
-                       return error(_("unable to remove %s from index"), patch->old_name);
-       }
-       if (!state->cached) {
-               if (!remove_or_warn(patch->old_mode, patch->old_name) && rmdir_empty) {
-                       remove_path(patch->old_name);
-               }
-       }
-       return 0;
-}
-
-static int add_index_file(struct apply_state *state,
-                         const char *path,
-                         unsigned mode,
-                         void *buf,
-                         unsigned long size)
-{
-       struct stat st;
-       struct cache_entry *ce;
-       int namelen = strlen(path);
-       unsigned ce_size = cache_entry_size(namelen);
-
-       if (!state->update_index)
-               return 0;
-
-       ce = xcalloc(1, ce_size);
-       memcpy(ce->name, path, namelen);
-       ce->ce_mode = create_ce_mode(mode);
-       ce->ce_flags = create_ce_flags(0);
-       ce->ce_namelen = namelen;
-       if (S_ISGITLINK(mode)) {
-               const char *s;
-
-               if (!skip_prefix(buf, "Subproject commit ", &s) ||
-                   get_sha1_hex(s, ce->sha1)) {
+ static void numstat_patch_list(struct apply_state *state,
+                               struct patch *patch)
+ {
+        for ( ; patch; patch = patch->next) {
+                const char *name;
+                name = patch->new_name ? patch->new_name : patch->old_name;
+                if (patch->is_binary)
+                        printf("-\t-\t");
+                else
+                        printf("%d\t%d\t", patch->lines_added, patch->lines_deleted);
+                write_name_quoted(name, stdout, state->line_termination);
+        }
+ }
+
+ static void show_file_mode_name(const char *newdelete, unsigned int mode, const char *name)
+ {
+        if (mode)
+                printf(" %s mode %06o %s\n", newdelete, mode, name);
+        else
+                printf(" %s %s\n", newdelete, name);
+ }
+
+ static void show_mode_change(struct patch *p, int show_name)
+ {
+        if (p->old_mode && p->new_mode && p->old_mode != p->new_mode) {
+                if (show_name)
+                        printf(" mode change %06o => %06o %s\n",
+                               p->old_mode, p->new_mode, p->new_name);
+                else
+                        printf(" mode change %06o => %06o\n",
+                               p->old_mode, p->new_mode);
+        }
+ }
+
+ static void show_rename_copy(struct patch *p)
+ {
+        const char *renamecopy = p->is_rename ? "rename" : "copy";
+        const char *old, *new;
+
+        /* Find common prefix */
+        old = p->old_name;
+        new = p->new_name;
+        while (1) {
+                const char *slash_old, *slash_new;
+                slash_old = strchr(old, '/');
+                slash_new = strchr(new, '/');
+                if (!slash_old ||
+                    !slash_new ||
+                    slash_old - old != slash_new - new ||
+                    memcmp(old, new, slash_new - new))
+                        break;
+                old = slash_old + 1;
+                new = slash_new + 1;
+        }
+        /* p->old_name thru old is the common prefix, and old and new
+         * through the end of names are renames
+         */
+        if (old != p->old_name)
+                printf(" %s %.*s{%s => %s} (%d%%)\n", renamecopy,
+                       (int)(old - p->old_name), p->old_name,
+                       old, new, p->score);
+        else
+                printf(" %s %s => %s (%d%%)\n", renamecopy,
+                       p->old_name, p->new_name, p->score);
+        show_mode_change(p, 0);
+ }
+
+ static void summary_patch_list(struct patch *patch)
+ {
+        struct patch *p;
+
+        for (p = patch; p; p = p->next) {
+                if (p->is_new)
+                        show_file_mode_name("create", p->new_mode, p->new_name);
+                else if (p->is_delete)
+                        show_file_mode_name("delete", p->old_mode, p->old_name);
+                else {
+                        if (p->is_rename || p->is_copy)
+                                show_rename_copy(p);
+                        else {
+                                if (p->score) {
+                                        printf(" rewrite %s (%d%%)\n",
+                                               p->new_name, p->score);
+                                        show_mode_change(p, 0);
+                                }
+                                else
+                                        show_mode_change(p, 1);
+                        }
+                }
+        }
+ }
+
+ static void patch_stats(struct apply_state *state, struct patch *patch)
+ {
+        int lines = patch->lines_added + patch->lines_deleted;
+
+        if (lines > state->max_change)
+                state->max_change = lines;
+        if (patch->old_name) {
+                int len = quote_c_style(patch->old_name, NULL, NULL, 0);
+                if (!len)
+                        len = strlen(patch->old_name);
+                if (len > state->max_len)
+                        state->max_len = len;
+        }
+        if (patch->new_name) {
+                int len = quote_c_style(patch->new_name, NULL, NULL, 0);
+                if (!len)
+                        len = strlen(patch->new_name);
+                if (len > state->max_len)
+                        state->max_len = len;
+        }
+ }
+
+ static int remove_file(struct apply_state *state, struct patch *patch, int rmdir_empty)
+ {
+        if (state->update_index) {
+                if (remove_file_from_cache(patch->old_name) < 0)
+                        return error(_("unable to remove %s from index"), patch->old_name);
+        }
+        if (!state->cached) {
+                if (!remove_or_warn(patch->old_mode, patch->old_name) && rmdir_empty) {
+                        remove_path(patch->old_name);
+                }
+        }
+        return 0;
+ }
+
+ static int add_index_file(struct apply_state *state,
+                          const char *path,
+                          unsigned mode,
+                          void *buf,
+                          unsigned long size)
+ {
+        struct stat st;
+        struct cache_entry *ce;
+        int namelen = strlen(path);
+        unsigned ce_size = cache_entry_size(namelen);
+
+        if (!state->update_index)
+                return 0;
+
+        ce = xcalloc(1, ce_size);
+        memcpy(ce->name, path, namelen);
+        ce->ce_mode = create_ce_mode(mode);
+        ce->ce_flags = create_ce_flags(0);
+        ce->ce_namelen = namelen;
+        if (S_ISGITLINK(mode)) {
+                const char *s;
+
+                if (!skip_prefix(buf, "Subproject commit ", &s) ||
+                    get_oid_hex(s, &ce->oid)) {
                        free(ce);
                        return error(_("corrupt patch for submodule %s"), path);
                }
@@ -4266,7 +4286,7 @@ static int add_index_file(struct apply_state *state,
                        }
                        fill_stat_cache_info(ce, &st);
                }
-               if (write_sha1_file(buf, size, blob_type, ce->sha1) < 0) {
+               if (write_sha1_file(buf, size, blob_type, ce->oid.hash) < 0) {
                        free(ce);
                        return error(_("unable to create backing store "
                                       "for newly created file %s"), path);
@@ -4415,7 +4435,7 @@ static int add_conflicted_stages_file(struct apply_state *state,
                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].hash);
+               oidcpy(&ce->oid, &patch->threeway_stage[stage - 1]);
                if (add_cache_entry(ce, ADD_CACHE_OK_TO_ADD) < 0) {
                        free(ce);
                        return error(_("unable to add cache entry for %s"),
@@ -4662,10 +4682,16 @@ static int apply_patch(struct apply_state *state,
                state->apply = 0;
 
        state->update_index = state->check_index && state->apply;
-       if (state->update_index && state->newfd < 0)
-               state->newfd = hold_locked_index(state->lock_file, 1);
+       if (state->update_index && state->newfd < 0) {
+               if (state->index_file)
+                       state->newfd = hold_lock_file_for_update(state->lock_file,
+                                                                state->index_file,
+                                                                LOCK_DIE_ON_ERROR);
+               else
+                       state->newfd = hold_locked_index(state->lock_file, 1);
+       }
 
-       if (state->check_index && read_cache() < 0) {
+       if (state->check_index && read_apply_cache(state) < 0) {
                error(_("unable to read index file"));
                res = -128;
                goto end;
@@ -4697,18 +4723,18 @@ static int apply_patch(struct apply_state *state,
        }
 
        if (state->fake_ancestor &&
-           build_fake_ancestor(list, state->fake_ancestor)) {
+           build_fake_ancestor(state, list)) {
                res = -128;
                goto end;
        }
 
-       if (state->diffstat)
+       if (state->diffstat && state->apply_verbosity > verbosity_silent)
                stat_patch_list(state, list);
 
-       if (state->numstat)
+       if (state->numstat && state->apply_verbosity > verbosity_silent)
                numstat_patch_list(state, list);
 
-       if (state->summary)
+       if (state->summary && state->apply_verbosity > verbosity_silent)
                summary_patch_list(list);
 
 end:
@@ -4718,16 +4744,16 @@ static int apply_patch(struct apply_state *state,
        return res;
 }
 
-int apply_option_parse_exclude(const struct option *opt,
-                              const char *arg, int unset)
+static int apply_option_parse_exclude(const struct option *opt,
+                                     const char *arg, int unset)
 {
        struct apply_state *state = opt->value;
        add_name_limit(state, arg, 1);
        return 0;
 }
 
-int apply_option_parse_include(const struct option *opt,
-                              const char *arg, int unset)
+static int apply_option_parse_include(const struct option *opt,
+                                     const char *arg, int unset)
 {
        struct apply_state *state = opt->value;
        add_name_limit(state, arg, 0);
@@ -4735,9 +4761,9 @@ int apply_option_parse_include(const struct option *opt,
        return 0;
 }
 
-int apply_option_parse_p(const struct option *opt,
-                        const char *arg,
-                        int unset)
+static int apply_option_parse_p(const struct option *opt,
+                               const char *arg,
+                               int unset)
 {
        struct apply_state *state = opt->value;
        state->p_value = atoi(arg);
@@ -4745,8 +4771,8 @@ int apply_option_parse_p(const struct option *opt,
        return 0;
 }
 
-int apply_option_parse_space_change(const struct option *opt,
-                                   const char *arg, int unset)
+static int apply_option_parse_space_change(const struct option *opt,
+                                          const char *arg, int unset)
 {
        struct apply_state *state = opt->value;
        if (unset)
@@ -4756,8 +4782,8 @@ int apply_option_parse_space_change(const struct option *opt,
        return 0;
 }
 
-int apply_option_parse_whitespace(const struct option *opt,
-                                 const char *arg, int unset)
+static int apply_option_parse_whitespace(const struct option *opt,
+                                        const char *arg, int unset)
 {
        struct apply_state *state = opt->value;
        state->whitespace_option = arg;
@@ -4766,8 +4792,8 @@ int apply_option_parse_whitespace(const struct option *opt,
        return 0;
 }
 
-int apply_option_parse_directory(const struct option *opt,
-                                const char *arg, int unset)
+static int apply_option_parse_directory(const struct option *opt,
+                                       const char *arg, int unset)
 {
        struct apply_state *state = opt->value;
        strbuf_reset(&state->root);
@@ -4843,10 +4869,12 @@ int apply_all_patches(struct apply_state *state,
                        goto end;
                }
                if (state->applied_after_fixing_ws && state->apply)
-                       warning("%d line%s applied after"
-                               " fixing whitespace errors.",
-                               state->applied_after_fixing_ws,
-                               state->applied_after_fixing_ws == 1 ? "" : "s");
+                       warning(Q_("%d line applied after"
+                                  " fixing whitespace errors.",
+                                  "%d lines applied after"
+                                  " fixing whitespace errors.",
+                                  state->applied_after_fixing_ws),
+                               state->applied_after_fixing_ws);
                else if (state->whitespace_error)
                        warning(Q_("%d line adds whitespace errors.",
                                   "%d lines add whitespace errors.",
@@ -4864,7 +4892,7 @@ int apply_all_patches(struct apply_state *state,
                state->newfd = -1;
        }
 
-       return !!errs;
+       res = !!errs;
 
 end:
        if (state->newfd >= 0) {
@@ -4872,5 +4900,89 @@ int apply_all_patches(struct apply_state *state,
                state->newfd = -1;
        }
 
+       if (state->apply_verbosity <= verbosity_silent) {
+               set_error_routine(state->saved_error_routine);
+               set_warn_routine(state->saved_warn_routine);
+       }
+
+       if (res > -1)
+               return res;
        return (res == -1 ? 1 : 128);
 }
+
+int apply_parse_options(int argc, const char **argv,
+                       struct apply_state *state,
+                       int *force_apply, int *options,
+                       const char * const *apply_usage)
+{
+       struct option builtin_apply_options[] = {
+               { OPTION_CALLBACK, 0, "exclude", state, N_("path"),
+                       N_("don't apply changes matching the given path"),
+                       0, apply_option_parse_exclude },
+               { OPTION_CALLBACK, 0, "include", state, N_("path"),
+                       N_("apply changes matching the given path"),
+                       0, apply_option_parse_include },
+               { OPTION_CALLBACK, 'p', NULL, state, N_("num"),
+                       N_("remove <num> leading slashes from traditional diff paths"),
+                       0, apply_option_parse_p },
+               OPT_BOOL(0, "no-add", &state->no_add,
+                       N_("ignore additions made by the patch")),
+               OPT_BOOL(0, "stat", &state->diffstat,
+                       N_("instead of applying the patch, output diffstat for the input")),
+               OPT_NOOP_NOARG(0, "allow-binary-replacement"),
+               OPT_NOOP_NOARG(0, "binary"),
+               OPT_BOOL(0, "numstat", &state->numstat,
+                       N_("show number of added and deleted lines in decimal notation")),
+               OPT_BOOL(0, "summary", &state->summary,
+                       N_("instead of applying the patch, output a summary for the input")),
+               OPT_BOOL(0, "check", &state->check,
+                       N_("instead of applying the patch, see if the patch is applicable")),
+               OPT_BOOL(0, "index", &state->check_index,
+                       N_("make sure the patch is applicable to the current index")),
+               OPT_BOOL(0, "cached", &state->cached,
+                       N_("apply a patch without touching the working tree")),
+               OPT_BOOL(0, "unsafe-paths", &state->unsafe_paths,
+                       N_("accept a patch that touches outside the working area")),
+               OPT_BOOL(0, "apply", force_apply,
+                       N_("also apply the patch (use with --stat/--summary/--check)")),
+               OPT_BOOL('3', "3way", &state->threeway,
+                        N_( "attempt three-way merge if a patch does not apply")),
+               OPT_FILENAME(0, "build-fake-ancestor", &state->fake_ancestor,
+                       N_("build a temporary index based on embedded index information")),
+               /* Think twice before adding "--nul" synonym to this */
+               OPT_SET_INT('z', NULL, &state->line_termination,
+                       N_("paths are separated with NUL character"), '\0'),
+               OPT_INTEGER('C', NULL, &state->p_context,
+                               N_("ensure at least <n> lines of context match")),
+               { OPTION_CALLBACK, 0, "whitespace", state, N_("action"),
+                       N_("detect new or modified lines that have whitespace errors"),
+                       0, apply_option_parse_whitespace },
+               { OPTION_CALLBACK, 0, "ignore-space-change", state, NULL,
+                       N_("ignore changes in whitespace when finding context"),
+                       PARSE_OPT_NOARG, apply_option_parse_space_change },
+               { OPTION_CALLBACK, 0, "ignore-whitespace", state, NULL,
+                       N_("ignore changes in whitespace when finding context"),
+                       PARSE_OPT_NOARG, apply_option_parse_space_change },
+               OPT_BOOL('R', "reverse", &state->apply_in_reverse,
+                       N_("apply the patch in reverse")),
+               OPT_BOOL(0, "unidiff-zero", &state->unidiff_zero,
+                       N_("don't expect at least one line of context")),
+               OPT_BOOL(0, "reject", &state->apply_with_reject,
+                       N_("leave the rejected hunks in corresponding *.rej files")),
+               OPT_BOOL(0, "allow-overlap", &state->allow_overlap,
+                       N_("allow overlapping hunks")),
+               OPT__VERBOSE(&state->apply_verbosity, N_("be verbose")),
+               OPT_BIT(0, "inaccurate-eof", options,
+                       N_("tolerate incorrectly detected missing new-line at the end of file"),
+                       APPLY_OPT_INACCURATE_EOF),
+               OPT_BIT(0, "recount", options,
+                       N_("do not trust the line counts in the hunk headers"),
+                       APPLY_OPT_RECOUNT),
+               { OPTION_CALLBACK, 0, "directory", state, N_("root"),
+                       N_("prepend <root> to all filenames"),
+                       0, apply_option_parse_directory },
+               OPT_END()
+       };
+
+       return parse_options(argc, argv, state->prefix, builtin_apply_options, apply_usage, 0);
+}