Skip to content

Commit a46160d

Browse files
chriscoolgitster
authored andcommitted
apply: make it possible to silently apply
This changes 'int apply_verbosely' into 'enum apply_verbosity', and changes the possible values of the variable from a bool to a tristate. The previous 'false' state is changed into 'verbosity_normal'. The previous 'true' state is changed into 'verbosity_verbose'. The new added state is 'verbosity_silent'. It should prevent anything to be printed on both stderr and stdout. This is needed because `git am` wants to first call apply functionality silently, if it can then fall back on 3-way merge in case of error. Printing on stdout, and calls to warning() or error() are not taken care of in this patch, as that will be done in following patches. Signed-off-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 90875ec commit a46160d

File tree

3 files changed

+48
-24
lines changed

3 files changed

+48
-24
lines changed

apply.c

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,11 @@ int check_apply_state(struct apply_state *state, int force_apply)
125125
return error(_("--3way outside a repository"));
126126
state->check_index = 1;
127127
}
128-
if (state->apply_with_reject)
129-
state->apply = state->apply_verbosely = 1;
128+
if (state->apply_with_reject) {
129+
state->apply = 1;
130+
if (state->apply_verbosity == verbosity_normal)
131+
state->apply_verbosity = verbosity_verbose;
132+
}
130133
if (!force_apply && (state->diffstat || state->numstat || state->summary || state->check || state->fake_ancestor))
131134
state->apply = 0;
132135
if (state->check_index && is_not_gitdir)
@@ -1620,8 +1623,9 @@ static void record_ws_error(struct apply_state *state,
16201623
return;
16211624

16221625
err = whitespace_error_string(result);
1623-
fprintf(stderr, "%s:%d: %s.\n%.*s\n",
1624-
state->patch_input_file, linenr, err, len, line);
1626+
if (state->apply_verbosity > verbosity_silent)
1627+
fprintf(stderr, "%s:%d: %s.\n%.*s\n",
1628+
state->patch_input_file, linenr, err, len, line);
16251629
free(err);
16261630
}
16271631

