Skip to content

Commit 5b0b57f

Browse files
chriscoolgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b429034 commit 5b0b57f

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

apply.c

+21-6
Original file line numberDiff line numberDiff line change
@@ -3993,12 +3993,21 @@ static int check_patch_list(struct apply_state *state, struct patch *patch)
39933993
return err;
39943994
}
39953995

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+
39964004
/* 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)
39984007
{
39994008
int pos;
40004009

4001-
if (read_cache() < 0)
4010+
if (read_apply_cache(state) < 0)
40024011
return -1;
40034012
pos = cache_name_pos(path, strlen(path));
40044013
if (pos < 0)
@@ -4071,7 +4080,7 @@ static int build_fake_ancestor(struct apply_state *state, struct patch *list)
40714080
; /* ok */
40724081
} else if (!patch->lines_added && !patch->lines_deleted) {
40734082
/* 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))
40754084
return error("mode change for %s, which is not "
40764085
"in current HEAD", name);
40774086
} else
@@ -4675,10 +4684,16 @@ static int apply_patch(struct apply_state *state,
46754684
state->apply = 0;
46764685

46774686
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+
}
46804695

4681-
if (state->check_index && read_cache() < 0) {
4696+
if (state->check_index && read_apply_cache(state) < 0) {
46824697
error(_("unable to read index file"));
46834698
res = -128;
46844699
goto end;

apply.h

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ struct apply_state {
6363
int unsafe_paths;
6464

6565
/* Other non boolean parameters */
66+
const char *index_file;
6667
enum apply_verbosity apply_verbosity;
6768
const char *fake_ancestor;
6869
const char *patch_input_file;

0 commit comments

Comments
 (0)