summaryrefslogtreecommitdiffstats
path: root/scripts/generate_history.sh
blob: cc70d55494fa74ac56ea58dc6a77a1aa345c8b33 (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
#!/usr/bin/bash

FORMAT="{\"subject\":\"%s\",\"abbr_hash\":\"%h\",\"hash\":\"%H\",\"date\":\"%cs\""

function generate_history_json () {
    local file="${1}"

    local logs=$(git --no-pager log --follow --pretty=format:"${FORMAT}" "${file}")

    if [ ! $? -eq 0 ]; then
        exit 1
    fi

    local count=0
    local name="${file}"

    while read -r line; do
        local hash=$(echo "${line}}" | jq -j '.hash')

        local pre_name="$(git --no-pager show --stat=10000 ${hash} | sed -e 's/ *\(.*\){\(.*\) => \(.*\)}/\1\2 => \1\3/'  | grep "=> ${name}" | xargs | cut -d' ' -f1)"

        if [[ ${count} -eq 0 ]]; then
            echo -n "[ "
        else
            echo -n ", "
        fi

        echo "${line}, \"filename\":\"${name}\"}"

        if [[ ! -z "${pre_name}" ]]; then
            name="$(echo ${pre_name})"
        fi

        count=$(( ${count} + 1 ))
    done < <(echo "${logs}")

    echo -n "]"
}

function generate_json () {
  local file="${1}"

  echo "{"
  echo "  \"file\" : \"${file}\","
  echo "  \"history\" : $(generate_history_json "${file}")"
  echo "}"
}

FILE=`cat`

tmp_file=$(mktemp)
generate_json ${FILE} > ${tmp_file}
haskell-mustache ${1} ${tmp_file}
rm ${tmp_file}