@@ -1816,7 +1820,7 @@ static int parse_single_patch(struct apply_state *state,
18161820
return error(_("new file %s depends on old contents"), patch->new_name);
18171821
if (0 < patch->is_delete && newlines)
18181822
return error(_("deleted file %s still has contents"), patch->old_name);
1819-
if (!patch->is_delete && !newlines && context)
1823+
if (!patch->is_delete && !newlines && context && state->apply_verbosity > verbosity_silent)
18201824
fprintf_ln(stderr,
18211825
_("** warning: "
18221826
"file %s becomes empty but is not deleted"),
@@ -2911,7 +2915,7 @@ static int apply_one_fragment(struct apply_state *state,
29112915
/* Ignore it, we already handled it */
29122916
break;
29132917
default:
2914-
if (state->apply_verbosely)
2918+
if (state->apply_verbosity > verbosity_normal)
29152919
error(_("invalid start of line: '%c'"), first);
29162920
applied_pos = -1;
29172921
goto out;
@@ -3026,7 +3030,7 @@ static int apply_one_fragment(struct apply_state *state,
30263030
state->apply = 0;
30273031
}
30283032

3029-
if (state->apply_verbosely && applied_pos != pos) {
3033+
if (state->apply_verbosity > verbosity_normal && applied_pos != pos) {
30303034
int offset = applied_pos - pos;
30313035
if (state->apply_in_reverse)
30323036
offset = 0 - offset;
@@ -3041,14 +3045,14 @@ static int apply_one_fragment(struct apply_state *state,
30413045
* Warn if it was necessary to reduce the number
30423046
* of context lines.
30433047
*/
3044-
if ((leading != frag->leading) ||
3045-
(trailing != frag->trailing))
3048+
if ((leading != frag->leading ||
3049+
trailing != frag->trailing) && state->apply_verbosity > verbosity_silent)
30463050
fprintf_ln(stderr, _("Context reduced to (%ld/%ld)"
30473051
" to apply fragment at %d"),
30483052
leading, trailing, applied_pos+1);
30493053
update_image(state, img, applied_pos, &preimage, &postimage);
30503054
} else {
3051-
if (state->apply_verbosely)
3055+
if (state->apply_verbosity > verbosity_normal)
30523056
error(_("while searching for:\n%.*s"),
30533057
(int)(old - oldlines), oldlines);
30543058
}
@@ -3539,7 +3543,8 @@ static int try_threeway(struct apply_state *state,
35393543
read_blob_object(&buf, pre_sha1, patch->old_mode))
35403544
return error("repository lacks the necessary blob to fall back on 3-way merge.");
35413545

3542-
fprintf(stderr, "Falling back to three-way merge...\n");
3546+
if (state->apply_verbosity > verbosity_silent)
3547+
fprintf(stderr, "Falling back to three-way merge...\n");
35433548

35443549
img = strbuf_detach(&buf, &len);
35453550
prepare_image(&tmp_image, img, len, 1);
@@ -3569,7 +3574,9 @@ static int try_threeway(struct apply_state *state,
35693574
status = three_way_merge(image, patch->new_name,
35703575
pre_sha1, our_sha1, post_sha1);
35713576
if (status < 0) {
3572-
fprintf(stderr, "Failed to fall back on three-way merge...\n");
3577+
if (state->apply_verbosity > verbosity_silent)
3578+
fprintf(stderr,
3579+
"Failed to fall back on three-way merge...\n");
35733580
return status;
35743581
}
35753582

@@ -3581,9 +3588,15 @@ static int try_threeway(struct apply_state *state,
35813588
hashcpy(patch->threeway_stage[0].hash, pre_sha1);
35823589
hashcpy(patch->threeway_stage[1].hash, our_sha1);
35833590
hashcpy(patch->threeway_stage[2].hash, post_sha1);
3584-
fprintf(stderr, "Applied patch to '%s' with conflicts.\n", patch->new_name);
3591+
if (state->apply_verbosity > verbosity_silent)
3592+
fprintf(stderr,
3593+
"Applied patch to '%s' with conflicts.\n",
3594+
patch->new_name);
35853595
} else {
3586-
fprintf(stderr, "Applied patch to '%s' cleanly.\n", patch->new_name);
3596+
if (state->apply_verbosity > verbosity_silent)
3597+
fprintf(stderr,
3598+
"Applied patch to '%s' cleanly.\n",
3599+
patch->new_name);
35873600
}
35883601
return 0;
35893602
}
@@ -3956,7 +3969,7 @@ static int check_patch_list(struct apply_state *state, struct patch *patch)
39563969
prepare_fn_table(state, patch);
39573970
while (patch) {
39583971
int res;
3959-
if (state->apply_verbosely)
3972+
if (state->apply_verbosity > verbosity_normal)
39603973
say_patch_name(stderr,
39613974
_("Checking patch %s..."), patch);
39623975
res = check_patch(state, patch);
@@ -4472,7 +4485,7 @@ static int write_out_one_reject(struct apply_state *state, struct patch *patch)
44724485
}
44734486

44744487
if (!cnt) {
4475-
if (state->apply_verbosely)
4488+
if (state->apply_verbosity > verbosity_normal)
44764489
say_patch_name(stderr,
44774490
_("Applied patch %s cleanly."), patch);
44784491
return 0;
@@ -4489,7 +4502,8 @@ static int write_out_one_reject(struct apply_state *state, struct patch *patch)
44894502
"Applying patch %%s with %d rejects...",
44904503
cnt),
44914504
cnt);
4492-
say_patch_name(stderr, sb.buf, patch);
4505+
if (state->apply_verbosity > verbosity_silent)
4506+
say_patch_name(stderr, sb.buf, patch);
44934507
strbuf_release(&sb);
44944508

44954509
cnt = strlen(patch->new_name);
@@ -4516,10 +4530,12 @@ static int write_out_one_reject(struct apply_state *state, struct patch *patch)
45164530
frag;
45174531
cnt++, frag = frag->next) {
45184532
if (!frag->rejected) {
4519-
fprintf_ln(stderr, _("Hunk #%d applied cleanly."), cnt);
4533+
if (state->apply_verbosity > verbosity_silent)
4534+
fprintf_ln(stderr, _("Hunk #%d applied cleanly."), cnt);
45204535
continue;
45214536
}
4522-
fprintf_ln(stderr, _("Rejected hunk #%d."), cnt);
4537+
if (state->apply_verbosity > verbosity_silent)
4538+
fprintf_ln(stderr, _("Rejected hunk #%d."), cnt);
45234539
fprintf(rej, "%.*s", frag->size, frag->patch);
45244540
if (frag->patch[frag->size-1] != '\n')
45254541
fputc('\n', rej);
@@ -4568,8 +4584,10 @@ static int write_out_results(struct apply_state *state, struct patch *list)
45684584
struct string_list_item *item;
45694585

45704586
string_list_sort(&cpath);
4571-
for_each_string_list_item(item, &cpath)
4572-
fprintf(stderr, "U %s\n", item->string);
4587+
if (state->apply_verbosity > verbosity_silent) {
4588+
for_each_string_list_item(item, &cpath)
4589+
fprintf(stderr, "U %s\n", item->string);
4590+
}
45734591
string_list_clear(&cpath, 0);
45744592

45754593
rerere(0);
@@ -4626,7 +4644,7 @@ static int apply_patch(struct apply_state *state,
46264644
listp = &patch->next;
46274645
}
46284646
else {
4629-
if (state->apply_verbosely)
4647+
if (state->apply_verbosity > verbosity_normal)
46304648
say_patch_name(stderr, _("Skipped patch '%s'."), patch);
46314649
free_patch(patch);
46324650
skipped_patch++;

apply.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ enum apply_ws_ignore {
1313
ignore_ws_change
1414
};
1515

16+
enum apply_verbosity {
17+
verbosity_silent = -1,
18+
verbosity_normal = 0,
19+
verbosity_verbose = 1
20+
};
21+
1622
/*
1723
* We need to keep track of how symlinks in the preimage are
1824
* manipulated by the patches. A patch to add a/b/c where a/b
@@ -51,13 +57,13 @@ struct apply_state {
5157
int allow_overlap;
5258
int apply_in_reverse;
5359
int apply_with_reject;
54-
int apply_verbosely;
5560
int no_add;
5661
int threeway;
5762
int unidiff_zero;
5863
int unsafe_paths;
5964

6065
/* Other non boolean parameters */
66+
enum apply_verbosity apply_verbosity;
6167
const char *fake_ancestor;
6268
const char *patch_input_file;
6369
int line_termination;

builtin/apply.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix)
7474
N_("leave the rejected hunks in corresponding *.rej files")),
7575
OPT_BOOL(0, "allow-overlap", &state.allow_overlap,
7676
N_("allow overlapping hunks")),
77-
OPT__VERBOSE(&state.apply_verbosely, N_("be verbose")),
77+
OPT__VERBOSE(&state.apply_verbosity, N_("be verbose")),
7878
OPT_BIT(0, "inaccurate-eof", &options,
7979
N_("tolerate incorrectly detected missing new-line at the end of file"),
8080
APPLY_OPT_INACCURATE_EOF),

0 commit comments

Comments
 (0)