aboutsummaryrefslogtreecommitdiff
path: root/comp2041/tigger/tigger-branch
blob: f4a547220ae3aa9c329d266d5f4947201910db93 (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
#!/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


# For some reason we have to make it so that we can only run this if we're after
# the first commit, even though this isn't a part of git and our program can do
# it without issues. 
if ! [ -e ".tigger/branch.unlock" ]; then
  printf "tigger-branch: error: this command can not be run until after the first commit\n" >&2
  exit 1
fi

# Case where we give it no arguments -> list the branches.
if [ "$(echo "$@" | tr -d " \n")" = "" ]; then
  printf "%s\n" "$(find .tigger/* -maxdepth 0 -type d | sort | grep -o -E "[\/][^\/]*$" | tr -d "/")"
  exit 0
fi

target_branch=$(echo "$@" | sed "s|-d||g" | tr -d " "); 
if [ "$target_branch" = "" ]; then
  # no branch provided
  printf "tigger-branch: error: no branch provided\n" >&2
  exit 1
fi

# Delete a branch, again taking care that we remove the target branch string 
# before testing if it contains -d.
if [ "$(echo "$@" | sed "s|$target_branch||g" | grep -o -E "\-d" | tr -d " ")" != "" ]; then

  if [ "$target_branch" = master ]; then
    printf "tigger-branch: error: can not delete branch '%s'\n" "$target_branch" >&2
    exit 1
  fi

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

  rm -r .tigger/"$target_branch"
  printf "Deleted branch '%s'\n" "$target_branch"
  exit 0
fi

# Adding branch logic should test if a branch exists before adding it.
if [ -d .tigger/"$target_branch"/ ]; then
  printf "tigger-branch: error: branch '%s' already exists\n" "$target_branch" >&2
  exit 1
fi

# We have to copy our current commit to the target branch.
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

mkdir --parents .tigger/"$target_branch"/
mkdir --parents .tigger/"$target_branch"/0/staged
cp -r .tigger/"$current_branch"/"$current_commit"/commit .tigger/"$target_branch"/0/

exit 0