aboutsummaryrefslogtreecommitdiffstats
path: root/src/extract-nix.sh
blob: e092db211290db79985a5ae9f6c1b558ca5f01a4 (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
#!/usr/bin/env bash

set -efu

input_file=${1-/dev/stdin}

tree-grepper \
  --query nix '((comment)+)' \
  --format json $input_file |
  jq 'sort_by(.file)' |
  jq '.[]' --indent 0 |
  while read -r line; do
    file=$(echo "$line" | jq -r .file)
    file_type=$(echo "$line" | jq -r .file_type)
    items=$(echo "$line" |
      jq '.matches[] | { file: $file, file_type: $file_type, match: . }' --arg file "$file" --arg file_type "$file_type" |
      jq '. | select(.match.text | test("TODO .+"))'
    )
    if test -z "$items"; then
      continue
    fi

    echo "$items" | jq --slurp '.[]' --indent 0 |
      while read -r item; do
        start_row=$(echo "$item" | jq '.match.start.row')
        end_row=$(echo "$item" | jq '.match.end.row')

        last_commit=$(git --no-pager blame -L "$start_row,$start_row" "$file" -p |
          head -n 1 |
          cut -d ' ' -f 1
        )

        text=$(echo "$item" |
          jq .match.text -r |
          sed 's/^# *TODO *//' |
          sed 's/^\/\* *TODO *//' |
          sed 's/ *\*\/$//')

        echo "$item" | jq '. + {"text": $text, "last_commit": $last_commit}' \
          --arg text "$text" \
          --arg last_commit "$last_commit" \
          -c
      done
  done