Skip to content

Commit 887b8ac

Browse files
pks-tgitster
authored andcommitted
add-patch: add support for in-memory index patching
With `run_add_p()` callers have the ability to apply changes from a specific revision to a repository's index. This infra supports several different modes, like for example applying changes to the index, worktree or both. One feature that is missing though is the ability to apply changes to an in-memory index different from the repository's index. Add a new function `run_add_p_index()` to plug this gap. This new function will be used in a subsequent commit. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 06abfd8 commit 887b8ac

File tree

2 files changed

+115
-3
lines changed

2 files changed

+115
-3
lines changed

add-patch.c

Lines changed: 107 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44
#include "git-compat-util.h"
55
#include "add-patch.h"
66
#include "advice.h"
7+
#include "commit.h"
78
#include "config.h"
89
#include "diff.h"
910
#include "editor.h"
1011
#include "environment.h"
1112
#include "gettext.h"
13+
#include "hex.h"
1214
#include "object-name.h"
1315
#include "pager.h"
1416
#include "read-cache-ll.h"
@@ -263,6 +265,8 @@ struct hunk {
263265

264266
struct add_p_state {
265267
struct repository *r;
268+
struct index_state *index;
269+
const char *index_file;
266270
struct interactive_config cfg;
267271
struct strbuf answer, buf;
268272

@@ -437,7 +441,7 @@ static void setup_child_process(struct add_p_state *s,
437441

438442
cp->git_cmd = 1;
439443
strvec_pushf(&cp->env,
440-
INDEX_ENVIRONMENT "=%s", s->r->index_file);
444+
INDEX_ENVIRONMENT "=%s", s->index_file);
441445
}
442446

443447
static int parse_range(const char **p,
@@ -1861,7 +1865,7 @@ static int patch_update_file(struct add_p_state *s,
18611865
strbuf_reset(&s->buf);
18621866
reassemble_patch(s, file_diff, 0, &s->buf);
18631867

1864-
discard_index(s->r->index);
1868+
discard_index(s->index);
18651869
if (s->mode->apply_for_checkout)
18661870
apply_for_checkout(s, &s->buf,
18671871
s->mode->is_reverse);
@@ -1872,9 +1876,11 @@ static int patch_update_file(struct add_p_state *s,
18721876
NULL, 0, NULL, 0))
18731877
error(_("'git apply' failed"));
18741878
}
1875-
if (repo_read_index(s->r) >= 0)
1879+
if (read_index_from(s->index, s->index_file, s->r->gitdir) >= 0 &&
1880+
s->index == s->r->index) {
18761881
repo_refresh_and_write_index(s->r, REFRESH_QUIET, 0,
18771882
1, NULL, NULL, NULL);
1883+
}
18781884
}
18791885

