apply: add --intent-to-add

Similar to 'git reset -N', this option makes 'git apply' automatically mark new files as intent-to-add so they are visible in the following 'git diff' command and could also be committed with 'git commit -a'. Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Nguyễn Thái Ngọc Duy committed May 26, 2018 at 14:08 UTC cff5dc09ed0d07461bf77e9abef57dc86a11ab77
4 files changed +35 -8
Documentation/git-apply.txt
+9 -1
@@ -9,7 +9,7 @@ git-apply - Apply a patch to files and/or to the index
9 SYNOPSIS
10 --------
11 [verse]
12 -'git apply' [--stat] [--numstat] [--summary] [--check] [--index] [--3way]
12 +'git apply' [--stat] [--numstat] [--summary] [--check] [--index | --intent-to-add] [--3way]
13 [--apply] [--no-add] [--build-fake-ancestor=<file>] [-R | --reverse]
14 [--allow-binary-replacement | --binary] [--reject] [-z]
15 [-p<n>] [-C<n>] [--inaccurate-eof] [--recount] [--cached]
@@ -74,6 +74,14 @@ OPTIONS
74 cached data, apply the patch, and store the result in the index
75 without using the working tree. This implies `--index`.
76
77 +--intent-to-add::
78 + When applying the patch only to the working tree, mark new
79 + files to be added to the index later (see `--intent-to-add`
80 + option in linkgit:git-add[1]). This option is ignored unless
81 + running in a Git repository and `--index` is not specified.
82 + Note that `--index` could be implied by other options such
83 + as `--cached` or `--3way`.
84 +
85 -3::
86 --3way::
87 When the patch does not apply cleanly, fall back on 3-way merge if
apply.c
+12 -7
@@ -141,6 +141,8 @@ int check_apply_state(struct apply_state *state, int force_apply)
141 return error(_("--cached outside a repository"));
142 state->check_index = 1;
143 }
144 + if (state->ita_only && (state->check_index || is_not_gitdir))
145 + state->ita_only = 0;
146 if (state->check_index)
147 state->unsafe_paths = 0;
148
@@ -4242,7 +4244,7 @@ static void patch_stats(struct apply_state *state, struct patch *patch)
4244
4245 static int remove_file(struct apply_state *state, struct patch *patch, int rmdir_empty)
4246 {
4245 - if (state->update_index) {
4247 + if (state->update_index && !state->ita_only) {
4248 if (remove_file_from_cache(patch->old_name) < 0)
4249 return error(_("unable to remove %s from index"), patch->old_name);
4250 }
@@ -4265,15 +4267,15 @@ static int add_index_file(struct apply_state *state,
4267 int namelen = strlen(path);
4268 unsigned ce_size = cache_entry_size(namelen);
4269
4268 - if (!state->update_index)
4269 - return 0;
4270 -
4270 ce = xcalloc(1, ce_size);
4271 memcpy(ce->name, path, namelen);
4272 ce->ce_mode = create_ce_mode(mode);
4273 ce->ce_flags = create_ce_flags(0);
4274 ce->ce_namelen = namelen;
4276 - if (S_ISGITLINK(mode)) {
4275 + if (state->ita_only) {
4276 + ce->ce_flags |= CE_INTENT_TO_ADD;
4277 + set_object_name_for_intent_to_add_entry(ce);
4278 + } else if (S_ISGITLINK(mode)) {
4279 const char *s;
4280
4281 if (!skip_prefix(buf, "Subproject commit ", &s) ||
@@ -4465,8 +4467,9 @@ static int create_file(struct apply_state *state, struct patch *patch)
4467
4468 if (patch->conflicted_threeway)
4469 return add_conflicted_stages_file(state, patch);
4468 - else
4470 + else if (state->update_index)
4471 return add_index_file(state, path, mode, buf, size);
4472 + return 0;
4473 }
4474
4475 /* phase zero is to remove, phase one is to create */
@@ -4686,7 +4689,7 @@ static int apply_patch(struct apply_state *state,
4689 if (state->whitespace_error && (state->ws_error_action == die_on_ws_error))
4690 state->apply = 0;
4691
4689 - state->update_index = state->check_index && state->apply;
4692 + state->update_index = (state->check_index || state->ita_only) && state->apply;
4693 if (state->update_index && !is_lock_file_locked(&state->lock_file)) {
4694 if (state->index_file)
4695 hold_lock_file_for_update(&state->lock_file,
@@ -4941,6 +4944,8 @@ int apply_parse_options(int argc, const char **argv,
4944 N_("instead of applying the patch, see if the patch is applicable")),
4945 OPT_BOOL(0, "index", &state->check_index,
4946 N_("make sure the patch is applicable to the current index")),
4947 + OPT_BOOL('N', "intent-to-add", &state->ita_only,
4948 + N_("mark new files with `git add --intent-to-add`")),
4949 OPT_BOOL(0, "cached", &state->cached,
4950 N_("apply a patch without touching the working tree")),
4951 OPT_BOOL_F(0, "unsafe-paths", &state->unsafe_paths,
apply.h
+1
@@ -45,6 +45,7 @@ struct apply_state {
45 int check; /* preimage must match working tree, don't actually apply */
46 int check_index; /* preimage must match the indexed version */
47 int update_index; /* check_index && apply */
48 + int ita_only; /* add intent-to-add entries to the index */
49
50 /* These control cosmetic aspect of the output */
51 int diffstat; /* just show a diffstat, and don't actually apply */
t/t2203-add-intent.sh
+13
@@ -245,6 +245,7 @@ test_expect_success 'diff-files/diff-cached shows ita as new/not-new files' '
245 test_cmp expected2 actual2
246 '
247
248 +
249 test_expect_success '"diff HEAD" includes ita as new files' '
250 git reset --hard &&
251 echo new >new-ita &&
@@ -262,4 +263,16 @@ test_expect_success '"diff HEAD" includes ita as new files' '
263 test_cmp expected actual
264 '
265
266 +test_expect_success 'apply --intent-to-add' '
267 + git reset --hard &&
268 + echo new >new-ita &&
269 + git add -N new-ita &&
270 + git diff >expected &&
271 + grep "new file" expected &&
272 + git reset --hard &&
273 + git apply --intent-to-add expected &&
274 + git diff >actual &&
275 + test_cmp expected actual
276 +'
277 +
278 test_done