@matheusbsbs / matheus-bagliano / commits / 10bea152a3
Fork 0

Add functions git_config_set() and git_config_set_multivar()

The function git_config_set() does exactly what you think it does. Given a key (in the form "core.filemode") and a value, it sets the key to the value. Example: git_config_set("core.filemode", "true"); The function git_config_set_multivar() is meant for setting variables which can have several values for the same key. Example: [diff] twohead = resolve twohead = recarsive the typo in the second line can be replaced by git_config_set_multivar("diff.twohead", "recursive", "^recar"); The third argument of the function is a POSIX extended regex which has to match the value. If there is no key/value pair with a matching value, a new key/value pair is added. These commands are also capable of unsetting (deleting) entries: git_config_set_multivar("diff.twohead", NULL, "sol"); will delete the entry twohead = resolve Signed-off-by: Johannes Schindelin <Johannes.Schindelin@gmx.de> Signed-off-by: Junio C Hamano <junkio@cox.net>

Johannes Schindelin committed Nov 17, 2005 at 22:32 UTC 10bea152a34b2bf1194ede5e0c9e5595ab2100f3
2 files changed +295 -1
cache.h
+2
@@ -386,6 +386,8 @@ extern int git_default_config(const char *, const char *);
386 extern int git_config(config_fn_t fn);
387 extern int git_config_int(const char *, const char *);
388 extern int git_config_bool(const char *, const char *);
389 +extern int git_config_set(const char *, const char *);
390 +extern int git_config_set_multivar(const char *, const char *, const char *);
391
392 #define MAX_GITNAME (1000)
393 extern char git_default_email[MAX_GITNAME];
config.c
+293 -1
@@ -1,5 +1,12 @@
1 -
1 +/*
2 + * GIT - The information manager from hell
3 + *
4 + * Copyright (C) Linus Torvalds, 2005
5 + * Copyright (C) Johannes Schindelin, 2005
6 + *
7 + */
8 #include "cache.h"
9 +#include <regex.h>
10
11 #define MAXNAME (256)
12
@@ -252,3 +259,288 @@ int git_config(config_fn_t fn)
259 }
260 return ret;
261 }
262 +
263 +/*
264 + * Find all the stuff for git_config_set() below.
265 + */
266 +static struct {
267 + int baselen;
268 + char* key;
269 + regex_t* value_regex;
270 + off_t offset;
271 + enum { START, SECTION_SEEN, SECTION_END_SEEN, KEY_SEEN } state;
272 + int seen;
273 +} store;
274 +
275 +static int store_aux(const char* key, const char* value)
276 +{
277 + switch (store.state) {
278 + case KEY_SEEN:
279 + if (!strcmp(key, store.key) &&
280 + (store.value_regex == NULL ||
281 + !regexec(store.value_regex, value, 0, NULL, 0))) {
282 + if (store.seen == 1) {
283 + fprintf(stderr,
284 + "Warning: %s has multiple values\n",
285 + key);
286 + }
287 + store.offset = ftell(config_file);
288 + store.seen++;
289 + }
290 + break;
291 + case SECTION_SEEN:
292 + if (strncmp(key, store.key, store.baselen+1)) {
293 + store.state = SECTION_END_SEEN;
294 + break;
295 + } else
296 + store.offset = ftell(config_file);
297 + /* fallthru */
298 + case SECTION_END_SEEN:
299 + case START:
300 + if (!strcmp(key, store.key) &&
301 + (store.value_regex == NULL ||
302 + !regexec(store.value_regex, value, 0, NULL, 0))) {
303 + store.offset = ftell(config_file);
304 + store.state = KEY_SEEN;
305 + store.seen++;
306 + } else if(!strncmp(key, store.key, store.baselen))
307 + store.state = SECTION_SEEN;
308 + }
309 + return 0;
310 +}
311 +
312 +static void store_write_section(int fd, const char* key)
313 +{
314 + write(fd, "[", 1);
315 + write(fd, key, store.baselen);
316 + write(fd, "]\n", 2);
317 +}
318 +
319 +static void store_write_pair(int fd, const char* key, const char* value)
320 +{
321 + int i;
322 +
323 + write(fd, "\t", 1);
324 + write(fd, key+store.baselen+1,
325 + strlen(key+store.baselen+1));
326 + write(fd, " = ", 3);
327 + for (i = 0; value[i]; i++)
328 + switch (value[i]) {
329 + case '\n': write(fd, "\\n", 2); break;
330 + case '\t': write(fd, "\\t", 2); break;
331 + case '"': case '\\': write(fd, "\\", 1);
332 + default: write(fd, value+i, 1);
333 + }
334 + write(fd, "\n", 1);
335 +}
336 +
337 +int git_config_set(const char* key, const char* value)
338 +{
339 + return git_config_set_multivar(key, value, NULL);
340 +}
341 +
342 +/*
343 + * If value==NULL, unset in (remove from) config,
344 + * if value_regex!=NULL, disregard key/value pairs where value does not match.
345 + *
346 + * Returns 0 on success.
347 + *
348 + * This function does this:
349 + *
350 + * - it locks the config file by creating ".git/config.lock"
351 + *
352 + * - it then parses the config using store_aux() as validator to find
353 + * the position on the key/value pair to replace. If it is to be unset,
354 + * it must be found exactly once.
355 + *
356 + * - the config file is mmap()ed and the part before the match (if any) is
357 + * written to the lock file, then the changed part and the rest.
358 + *
359 + * - the config file is removed and the lock file rename()d to it.
360 + *
361 + */
362 +int git_config_set_multivar(const char* key, const char* value,
363 + const char* value_regex)
364 +{
365 + int i;
366 + struct stat st;
367 + int fd;
368 + char* config_file = strdup(git_path("config"));
369 + char* lock_file = strdup(git_path("config.lock"));
370 +
371 + /*
372 + * Since "key" actually contains the section name and the real
373 + * key name separated by a dot, we have to know where the dot is.
374 + */
375 + for (store.baselen = 0;
376 + key[store.baselen] != '.' && key[store.baselen];
377 + store.baselen++);
378 + if (!key[store.baselen] || !key[store.baselen+1]) {
379 + fprintf(stderr, "key does not contain a section: %s\n", key);
380 + return 2;
381 + }
382 +
383 + /*
384 + * Validate the key and while at it, lower case it for matching.
385 + */
386 + store.key = (char*)malloc(strlen(key)+1);
387 + for (i = 0; key[i]; i++)
388 + if (i != store.baselen && (!isalnum(key[i]) ||
389 + (i == store.baselen+1 && !isalpha(key[i])))) {
390 + fprintf(stderr, "invalid key: %s\n", key);
391 + free(store.key);
392 + return 1;
393 + } else
394 + store.key[i] = tolower(key[i]);
395 +
396 + /*
397 + * The lock_file serves a purpose in addition to locking: the new
398 + * contents of .git/config will be written into it.
399 + */
400 + fd = open(lock_file, O_WRONLY | O_CREAT | O_EXCL, 0666);
401 + if (fd < 0) {
402 + fprintf(stderr, "could not lock config file\n");
403 + free(store.key);
404 + return -1;
405 + }
406 +
407 + /*
408 + * If .git/config does not exist yet, write a minimal version.
409 + */
410 + if (stat(config_file, &st)) {
411 + static const char contents[] =
412 + "#\n"
413 + "# This is the config file\n"
414 + "#\n"
415 + "\n";
416 +
417 + free(store.key);
418 +
419 + /* if nothing to unset, error out */
420 + if (value == NULL) {
421 + close(fd);
422 + unlink(lock_file);
423 + return 5;
424 + }
425 +
426 + store.key = (char*)key;
427 +
428 + write(fd, contents, sizeof(contents)-1);
429 + store_write_section(fd, key);
430 + store_write_pair(fd, key, value);
431 + } else{
432 + int in_fd;
433 + char* contents;
434 + int offset, new_line = 0;
435 +
436 + if (value_regex == NULL)
437 + store.value_regex = NULL;
438 + else {
439 + store.value_regex = (regex_t*)malloc(sizeof(regex_t));
440 + if (regcomp(store.value_regex, value_regex,
441 + REG_EXTENDED)) {
442 + fprintf(stderr, "Invalid pattern: %s",
443 + value_regex);
444 + free(store.value_regex);
445 + return 6;
446 + }
447 + }
448 +
449 + store.offset = 0;
450 + store.state = START;
451 + store.seen = 0;
452 +
453 + /*
454 + * After this, store.offset will contain the *end* offset
455 + * of the last match, or remain at 0 if no match was found.
456 + * As a side effect, we make sure to transform only a valid
457 + * existing config file.
458 + */
459 + if (git_config(store_aux)) {
460 + fprintf(stderr, "invalid config file\n");
461 + free(store.key);
462 + if (store.value_regex != NULL) {
463 + regfree(store.value_regex);
464 + free(store.value_regex);
465 + }
466 + return 3;
467 + }
468 +
469 + free(store.key);
470 + if (store.value_regex != NULL) {
471 + regfree(store.value_regex);
472 + free(store.value_regex);
473 + }
474 +
475 + /* if nothing to unset, error out */
476 + if (store.seen == 0 && value == NULL) {
477 + close(fd);
478 + unlink(lock_file);
479 + return 5;
480 + }
481 +
482 + store.key = (char*)key;
483 +
484 + in_fd = open(config_file, O_RDONLY, 0666);
485 + contents = mmap(NULL, st.st_size, PROT_READ,
486 + MAP_PRIVATE, in_fd, 0);
487 + close(in_fd);
488 +
489 + if (store.offset == 0) {
490 + store.offset = offset = st.st_size;
491 + } else if (store.state != KEY_SEEN) {
492 + offset = store.offset;
493 + } else {
494 + int equal_offset = st.st_size,
495 + bracket_offset = st.st_size;
496 +
497 + if (value == NULL && store.seen > 1) {
498 + fprintf(stderr, "Cannot remove multivar (%s has %d values\n", key, store.seen);
499 + close(fd);
500 + unlink(lock_file);
501 + return 7;
502 + }
503 + for (offset = store.offset-2; offset > 0
504 + && contents[offset] != '\n'; offset--)
505 + switch (contents[offset]) {
506 + case '=': equal_offset = offset; break;
507 + case ']': bracket_offset = offset; break;
508 + }
509 + if (bracket_offset < equal_offset) {
510 + new_line = 1;
511 + offset = bracket_offset+1;
512 + } else
513 + offset++;
514 + }
515 +
516 + /* write the first part of the config */
517 + write(fd, contents, offset);
518 + if (new_line)
519 + write(fd, "\n", 1);
520 +
521 + /* write the pair (value == NULL means unset) */
522 + if (value != NULL) {
523 + if (store.state == START)
524 + store_write_section(fd, key);
525 + store_write_pair(fd, key, value);
526 + }
527 +
528 + /* write the rest of the config */
529 + if (store.offset < st.st_size)
530 + write(fd, contents + store.offset,
531 + st.st_size - store.offset);
532 +
533 + munmap(contents, st.st_size);
534 + unlink(config_file);
535 + }
536 +
537 + close(fd);
538 +
539 + if (rename(lock_file, config_file) < 0) {
540 + fprintf(stderr, "Could not rename the lock file?\n");
541 + return 4;
542 + }
543 +
544 + return 0;
545 +}
546 +