xaizek / xscripts (License: Apache-2.0) (since 2020-06-28)
Small collection of some scripts I find useful.
<root> / gt-do (cae3104b155752c4fdd93b6b10ea401b2820bcb3) (3,088B) (mode 100755) [raw]
#!/bin/bash
# Copyright 2020-2021 xaizek <xaizek@posteo.net>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# This file last updated: 12 March, 2021

# The script dispatches repository-specific commands that are stored under
# `.git/actions`.
#
# Usage:
# * gt-do
#   List commands of current repository.
# * gt-do -h|--help
#   List options and commands of current repository.
# * gt-do -e|--edit action
#   Open specified command in the editor for editing.  If your editor can create
#   non-existing directories and mark scripts executable on saving, this command
#   is also enough to effortlessly create new actions.
# * gt-do action [args...]
#   Run specified command named action with the optional list of arguments.

# Related post: https://reversed.top/2020-06-29/repository-specific-commands/

actions_dir="$(git rev-parse --absolute-git-dir 2>/dev/null)/actions/"
if [ $? -ne 0 ]; then
    echo "Git repository isn't found"
    exit 1
fi

export GTDO_WORKTREE_ROOT=$(git rev-parse --show-toplevel 2>/dev/null)
if [ $? -ne 0 ]; then
    echo "Can't find worktree"
    exit 2
fi

export GTDO_CWD=$PWD
cd "$GTDO_WORKTREE_ROOT"

actions=
if [ -d "$actions_dir" ]; then
    actions=$(find "$actions_dir" -executable \
                                  -xtype f \
                                  -maxdepth 1 \
                                  -exec basename {} \; | sort)
fi

if [ $# -eq 0 ]; then
    if [ -z "$actions" ]; then
        echo "No actions in current repository ($GTDO_WORKTREE_ROOT)"
    else
        echo "Actions of current repository ($GTDO_WORKTREE_ROOT):"
        echo "$actions" | sed 's/^/ * /'
    fi
    exit 0
fi

if [ $# -eq 1 ] && ( [ "$1" = "-h" ] || [ "$1" = "--help" ] ); then
    echo "Usage: $(basename $0) [-h|--help]"
    echo "Usage: $(basename $0) -e|--edit action"
    echo "Usage: $(basename $0) action [args...]"
    echo
    echo "Actions of current repository ($GTDO_WORKTREE_ROOT):"
    echo "$actions" | sed 's/^/ * /'
    exit 0
fi

if [ $# -eq 2 ] && ( [ "$1" = "-e" ] || [ "$1" = "--edit" ] ); then
    if [ "${2:0:1}" = "-" ]; then
        echo 1>&2 "Action name can't start with a dash: $2"
        exit 3
    fi

    actionfile="$actions_dir/$2"
    exec ${EDITOR:-vim} "$actionfile"
fi

if [ "${1:0:1}" = "-" ]; then
    echo 1>&2 "Unrecognized option or invocation form: $1"
    exit 4
fi

action=$1
shift

actionfile="$actions_dir/$action"
if [ ! -f "$actionfile" ]; then
    echo Not an action: "$action"
    exit 5
fi
if [ ! -x "$actionfile" ]; then
    echo Disabled action: "$action"
    exit 6
fi

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