mirror of
https://github.com/git/git.git
synced 2024-11-24 18:33:43 +08:00
git_config_set: do not use a state machine
While a neat theoretical construct, state machines are hard to read. In this instance, it does not even make a whole lot of sense because we are more interested in flags, anyway: has the section been seen? Has the key been seen? Does the current section match the key we are looking for? Besides, the state `SECTION_SEEN` was named in a misleading way: it did not indicate that we saw the section matching the key we are looking for, but it instead indicated that we are *currently* in that section. Let's just replace the state machine logic by clear and obvious flags. This will also make it easier to review the upcoming patches to use the newly-introduced `event_fn` callback of the config parser. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
parent
668b9ade6b
commit
5221c3159f
59
config.c
59
config.c
@ -2296,7 +2296,7 @@ struct config_store_data {
|
||||
int multi_replace;
|
||||
size_t *seen;
|
||||
unsigned int seen_nr, seen_alloc;
|
||||
enum { START, SECTION_SEEN, SECTION_END_SEEN, KEY_SEEN } state;
|
||||
unsigned int key_seen:1, section_seen:1, is_keys_section:1;
|
||||
};
|
||||
|
||||
static int matches(const char *key, const char *value,
|
||||
@ -2319,8 +2319,7 @@ static int store_aux(const char *key, const char *value, void *cb)
|
||||
size_t section_len;
|
||||
struct config_store_data *store = cb;
|
||||
|
||||
switch (store->state) {
|
||||
case KEY_SEEN:
|
||||
if (store->key_seen) {
|
||||
if (matches(key, value, store)) {
|
||||
if (store->seen_nr == 1 && store->multi_replace == 0) {
|
||||
warning(_("%s has multiple values"), key);
|
||||
@ -2332,8 +2331,8 @@ static int store_aux(const char *key, const char *value, void *cb)
|
||||
store->seen[store->seen_nr] = cf->do_ftell(cf);
|
||||
store->seen_nr++;
|
||||
}
|
||||
break;
|
||||
case SECTION_SEEN:
|
||||
return 0;
|
||||
} else if (store->is_keys_section) {
|
||||
/*
|
||||
* What we are looking for is in store->key (both
|
||||
* section and var), and its section part is baselen
|
||||
@ -2348,10 +2347,9 @@ static int store_aux(const char *key, const char *value, void *cb)
|
||||
|
||||
if ((section_len != store->baselen) ||
|
||||
memcmp(key, store->key, section_len+1)) {
|
||||
store->state = SECTION_END_SEEN;
|
||||
break;
|
||||
store->is_keys_section = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Do not increment matches: this is no match, but we
|
||||
* just made sure we are in the desired section.
|
||||
@ -2359,27 +2357,29 @@ static int store_aux(const char *key, const char *value, void *cb)
|
||||
ALLOC_GROW(store->seen, store->seen_nr + 1,
|
||||
store->seen_alloc);
|
||||
store->seen[store->seen_nr] = cf->do_ftell(cf);
|
||||
/* fallthru */
|
||||
case SECTION_END_SEEN:
|
||||
case START:
|
||||
if (matches(key, value, store)) {
|
||||
ALLOC_GROW(store->seen, store->seen_nr + 1,
|
||||
store->seen_alloc);
|
||||
store->seen[store->seen_nr] = cf->do_ftell(cf);
|
||||
store->state = KEY_SEEN;
|
||||
store->seen_nr++;
|
||||
} else {
|
||||
if (strrchr(key, '.') - key == store->baselen &&
|
||||
!strncmp(key, store->key, store->baselen)) {
|
||||
store->state = SECTION_SEEN;
|
||||
ALLOC_GROW(store->seen,
|
||||
store->seen_nr + 1,
|
||||
store->seen_alloc);
|
||||
store->seen[store->seen_nr] =
|
||||
cf->do_ftell(cf);
|
||||
}
|
||||
}
|
||||
|
||||
if (matches(key, value, store)) {
|
||||
ALLOC_GROW(store->seen, store->seen_nr + 1,
|
||||
store->seen_alloc);
|
||||
store->seen[store->seen_nr] = cf->do_ftell(cf);
|
||||
store->seen_nr++;
|
||||
store->key_seen = 1;
|
||||
store->section_seen = 1;
|
||||
store->is_keys_section = 1;
|
||||
} else {
|
||||
if (strrchr(key, '.') - key == store->baselen &&
|
||||
!strncmp(key, store->key, store->baselen)) {
|
||||
store->section_seen = 1;
|
||||
store->is_keys_section = 1;
|
||||
ALLOC_GROW(store->seen,
|
||||
store->seen_nr + 1,
|
||||
store->seen_alloc);
|
||||
store->seen[store->seen_nr] =
|
||||
cf->do_ftell(cf);
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -2637,7 +2637,6 @@ int git_config_set_multivar_in_file_gently(const char *config_filename,
|
||||
|
||||
ALLOC_GROW(store.seen, 1, store.seen_alloc);
|
||||
store.seen[0] = 0;
|
||||
store.state = START;
|
||||
store.seen_nr = 0;
|
||||
|
||||
/*
|
||||
@ -2705,7 +2704,7 @@ int git_config_set_multivar_in_file_gently(const char *config_filename,
|
||||
new_line = 0;
|
||||
if (store.seen[i] == 0) {
|
||||
store.seen[i] = copy_end = contents_sz;
|
||||
} else if (store.state != KEY_SEEN) {
|
||||
} else if (!store.key_seen) {
|
||||
copy_end = store.seen[i];
|
||||
} else
|
||||
copy_end = find_beginning_of_line(
|
||||
@ -2729,7 +2728,7 @@ int git_config_set_multivar_in_file_gently(const char *config_filename,
|
||||
|
||||
/* write the pair (value == NULL means unset) */
|
||||
if (value != NULL) {
|
||||
if (store.state == START) {
|
||||
if (!store.section_seen) {
|
||||
if (write_section(fd, key, &store) < 0)
|
||||
goto write_err_out;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user