diff options
| author | Danilo M. <danix@danix.xyz> | 2026-10-16 11:17:56 +0200 |
|---|---|---|
| committer | Danilo M. <danix@danix.xyz> | 2026-10-16 11:17:56 +0200 |
| commit | b81fe900056e4c14738d4ceb4e2dfe80ae2e4bff (patch) | |
| tree | 6a80fa1fd6ec5e75453a7e7b345c996bea2bc857 /desktop/modules | |
| parent | 044ed0720b9af19ad0e586adf907f77f908f0bf3 (diff) | |
| download | quickshell-b81fe900056e4c14738d4ceb4e2dfe80ae2e4bff.tar.gz quickshell-b81fe900056e4c14738d4ceb4e2dfe80ae2e4bff.zip | |
feat(git): parse git config -z --list for the page
NUL between entries and the first newline between name and value, so
values holding = or newlines survive. The key is after the last dot, so
color.branch.remote groups under color.branch.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Diffstat (limited to 'desktop/modules')
| -rw-r--r-- | desktop/modules/git/Git.js | 27 | ||||
| -rw-r--r-- | desktop/modules/git/test_git.js | 36 |
2 files changed, 63 insertions, 0 deletions
diff --git a/desktop/modules/git/Git.js b/desktop/modules/git/Git.js new file mode 100644 index 0000000..008fefc --- /dev/null +++ b/desktop/modules/git/Git.js @@ -0,0 +1,27 @@ +// 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. + +.pragma library + +// Pure helpers for the git page, tested by test_git.js under node. + +// `gitcfg list` (git config -z --list) into [{name, section, key, value}], +// in file order. The key is after the last dot, so a subsection stays in the +// section: color.branch.remote is section color.branch. +function parse(text) { + return text.split("\0").filter(e => e !== "").map(e => { + const nl = e.indexOf("\n"); + const name = nl < 0 ? e : e.slice(0, nl); + const dot = name.lastIndexOf("."); + return { name, section: name.slice(0, dot), key: name.slice(dot + 1), + value: nl < 0 ? "" : e.slice(nl + 1) }; + }); +} diff --git a/desktop/modules/git/test_git.js b/desktop/modules/git/test_git.js new file mode 100644 index 0000000..c18d084 --- /dev/null +++ b/desktop/modules/git/test_git.js @@ -0,0 +1,36 @@ +// 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. + +// Run: node desktop/modules/git/test_git.js +// Git.js is a QML library, so its .pragma line is stripped before node +// evaluates it. + +const fs = require("fs"); +const assert = require("assert"); + +const src = fs.readFileSync(__dirname + "/Git.js", "utf8") + .replace(/^\.pragma library$/m, ""); +const G = new Function(src + "\nreturn { parse };")(); + +// `gitcfg list` output: name, newline, value, NUL. A subsection's dots stay +// in the section; a value keeps its = and its own newlines; a valueless +// boolean has no newline at all. +assert.deepStrictEqual(G.parse("user.name\nJane D.\0color.branch.remote\nyellow\0" + + "alias.x\n!f() { a=1\nb; }\0x.flag\0"), [ + { name: "user.name", section: "user", key: "name", value: "Jane D." }, + { name: "color.branch.remote", section: "color.branch", key: "remote", value: "yellow" }, + { name: "alias.x", section: "alias", key: "x", value: "!f() { a=1\nb; }" }, + { name: "x.flag", section: "x", key: "flag", value: "" }, +]); +// No config file: gitcfg prints nothing. +assert.deepStrictEqual(G.parse(""), []); + +console.log("test_git: ok"); |
