diff options
| author | Danilo M. <danix@danix.xyz> | 2026-10-16 11:17:24 +0200 |
|---|---|---|
| committer | Danilo M. <danix@danix.xyz> | 2026-10-16 11:17:24 +0200 |
| commit | 044ed0720b9af19ad0e586adf907f77f908f0bf3 (patch) | |
| tree | f52caf92c02650cdf74121061f4c01dc7e5c376e /desktop/modules/git/gitcfg | |
| parent | 23f666e92ce0ea6b40f241a33edee43160ee0d81 (diff) | |
| download | quickshell-044ed0720b9af19ad0e586adf907f77f908f0bf3.tar.gz quickshell-044ed0720b9af19ad0e586adf907f77f908f0bf3.zip | |
feat(git): gitcfg, global git config through git itself
list, set, add and unset over git config --global. --fixed-value lets a
write touch one value of a multi-valued key. set and unset check the old
value is still there first: --replace-all with no match appends and exits
0, and the file may have changed in an editor since the page read it.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Diffstat (limited to 'desktop/modules/git/gitcfg')
| -rwxr-xr-x | desktop/modules/git/gitcfg | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/desktop/modules/git/gitcfg b/desktop/modules/git/gitcfg new file mode 100755 index 0000000..df955bf --- /dev/null +++ b/desktop/modules/git/gitcfg @@ -0,0 +1,56 @@ +#!/bin/bash +# +# Copyright (C) 2026 Danilo M. <danix@danix.xyz> +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License version 2 as +# published by the Free Software Foundation. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# The global git config, for the drawer's git page. +# +# gitcfg list git config --global -z --list +# 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> +# +# Every write goes through git config, so the file keeps its comments, layout +# and quoting. --fixed-value makes a value a literal rather than a regex, so a +# write touches one value of a multi-valued key, and the `--` before the key +# keeps a leading dash an argument rather than an option. +# +# A write prints nothing on success. On failure it prints the reason on +# stdout, for the page, and exits 1. + +set -u + +usage() { + printf 'usage: %s list | set <key> <old> <new> | add <key> <value> | unset <key> <value>\n' \ + "${0##*/}" >&2 + exit 2 +} + +g() { + local err + 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. +has() { + git config --global --fixed-value --get-all -- "$1" "$2" >/dev/null \ + || { printf '%s no longer holds that value\n' "$1"; exit 1; } +} + +case "${1:-}" in + list) [[ $# -eq 1 ]] || usage; 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" ;; + *) usage ;; +esac |
