aboutsummaryrefslogtreecommitdiff
path: root/comp2041/tigger/tigger-branch
diff options
context:
space:
mode:
Diffstat (limited to 'comp2041/tigger/tigger-branch')
-rwxr-xr-xcomp2041/tigger/tigger-branch68
1 files changed, 68 insertions, 0 deletions
diff --git a/comp2041/tigger/tigger-branch b/comp2041/tigger/tigger-branch
new file mode 100755
index 0000000..f4a5472
--- /dev/null
+++ b/comp2041/tigger/tigger-branch
@@ -0,0 +1,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