diff options
Diffstat (limited to 'pkgs/lustrate/lustrate.sh')
| -rw-r--r-- | pkgs/lustrate/lustrate.sh | 125 |
1 files changed, 125 insertions, 0 deletions
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" |
