-
Notifications
You must be signed in to change notification settings - Fork 5
/
git-case-field
executable file
·63 lines (45 loc) · 1.53 KB
/
git-case-field
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
#!/bin/bash
#
# Assumptions:
# - in a git repository
# - we can access git-case-common
# - the branch defined by GIT_CASE_HEAD exists (see git-case-common)
source $(dirname $0)/git-case-common
test -z "$1" && die "$PROGRAM [-d] <field-name> [<value>]"
if test "$1" = "-d" ; then
test "${#@}" = 2 || die "when deleting syntax is: $PROGRAM -d <field-name>"
action="removed"
FIELD="$2"
elif test "${#@}" = 1 ; then
action="read"
FIELD="$1"
elif test "${#@}" = 2 ; then
action="set"
FIELD="$1" ; shift
VALUE="$@"
else
die "too many arguments"
fi
echo "$FIELD" | grep -q '^[a-zA-Z0-9][a-zA-Z0-9._-]*$' || die "format of field '$FIELD' is invalid"
ID=$(cat "$GIT_CASE_ACTIVE" 2>/dev/null || true)
test -z "$ID" && die "no active case set"
shortID=$(gen_short_case_id "$ID")
FIELD_FILE="$ID/fields/$FIELD"
count=$(git ls-tree $GIT_CASE_HEAD "$FIELD_FILE" | wc -l)
if test "$action" = "removed" -a "$count" -ne "1" ; then
die "no field '$FIELD' set on case $shortID"
fi
# handle read first
if test "$action" = "read" ; then
test "$count" -ne "1" && die "no field '$FIELD' set on case $shortID"
exec git show "$GIT_CASE_HEAD:$FIELD_FILE"
fi
# we work in the working dir
go_to_git_case_work_dir
git read-tree -i --reset "$GIT_CASE_HEAD"
if test "$action" = "set" ; then
add_file_to_index "$FIELD_FILE" "$VALUE"
else
git update-index --remove "$FIELD_FILE"
fi
commit_index_and_update_branch "field '$FIELD' $action on $ID" "-p $GIT_CASE_HEAD"