commit 8dd3c358110dd1ea818082d86ce6bb7617f773c9
parent cf4569d01ed32bcdaf3251872bcb070c8aeef8b2
Author: Francesco Saccone <francesco@francescosaccone.com>
Date: Wed, 26 Mar 2025 14:02:37 +0100
refactor: use common shell scripts in stagitCreate and stagitPostReceive
Signed-off-by: Francesco Saccone <francesco@francescosaccone.com>
Diffstat:
1 file changed, 61 insertions(+), 98 deletions(-)
diff --git a/hosts/server/scripts.nix b/hosts/server/scripts.nix
@@ -3,64 +3,70 @@
pkgs,
inputs,
}:
+let
+ stagit = rec {
+ destDir = "/var/tmp/stagit";
+ cacheFile = "${destDir}/.htmlcache";
+ reposDir = config.modules.git.directory;
+
+ createIndex = ''
+ ${pkgs.stagit}/bin/stagit-index ${reposDir}/*/ > ${destDir}/index.html
+
+ # Symlink favicon.png, logo.png and stagit.css from site
+ ${pkgs.sbase}/bin/ln -sf \
+ ${inputs.site}/public/icon/256.png \
+ ${destDir}/favicon.png
+
+ ${pkgs.sbase}/bin/ln -sf \
+ ${inputs.site}/public/icon/32.png \
+ ${destDir}/logo.png
+
+ ${pkgs.sbase}/bin/ln -sf \
+ ${inputs.site}/public/stagit.css \
+ ${destDir}/style.css
+ '';
+
+ createRepository = name: ''
+ ${pkgs.sbase}/bin/mkdir -p ${destDir}/${name}
+ cd ${destDir}/${name}
+ ${pkgs.stagit}/bin/stagit \
+ -c ${cacheFile} \
+ -u https://${config.networking.domain}/git/${name}/ \
+ ${reposDir}/${name}
+
+ # Make the log.html file the index page
+ ${pkgs.sbase}/bin/ln -sf \
+ ${destDir}/${name}/log.html \
+ ${destDir}/${name}/index.html
+
+ # Symlink favicon.png, logo.png and style.css in repos from
+ # index
+ ${pkgs.sbase}/bin/ln -sf \
+ ${destDir}/favicon.png \
+ ${destDir}/${name}/favicon.png
+
+ ${pkgs.sbase}/bin/ln -sf \
+ ${destDir}/logo.png \
+ ${destDir}/${name}/logo.png
+
+ ${pkgs.sbase}/bin/ln -sf \
+ ${destDir}/style.css \
+ ${destDir}/${name}/style.css
+ '';
+ };
+in
{
stagitCreate =
let
- destDir = "/var/tmp/stagit";
- reposDir = config.modules.git.directory;
- flags = builtins.concatStringsSep " " [
- "-u https://${config.networking.domain}/git/${name}/"
- ];
-
- indexScript = pkgs.writeShellScriptBin "index" ''
- ${pkgs.stagit}/bin/stagit-index ${reposDir}/*/ > ${destDir}/index.html
-
- # Symlink favicon.png, logo.png and stagit.css from site
- ${pkgs.sbase}/bin/ln -sf \
- ${inputs.site}/public/icon/256.png \
- ${destDir}/favicon.png
-
- ${pkgs.sbase}/bin/ln -sf \
- ${inputs.site}/public/icon/32.png \
- ${destDir}/logo.png
-
- ${pkgs.sbase}/bin/ln -sf \
- ${inputs.site}/public/stagit.css \
- ${destDir}/style.css
- '';
-
- repositoriesScript =
+ createRepositories =
config.modules.git.repositories
|> builtins.attrNames
- |> builtins.map (name: ''
- ${pkgs.sbase}/bin/mkdir -p ${destDir}/${name}
- cd ${destDir}/${name}
- ${pkgs.stagit}/bin/stagit ${flags} ${reposDir}/${name}
-
- # Make the log.html file the index page
- ${pkgs.sbase}/bin/ln -sf \
- ${destDir}/${name}/log.html \
- ${destDir}/${name}/index.html
-
- # Symlink favicon.png, logo.png and style.css in repos from
- # index
- ${pkgs.sbase}/bin/ln -sf \
- ${destDir}/favicon.png \
- ${destDir}/${name}/favicon.png
-
- ${pkgs.sbase}/bin/ln -sf \
- ${destDir}/logo.png \
- ${destDir}/${name}/logo.png
-
- ${pkgs.sbase}/bin/ln -sf \
- ${destDir}/style.css \
- ${destDir}/${name}/style.css
- '')
- |> pkgs.writeShellScriptBin "repositories";
+ |> builtins.map stagit.createRepository
+ |> builtins.concatStringsSep "\n";
script = pkgs.writeShellScriptBin "stagit-create" ''
- ${indexScript}
- ${repositoriesScript}
+ ${stagit.createIndex}
+ ${createRepositories}
'';
in
"${script}/bin/stagit-create";
@@ -68,14 +74,6 @@
stagitPostReceive =
{ name }:
let
- destDir = "/var/tmp/stagit";
- cacheFile = "${destDir}/.htmlcache";
- reposDir = config.modules.git.directory;
- flags = builtins.concatStringsSep " " [
- "-c ${cacheFile}"
- "-u https://${config.networking.domain}/git/${name}/"
- ];
-
script = pkgs.writeShellScriptBin "stagit" ''
# Define is_force=1 if 'git push -f' was used
null_ref="0000000000000000000000000000000000000000"
@@ -95,47 +93,12 @@
# If is_force = 1, remove commits and cache file
if ${pkgs.sbase}/bin/test $is_force = "1"; then
- ${pkgs.sbase}/bin/rm -f ${cacheFile}
- ${pkgs.sbase}/bin/rm -rf ${reposDir}/${name}/commit
+ ${pkgs.sbase}/bin/rm -f ${stagit.cacheFile}
+ ${pkgs.sbase}/bin/rm -rf ${stagit.reposDir}/${name}/commit
fi
- ${pkgs.sbase}/bin/mkdir -p ${destDir}/${name}
- cd ${destDir}/${name}
- ${pkgs.stagit}/bin/stagit ${flags} ${reposDir}/${name}
- ${pkgs.stagit}/bin/stagit-index ${reposDir}/*/ \
- > ${destDir}/index.html
-
- # Make the log.html file the index page
- ${pkgs.sbase}/bin/ln -sf \
- ${destDir}/${name}/log.html \
- ${destDir}/${name}/index.html
-
- # Symlink favicon.png, logo.png and stagit.css from site
- ${pkgs.sbase}/bin/ln -sf \
- ${inputs.site}/public/icon/256.png \
- ${destDir}/favicon.png
-
- ${pkgs.sbase}/bin/ln -sf \
- ${inputs.site}/public/icon/32.png \
- ${destDir}/logo.png
-
- ${pkgs.sbase}/bin/ln -sf \
- ${inputs.site}/public/stagit.css \
- ${destDir}/style.css
-
- # Symlink favicon.png, logo.png and style.css in repos from
- # index
- ${pkgs.sbase}/bin/ln -sf \
- ${destDir}/favicon.png \
- ${destDir}/${name}/favicon.png
-
- ${pkgs.sbase}/bin/ln -sf \
- ${destDir}/logo.png \
- ${destDir}/${name}/logo.png
-
- ${pkgs.sbase}/bin/ln -sf \
- ${destDir}/style.css \
- ${destDir}/${name}/style.css
+ ${stagit.createIndex}
+ ${stagit.createRepository name}
'';
in
"${script}/bin/stagit";