| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 | #!/bin/bash
__die_if() {
    #
    # Die if blocking condition $1 is detected
    #
    local condition="$1"
    local x
    case "$condition" in
        nogit)
            git rev-parse HEAD >&/dev/null\
             || die "cannot do this outside git repo"
            ;;
        norelbr)
            local relsrc=$(ini 1value project:relsrc)
            __git_info curbranch \
              | grep -qFx "$relsrc" \
             || die "you are not on release source branch: $relsrc"
            ;;
        dirty)
            git diff --shortstat 2>/dev/null \
              | grep -q . \
             && die "tree is dirty: $dirt"
            ;;
        novertag)
            __git_info lasttag \
              | grep -q . \
             || die "cannot find last tag"
            ;;
        nobump)
            git diff-tree --no-commit-id --name-only -r HEAD \
              | grep -qFx mkit.ini \
             || die "last change must be version bump in mkit.ini"
            ;;
        wip)
            __git_info reldiff \
              | grep '^....... WIP ' \
             && die "WIP commit since $(__git_info lasttag)"
            ;;
        old_c)
            x=$(__ver_info nextver_g)
            __ver_info currver_c \
              | grep -qFx "$x" \
             || die "new version not in mkit.ini: $x"
            ;;
    esac
}
__git_info() {
    #
    # Get git info $1
    #
    local info="$1"
    case "$info" in
        lasttag)    git tag | grep ^v | sort -V | tail -n1  ;;
        curbranch)  git rev-parse --abbrev-ref HEAD         ;;
        reldiff)    git log --oneline "$(__git_info lasttag)..HEAD" --name-only ;;
    esac
}
__ver_info() {
    #
    # Get git info $1
    #
    local info="$1"
    case "$info" in
        lastver_g)  __git_info lasttag | sed s/^v// ;;
        nextver_g)  __make_ver "$level" "$(__ver_info lastver_g)" ;;
        currver_c)  ini 1value project:version ;;
        nextver_c)  __make_ver "$level" "$(__ver_info currver_c)" ;;
    esac
}
__make_ver() {
    local level=$1
    local old=$2
    local oldx=${old%.*.*}
    local oldz=${old#*.*.}
    local tmpy=${old%.*}
    local oldy=${tmpy#*.}
    case $level in
        x) new="$((oldx+1)).0.0"            ;;
        y) new="$oldx.$((oldy+1)).0"        ;;
        z) new="$oldx.$oldy.$((oldz+1))"    ;;
        *) die "invalid release level: $1"  ;;
    esac
    echo "$new"
}
__release() {
    #
    # Prepare release
    #
    # Span release routines: make a signed tag, check branch
    # and update release branch
    #
    # FIXME: Using project:prerl as build.sh:get_version() does may not be
    #        compatible with this release strategy
    #
    local level=$1
    local newtag
    __die_if nogit
    __die_if norelbr
    __die_if dirty
    __die_if novertag
    __die_if nobump
    __die_if wip
    __die_if old_c
    newtag=v$(__ver_info nextver_g)
    set -e
    debug_var newtag
    $MKIT_DRY && return
    git tag -m "$MKIT_PROJ_NAME $newtag - $CODENAME" "$newtag"
    git branch -f "$(ini 1value project:reldst)" "$newtag"
}
__git_msg_vbump() {
    echo "Bump version"
    echo ""
    echo "Overview of changes:"
    echo ""
    __git_info reldiff \
      | sed '
            s/^[a-f0-9]\{7\} / *  &/; t PATHS
            s/^/        /
            :PATHS
        '
}
__vbump() {
    local level="$1"
    local lastver   # current from mkit.ini
    local nextver   # after the bump
    __die_if nogit
    __die_if norelbr
    __die_if dirty
    lastver=$(__ver_info currver_c)
    nextver=$(__ver_info nextver_c)
    debug_var lastver nextver
    $MKIT_DRY && return
    update_version "$nextver" mkit.ini \
      || die "failed to update version in mkit.ini"
    git add mkit.ini \
      || die "failed to add mkit.ini to the index"
    git commit -e -m "$(__git_msg_vbump)"
}
vbump_x() {
    __vbump x
}
vbump_y() {
    __vbump y
}
vbump_z() {
    __vbump z
}
release_x() {
    __release x
}
release_y() {
    __release y
}
release_z() {
    __release z
}
 |