-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgit-worktree-wrapper.sh
executable file
·78 lines (63 loc) · 2.11 KB
/
git-worktree-wrapper.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env bash
#
# git-worktree-wrapper
#
# Wrapper script to easily execute common git-worktree commands.
# This script overrides git's checkout/branch commands
# when working within bare repositories (worktrees).
#
# Quiet built-in checkouts are made after each overridden checkout
# to trigger post-checkout hooks.
#
# shellcheck disable=SC2164
set -uo pipefail
# git command overrider,
# selects override function according to the command passed to git (`$1`).
# Commands supported: `checkout` and `branch`
gww::override_command() {
case "${1:-}" in
checkout) checkout::override_commands "${@:-}" ;;
branch) branch::override_commands "${@:-}" ;;
*) utils::git "${@:-}" ;;
esac
}
# Detects if the repository is a bare repository and triggers
# the command overrider, else runs vanilla git.
gww::main() {
local is_bare_repo
is_bare_repo=$(
utils::git config --local --get core.bare 2> /dev/null | grep -i true | wc -w
)
if [[ "${is_bare_repo}" == 1 ]]; then
gww::override_command "${@:-}"
else
utils::git "${@:-}"
fi
}
# Checks and initializes script's libraries and environment
gww::init() {
local script_real_dir
script_real_dir="$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")"
if [ "$0" == "${BASH_SOURCE[0]}" ]; then
echo "git-worktree-wrapper must be sourced instead of executed."
exit 1
fi
# shellcheck source=libs/_init_libs.sh
source "${script_real_dir}/libs/_init_libs.sh"
}
# This script must be sourced to be able "cd" within the current shell,
# then we can't just use `set -e` on pipefails: this would close the shell.
# Here I emulate `set -e` by wrapping the script's execution in a while loop and
# forcing a `break` after an error is trapped.
# This is why we can ignore shellcheck's SC2164 code, since "cd'ing" into an
# inexisting directory returns an error that will be trapped and then
# will abort the script, keeping the current shell.
trap 'break' ERR;
while true; do
gww::init
gww::main "${@:-}"
break
done
# Revert traps and shell options
trap - ERR
set +u +o pipefail