提交 4f4d0b42 编写于 作者: J Junio C Hamano

Merge branch 'js/empty-config-section-fix'

"git config --unset a.b", when "a.b" is the last variable in an
otherwise empty section "a", left an empty section "a" behind, and
worse yet, a subsequent "git config a.c value" did not reuse that
empty shell and instead created a new one.  These have been
(partially) corrected.

* js/empty-config-section-fix:
  git_config_set: reuse empty sections
  git config --unset: remove empty sections (in the common case)
  git_config_set: make use of the config parser's event stream
  git_config_set: do not use a state machine
  config_set_store: rename some fields for consistency
  config: avoid using the global variable `store`
  config: introduce an optional event stream while parsing
  t1300: `--unset-all` can leave an empty section behind (bug)
  t1300: add a few more hairy examples of sections becoming empty
  t1300: remove unreasonable expectation from TODO
  t1300: avoid relying on a bug
  config --replace-all: avoid extra line breaks
  t1300: demonstrate that --replace-all can "invent" newlines
  t1300: rename it to reflect that `repo-config` was deprecated
  git_config_set: fix off-by-two
此差异已折叠。
......@@ -28,15 +28,40 @@ enum config_origin_type {
CONFIG_ORIGIN_CMDLINE
};
enum config_event_t {
CONFIG_EVENT_SECTION,
CONFIG_EVENT_ENTRY,
CONFIG_EVENT_WHITESPACE,
CONFIG_EVENT_COMMENT,
CONFIG_EVENT_EOF,
CONFIG_EVENT_ERROR
};
/*
* The parser event function (if not NULL) is called with the event type and
* the begin/end offsets of the parsed elements.
*
* Note: for CONFIG_EVENT_ENTRY (i.e. config variables), the trailing newline
* character is considered part of the element.
*/
typedef int (*config_parser_event_fn_t)(enum config_event_t type,
size_t begin_offset, size_t end_offset,
void *event_fn_data);
struct config_options {
unsigned int respect_includes : 1;
const char *commondir;
const char *git_dir;
config_parser_event_fn_t event_fn;
void *event_fn_data;
};
typedef int (*config_fn_t)(const char *, const char *, void *);
extern int git_default_config(const char *, const char *, void *);
extern int git_config_from_file(config_fn_t fn, const char *, void *);
extern int git_config_from_file_with_options(config_fn_t fn, const char *,
void *,
const struct config_options *);
extern int git_config_from_mem(config_fn_t fn, const enum config_origin_type,
const char *name, const char *buf, size_t len, void *data);
extern int git_config_from_blob_oid(config_fn_t fn, const char *name,
......
......@@ -108,6 +108,7 @@ bar = foo
[beta]
baz = multiple \
lines
foo = bar
EOF
test_expect_success 'unset with cont. lines' '
......@@ -118,6 +119,7 @@ cat > expect <<\EOF
[alpha]
bar = foo
[beta]
foo = bar
EOF
test_expect_success 'unset with cont. lines is correct' 'test_cmp expect .git/config'
......@@ -1411,7 +1413,7 @@ test_expect_success 'urlmatch with wildcard' '
'
# good section hygiene
test_expect_failure 'unsetting the last key in a section removes header' '
test_expect_success '--unset last key removes section (except if commented)' '
cat >.git/config <<-\EOF &&
# some generic comment on the configuration file itself
# a comment specific to this "section" section.
......@@ -1425,13 +1427,86 @@ test_expect_failure 'unsetting the last key in a section removes header' '
cat >expect <<-\EOF &&
# some generic comment on the configuration file itself
# a comment specific to this "section" section.
[section]
# some intervening lines
# that should also be dropped
# please be careful when you update the above variable
EOF
git config --unset section.key &&
test_cmp expect .git/config
test_cmp expect .git/config &&
cat >.git/config <<-\EOF &&
[section]
key = value
[next-section]
EOF
cat >expect <<-\EOF &&
[next-section]
EOF
git config --unset section.key &&
test_cmp expect .git/config &&
q_to_tab >.git/config <<-\EOF &&
[one]
Qkey = "multiline \
QQ# with comment"
[two]
key = true
EOF
git config --unset two.key &&
! grep two .git/config &&
q_to_tab >.git/config <<-\EOF &&
[one]
Qkey = "multiline \
QQ# with comment"
[one]
key = true
EOF
git config --unset-all one.key &&
test_line_count = 0 .git/config &&
q_to_tab >.git/config <<-\EOF &&
[one]
Qkey = true
Q# a comment not at the start
[two]
Qkey = true
EOF
git config --unset two.key &&
grep two .git/config &&
q_to_tab >.git/config <<-\EOF &&
[one]
Qkey = not [two "subsection"]
[two "subsection"]
[two "subsection"]
Qkey = true
[TWO "subsection"]
[one]
EOF
git config --unset two.subsection.key &&
test "not [two subsection]" = "$(git config one.key)" &&
test_line_count = 3 .git/config
'
test_expect_success '--unset-all removes section if empty & uncommented' '
cat >.git/config <<-\EOF &&
[section]
key = value1
key = value2
EOF
git config --unset-all section.key &&
test_line_count = 0 .git/config
'
test_expect_failure 'adding a key into an empty section reuses header' '
test_expect_success 'adding a key into an empty section reuses header' '
cat >.git/config <<-\EOF &&
[section]
EOF
......@@ -1611,4 +1686,25 @@ test_expect_success '--local requires a repo' '
test_expect_code 128 nongit git config --local foo.bar
'
test_expect_success '--replace-all does not invent newlines' '
q_to_tab >.git/config <<-\EOF &&
[abc]key
QkeepSection
[xyz]
Qkey = 1
[abc]
Qkey = a
EOF
q_to_tab >expect <<-\EOF &&
[abc]
QkeepSection
[xyz]
Qkey = 1
[abc]
Qkey = b
EOF
git config --replace-all abc.key b &&
test_cmp .git/config expect
'
test_done
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册