aboutsummaryrefslogtreecommitdiff
path: root/comp2041/tigger/tigger-show
blob: 05d8405ef9e44a7b1d6dc4c184f0d6a96c68e8dd (plain)
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
#!/bin/dash

# Test if we have a valid repo before doing anything.
if ! [ -d ".tigger/" ]; then
  printf "tigger-add: error: tigger repository directory .tigger not found\n" >&2
  exit 1
fi

if [ "$(printf "%s" "$@")" = "" ]; then
  printf "tigger-show: error: no arguments provided\n" >&2
  exit 1
fi

branch=$(cat .tigger/branch.tig)
filename=$(echo "$@" | cut -d':' -f2-2)
commit=$(echo "$@" | cut -d':' -f1-1)

# Lots of error checking here and unnecessarily distinct printing per error.

target="" # LAMBDA CAPTURE
if [ "$commit" != "" ]; then
  target=".tigger/$branch/$commit/commit/$filename"

  if ! [ -e ".tigger/$branch/$commit/message.tig" ]; then
    printf "tigger-show: error: unknown commit '%s'\n" "$commit" >&2
    exit 1
  fi

  if ! [ -e "$target" ]; then
    printf "tigger-show: error: '%s' not found in commit %s\n" "$filename" "$commit" >&2
    exit 1
  fi

  printf "%s\n" "$(cat "$target")"
  exit 0

else
  commit=$(find .tigger/"$branch"/ -maxdepth 1 -type d | grep -o -E "[\/][^\/]*$" | tr -d "/" | sort -n -r | head -n 1)
  if [ "$commit" = "" ]; then
    commit="0"
    mkdir --parents ".tigger/$branch/$commit/"
  fi

  if ! [ -e ".tigger/$branch/$commit/staged/$filename" ]; then
    printf "tigger-show: error: '%s' not found in index\n" "$filename" >&2
    exit 1
  fi

  target=".tigger/$branch/$commit/staged/$filename"
fi

if ! [ -e ".tigger/$branch/$commit/" ]; then
  printf "tigger-show: error: unknown commit '%s'\n" "$commit" >&2
  exit 1
fi

if ! [ -e "$target" ]; then
  printf "tigger-show: error: '%s' not found in commit %s\n" "$filename" "$commit" >&2
  exit 1
fi

printf "%s\n" "$(cat "$target")"
exit 0