summaryrefslogtreecommitdiffstats
path: root/commit.sh
blob: 096631acd666723beba95952428fff696d5c9daf (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
##
# creat_commit_dir_local
#
# Create new commit directory, and assign its commit ID.  Commit ID is
# printed to stdout.
##
function creat_commit_dir_local
{
    cd "$path"
    mktemp -d XXXXXXXX
}

##
# systrunk commit [<branch>=TRAC]
#
# Create a snapshot of the current state of the worktree and record
# it in the repository.  Your worktree must be up-to-date with <branch>
# before you can commit.  The branch committed to is updated to
# reference the new commit.  If <branch> does not exist in the
# repository, it is created.  If <branch> is a commit ID instead of
# a symbolic reference, the new commit will not exist on any branch,
# but an anonymous one referenced by TRAC.
##
function systr_commit
{
    read remote <.systr/remote
    read path   <.systr/path
    read BASE   <.systr/BASE
    read TRAC   <.systr/TRAC

    if [ $# -gt 1 ]; then
        branch=$2
    else
        branch="$TRAC"
    fi

    branchcommit=$(get_commit "$branch") || branchcommit="$BASE"

    if [[ "$BASE" != "$branchcommit" ]]; then
        echo "Worktree is out-of-date, won't commit"
        exit
    fi

    # local repository #
    if [[ "$remote" == "" ]]; then
        newcommit=$(creat_commit_dir_local)
        mesgfile=$(mktemp -t systr-edit-mesg.XXXXXX)
        echo "" >$mesgfile
        echo "# --" >>$mesgfile
        systrunk status | sed 's/^/# /' >>$mesgfile
        vi $mesgfile

        echo "Sending files..."

        rsync -az --info=progress2 --info=stats2 \
            --delete --exclude='*.systr' \
            --link-dest="$path/$branchcommit/" \
            . "$path/$newcommit/"

        sed '/^#/ d' <$mesgfile >"$path/$newcommit/.mesg.systr"
        echo "$branchcommit" >"$path/$newcommit/.commit.systr"

        if [ -f ".systr/author" ]; then
            cat .systr/author >"$path/$newcommit/.author.systr"
        else
            echo "Anonymous" >"$path/$newcommit/.author.systr"
        fi

        if [ -f ".systr/email" ]; then
            cat .systr/email >"$path/$newcommit/.email.systr"
        else
            echo "<anon>" >"$path/$newcommit/.email.systr"
        fi

        echo "$newcommit" >"$path/$branch"
        echo "$newcommit" >.systr/BASE

        if [[ "$TRAC" == "$BASE" ]]; then
            echo "$newcommit" >.systr/TRAC
        else
            echo "$branch" >.systr/TRAC
        fi

    # remote repository #
    else
        exit

    fi
}