diff options
| author | Alejandro Soto <alejandro@34project.org> | 2026-04-15 00:20:25 -0600 |
|---|---|---|
| committer | Alejandro Soto <alejandro@34project.org> | 2026-04-15 02:32:09 -0600 |
| commit | 48e40ad4cff334d3187a7ee90bfbfcc44213fee8 (patch) | |
| tree | 3a4ef20700323894e56c57d8348ed973557e1e3e | |
| parent | f788e70bad24a8ee24813bc0096a4a8c9a7294a9 (diff) | |
pkgs: lustrate: initial commit
| -rw-r--r-- | pkgs/default.nix | 1 | ||||
| -rw-r--r-- | pkgs/lustrate/default.nix | 6 | ||||
| -rw-r--r-- | pkgs/lustrate/filters.nix | 1 | ||||
| -rw-r--r-- | pkgs/lustrate/lustrate.sh | 125 |
4 files changed, 133 insertions, 0 deletions
diff --git a/pkgs/default.nix b/pkgs/default.nix index cfc0dda..252eb92 100644 --- a/pkgs/default.nix +++ b/pkgs/default.nix @@ -14,6 +14,7 @@ in { increment-zone-serials = callPackage ./increment-zone-serials {}; ipxe = callPackage ./ipxe/default.nix {}; kbuild-standalone = callPackage ./kbuild-standalone.nix {}; + lustrate = callPackage ./lustrate {}; mssql-tools = callPackage ./mssql-tools.nix {}; oregano = callPackage ./oregano {}; pass-bcr = callPackage ./pass-bcr {}; diff --git a/pkgs/lustrate/default.nix b/pkgs/lustrate/default.nix new file mode 100644 index 0000000..cf0a322 --- /dev/null +++ b/pkgs/lustrate/default.nix @@ -0,0 +1,6 @@ +{writeShellApplication, ...}: +writeShellApplication { + name = "lustrate"; + runtimeInputs = []; + text = import ./filters.nix + builtins.readFile ./lustrate.sh; +} diff --git a/pkgs/lustrate/filters.nix b/pkgs/lustrate/filters.nix new file mode 100644 index 0000000..1bb3788 --- /dev/null +++ b/pkgs/lustrate/filters.nix @@ -0,0 +1 @@ +# This file has been lustrated. diff --git a/pkgs/lustrate/lustrate.sh b/pkgs/lustrate/lustrate.sh new file mode 100644 index 0000000..8435f61 --- /dev/null +++ b/pkgs/lustrate/lustrate.sh @@ -0,0 +1,125 @@ +LUSTRATE="$(realpath "$0")" + +usage() { + echo "Usage: $LUSTRATE <src repo> <dst repo>" >&2 + exit 1 +} + +filter="" +set_filter() { + [ -z "$filter" ] || usage + filter="$1" +} + +src_repo="" +dst_repo="" + +positional_arg() { + if [ -z "$src_repo" ]; then + src_repo="$1" + elif [ -z "$dst_repo" ]; then + dst_repo="$1" + else + usage + fi +} + +no_more_options="" +while [ $# -gt 0 ]; do + if [ -n "$no_more_options" ]; then + positional_arg "$1" + else + case "$1" in + "--tree") + set_filter tree + ;; + + "--msg") + set_filter msg + ;; + + "--") + no_more_options=1 + ;; + + *) + positional_arg "$1" + esac + fi + + shift +done + +if [ -n "$filter" ]; then + case "$filter" in + "tree") + for path in "${filtered_paths[@]}"; do + rm -frv -- "$path" + done + + for path in "${cleared_dirs[@]}"; do + if [ -d "$path" ]; then + rm -frv -- "$path" + mkdir "$path" + echo "# This directory has been lustrated." >"$path/README.md" + fi + done + + for path in "${cleared_files[@]}"; do + if [ -f "$path" ]; then + rm -frv -- "$path" + echo "# This file has been lustrated." >"$path" + fi + done + + # This prevents the script from matching itself + LUSTRATE="lustrate" + find . -type f -exec sed -i "/#+$LUSTRATE/, /#-$LUSTRATE/d" -- {} \; + ;; + + "msg") + sed -e "$LUSTRATE_MSG_SED_SCRIPT" + ;; + + *) + usage + ;; + esac + + exit +fi + +if [ -z "$src_repo" ] || [ -z "$dst_repo" ]; then + usage +fi + +SCRIPT="$extra_msg_script" +for regex in "${cleared_msg_regexes[@]}"; do + SCRIPT="$SCRIPT;s/$regex/\1\[lustrated\]\2/g" +done + +# Special case for flake lock updates +SCRIPT="$SCRIPT;/Flake lock file updates:/,\$d" + +export LUSTRATE_MSG_SED_SCRIPT="$SCRIPT" +export FILTER_BRANCH_SQUELCH_WARNING=1 + +trap 'rm -fr -- "$tmp_repo"' EXIT +tmp_repo="$(mktemp -d)" + +git -C "$tmp_repo" -c init.defaultBranch=master init +git -C "$tmp_repo" fetch -- "$(realpath "$src_repo")" + +REF_PWD="$PWD" +cd "$tmp_repo" + +git reset --hard FETCH_HEAD +git clean -dfx + +git filter-branch \ + --tree-filter "$LUSTRATE --tree" \ + --msg-filter "$LUSTRATE --msg" \ + --prune-empty + +cd "$REF_PWD" +mv -T -- "$tmp_repo" "$dst_repo" |
