Skip to content

Commit cff5dc0

Browse files
pcloudsgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 8fc8f05 commit cff5dc0

File tree

4 files changed

+35
-8
lines changed

4 files changed

+35
-8
lines changed

Documentation/git-apply.txt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ git-apply - Apply a patch to files and/or to the index
99
SYNOPSIS
1010
--------
1111
[verse]
12-
'git apply' [--stat] [--numstat] [--summary] [--check] [--index] [--3way]
12+
'git apply' [--stat] [--numstat] [--summary] [--check] [--index | --intent-to-add] [--3way]
1313
[--apply] [--no-add] [--build-fake-ancestor=<file>] [-R | --reverse]
1414
[--allow-binary-replacement | --binary] [--reject] [-z]
1515
[-p<n>] [-C<n>] [--inaccurate-eof] [--recount] [--cached]
@@ -74,6 +74,14 @@ OPTIONS
7474
cached data, apply the patch, and store the result in the index
7575
without using the working tree. This implies `--index`.
7676

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+
7785
-3::
7886
--3way::
7987
When the patch does not apply cleanly, fall back on 3-way merge if

apply.c

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@ int check_apply_state(struct apply_state *state, int force_apply)
141141
return error(_("--cached outside a repository"));
142142
state->check_index = 1;
143143
}
144+
if (state->ita_only && (state->check_index || is_not_gitdir))
145+
state->ita_only = 0;
144146
if (state->check_index)
145147
state->unsafe_paths = 0;
146148

@@ -4242,7 +4244,7 @@ static void patch_stats(struct apply_state *state, struct patch *patch)
42424244

42434245
static int remove_file(struct apply_state *state, struct patch *patch, int rmdir_empty)
42444246
{
4245-
if (state->update_index) {
4247+
if (state->update_index && !state->ita_only) {
42464248
if (remove_file_from_cache(patch->old_name) < 0)
42474249
return error(_("unable to remove %s from index"), patch->old_name);
42484250
}
@@ -4265,15 +4267,15 @@ static int add_index_file(struct apply_state *state,
42654267
int namelen = strlen(path);
42664268
unsigned ce_size = cache_entry_size(namelen);
42674269

4268-
if (!state->update_index)
4269-
return 0;
4270-
42714270
ce = xcalloc(1, ce_size);
42724271
memcpy(ce->name, path, namelen);
42734272
ce->ce_mode = create_ce_mode(mode);
42744273
ce->ce_flags = create_ce_flags(0);
42754274
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)) {
42774279
const char *s;
42784280

42794281
if (!skip_prefix(buf, "Subproject commit ", &s) ||
@@ -4465,8 +4467,9 @@ static int create_file(struct apply_state *state, struct patch *patch)
44654467

44664468
if (patch->conflicted_threeway)
44674469
return add_conflicted_stages_file(state, patch);
4468-
else
4470+
else if (state->update_index)
44694471
return add_index_file(state, path, mode, buf, size);
4472+
return 0;
44704473
}
44714474

44724475
/* phase zero is to remove, phase one is to create */
@@ -4686,7 +4689,7 @@ static int apply_patch(struct apply_state *state,
46864689
if (state->whitespace_error && (state->ws_error_action == die_on_ws_error))
46874690
state->apply = 0;
46884691

4689-
state->update_index = state->check_index && state->apply;
4692+
state->update_index = (state->check_index || state->ita_only) && state->apply;
46904693
if (state->update_index && !is_lock_file_locked(&state->lock_file)) {
46914694
if (state->index_file)
46924695
hold_lock_file_for_update(&state->lock_file,
@@ -4941,6 +4944,8 @@ int apply_parse_options(int argc, const char **argv,
49414944
N_("instead of applying the patch, see if the patch is applicable")),
49424945
OPT_BOOL(0, "index", &state->check_index,
49434946
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`")),
49444949
OPT_BOOL(0, "cached", &state->cached,
49454950
N_("apply a patch without touching the working tree")),
49464951
OPT_BOOL_F(0, "unsafe-paths", &state->unsafe_paths,

apply.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ struct apply_state {
4545
int check; /* preimage must match working tree, don't actually apply */
4646
int check_index; /* preimage must match the indexed version */
4747
int update_index; /* check_index && apply */
48+
int ita_only; /* add intent-to-add entries to the index */
4849

4950
/* These control cosmetic aspect of the output */
5051
int diffstat; /* just show a diffstat, and don't actually apply */

t/t2203-add-intent.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ test_expect_success 'diff-files/diff-cached shows ita as new/not-new files' '
245245
test_cmp expected2 actual2
246246
'
247247

248+
248249
test_expect_success '"diff HEAD" includes ita as new files' '
249250
git reset --hard &&
250251
echo new >new-ita &&
@@ -262,4 +263,16 @@ test_expect_success '"diff HEAD" includes ita as new files' '
262263
test_cmp expected actual
263264
'
264265

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+
265278
test_done

0 commit comments

Comments
 (0)