xaizek / xscripts (License: Apache-2.0) (since 2020-06-28)
Small collection of some scripts I find useful.
Commit e234be9f38c8545b0ce24100183901493adc9077

Add gt-do script
Author: xaizek
Author date (UTC): 2020-06-28 21:10
Committer name: xaizek
Committer date (UTC): 2020-06-28 21:39
Parent(s): bf1a6f91139be7c164c150975c70f0d9d58f48b2
Signing key: 99DC5E4DB05F6BE2
Tree: 9578f8b8e9a7e513f5f87a187da54a7529f5c8c0
File Lines added Lines deleted
README.md 1 0
gt-do 104 0
File README.md changed (mode: 100644) (index 71782a4..171ecac)
... ... Look at the top comments inside scripts.
22 22 | envis-diff | Improved highlighting of whitespace in diff | envis-diff | Improved highlighting of whitespace in diff
23 23 | gifrec | Handy script to record interaction as a GIF | gifrec | Handy script to record interaction as a GIF
24 24 | gt-clean-merged-branches | Interactive deletion of old branches in a Git repo | gt-clean-merged-branches | Interactive deletion of old branches in a Git repo
25 | gt-do | Dispatcher of repository-specific commands
25 26
26 27 ### License ### License
27 28
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" "$@"
Hints

Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://code.reversed.top/user/xaizek/xscripts

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@code.reversed.top/user/xaizek/xscripts

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a pull request:
... clone the repository ...
... make some changes and some commits ...
git push origin master