core.bash

#!/usr/bin/env bash

import core/core-revert
import core/core-update
import core/core-unsorted
import core/core-url

## Aliases:

function core(){
    run core help 
}

function core_status(){
    run check
}
function core_check(){
    run check
}

function core_help(){
    # @export_start(Internal.prompt_choose_function)   
    prompt_choose_function \
        "# bent [command]" \
            "run help" "help" "View all functions and extended help" \
            "run core save" "save" "Save & upload your project" \
            "run core upload" "upload" "upload your project (doesn't save first)" \
            "run core update" "update" "Download all changes" \
            "'" \
            "run core tag" "tag" "Create a numbered release of your project" \
            "run core revert" "revert" "Rollback to a previous save of your project" \
            "run core merge" "merge" "Merge current branch into another branch" \
            "run core check" "check" "Check the status of your project" \
            "run core url" "url" "Get special urls to git hosts" \
            "run core ignore" "ignore" "Download a .gitignore file from Github's gitignore repo" \
        \
    ;
    # @export_end(Internal.prompt_choose_function)   
}

function core_save(){
    is_project_dir || return;

    ## @TODO
    # Use git status --porcelain=v1 -b to check if save is needed
    # porcelain prints `## version_1...origin/version_1 [ahead 1]` when the commit is ahead of the remote
        # If it is not ahead of the remote, then it will print `## version_1...origin/version_1`
        # I'm guessing it does [behind 1] if it's behind the remote
        # and it does print [ahead 1, behind 1] if that's the case
    # It prints M dir/filename \n D dir/file \n A file \n ?? file for files that are changed,deleted,added, untracked
    #
    # Then test if current branch is ahead of remote
    
    # @TODO List changed files 
    # @TODO Ask which changed files should be saved

    msg
    msg_status "Saving project found at $(project_dir)"
    changes=$(str_trim "$(changed_files)")

    if [[ "$changes" == "" ]]; then
        msg "No changes, nothing to save."
        prompt_yes_or_quit "Nothing to save. Upload anyway?" na \
            && return;
        run core upload
        return;
    fi

    msg
    header "Current Status"
    run check
    line_break

    msg_instruct "[enter] for generic message"
    prompt_quit "What has changed?: " changes -e \
        && return;

    if [[ $changes == "" ]]; then
        changes="Save made by GitBent with no description from user."
    fi
    git add -A
    git commit -m "${changes}"

    prompt_yes_or_quit "Upload changes?" \
        && return;
    run core upload
}


function core_upload(){
    is_project_dir || return;
    # @TODO only provide -u origin cur_branch when remote is not set, to avoid the message:
        # "Branch 'test-git-operations' set up to track remote branch 'test-git-operations' from 'origin'."

    run check 

    git push -u origin "$(cur_branch)"

    run check 
}

function core_tag(){
    is_project_dir || return;

    curBranch=$(project_dir)
    msg_status "Release project at ${projDir}."
    bent show versions
    msg_instruct "[ctrl-c] to exit"
    prompt_quit "Enter version name to release:" branch \
        && return;

    prompt_quit "Enter release version number (ex: 1.0.0)" release \
        && return;

    prompt_quit "Description for this release: " description \
        && return

    git tag -a "$release" -m "$description" "$branch"
    git push --tags
    # git push origin :$branch
}