summaryrefslogblamecommitdiffstats
path: root/checkout.sh
blob: 59cc82e51b2b221b88e9528a13631da06419bc33 (plain) (tree)
1
  





























































                                                                  
                                                 


                                                                   


                                                                  




                                                         


                                                         

      
                
 





                                  





                                                          



                                       
                                                     
                               



                                       






                                      
                                              
 
##
# systr_check_repo <path>
#
# Assert that the local repository path exists and is valid.
##
function systr_check_repo
{
    if [ $# -lt 1 ]; then
        echo "Fatal: too few arguments to systr_check_repo"
        exit 1
    fi

    path="$1"

    (
        cd "$path"

        if [ ! -f "refs/HEAD" ]; then
            echo "Fatal: $path is not a repository"
            exit 1
        fi
    )
}

##
# systr_init_wktree [<remote>] <path>
#
# Assert that the repository at <remote>:<path> exists, then setup
# the .systr directory at the CWD.
##
function systr_init_wktree
{
    if [ $# -lt 1 ]; then
        echo "Fatal: too few arguments to systr_init_wktree"
        exit 1
    fi

    if [ $# -gt 1 ]; then
        remote="$1"
        shift
    else
        remote=""
    fi

    path="$1"

    if [[ "$remote" == "" ]]; then
        systr_check_repo "$path"
    else
        ssh "$remote" "systrunk check-repo \"$path\""
    fi

    mkdir -p .systr/

    echo "$remote" >.systr/remote
    echo "$path"   >.systr/path
    echo "NULL"    >.systr/BASE
    echo "NULL"    >.systr/TRAC

    echo "Setup worktree at $(pwd)"
}

##
# systrunk checkout <version> [[<remote>] <path>]
#
# Reset a worktree to the state at <version>.  If <path> is given,
# checkout also initializes a new worktree at the CWD.  If <remote>
# is given, the path is assumed to be located on a remote machine.
# While resetting to the state at <version>, all local uncommitted
# changes are lost.
#
# After checkout, BASE and TRAC will be set to <version>.
##
function systr_checkout
{
    if [ $# -lt 1 ]; then
        echo "Fatal: too few arguments to systr_checkout"
        exit 1
    fi

    version="$1"

    if [ $# -gt 1 ]; then
        shift
        systr_init_wktree "$@"
        read remote <.systr/remote
        read path   <.systr/path
    fi

    if [[ "$version" == "NULL" ]]; then
        echo "Checking out NULL :: Checkout not performed"
        exit
    fi

    if [[ "$version" == "TRAC" ]]; then
        version="$TRAC"
    fi

    commit=$(systr_repo_resolve_reference "$version")
    echo "$commit" >.systr/BASE

    if [[ "$version" != "BASE" ]]; then
        echo "$version" >.systr/TRAC
    fi

    if [[ "$commit" == "NULL" ]]; then
        echo "Nothing to checkout"
        exit
    fi

    echo "Checking out files..."
    systr_rsync_normal "$path/revs/$commit/" .
}