apply: learn to use a different index file

Sometimes we want to apply in a different index file. Before the apply functionality was libified it was possible to use the GIT_INDEX_FILE environment variable, for this purpose. But now, as the apply functionality has been libified, it should be possible to do that in a libified way. Signed-off-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Christian Couder committed Sep 4, 2016 at 22:18 UTC 5b0b57fd91ce684679fdac1c3ae3a50c6aa3943e
2 files changed +22 -6
apply.c
+21 -6
@@ -3993,12 +3993,21 @@ static int check_patch_list(struct apply_state *state, struct patch *patch)
3993 return err;
3994 }
3995
3996 +static int read_apply_cache(struct apply_state *state)
3997 +{
3998 + if (state->index_file)
3999 + return read_cache_from(state->index_file);
4000 + else
4001 + return read_cache();
4002 +}
4003 +
4004 /* This function tries to read the sha1 from the current index */
3997 -static int get_current_sha1(const char *path, unsigned char *sha1)
4005 +static int get_current_sha1(struct apply_state *state, const char *path,
4006 + unsigned char *sha1)
4007 {
4008 int pos;
4009
4001 - if (read_cache() < 0)
4010 + if (read_apply_cache(state) < 0)
4011 return -1;
4012 pos = cache_name_pos(path, strlen(path));
4013 if (pos < 0)
@@ -4071,7 +4080,7 @@ static int build_fake_ancestor(struct apply_state *state, struct patch *list)
4080 ; /* ok */
4081 } else if (!patch->lines_added && !patch->lines_deleted) {
4082 /* mode-only change: update the current */
4074 - if (get_current_sha1(patch->old_name, sha1))
4083 + if (get_current_sha1(state, patch->old_name, sha1))
4084 return error("mode change for %s, which is not "
4085 "in current HEAD", name);
4086 } else
@@ -4675,10 +4684,16 @@ static int apply_patch(struct apply_state *state,
4684 state->apply = 0;
4685
4686 state->update_index = state->check_index && state->apply;
4678 - if (state->update_index && state->newfd < 0)
4679 - state->newfd = hold_locked_index(state->lock_file, 1);
4687 + if (state->update_index && state->newfd < 0) {
4688 + if (state->index_file)
4689 + state->newfd = hold_lock_file_for_update(state->lock_file,
4690 + state->index_file,
4691 + LOCK_DIE_ON_ERROR);
4692 + else
4693 + state->newfd = hold_locked_index(state->lock_file, 1);
4694 + }
4695
4681 - if (state->check_index && read_cache() < 0) {
4696 + if (state->check_index && read_apply_cache(state) < 0) {
4697 error(_("unable to read index file"));
4698 res = -128;
4699 goto end;
apply.h
+1
@@ -63,6 +63,7 @@ struct apply_state {
63 int unsafe_paths;
64
65 /* Other non boolean parameters */
66 + const char *index_file;
67 enum apply_verbosity apply_verbosity;
68 const char *fake_ancestor;
69 const char *patch_input_file;