File gt-do added (mode: 100755) (index 0000000..fec71cc) |
|
1 |
|
#!/bin/bash |
|
2 |
|
# Copyright 2020 xaizek <xaizek@posteo.net> |
|
3 |
|
# |
|
4 |
|
# Licensed under the Apache License, Version 2.0 (the "License"); |
|
5 |
|
# you may not use this file except in compliance with the License. |
|
6 |
|
# You may obtain a copy of the License at |
|
7 |
|
# |
|
8 |
|
# http://www.apache.org/licenses/LICENSE-2.0 |
|
9 |
|
# |
|
10 |
|
# Unless required by applicable law or agreed to in writing, software |
|
11 |
|
# distributed under the License is distributed on an "AS IS" BASIS, |
|
12 |
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 |
|
# See the License for the specific language governing permissions and |
|
14 |
|
# limitations under the License. |
|
15 |
|
|
|
16 |
|
# The script dispatches repository-specific commands that are stored under |
|
17 |
|
# `.git/actions`. |
|
18 |
|
# |
|
19 |
|
# Usage: |
|
20 |
|
# * gt-do |
|
21 |
|
# List commands of current repository. |
|
22 |
|
# * gt-do -h|--help |
|
23 |
|
# List options and commands of current repository. |
|
24 |
|
# * gt-do -e|--edit action |
|
25 |
|
# Open specified command in the editor for editing. If your editor can create |
|
26 |
|
# non-existing directories and mark scripts executable on saving, this command |
|
27 |
|
# is also enough to effortlessly create new actions. |
|
28 |
|
# * gt-do action [args...] |
|
29 |
|
# Run specified command named action with the optional list of arguments. |
|
30 |
|
|
|
31 |
|
# Related post: https://reversed.top/2020-06-29/repository-specific-commands/ |
|
32 |
|
|
|
33 |
|
actions_dir="$(git rev-parse --absolute-git-dir 2>/dev/null)/actions/" |
|
34 |
|
if [ $? -ne 0 ]; then |
|
35 |
|
echo "Git repository isn't found" |
|
36 |
|
exit 1 |
|
37 |
|
fi |
|
38 |
|
|
|
39 |
|
export GTDO_WORKTREE_ROOT=$(git rev-parse --show-toplevel 2>/dev/null) |
|
40 |
|
if [ $? -ne 0 ]; then |
|
41 |
|
echo "Can't find worktree" |
|
42 |
|
exit 2 |
|
43 |
|
fi |
|
44 |
|
|
|
45 |
|
export GTDO_CWD=$PWD |
|
46 |
|
cd "$GTDO_WORKTREE_ROOT" |
|
47 |
|
|
|
48 |
|
actions= |
|
49 |
|
if [ -d "$actions_dir" ]; then |
|
50 |
|
actions=$(find "$actions_dir" -executable \ |
|
51 |
|
-xtype f \ |
|
52 |
|
-maxdepth 1 \ |
|
53 |
|
-exec basename {} \;) |
|
54 |
|
fi |
|
55 |
|
|
|
56 |
|
if [ $# -eq 0 ]; then |
|
57 |
|
if [ -z "$actions" ]; then |
|
58 |
|
echo No actions |
|
59 |
|
else |
|
60 |
|
echo "Actions of current repository:" |
|
61 |
|
echo "$actions" | sed 's/^/ * /' |
|
62 |
|
fi |
|
63 |
|
exit 0 |
|
64 |
|
fi |
|
65 |
|
|
|
66 |
|
if [ $# -eq 1 ] && ( [ "$1" = "-h" ] || [ "$1" = "--help" ] ); then |
|
67 |
|
echo "Usage: $(basename $0) [-h|--help]" |
|
68 |
|
echo "Usage: $(basename $0) -e|--edit action" |
|
69 |
|
echo "Usage: $(basename $0) action [args...]" |
|
70 |
|
echo |
|
71 |
|
echo "Actions of current repository:" |
|
72 |
|
echo "$actions" | sed 's/^/ * /' |
|
73 |
|
exit 0 |
|
74 |
|
fi |
|
75 |
|
|
|
76 |
|
if [ $# -eq 2 ] && ( [ "$1" = "-e" ] || [ "$1" = "--edit" ] ); then |
|
77 |
|
if [ "${2:0:1}" = "-" ]; then |
|
78 |
|
echo 1>&2 "Action name can't start with a dash: $2" |
|
79 |
|
exit 3 |
|
80 |
|
fi |
|
81 |
|
|
|
82 |
|
actionfile="$actions_dir/$2" |
|
83 |
|
exec $EDITOR "$actionfile" |
|
84 |
|
fi |
|
85 |
|
|
|
86 |
|
if [ "${1:0:1}" = "-" ]; then |
|
87 |
|
echo 1>&2 "Unrecognized option or invocation form: $1" |
|
88 |
|
exit 4 |
|
89 |
|
fi |
|
90 |
|
|
|
91 |
|
action=$1 |
|
92 |
|
shift |
|
93 |
|
|
|
94 |
|
actionfile="$actions_dir/$action" |
|
95 |
|
if [ ! -f "$actionfile" ]; then |
|
96 |
|
echo Not an action: "$action" |
|
97 |
|
exit 5 |
|
98 |
|
fi |
|
99 |
|
if [ ! -x "$actionfile" ]; then |
|
100 |
|
echo Disabled action: "$action" |
|
101 |
|
exit 6 |
|
102 |
|
fi |
|
103 |
|
|
|
104 |
|
exec "$actionfile" "$@" |