diff options
Diffstat (limited to 'desktop/modules/git/gitcfg')
| -rwxr-xr-x | desktop/modules/git/gitcfg | 26 |
1 files changed, 19 insertions, 7 deletions
diff --git a/desktop/modules/git/gitcfg b/desktop/modules/git/gitcfg index df955bf..a5641f7 100755 --- a/desktop/modules/git/gitcfg +++ b/desktop/modules/git/gitcfg @@ -13,7 +13,7 @@ # # The global git config, for the drawer's git page. # -# gitcfg list git config --global -z --list +# gitcfg list git config --global -z --list, or error=<why> # gitcfg set <key> <old> <new> replace the one value equal to <old> # gitcfg add <key> <value> add a value # gitcfg unset <key> <value> remove the one value equal to <value> @@ -39,16 +39,28 @@ g() { err="$(git config --global "$@" 2>&1)" || { printf '%s\n' "${err:-git config failed}"; exit 1; } } -# --replace-all with no value equal to <old> appends <new> and succeeds, and -# --unset of a missing value fails silently, so both check first: the file -# may have changed in the editor since the page read it. +# Exactly one value equal to <value>, or refuse. --replace-all with no match +# appends <new> and succeeds, and --unset of a missing value fails silently: +# the file may have changed in the editor since the page read it. With two +# equal values --replace-all merges both rows into one and --unset fails, so a +# duplicate is left to the editor. has() { - git config --global --fixed-value --get-all -- "$1" "$2" >/dev/null \ - || { printf '%s no longer holds that value\n' "$1"; exit 1; } + local n + n="$(git config --global -z --fixed-value --get-all -- "$1" "$2" | tr -cd '\0' | wc -c)" + case "$n" in + 1) ;; + 0) printf '%s no longer holds that value\n' "$1"; exit 1 ;; + *) printf '%s holds that value twice or more; use the editor\n' "$1"; exit 1 ;; + esac } case "${1:-}" in - list) [[ $# -eq 1 ]] || usage; exec git config --global -z --list ;; + # A file git cannot parse prints nothing on stdout, which would read as + # an empty config, so its error comes out as one error= line instead. + list) [[ $# -eq 1 ]] || usage + err="$(git config --global --list 2>&1 >/dev/null)" \ + || { printf 'error=%s\n' "${err:-git config failed}"; exit 1; } + exec git config --global -z --list ;; set) [[ $# -eq 4 ]] || usage; has "$2" "$3"; g --fixed-value --replace-all -- "$2" "$4" "$3" ;; add) [[ $# -eq 3 ]] || usage; g --add -- "$2" "$3" ;; unset) [[ $# -eq 3 ]] || usage; has "$2" "$3"; g --fixed-value --unset -- "$2" "$3" ;; |
