commit 36f0f538f0a7ce007f6429b7ac8bc67b49420dbb
parent 345eae4260a34b6e3be54a3075ce1c5b4c4fe853
Author: Jacob R. Edwards <n/a>
Date: Sun, 4 Dec 2022 17:32:55 -0600
Add tagging script
This provides an easy way to tag files (or anything really) by
simply appending their name to a file (or "tag"). The notin, addnotin,
and unroot scripts were written with this in mind, but I believe
they're generally useful too.
I'v been using a similar system for awhile but didn't add it here
because I didn't like the implementation.
Diffstat:
| A | local/bin/bin/tag | | | 102 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1 file changed, 102 insertions(+), 0 deletions(-)
diff --git a/local/bin/bin/tag b/local/bin/bin/tag
@@ -0,0 +1,102 @@
+#!/bin/sh
+# Copyright 2022 Jacob R. Edwards
+# Filesystem tagging system
+#
+# Each name from the standard input (made relative the current
+# directory or base (set with -b)) is appended to the given tagfiles
+# if they don't contain them already. Tagfiles are paths relative
+# to the current directory unless a tag directory is given (-t).
+#
+# In automatic mode (-a), both the tag and base directories are
+# filled in automatically. To find the tag directory, the directory
+# 'tags' is recursively searched for in each parent directory. The
+# base directory is generated by getting the parent directory of the
+# tags directory.
+#
+# Raw mode (-r) does not alter input in any way and is suitable for
+# tagging things other than files.
+
+error() {
+ echo "error: $1" 1>&2
+ exit 1
+}
+
+usage() {
+ {
+ test $# -gt 0 &&
+ echo "error: $1"
+ echo 'usage: tag [-ar] [-b base] [-t tags] tagfile [...]'
+ } 1>&2
+ exit 1
+}
+
+findtags() {
+ dir="$1"
+ while test "$dir" != '/' -a '!' -d "$dir"/tags
+ do
+ dir="$(dirname "$dir")"
+ done
+ ! test -d "$dir"/tags &&
+ return 1
+ echo "$dir"/tags
+}
+
+rebase() (
+ root | (test "$base" && cd "$base"; unroot) | awk '{
+ if (/^\//)
+ printf "Unable to rebase '%s' (passing as is)\n", $0 > "/dev/stderr"
+ print
+ }'
+)
+
+add() (
+ test "$tags" &&
+ cd "$tags"
+ addnotin "$@"
+)
+
+auto=false
+raw=false
+while test $# -gt 0
+do
+ case "$1" in
+ (-a)
+ auto=true ;;
+ (-r)
+ raw=true ;;
+ (-b)
+ base="$2"
+ shift ;;
+ (-t)
+ tags="$2"
+ shift ;;
+ (--)
+ shift
+ break ;;
+ (-*)
+ usage "$1: Invalid option" ;;
+ (*)
+ break ;;
+ esac
+ shift
+done
+
+test $# -eq 0 &&
+ usage 'No tag files given'
+
+if $auto
+then
+ if ! test "$tags"; then
+ tags="$(findtags "$PWD")" ||
+ error 'Unable to find tags directory'
+ fi
+ ! test "$base" &&
+ base="$(dirname "$tags")"
+fi
+
+if $raw
+then
+ add "$@"
+else
+ rebase | add "$@"
+fi