mirror of
https://github.com/git/git
synced 2024-09-13 05:14:36 +00:00
Merge branch 'av/maint-config-reader'
* av/maint-config-reader: After renaming a section, print any trailing variable definitions Make section_name_match start on '[', and return the length on success
This commit is contained in:
commit
a7c1ef3e03
41
config.c
41
config.c
|
@ -1174,7 +1174,9 @@ int git_config_set_multivar(const char *key, const char *value,
|
||||||
static int section_name_match (const char *buf, const char *name)
|
static int section_name_match (const char *buf, const char *name)
|
||||||
{
|
{
|
||||||
int i = 0, j = 0, dot = 0;
|
int i = 0, j = 0, dot = 0;
|
||||||
for (; buf[i] && buf[i] != ']'; i++) {
|
if (buf[i] != '[')
|
||||||
|
return 0;
|
||||||
|
for (i = 1; buf[i] && buf[i] != ']'; i++) {
|
||||||
if (!dot && isspace(buf[i])) {
|
if (!dot && isspace(buf[i])) {
|
||||||
dot = 1;
|
dot = 1;
|
||||||
if (name[j++] != '.')
|
if (name[j++] != '.')
|
||||||
|
@ -1195,7 +1197,17 @@ static int section_name_match (const char *buf, const char *name)
|
||||||
if (buf[i] != name[j++])
|
if (buf[i] != name[j++])
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
return (buf[i] == ']' && name[j] == 0);
|
if (buf[i] == ']' && name[j] == 0) {
|
||||||
|
/*
|
||||||
|
* We match, now just find the right length offset by
|
||||||
|
* gobbling up any whitespace after it, as well
|
||||||
|
*/
|
||||||
|
i++;
|
||||||
|
for (; buf[i] && isspace(buf[i]); i++)
|
||||||
|
; /* do nothing */
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* if new_name == NULL, the section is removed instead */
|
/* if new_name == NULL, the section is removed instead */
|
||||||
|
@ -1225,11 +1237,13 @@ int git_config_rename_section(const char *old_name, const char *new_name)
|
||||||
while (fgets(buf, sizeof(buf), config_file)) {
|
while (fgets(buf, sizeof(buf), config_file)) {
|
||||||
int i;
|
int i;
|
||||||
int length;
|
int length;
|
||||||
|
char *output = buf;
|
||||||
for (i = 0; buf[i] && isspace(buf[i]); i++)
|
for (i = 0; buf[i] && isspace(buf[i]); i++)
|
||||||
; /* do nothing */
|
; /* do nothing */
|
||||||
if (buf[i] == '[') {
|
if (buf[i] == '[') {
|
||||||
/* it's a section */
|
/* it's a section */
|
||||||
if (section_name_match (&buf[i+1], old_name)) {
|
int offset = section_name_match(&buf[i], old_name);
|
||||||
|
if (offset > 0) {
|
||||||
ret++;
|
ret++;
|
||||||
if (new_name == NULL) {
|
if (new_name == NULL) {
|
||||||
remove = 1;
|
remove = 1;
|
||||||
|
@ -1240,14 +1254,29 @@ int git_config_rename_section(const char *old_name, const char *new_name)
|
||||||
ret = write_error(lock->filename);
|
ret = write_error(lock->filename);
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
continue;
|
/*
|
||||||
|
* We wrote out the new section, with
|
||||||
|
* a newline, now skip the old
|
||||||
|
* section's length
|
||||||
|
*/
|
||||||
|
output += offset + i;
|
||||||
|
if (strlen(output) > 0) {
|
||||||
|
/*
|
||||||
|
* More content means there's
|
||||||
|
* a declaration to put on the
|
||||||
|
* next line; indent with a
|
||||||
|
* tab
|
||||||
|
*/
|
||||||
|
output -= 1;
|
||||||
|
output[0] = '\t';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
remove = 0;
|
remove = 0;
|
||||||
}
|
}
|
||||||
if (remove)
|
if (remove)
|
||||||
continue;
|
continue;
|
||||||
length = strlen(buf);
|
length = strlen(output);
|
||||||
if (write_in_full(out_fd, buf, length) != length) {
|
if (write_in_full(out_fd, output, length) != length) {
|
||||||
ret = write_error(lock->filename);
|
ret = write_error(lock->filename);
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
|
@ -459,6 +459,28 @@ EOF
|
||||||
|
|
||||||
test_expect_success "rename succeeded" "test_cmp expect .git/config"
|
test_expect_success "rename succeeded" "test_cmp expect .git/config"
|
||||||
|
|
||||||
|
cat >> .git/config << EOF
|
||||||
|
[branch "vier"] z = 1
|
||||||
|
EOF
|
||||||
|
|
||||||
|
test_expect_success "rename a section with a var on the same line" \
|
||||||
|
'git config --rename-section branch.vier branch.zwei'
|
||||||
|
|
||||||
|
cat > expect << EOF
|
||||||
|
# Hallo
|
||||||
|
#Bello
|
||||||
|
[branch "zwei"]
|
||||||
|
x = 1
|
||||||
|
[branch "zwei"]
|
||||||
|
y = 1
|
||||||
|
[branch "drei"]
|
||||||
|
weird
|
||||||
|
[branch "zwei"]
|
||||||
|
z = 1
|
||||||
|
EOF
|
||||||
|
|
||||||
|
test_expect_success "rename succeeded" "test_cmp expect .git/config"
|
||||||
|
|
||||||
cat >> .git/config << EOF
|
cat >> .git/config << EOF
|
||||||
[branch "zwei"] a = 1 [branch "vier"]
|
[branch "zwei"] a = 1 [branch "vier"]
|
||||||
EOF
|
EOF
|
||||||
|
|
Loading…
Reference in a new issue