Browse code

Add support for creating symlinks and directories

Robert Cranston authored on 14/04/2020 01:56:35
Showing 2 changed files

... ...
@@ -29,6 +29,11 @@ effort to accomodate these (specifically, it copies standard in to all the
29 29
 scripts if it is not a terminal, simply lets standard out/error through, and
30 30
 returns with the first error code that is non-zero).
31 31
 
32
+`git-multihook` can be run manually, in which case it parses `man githooks` and
33
+creates the symlinks and directories mentioned above (you probably want to
34
+place it in the [hooks path][] first). A list of hooks to skip can be given as
35
+arguments.
36
+
32 37
 [githooks]: https://git-scm.com/docs/githooks
33 38
 [hooks path]: https://git-scm.com/docs/git-config#Documentation/git-config.txt-corehooksPath
34 39
 [`$GIT_DIR`]: https://www.git-scm.com/docs/git#Documentation/git.txt-codeGITDIRcode
... ...
@@ -5,20 +5,45 @@ set -euC
5 5
 prog="$(basename "$0")"
6 6
 dir="$(dirname "$0")"
7 7
 
8
-# Delegate to executable files in `*.d` directory.
9
-if ! [ -t 1 ]
8
+# Were we run by git or manually?
9
+if [ "$prog" = "git-multihook" ]
10 10
 then
11
-    stdin="$(cat)"
12
-fi
13
-for file in "$dir/${prog}.d/"*
14
-do
15
-    if [ -x "$file" ]
16
-    then
17
-        if ! [ -t 1 ]
11
+    # Set up hook symlinks and `*.d` directories.
12
+    man githooks \
13
+    | awk '/^[^ ]/{x=$0=="HOOKS"};x&&/^   [^ ]/' \
14
+    | while read hook
15
+    do
16
+        if printf "%s\n" "$@" | grep -q '^'"$hook"'$'
17
+        then
18
+            continue
19
+        fi
20
+        if ! [ -e "$dir/$hook" ]
21
+        then
22
+            ln -s "$prog" "$dir/$hook"
23
+            printf "%s\n" "$dir/$hook"
24
+        fi
25
+        if ! [ -e "$dir/${hook}.d" ]
18 26
         then
19
-            printf "%s\n" "$stdin" | "$file" "$@" || exit "$?"
20
-        else
21
-            "$file" "$@" || exit "$?"
27
+            mkdir -p "$dir/${hook}.d"
28
+            printf "%s\n" "$dir/${hook}.d"
22 29
         fi
30
+    done
31
+else
32
+    # Delegate to executable files in `*.d` directory.
33
+    if ! [ -t 1 ]
34
+    then
35
+        stdin="$(cat)"
23 36
     fi
24
-done
37
+    for file in "$dir/${prog}.d/"*
38
+    do
39
+        if [ -x "$file" ]
40
+        then
41
+            if ! [ -t 1 ]
42
+            then
43
+                printf "%s\n" "$stdin" | "$file" "$@" || exit "$?"
44
+            else
45
+                "$file" "$@" || exit "$?"
46
+            fi
47
+        fi
48
+    done
49
+fi