switch.bash

#!/usr/bin/env bash

function switch_v(){
    run switch version
}
function switch_p(){
    run switch project
}
function switch_f(){
    run switch feature
}
function switch_dv(){
    run switch default version
}
function switch_url(){
    run switch host url
}

function switch_version(){
    run switch branch
}

function switch(){
    msg_header "Shorthand with:"
    # msg_instruct "[bent switch v], [bent switch f], [bent switch p]"
    msg_instruct "[bent switch v], [bent switch url], [bent switch dv] "
    n="$(color_li 0)"
    d="$(color_li 1)"
    f="${cOff}"
    prompt_choose_function \
        "# bent switch [command] " \
        "run switch version" "version" "Switch to a different version of your project" \
        "run switch host url" "host url" "Change your git host" \
        "run switch default version" "default version" "Go online to change the default version of your project" \
    ;
        # @TODO "run switch host" : To switch between `origin` and other remotes that can be added. `host url` changes the url for origin
        # @TODO "run switch feature" "${n}feature$f: (unavailable)Switch to a different feature of your project" \
        # @TODO "run switch project" "${d}project$f: (unavailable)Switch to a different project" \
}

function switch_help(){
    # echo "switch help"
    run switch
}

function switch_branch(){
    is_project_dir || return
    local branchList;

    branch_list_unique branchList refs/heads refs/remotes/origin

    curBranch="$(cur_branch)"
    
    chooseCommand="prompt_choose choice '#Choose Version' "
    options=""
    for branch in "${branchList[@]}"; do
        branch=$(str_trim "${branch}")
        key="${branch}"
        branch="${branch#remote: }"
        shorthand="${branch}"
        if [[ "${branch}" == "${curBranch}" ]];then
            shorthand="current"
        fi
        chooseCommand+="'${branch}' '${shorthand}' '${key}' "
    done;

    eval $chooseCommand

    prompt_exited "$choice" && return;

    git checkout "${choice}"

    return;
}

function switch_project(){
    # @TODO implement this feature
    
    msg_header "Switch between projects"
    msg
    msg_mistake "This feature is not yet available. But you can:"
    msg_instruct "[cd /path/to/your/project]"
    msg "Then run [bent ...] to work with that project"

}

function switch_feature(){
    is_project_dir || return;

	# @TODO implement this feature

    msg_header "Switch between features"
    msg_mistake "Feature switching is not yet implemented."
    msg "But, features are simply different versions of your project, so here is [bent switch version]"
    msg
    
    run switch version
}

function switch_default_version(){
    is_project_dir || return;

    header "We'll guide you through it. (instructions best suited for github)"
    
    ## @TODO learn about the problems this can cause & notify the user of them.
    ## @TODO Rename this function
    ## @TODO add good instructions for each git host

    url="$(url default_branch)"
    
    msg "  0. Versions are called 'branches' on git"
    msg "  1. Go to ${url}"
    msg "  2. Click the dropdown under 'Default Branch'"
    msg "  3. Choose the version (branch) you want as your default"
    msg "  5. Click 'Update' button & confirm the popup. "

    msg
}

function switch_host_url(){

    # @TODO I don't know... Just make this better. More guided. Clearer information?
    # @TODO use prompt_exited
    # @TODO present remote name & url for confirmation before setting 

    header "Your current urls are: "
    git remote -v
    msg ""
    msg "1. You will usually change 'origin' "
    msg "2. If there are others, you might need to update them as well."
    msg ""
    msg_instruct "[ctrl-c] to exit."
    prompt_quit "Which one will you change? [type it]: " remote \
        && return
    prompt_quit "Enter new URL: " url \
        && return
    msg ""
    git remote set-url ${remote} ${url}
    git remote set-url --push ${remote} ${url}
    msg_status "Host url updated"
    #@TODO offer upload after changing url

}