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

# TODO Add support for ammendments
#
# The user can ammend more information to an issue which is located at
# a different place by referencing the issue's id.  Example:
#
# ```bash
# #!/usr/bin/env bash
#
# set -efu
#
# ls -al
# # TODO Original issue
# #
# # @id original-issue
#
# ls
# # @original-issue more information on the issue
# ```

# TODO Only one issue per comment block
#
# Only the first TODO/FIXME inside a comment block should be considered
# as the start of an issue.

# TODO Add support for other keywords
#
# Additionally to TODO, also FIXME should start an issue.  There might
# be more interesting keywords.

# TODO Add tags
#
# Users can add tags inside issue title and description.  Tags are slugs
# and start with @

# TODO Add filter by tags
#
# Users can filter issues for tags with the option -t/--tag @tag.

# TODO Add command line modes list and show
#
# `anissue list` lists all issues in the current directory
# `anissue show <issue>` shows an issue using it's id or automatically
# assigned identifier

# TODO Generate and show hash for each issue


set -efu

git ls-files --cached --exclude-standard --other |
  while read -r input_file; do
    # TODO Add support for all tree-grepper supported files
    #
    # tree-grepper supported files can be listed through `tree-grepper
    # --languages`.
    ext=
    case $input_file in
      *.elm) ext="elm" ;;
      *.nix) ext="nix" ;;
      *.sh) ext="sh" ;;
      *) ;;
    esac

    if test -z $ext; then
      echo "warning, file $input_file ignored" >&2
      continue
    fi

    "$(dirname "$0")"/extract-$ext.sh "$input_file"
  done |
  while read -r item; do
    start_row=$(echo "$item" | jq '.match.start.row')
    end_row=$(echo "$item" | jq '.match.end.row')

    last_commit=$(echo "$item" | jq '.last_commit' -r)
    text=$(echo "$item" | jq '.text' -r)
    file=$(echo "$item" | jq '.file' -r)

    first_commit=$(git --no-pager log --reverse -S"$text" --format=%H | \
      head -n 1)

    created_at=$(git show $first_commit --no-patch --format=%ad)
    heading=$(echo "$text" | sed '/^$/Q' | sed 's/.*TODO //')
    # TODO body is not extracted in shell scripts
    body=$(echo "$text" | tail -n +$(($(echo "$heading" | wc -l) + 2)) | \
      awk -F '[^ ]' '
        NR == 1 {n = length($1)}
        {sub("^ {1,"n"}", ""); print}'
          )

    GREEN='\033[0;32m'
    BOLD=$(tput bold)
    NORMAL=$(tput sgr0)
    NC='\033[0m'
    echo -e "$GREEN--- $created_at --- $file$NC"
    echo
    echo "$BOLD$heading$NORMAL" | fold -s
    if test -n "$body"; then
      echo
      echo "$body" | fold -s
      echo
    fi
    echo
    cat "$file" | nl -w 4 -s "| " -p -d '' -b a| tail -n +$(($start_row - 2)) | head -n $(($end_row - $start_row + 7))
    echo
    echo
  done