summaryrefslogtreecommitdiff
path: root/pkgs/lustrate/lustrate.sh
blob: 8435f61e080832235248b9e6b76221a473b8ac90 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
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"