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

function commits_count () {
    local file=${1}
    local res=$(git --no-pager log --pretty=format:"" "${file}" | wc -l)

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

    echo -n "$(( ${res} ))"
}

function print_commit_format () {
    local file=${1}
    local nth=${2}
    local format=${3}

    git --no-pager log --skip=${nth} -n1 --pretty=format:"${format}" "${file}"
}

function generate_json_history () {
  local file="${1}"
  local count=$(commits_count ${file})

  echo "{"
  echo "  \"file\" : \"${file}\","
  echo "  \"history\" :"
  echo "    ["
  for i in $(seq 0 ${count}); do
      subject="$(print_commit_format ${file} ${i} '%s')"
      abbr_hash="$(print_commit_format ${file} ${i} '%h')"
      hash="$(print_commit_format ${file} ${i} '%H')"
      date="$(print_commit_format ${file} ${i} '%cs')"

  echo "      {"
  echo "        \"subject\" : \"${subject}\","
  echo "        \"abbr_hash\" : \"${abbr_hash}\","
  echo "        \"hash\" : \"${hash}\","
  echo "        \"date\" : \"${date}\""
  echo -n \
       "      }"

      if [ ! ${i} -eq ${count} ]; then
          echo ","
      else
          echo ""
      fi
  done

  echo "    ]"
  echo "}"
}

FILE=`cat`

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