# -*- coding: utf-8 mode: sh -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=utf-8

function __pman_pdev_branches() {
    local toplevel="$(git rev-parse --show-toplevel 2>/dev/null)"
    [ -n "$toplevel" ] || return 0

    (
        # cf pman.conf.sh
        UPSTREAM=
        DEVELOP=develop
        FEATURE=wip/
        RELEASE=release-
        MAIN=master
        TAG_PREFIX=
        TAG_SUFFIX=
        HOTFIX=hotfix-
        DIST=
        [ -f "$toplevel/.pman.conf" ] && source "$toplevel/.pman.conf"
        # lister les branches
        branches="$DEVELOP|$FEATURE.*"
        [ -n "$UPSTREAM" ] && branches="$branches|$UPSTREAM"
        remote=origin/
        {
            git for-each-ref refs/heads/ --format='%(refname:short)' 2>/dev/null
            git for-each-ref "refs/remotes/$remote" --format='%(refname:short)' 2>/dev/null |
                grep -F "$remote" |
                cut -c $((${#remote} + 1))-
        } | LANG=C sort -u | grep -E "^($branches)\$"
    )
}

function __pdev_completion() {
    local cur
    _get_comp_words_by_ref cur
    COMPREPLY=($(compgen -W "$(__pman_pdev_branches)" "$cur"))
}
complete -F __pdev_completion pdev