aboutsummaryrefslogtreecommitdiff
path: root/comp2041/tigger/tigger-rm
blob: e50420ad5c547e3a0b21b64d6fd3e306bb2d3f1f (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
64
65
66
#!/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

# Get current branch, then current commit (default 0).
branch=$(cat .tigger/branch.tig)
mkdir --parents ".tigger/$branch/"
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

commit_files=".tigger/$branch/$commit/"
mkdir --parents "$commit_files/commit/"
mkdir --parents "$commit_files/staged/"

for filename in $(echo "$@" | sed "s|--force||g" | sed "s|--cached||g"); do
  
  basename=$(basename "$filename")
  if ! [ -e .tigger/"$branch"/"$commit"/staged/"$basename" ]; then
    printf "tigger-rm: error: '%s' is not in the tigger repository\n" "$basename" >&2
    exit 1
  fi

  # Accidental deletion checking, should be non-cached and non-forced.
  if [ "$(echo "$@" | grep -o -E "\-\-force")" = "" ]; then

    if [ "$(cmp --silent .tigger/"$branch"/"$commit"/staged/"$basename" "$basename"; echo $?)" -ne 0 ]; then
    
      if [ "$(cmp --silent .tigger/"$branch"/"$commit"/staged/"$basename" .tigger/"$branch"/"$commit"/commit/"$basename"; echo $?)" -ne 0 ]; then
        printf "tigger-rm: error: '%s' in index is different to both the working file and the repository\n" "$basename" >&2
        exit 1
      fi

      if ! [ "$(echo "$@" | grep -o -E "\-\-cached")" != "" ]; then
        printf "tigger-rm: error: '%s' in the repository is different to the working file\n" "$basename" >&2
        exit 1
      fi

    fi
    

    if ! [ "$(echo "$@" | grep -o -E "\-\-cached")" != "" ]; then
      if [ "$(cmp --silent .tigger/"$branch"/"$commit"/staged/"$basename" .tigger/"$branch"/"$commit"/commit/"$basename"; echo $?)" -ne 0 ]; then
        printf "tigger-rm: error: '%s' has staged changes in the index\n" "$basename" >&2
        exit 1
      fi
    fi
  fi

  rm --force .tigger/"$branch"/"$commit"/staged/"$basename"
  # If the cached option is specified the files are removed only from the index.
  if [ "$(echo "$@" | grep -o -E "\-\-cached")" != "" ]; then
    continue
  fi

  rm --force "$basename"

done

exit 0