summaryrefslogtreecommitdiffstats
path: root/checkout.sh
blob: ab9d4bdf3f382adcdf5fd27ed7e3cc9c3d189b98 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
##
# 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
# and is contacted over SSH.  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
{
    # check arguments #
    if [ $# -lt 2 ]; then
        echo "Missing required parameters"
        exit
    fi

    # init worktree #
    if [ $# -gt 2 ]; then
        init_wktree $@
    fi

    # perform checkout #
    read remote <.systr/remote
    read path   <.systr/path
    version=$2

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

    commit=$(get_commit "$version")
    symref=$(get_symref "$version")

    echo "$commit" >.systr/BASE
    echo "$symref" >.systr/TRAC

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

    echo "Checking out files..."

    # local repository #
    if [[ "$remote" == "" ]]; then
        rsync -az --info=progress2 --info=stats2 \
            --delete --exclude='*.systr' \
            "$path/$commit/" .

    # remote repository #
    else
        rsync -az -e ssh --info=progress2 --info=stats2 \
            --delete --exclude='*.systr' \
            "$remote:$path/$commit/" .

    fi
}