18801886
putchar('\n');
@@ -1887,6 +1893,8 @@ int run_add_p(struct repository *r, enum add_p_mode mode,
18871893
{
18881894
struct add_p_state s = {
18891895
.r = r,
1896+
.index = r->index,
1897+
.index_file = r->index_file,
18901898
.answer = STRBUF_INIT,
18911899
.buf = STRBUF_INIT,
18921900
.plain = STRBUF_INIT,
@@ -1945,3 +1953,99 @@ int run_add_p(struct repository *r, enum add_p_mode mode,
19451953
add_p_state_clear(&s);
19461954
return 0;
19471955
}
1956+
1957+
int run_add_p_index(struct repository *r,
1958+
struct index_state *index,
1959+
const char *index_file,
1960+
struct interactive_options *opts,
1961+
const char *revision,
1962+
const struct pathspec *ps)
1963+
{
1964+
struct patch_mode mode = {
1965+
.apply_args = { "--cached", NULL },
1966+
.apply_check_args = { "--cached", NULL },
1967+
.prompt_mode = {
1968+
N_("Stage mode change [y,n,q,a,d%s,?]? "),
1969+
N_("Stage deletion [y,n,q,a,d%s,?]? "),
1970+
N_("Stage addition [y,n,q,a,d%s,?]? "),
1971+
N_("Stage this hunk [y,n,q,a,d%s,?]? ")
1972+
},
1973+
.edit_hunk_hint = N_("If the patch applies cleanly, the edited hunk "
1974+
"will immediately be marked for staging."),
1975+
.help_patch_text =
1976+
N_("y - stage this hunk\n"
1977+
"n - do not stage this hunk\n"
1978+
"q - quit; do not stage this hunk or any of the remaining "
1979+
"ones\n"
1980+
"a - stage this hunk and all later hunks in the file\n"
1981+
"d - do not stage this hunk or any of the later hunks in "
1982+
"the file\n"),
1983+
.index_only = 1,
1984+
};
1985+
struct add_p_state s = {
1986+
.r = r,
1987+
.index = index,
1988+
.index_file = index_file,
1989+
.answer = STRBUF_INIT,
1990+
.buf = STRBUF_INIT,
1991+
.plain = STRBUF_INIT,
1992+
.colored = STRBUF_INIT,
1993+
.mode = &mode,
1994+
.revision = revision,
1995+
};
1996+
struct strbuf parent_revision = STRBUF_INIT;
1997+
char parent_tree_oid[GIT_MAX_HEXSZ + 1];
1998+
size_t binary_count = 0;
1999+
struct commit *commit;
2000+
int ret;
2001+
2002+
commit = lookup_commit_reference_by_name(revision);
2003+
if (!commit) {
2004+
err(&s, _("Revision does not refer to a commit"));
2005+
ret = -1;
2006+
goto out;
2007+
}
2008+
2009+
if (commit->parents)
2010+
oid_to_hex_r(parent_tree_oid, get_commit_tree_oid(commit->parents->item));
2011+
else
2012+
oid_to_hex_r(parent_tree_oid, r->hash_algo->empty_tree);
2013+
2014+
strbuf_addf(&parent_revision, "%s~", revision);
2015+
mode.diff_cmd[0] = "diff-tree";
2016+
mode.diff_cmd[1] = "-r";
2017+
mode.diff_cmd[2] = parent_tree_oid;
2018+
2019+
interactive_config_init(&s.cfg, r, opts);
2020+
2021+
if (parse_diff(&s, ps) < 0) {
2022+
ret = -1;
2023+
goto out;
2024+
}
2025+
2026+
for (size_t i = 0; i < s.file_diff_nr; i++) {
2027+
if (s.file_diff[i].binary && !s.file_diff[i].hunk_nr)
2028+
binary_count++;
2029+
else if (patch_update_file(&s, s.file_diff + i))
2030+
break;
2031+
}
2032+
2033+
if (s.file_diff_nr == 0) {
2034+
err(&s, _("No changes."));
2035+
ret = -1;
2036+
goto out;
2037+
}
2038+
2039+
if (binary_count == s.file_diff_nr) {
2040+
err(&s, _("Only binary files changed."));
2041+
ret = -1;
2042+
goto out;
2043+
}
2044+
2045+
ret = 0;
2046+
2047+
out:
2048+
strbuf_release(&parent_revision);
2049+
add_p_state_clear(&s);
2050+
return ret;
2051+
}

add-patch.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include "color.h"
55

6+
struct index_state;
67
struct pathspec;
78
struct repository;
89

@@ -53,4 +54,11 @@ int run_add_p(struct repository *r, enum add_p_mode mode,
5354
struct interactive_options *opts, const char *revision,
5455
const struct pathspec *ps);
5556

57+
int run_add_p_index(struct repository *r,
58+
struct index_state *index,
59+
const char *index_file,
60+
struct interactive_options *opts,
61+
const char *revision,
62+
const struct pathspec *ps);
63+
5664
#endif

0 commit comments

Comments
 (0)