mirror of
https://github.com/git/git.git
synced 2024-11-18 00:13:58 +01:00
builtin/apply: move 'ws_error_action' into 'struct apply_state'
To libify the apply functionality the 'ws_error_action' variable should not be static and global to the file. Let's move it into 'struct apply_state'. Signed-off-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
parent
7243f5f350
commit
e9c6b279b8
@ -21,6 +21,13 @@
|
|||||||
#include "ll-merge.h"
|
#include "ll-merge.h"
|
||||||
#include "rerere.h"
|
#include "rerere.h"
|
||||||
|
|
||||||
|
enum ws_error_action {
|
||||||
|
nowarn_ws_error,
|
||||||
|
warn_on_ws_error,
|
||||||
|
die_on_ws_error,
|
||||||
|
correct_ws_error
|
||||||
|
};
|
||||||
|
|
||||||
struct apply_state {
|
struct apply_state {
|
||||||
const char *prefix;
|
const char *prefix;
|
||||||
int prefix_length;
|
int prefix_length;
|
||||||
@ -61,6 +68,7 @@ struct apply_state {
|
|||||||
int has_include;
|
int has_include;
|
||||||
|
|
||||||
/* These control whitespace errors */
|
/* These control whitespace errors */
|
||||||
|
enum ws_error_action ws_error_action;
|
||||||
const char *whitespace_option;
|
const char *whitespace_option;
|
||||||
int whitespace_error;
|
int whitespace_error;
|
||||||
int squelch_whitespace_errors;
|
int squelch_whitespace_errors;
|
||||||
@ -74,12 +82,6 @@ static const char * const apply_usage[] = {
|
|||||||
NULL
|
NULL
|
||||||
};
|
};
|
||||||
|
|
||||||
static enum ws_error_action {
|
|
||||||
nowarn_ws_error,
|
|
||||||
warn_on_ws_error,
|
|
||||||
die_on_ws_error,
|
|
||||||
correct_ws_error
|
|
||||||
} ws_error_action = warn_on_ws_error;
|
|
||||||
|
|
||||||
static enum ws_ignore {
|
static enum ws_ignore {
|
||||||
ignore_ws_none,
|
ignore_ws_none,
|
||||||
@ -90,28 +92,28 @@ static enum ws_ignore {
|
|||||||
static void parse_whitespace_option(struct apply_state *state, const char *option)
|
static void parse_whitespace_option(struct apply_state *state, const char *option)
|
||||||
{
|
{
|
||||||
if (!option) {
|
if (!option) {
|
||||||
ws_error_action = warn_on_ws_error;
|
state->ws_error_action = warn_on_ws_error;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!strcmp(option, "warn")) {
|
if (!strcmp(option, "warn")) {
|
||||||
ws_error_action = warn_on_ws_error;
|
state->ws_error_action = warn_on_ws_error;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!strcmp(option, "nowarn")) {
|
if (!strcmp(option, "nowarn")) {
|
||||||
ws_error_action = nowarn_ws_error;
|
state->ws_error_action = nowarn_ws_error;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!strcmp(option, "error")) {
|
if (!strcmp(option, "error")) {
|
||||||
ws_error_action = die_on_ws_error;
|
state->ws_error_action = die_on_ws_error;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!strcmp(option, "error-all")) {
|
if (!strcmp(option, "error-all")) {
|
||||||
ws_error_action = die_on_ws_error;
|
state->ws_error_action = die_on_ws_error;
|
||||||
state->squelch_whitespace_errors = 0;
|
state->squelch_whitespace_errors = 0;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!strcmp(option, "strip") || !strcmp(option, "fix")) {
|
if (!strcmp(option, "strip") || !strcmp(option, "fix")) {
|
||||||
ws_error_action = correct_ws_error;
|
state->ws_error_action = correct_ws_error;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
die(_("unrecognized whitespace option '%s'"), option);
|
die(_("unrecognized whitespace option '%s'"), option);
|
||||||
@ -135,7 +137,7 @@ static void parse_ignorewhitespace_option(const char *option)
|
|||||||
static void set_default_whitespace_mode(struct apply_state *state)
|
static void set_default_whitespace_mode(struct apply_state *state)
|
||||||
{
|
{
|
||||||
if (!state->whitespace_option && !apply_default_whitespace)
|
if (!state->whitespace_option && !apply_default_whitespace)
|
||||||
ws_error_action = (state->apply ? warn_on_ws_error : nowarn_ws_error);
|
state->ws_error_action = (state->apply ? warn_on_ws_error : nowarn_ws_error);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -1670,12 +1672,12 @@ static int parse_fragment(struct apply_state *state,
|
|||||||
leading++;
|
leading++;
|
||||||
trailing++;
|
trailing++;
|
||||||
if (!state->apply_in_reverse &&
|
if (!state->apply_in_reverse &&
|
||||||
ws_error_action == correct_ws_error)
|
state->ws_error_action == correct_ws_error)
|
||||||
check_whitespace(state, line, len, patch->ws_rule);
|
check_whitespace(state, line, len, patch->ws_rule);
|
||||||
break;
|
break;
|
||||||
case '-':
|
case '-':
|
||||||
if (state->apply_in_reverse &&
|
if (state->apply_in_reverse &&
|
||||||
ws_error_action != nowarn_ws_error)
|
state->ws_error_action != nowarn_ws_error)
|
||||||
check_whitespace(state, line, len, patch->ws_rule);
|
check_whitespace(state, line, len, patch->ws_rule);
|
||||||
deleted++;
|
deleted++;
|
||||||
oldlines--;
|
oldlines--;
|
||||||
@ -1683,7 +1685,7 @@ static int parse_fragment(struct apply_state *state,
|
|||||||
break;
|
break;
|
||||||
case '+':
|
case '+':
|
||||||
if (!state->apply_in_reverse &&
|
if (!state->apply_in_reverse &&
|
||||||
ws_error_action != nowarn_ws_error)
|
state->ws_error_action != nowarn_ws_error)
|
||||||
check_whitespace(state, line, len, patch->ws_rule);
|
check_whitespace(state, line, len, patch->ws_rule);
|
||||||
added++;
|
added++;
|
||||||
newlines--;
|
newlines--;
|
||||||
@ -2396,7 +2398,8 @@ static int line_by_line_fuzzy_match(struct image *img,
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int match_fragment(struct image *img,
|
static int match_fragment(struct apply_state *state,
|
||||||
|
struct image *img,
|
||||||
struct image *preimage,
|
struct image *preimage,
|
||||||
struct image *postimage,
|
struct image *postimage,
|
||||||
unsigned long try,
|
unsigned long try,
|
||||||
@ -2417,7 +2420,7 @@ static int match_fragment(struct image *img,
|
|||||||
preimage_limit = preimage->nr;
|
preimage_limit = preimage->nr;
|
||||||
if (match_end && (preimage->nr + try_lno != img->nr))
|
if (match_end && (preimage->nr + try_lno != img->nr))
|
||||||
return 0;
|
return 0;
|
||||||
} else if (ws_error_action == correct_ws_error &&
|
} else if (state->ws_error_action == correct_ws_error &&
|
||||||
(ws_rule & WS_BLANK_AT_EOF)) {
|
(ws_rule & WS_BLANK_AT_EOF)) {
|
||||||
/*
|
/*
|
||||||
* This hunk extends beyond the end of img, and we are
|
* This hunk extends beyond the end of img, and we are
|
||||||
@ -2489,7 +2492,7 @@ static int match_fragment(struct image *img,
|
|||||||
return line_by_line_fuzzy_match(img, preimage, postimage,
|
return line_by_line_fuzzy_match(img, preimage, postimage,
|
||||||
try, try_lno, preimage_limit);
|
try, try_lno, preimage_limit);
|
||||||
|
|
||||||
if (ws_error_action != correct_ws_error)
|
if (state->ws_error_action != correct_ws_error)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -2601,7 +2604,8 @@ static int match_fragment(struct image *img,
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int find_pos(struct image *img,
|
static int find_pos(struct apply_state *state,
|
||||||
|
struct image *img,
|
||||||
struct image *preimage,
|
struct image *preimage,
|
||||||
struct image *postimage,
|
struct image *postimage,
|
||||||
int line,
|
int line,
|
||||||
@ -2645,7 +2649,7 @@ static int find_pos(struct image *img,
|
|||||||
try_lno = line;
|
try_lno = line;
|
||||||
|
|
||||||
for (i = 0; ; i++) {
|
for (i = 0; ; i++) {
|
||||||
if (match_fragment(img, preimage, postimage,
|
if (match_fragment(state, img, preimage, postimage,
|
||||||
try, try_lno, ws_rule,
|
try, try_lno, ws_rule,
|
||||||
match_beginning, match_end))
|
match_beginning, match_end))
|
||||||
return try_lno;
|
return try_lno;
|
||||||
@ -2858,7 +2862,7 @@ static int apply_one_fragment(struct apply_state *state,
|
|||||||
start = newlines.len;
|
start = newlines.len;
|
||||||
if (first != '+' ||
|
if (first != '+' ||
|
||||||
!state->whitespace_error ||
|
!state->whitespace_error ||
|
||||||
ws_error_action != correct_ws_error) {
|
state->ws_error_action != correct_ws_error) {
|
||||||
strbuf_add(&newlines, patch + 1, plen);
|
strbuf_add(&newlines, patch + 1, plen);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@ -2936,7 +2940,7 @@ static int apply_one_fragment(struct apply_state *state,
|
|||||||
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
|
|
||||||
applied_pos = find_pos(img, &preimage, &postimage, pos,
|
applied_pos = find_pos(state, img, &preimage, &postimage, pos,
|
||||||
ws_rule, match_beginning, match_end);
|
ws_rule, match_beginning, match_end);
|
||||||
|
|
||||||
if (applied_pos >= 0)
|
if (applied_pos >= 0)
|
||||||
@ -2972,10 +2976,10 @@ static int apply_one_fragment(struct apply_state *state,
|
|||||||
if (new_blank_lines_at_end &&
|
if (new_blank_lines_at_end &&
|
||||||
preimage.nr + applied_pos >= img->nr &&
|
preimage.nr + applied_pos >= img->nr &&
|
||||||
(ws_rule & WS_BLANK_AT_EOF) &&
|
(ws_rule & WS_BLANK_AT_EOF) &&
|
||||||
ws_error_action != nowarn_ws_error) {
|
state->ws_error_action != nowarn_ws_error) {
|
||||||
record_ws_error(state, WS_BLANK_AT_EOF, "+", 1,
|
record_ws_error(state, WS_BLANK_AT_EOF, "+", 1,
|
||||||
found_new_blank_lines_at_end);
|
found_new_blank_lines_at_end);
|
||||||
if (ws_error_action == correct_ws_error) {
|
if (state->ws_error_action == correct_ws_error) {
|
||||||
while (new_blank_lines_at_end--)
|
while (new_blank_lines_at_end--)
|
||||||
remove_last_line(&postimage);
|
remove_last_line(&postimage);
|
||||||
}
|
}
|
||||||
@ -2986,7 +2990,7 @@ static int apply_one_fragment(struct apply_state *state,
|
|||||||
* apply_patch->check_patch_list->check_patch->
|
* apply_patch->check_patch_list->check_patch->
|
||||||
* apply_data->apply_fragments->apply_one_fragment
|
* apply_data->apply_fragments->apply_one_fragment
|
||||||
*/
|
*/
|
||||||
if (ws_error_action == die_on_ws_error)
|
if (state->ws_error_action == die_on_ws_error)
|
||||||
state->apply = 0;
|
state->apply = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -4530,7 +4534,7 @@ static int apply_patch(struct apply_state *state,
|
|||||||
if (!list && !skipped_patch)
|
if (!list && !skipped_patch)
|
||||||
die(_("unrecognized input"));
|
die(_("unrecognized input"));
|
||||||
|
|
||||||
if (state->whitespace_error && (ws_error_action == die_on_ws_error))
|
if (state->whitespace_error && (state->ws_error_action == die_on_ws_error))
|
||||||
state->apply = 0;
|
state->apply = 0;
|
||||||
|
|
||||||
state->update_index = state->check_index && state->apply;
|
state->update_index = state->check_index && state->apply;
|
||||||
@ -4645,6 +4649,7 @@ static void init_apply_state(struct apply_state *state, const char *prefix)
|
|||||||
state->p_value = 1;
|
state->p_value = 1;
|
||||||
state->p_context = UINT_MAX;
|
state->p_context = UINT_MAX;
|
||||||
state->squelch_whitespace_errors = 5;
|
state->squelch_whitespace_errors = 5;
|
||||||
|
state->ws_error_action = warn_on_ws_error;
|
||||||
strbuf_init(&state->root, 0);
|
strbuf_init(&state->root, 0);
|
||||||
|
|
||||||
git_apply_config();
|
git_apply_config();
|
||||||
@ -4801,7 +4806,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix)
|
|||||||
squelched),
|
squelched),
|
||||||
squelched);
|
squelched);
|
||||||
}
|
}
|
||||||
if (ws_error_action == die_on_ws_error)
|
if (state.ws_error_action == die_on_ws_error)
|
||||||
die(Q_("%d line adds whitespace errors.",
|
die(Q_("%d line adds whitespace errors.",
|
||||||
"%d lines add whitespace errors.",
|
"%d lines add whitespace errors.",
|
||||||
state.whitespace_error),
|
state.whitespace_error),
|
||||||
|
Loading…
Reference in New Issue
Block a user