aboutsummaryrefslogtreecommitdiff
path: root/comp2041/tigger/tigger-checkout
blob: a1a5a21c5a7dfc2346110a56d61a6840329318b6 (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/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

# Case where we give it no arguments -> early out (without error?)
target_branch=$(echo "$@" | tr -d " "); 
if [ "$target_branch" = "" ]; then
  # no branch provided, idk if we should print anything so just early out
  exit 1
fi

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


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

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

for filename in *; do
  basename=$(basename "$filename")

  # nothing in dir, just exit (should exit)
  if ! [ -e "$filename" ]; then
    continue
  fi 
  
  # if it's in staging, do not remove
  #if [ -e .tigger/"$current_branch"/"$current_commit"/staged/"$basename" ]; then
  #  continue
  #fi

  # if it doesn't exist in the target commit, remove
  if ! [ -e .tigger/"$target_branch"/"$target_commit"/commit/"$basename" ]; then
    rm "$basename"
    continue
  fi
done

for filename in .tigger/"$target_branch"/"$target_commit"/commit/*; do
  basename=$(basename "$filename")

  # nothing in dir, just exit (should exit)
  if ! [ -e "$filename" ]; then
    continue
  fi 
  
  # if it's in the current dir, do nothing TODO fix if different
  if [ -e "$basename" ]; then

    # If it's the same AND not different to current staging, don't copy.
    if [ "$(diff .tigger/"$target_branch"/"$target_commit"/commit/"$basename" "$basename")" = "" ]; then
      if [ "$(diff .tigger/"$current_branch"/"$current_commit"/staged/"$basename" "$basename")" != "" ]; then
        continue
      fi
    fi

  fi

  cp "$filename" ./
done

# Non-compliant tigger vs git behaviour: copy our staged to theirs.
rm -rf .tigger/"$target_branch"/"$target_commit"/staged
cp -r .tigger/"$current_branch"/"$current_commit"/staged .tigger/"$target_branch"/"$target_commit"/staged

echo "$target_branch" > .tigger/branch.tig
printf "Switched to branch '%s'\n" "$target_branch"
exit 0