apply: change error_routine when silent
To avoid printing anything when applying with `state->apply_verbosity == verbosity_silent`, let's save the existing warn and error routines before applying, and let's replace them with a routine that does nothing. Then after applying, let's restore the saved routines. Note that, as we need to restore the saved routines in all cases, we cannot return early any more in apply_all_patches(). Helped-by: Stefan Beller <sbeller@google.com> Signed-off-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Christian Couder committed
Sep 4, 2016 at 22:18 UTC
45b78d8ba3c9e542f1375171090fe10baef6b2b2
2 files changed
+28
-1
apply.c
+20
-1
@@ -112,6 +112,11 @@ void clear_apply_state(struct apply_state *state)
112
/* &state->fn_table is cleared at the end of apply_patch() */
113
}
114
115
+static void mute_routine(const char *msg, va_list params)
116
+{
117
+ /* do nothing */
118
+}
119
+
120
int check_apply_state(struct apply_state *state, int force_apply)
121
{
122
int is_not_gitdir = !startup_info->have_repository;
@@ -144,6 +149,13 @@ int check_apply_state(struct apply_state *state, int force_apply)
149
if (!state->lock_file)
150
return error("BUG: state->lock_file should not be NULL");
151
152
+ if (state->apply_verbosity <= verbosity_silent) {
153
+ state->saved_error_routine = get_error_routine();
154
+ state->saved_warn_routine = get_warn_routine();
155
+ set_error_routine(mute_routine);
156
+ set_warn_routine(mute_routine);
157
+ }
158
+
159
return 0;
160
}
161
@@ -4864,7 +4876,7 @@ int apply_all_patches(struct apply_state *state,
4876
state->newfd = -1;
4877
}
4878
4867
- return !!errs;
4879
+ res = !!errs;
4880
4881
end:
4882
if (state->newfd >= 0) {
@@ -4872,5 +4884,12 @@ end:
4884
state->newfd = -1;
4885
}
4886
4887
+ if (state->apply_verbosity <= verbosity_silent) {
4888
+ set_error_routine(state->saved_error_routine);
4889
+ set_warn_routine(state->saved_warn_routine);
4890
+ }
4891
+
4892
+ if (res > -1)
4893
+ return res;
4894
return (res == -1 ? 1 : 128);
4895
}
apply.h
+8
@@ -94,6 +94,14 @@ struct apply_state {
94
*/
95
struct string_list fn_table;
96
97
+ /*
98
+ * This is to save reporting routines before using
99
+ * set_error_routine() or set_warn_routine() to install muting
100
+ * routines when in verbosity_silent mode.
101
+ */
102
+ void (*saved_error_routine)(const char *err, va_list params);
103
+ void (*saved_warn_routine)(const char *warn, va_list params);
104
+
105
/* These control whitespace errors */
106
enum apply_ws_error_action ws_error_action;
107
enum apply_ws_ignore ws_ignore_action;