summaryrefslogtreecommitdiffstats
path: root/repo-access.sh
blob: 3e9fa8d5de88bf4eefb94559c1ff0234f7a510b2 (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
##
# systr_repo_check [<path>=PATH]
#
# Assert that the local repository path exists and is valid.
##
function systr_repo_check
{
    if [ $# -gt 0 ]; then
        path="$1"
    fi

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

##
# systr_repo_resolve_reference <ref>
#
# Resolve a revision name to a commit ID.  The result is printed to
# stdout.
##
function systr_repo_resolve_reference
{
    if [ $# -lt 1 ]; then
        echo "Fatal: too few args to repo_resolve_reference" >&2
        exit 1
    fi

    ref="$1"

    if [[ "$ref" == "BASE" ]]; then
        cat .systr/BASE
    elif [[ "$ref" == "TRAC" ]]; then
        systr_repo_resolve_reference "$TRAC"
    elif [[ "$ref" == "NULL" ]]; then
        echo "NULL"
    else
        (
            cd "$path"

            if [ -f "refs/$ref" ]; then
                cat "refs/$ref"
            elif [ -f "revs/$ref/.commit.systr" ]; then
                echo "$ref"
            else
                echo "$ref is not a revision" >&2
                exit 1
            fi
        )
    fi
}

##
# systr_repo_commit_dist <a> <b>
#
# Determine how many commits have occurred between <a> and <b>.
# IE:  How far behind <b> is <a>?  If <a> and <b> are (or
# reference) the same commit, the result it zero.  Result
# written to stdout.
##
function systr_repo_commit_dist
{
    if [ $# -lt 2 ]; then
        echo "Fatal: too few args to repo_commit_dist" >&2
        exit 1
    fi

    a=$(systr_repo_resolve_reference "$1")
    b=$(systr_repo_resolve_reference "$2")
    d=$((0))

    while [[ "$b" != "NULL" ]]
    do
        if [[ "$b" == "$a" ]]; then
            echo "$d"
            exit
        fi

        d=$(($d+1))
        read b <"$path/revs/$b/.commit.systr"
    done

    echo "Inf"
}

##
# systrunk shortlog [<commit>=BASE]
#
# View commit history.  Generate list of commit IDs only, each
# one a newline.  Written to stdout.
##
function systr_short_log
{
    if [ $# -lt 1 ]; then
        commit="$BASE"
    else
        commit=$(systr_repo_resolve_reference "$1")
    fi

    while [[ "$commit" != "NULL" ]]
    do
        echo "$commit"
        read commit <"$path/revs/$commit/.commit.systr"
    done
}

##
# systr_repo_merge_base <a> <b>
#
# Find the merge base between <a> and <b>.  That is, determine
# which other commit is the closest common ancestor of both
# <a> and <b>.  Result or "NULL" written to stdout.
##
function systr_repo_merge_base
{
    if [ $# -lt 2 ]; then
        echo "Fatal: too few args to repo_merge_base" >&2
        exit 1
    fi

    a=$(systr_repo_resolve_reference "$1")
    b=$(systr_repo_resolve_reference "$2")

    while [[ "$a" != "NULL" ]]
    do
        check=$(systr_short_log "$b" | grep "$a")

        if [[ "$check" == "$a" ]]; then
            echo "$a"
            exit
        fi

        read a <"$path/revs/$a/.commit.systr"
    done
}

##
# systrunk log [<commit>=BASE]
#
# View commit history.  Generate a dump of all commits reachable
# from the given commit, or BASE if no argument is given.
##
function systr_log
{
    if [ $# -lt 1 ]; then
        commit="$BASE"
    else
        commit=$(systr_repo_resolve_reference "$1")
    fi

    while [[ "$commit" != "NULL" ]]
    do
        (
            cd "$path/revs/$commit/"

            read date <.date.systr
            read author <.author.systr
            read email <.email.systr

            printf '%s %s\n'     "$commit" "$date"
            printf '%s <%s>\n\n' "$author" "$email"
            cat .mesg.systr
            echo ""
            echo ""
        )

        read commit <"$path/revs/$commit/.commit.systr"
    done
}

##
# systrunk init <name>
#
# Initialize a repository called "<name>.systr" in the current
# directory.
##
function systr_init
{
    if [ $# -lt 1 ]; then
        echo "Fatal: too few args to init" >&2
        exit 1
    fi

    name="$1"

    mkdir "$name.systr"
    cd "$name.systr"

    mkdir refs revs
    echo "NULL" >refs/HEAD
}

##
# systrunk status
#
# View current information such as which branch your worktree is
# TRACking, which commit it is BASEd on, and how far out-of-date
# it is.  Additionally, report whether a merge or update operation
# is currently in-progress.
##
function systr_status
{
    if [[ "$MERG" != "NULL" ]]; then
        mergcommit=$(systr_repo_resolve_reference "$MERG")

        if [[ "$MERG" == "TRAC" ]]; then
            echo "Update in progress, to $mergcommit"
        else
            echo "Merge in progress, from $MERG ($mergcommit)"
        fi

        echo ""
    fi

    traccommit=$(systr_repo_resolve_reference "$TRAC")

    if [[ "$TRAC" == "$traccommit" ]]; then
        echo "Not tracking any branch"
    else
        echo "Tracking $TRAC ($traccommit)"
    fi

    if [[ "$BASE" == "NULL" ]]; then
        echo "BASE unset"
    else
        echo "BASE at $BASE  {$updated}"
    fi

    dist=$(systr_repo_commit_dist "$BASE" "$TRAC")

    if [[ "$TRAC" != "$traccommit" ]]; then
        if [[ "$dist" == "0" ]]; then
            echo "Up-to-date"
        else
            echo "Behind $dist commits"
        fi
    fi
}