Skip to content

Commit b17e659

Browse files
dschoJunio C Hamano
authored andcommitted
Allow hierarchical section names
A .git/config like follows becomes valid with this patch: [remote.junio] url = git://git.kernel.org/pub/scm/git/git.git pull = master:junio todo:todo +pu:pu [remote.ibook] url = ibook:git/ pull = master:ibook push = master:quetzal (This patch only does the ini file thing, git-fetch and friends still ignore these values). Signed-off-by: Johannes Schindelin <Johannes.Schindelin@gmx.de> Signed-off-by: Junio C Hamano <junkio@cox.net>
1 parent 3dd94e3 commit b17e659

File tree

2 files changed

+29
-9
lines changed

2 files changed

+29
-9
lines changed

config.c

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ static int get_base_var(char *name)
143143
return -1;
144144
if (c == ']')
145145
return baselen;
146-
if (!isalnum(c))
146+
if (!isalnum(c) && c != '.')
147147
return -1;
148148
if (baselen > MAXNAME / 2)
149149
return -1;
@@ -405,28 +405,29 @@ int git_config_set_multivar(const char* key, const char* value,
405405
int fd;
406406
char* config_file = strdup(git_path("config"));
407407
char* lock_file = strdup(git_path("config.lock"));
408-
409-
store.multi_replace = multi_replace;
408+
const char* last_dot = strrchr(key, '.');
410409

411410
/*
412411
* Since "key" actually contains the section name and the real
413412
* key name separated by a dot, we have to know where the dot is.
414413
*/
415-
for (store.baselen = 0;
416-
key[store.baselen] != '.' && key[store.baselen];
417-
store.baselen++);
418-
if (!key[store.baselen] || !key[store.baselen+1]) {
414+
415+
if (last_dot == NULL) {
419416
fprintf(stderr, "key does not contain a section: %s\n", key);
420417
return 2;
421418
}
419+
store.baselen = last_dot - key;
420+
421+
store.multi_replace = multi_replace;
422422

423423
/*
424424
* Validate the key and while at it, lower case it for matching.
425425
*/
426426
store.key = (char*)malloc(strlen(key)+1);
427427
for (i = 0; key[i]; i++)
428-
if (i != store.baselen && (!isalnum(key[i]) ||
429-
(i == store.baselen+1 && !isalpha(key[i])))) {
428+
if (i != store.baselen &&
429+
((!isalnum(key[i]) && key[i] != '.') ||
430+
(i == store.baselen+1 && !isalpha(key[i])))) {
430431
fprintf(stderr, "invalid key: %s\n", key);
431432
free(store.key);
432433
return 1;

t/t1300-config-set.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,5 +248,24 @@ test_expect_failure 'invalid key' 'git-config-set inval.2key blabla'
248248

249249
test_expect_success 'correct key' 'git-config-set 123456.a123 987'
250250

251+
test_expect_success 'hierarchical section' \
252+
'git-config-set 1.2.3.alpha beta'
253+
254+
cat > expect << EOF
255+
[beta] ; silly comment # another comment
256+
noIndent= sillyValue ; 'nother silly comment
257+
258+
# empty line
259+
; comment
260+
[nextSection]
261+
NoNewLine = wow2 for me
262+
[123456]
263+
a123 = 987
264+
[1.2.3]
265+
alpha = beta
266+
EOF
267+
268+
test_expect_success 'hierarchical section value' 'cmp .git/config expect'
269+
251270
test_done
252271

0 commit comments

Comments
 (0)