Properly handle multiple values for settings

Fixes an error that would cause values to be duplicated when specifying
multiple values for a setting.

Prior to this commit specifying multiple values for a setting would
blindly replace one occurrence and add the rest, leaving any possible
other occurrences intact. When starting fresh this would not be a
problem, but using it multiple times would add the same settings
repeatedly.

- gitto/config.scm (merge-setting): If the given value is a of type
  `list', replace the `%a' specifier for each item in it.

- gitto/config.scm (merge-settings): Move the handling of the case of
  `v' being a list into `merge-setting'.

- gitto/config.scm (split-setting): Rename `parse-setting' to
  `split-setting'. Return a cons cell instead of a list.

- gitto/config.scm (read-setting): New function.

- gitto/config.scm (read-config): Remove unused variable. Use
  `read-setting' to get the right value for each line of the config
  file.

- gitto/config.scm (write-setting): In case we're dealing with a list,
  print each value separately.
This commit is contained in:
Tom Willemse 2013-05-29 23:09:42 +02:00
parent 1027b3c7d6
commit 62627e6cf6

View file

@ -52,34 +52,38 @@
lst)) lst))
(define (merge-setting repo-name lst var val) (define (merge-setting repo-name lst var val)
(assoc-set! lst var (format #f val repo-name))) (if (list? val)
(assoc-set! lst var (map (lambda (v) (format #f v repo-name)) val))
(assoc-set! lst var (format #f val repo-name))))
(define (merge-settings repo-name x y) (define (merge-settings repo-name x y)
(let ((lst (if x (list-copy x) '()))) (let ((lst (if x (list-copy x) '())))
(for-each (for-each
(lambda (v) (lambda (v)
(if (list? v) (set! lst (merge-setting repo-name lst (car v) (cdr v))))
(begin
(set! lst (merge-setting repo-name lst (car v) (cadr v)))
(set! lst (append lst (map (lambda (s)
(cons (car v)
(format #f s repo-name)))
(cddr v)))))
(set! lst (merge-setting repo-name lst (car v) (cdr v)))))
y) y)
lst)) lst))
(define (parse-setting line) (define (split-setting line)
(let ((idx (string-index line #\=))) (let ((idx (string-index line #\=)))
(list (cons (string-trim-both (substring line 0 idx)) (cons (string-trim-both (substring line 0 idx))
(string-trim-both (substring line (1+ idx))))))) (string-trim-both (substring line (1+ idx))))))
(define (read-setting settings line)
(let* ((new-setting (split-setting line))
(var (car new-setting)) (val (cdr new-setting))
(current-value (assoc-ref settings var)))
(if current-value
(if (list? current-value)
(assoc-set! settings var (append current-value (list val)))
(assoc-set! settings var (list current-value val)))
(assoc-set! settings var val))))
(define (read-config repo-location) (define (read-config repo-location)
(let ((port (open-input-file (let ((port (open-input-file
(string-append repo-location "/.git/config"))) (string-append repo-location "/.git/config")))
(config '()) (config '())
(current-section #f) (current-section #f))
(assign-pos #f))
(do ((line (read-line port) (read-line port))) (do ((line (read-line port) (read-line port)))
((eof-object? line)) ((eof-object? line))
(cond ((string= line "[" 0 1) (cond ((string= line "[" 0 1)
@ -89,8 +93,7 @@
(set! current-section section))) (set! current-section section)))
((string-contains line "=") ((string-contains line "=")
(set-cdr! current-section (set-cdr! current-section
(append (cdr current-section) (read-setting (cdr current-section) line)))))
(parse-setting line))))))
(close-port port) (close-port port)
config)) config))
@ -105,4 +108,8 @@
(for-each write-setting (cdr section))) (for-each write-setting (cdr section)))
(define (write-setting setting) (define (write-setting setting)
(format #t "~8t~a = ~a~%" (car setting) (cdr setting))) (let ((value (cdr setting)))
(if (list? value)
(map (lambda (v)
(format #t "~8t~a = ~a~%" (car setting) v)) value)
(format #t "~8t~a = ~a~%" (car setting) (cdr setting)))))