# gen-changelog -rw-r--r-- 2.1 KiB View raw
                                                                                
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
#!/usr/bin/env bash

get_tag() {
    local rev="$1"
    local lastTag="$2"

    local tag=''
    IFS='-' read -ra tag <<< "$(git describe --tags "${rev}")"

    if [[ -z "${tag[1]}" ]]; then
        echo -n "${tag[0]}"
    else
        echo -n "${lastTag}"
    fi
}

get_tag_date() {
    local tag="$1"

    local date=''
    date="$(git log -1 --pretty=tformat:%ad --date=short "${tag}")"
    echo -n "${date}"
}

check_git_tag() {  
    local rev="$1"

    if git describe --tags "${rev}" &> /dev/null; then
        local tag=''
        tag="$(get_tag "${rev}" "${lastTag}")"

        # echo "Last Tag: ${lastTag} Current: ${tag}"  
        if [[ "${lastTag}" != "${tag}" ]]; then
            local date=''
            date="$(get_tag_date "${tag}")"
            echo '' # Add a blank line
            echo "## [${tag}] - ${date}"
            lastTag="${tag}"
        fi
    fi
}

get_changelog_notes() {
    local rev="$1"

    if git notes --ref=changelog list "${rev}" &> /dev/null; then
        local line=''
        line="$(git notes --ref=changelog show "${rev}")"
        echo "- ${line}"
    fi
}

get_changelog_notes_by_section() {
    local rev="$1"
    local section="$2"

    if git notes --ref=changelog list "${rev}" &> /dev/null; then
        local line=''
        line="$(git notes --ref=changelog show "${rev}")"
        if [[ "${line}" =~ ^${section} ]]; then
            #echo "${rev} - ${line}"
            echo "- ${line}"
        fi
    fi
}
generate_header() {
    echo "# Changelog"
    echo 'All notable changes to this project will be documented in this file.'
    echo "" # Blank line intentional
    echo 'The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)'
    printf '\n## %s\n' "[Unreleased]"
}

main() {
    local lastTag=''
    generate_header

    sections=('\[Added\]' '\[Changed\]' '\[Deprecated\]' '\[Removed\]' '\[Fixed\]' '\[Security\]')
    for rev in $(git rev-list HEAD); do
        for section in "${sections[@]}"; do
            check_git_tag "${rev}"
            get_changelog_notes_by_section "${rev}" "${section}"
        done
    done

    exit 0
}

main "$@"