aboutsummaryrefslogtreecommitdiff
path: root/comp2041/tigger/tigger-commit
diff options
context:
space:
mode:
authorNicolas James <Eele1Ephe7uZahRie@tutanota.com>2025-02-13 18:00:17 +1100
committerNicolas James <Eele1Ephe7uZahRie@tutanota.com>2025-02-13 18:00:17 +1100
commit98cef5e9a772602d42acfcf233838c760424db9a (patch)
tree5277fa1d7cc0a69a0f166fcbf10fd320f345f049 /comp2041/tigger/tigger-commit
initial commit
Diffstat (limited to 'comp2041/tigger/tigger-commit')
-rwxr-xr-xcomp2041/tigger/tigger-commit88
1 files changed, 88 insertions, 0 deletions
diff --git a/comp2041/tigger/tigger-commit b/comp2041/tigger/tigger-commit
new file mode 100755
index 0000000..b2c7573
--- /dev/null
+++ b/comp2041/tigger/tigger-commit
@@ -0,0 +1,88 @@
+#!/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/"
+file_dir="$commit_files/commit/"
+mkdir --parents "$file_dir"
+mkdir --parents "$commit_files/staged/"
+
+
+# -a causes all files already in the index to have their contents from the
+# current directory added to the index before the commit. We have to remove
+# the message arg before testing the regex here.
+message=$(echo "$@" | grep -o -E "\-m\s?.*" | cut -d' ' -f2- | tr -d \"\')
+if [ "$(echo "$@" | sed "s|$message||g" | grep -o -E "\-a")" != "" ]; then
+ for filename in "$commit_files"/staged/*; do
+ basename=$(basename "$filename")
+ if ! [ -e "$basename" ]; then
+ continue
+ fi
+ cp "$basename" "$commit_files"/staged/
+ done
+fi
+
+
+# If all files in staging are equal to those in commit, we have nothing to commit.
+different=""
+for filename in "$commit_files"/staged/*; do
+ basename=$(basename "$filename")
+
+ EXISTS="$(cmp --silent .tigger/"$branch"/"$commit"/commit/"$basename" "$filename"; echo $?)"
+ if [ "$EXISTS" -ne 0 ]; then
+ different="true"
+ break
+ fi
+done
+for filename in "$commit_files"/commit/*; do
+ basename=$(basename "$filename")
+
+ EXISTS="$(cmp --silent .tigger/"$branch"/"$commit"/staged/"$basename" "$filename"; echo $?)"
+ if [ "$EXISTS" -ne 0 ]; then
+ different="true"
+ break
+ fi
+done
+if [ "$different" = "" ] || [ -z "$(find .tigger/"$branch"/"$commit"/staged/ .tigger/"$branch"/"$commit"/commit/ -maxdepth 1 -type f)" ] ; then
+ printf "nothing to commit\n"
+ exit 0
+fi
+
+
+# Copy all from staged into commit.
+#for filename in "$commit_files"/staged/*; do
+# cp "$filename" "$file_dir"
+#done
+rm -r --force "$commit_files"/commit/
+cp -r "$commit_files"/staged "$commit_files"/commit/
+
+
+# Copy our commit message and setup our new commit (n + 1).
+#printf "%s > .tigger/%s/%s/message.tig\n" "$branch" "$commit"
+printf "%s" "$message" > .tigger/"$branch"/"$commit"/message.tig
+new_commit="$((($(cat .tigger/commit_count.tig) + 1)))"
+mkdir --parents ".tigger/$branch/$new_commit/commit"
+cp -r ".tigger/$branch/$commit/commit" ".tigger/$branch/$new_commit/"
+cp -r ".tigger/$branch/$commit/commit" ".tigger/$branch/$new_commit/staged"
+touch .tigger/branch.unlock
+
+# Increment our commit count value (which isn't used outside of printing).
+echo "$new_commit" > .tigger/commit_count.tig
+
+printf "Committed as commit %s\n" "$((($(printf "%s" "$new_commit") - 1)))"
+exit 0