Skip to content

Commit ea6a234

Browse files
author
lu0
committed
Fix shell convention warnings and explain closure of #3
1 parent f99daec commit ea6a234

File tree

4 files changed

+33
-15
lines changed

4 files changed

+33
-15
lines changed

git-worktree-wrapper.sh

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
# shellcheck disable=SC2164
1616
set -uo pipefail
1717

18-
18+
# git command overrider,
19+
# selects override function according to the command passed to git (`$1`).
20+
# Commands supported: `checkout` and `branch`
1921
gww::override_command() {
2022
case "${1:-}" in
2123
checkout) checkout::override_commands "${@:-}" ;;
@@ -24,7 +26,8 @@ gww::override_command() {
2426
esac
2527
}
2628

27-
29+
# Detects if the repository is a bare repository and triggers
30+
# the command overrider, else runs vanilla git.
2831
gww::main() {
2932
local is_bare_repo
3033

@@ -39,6 +42,7 @@ gww::main() {
3942
fi
4043
}
4144

45+
# Checks and initializes script's libraries and environment
4246
gww::init() {
4347
local script_real_dir
4448

@@ -49,12 +53,18 @@ gww::init() {
4953
exit 1
5054
fi
5155

52-
# Load libraries
5356
# shellcheck source=libs/_init_libs.sh
5457
source "${script_real_dir}/libs/_init_libs.sh"
5558
}
5659

5760

61+
# This script must be sourced to be able "cd" within the current shell,
62+
# then we can't just use `set -e` on pipefails: this would close the shell.
63+
# Here I emulate `set -e` by wrapping the script's execution in a while loop and
64+
# forcing a `break` after an error is trapped.
65+
# This is why we can ignore shellcheck's SC2164 code, since "cd'ing" into an
66+
# inexisting directory returns an error that will be trapped and then
67+
# will abort the script, keeping the current shell.
5868
trap 'break' ERR;
5969

6070
while true; do
@@ -63,5 +73,6 @@ while true; do
6373
break
6474
done
6575

76+
# Revert traps and shell options
6677
trap - ERR
6778
set +u +o pipefail

libs/_branch.sh

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
#!/usr/bin/env bash
2+
# shellcheck disable=SC2164
3+
14
#
25
# Functions to wrap git's branch command in bare repositories
36
#
47

5-
68
# Override branch commands. Triggered by:
79
# > git branch ...
810
branch::override_commands() {
@@ -22,16 +24,15 @@ branch::override_commands() {
2224
# Delete worktree then branch
2325
branch::_delete_worktree_and_branch() {
2426
local opt="${2:-}" branch="${3:-}"
25-
local to_dir cd_to_bare_dir worktree_abs_dir
26-
local bare_dir
27+
local cd_to_bare_dir=false
28+
local bare_dir to_dir worktree_abs_dir
2729

2830
bare_dir="$(utils::get_bare_dir)"
2931
to_dir=$(utils::branch_to_dir_name "${branch}")
30-
cd_to_bare_dir=false
3132

3233
worktree_abs_dir="${bare_dir%%/}/${to_dir}"
3334

34-
case "${2}" in
35+
case "${opt}" in
3536
-d)
3637
# Soft remove
3738
utils::git worktree remove "${worktree_abs_dir}" 2> /dev/null \

libs/_checkout.sh

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
#
1+
#!/usr/bin/env bash
2+
# shellcheck disable=SC2164
3+
4+
#
25
# Functions to wrap git's checkout command in bare repositories
36
#
47
# Uses globals:
@@ -46,7 +49,7 @@ checkout::override_commands() {
4649
checkout::_create_branch_and_worktree() {
4750

4851
local opt="${2:-}" to="${3:-}" from="${4:-}"
49-
local to_dir existing_branch existing_worktree
52+
local to_dir existing_branch
5053

5154
if [ -z "${from:-}" ]; then
5255
# Use current branch as reference
@@ -116,14 +119,13 @@ checkout::_switch_to_branch_and_worktree() {
116119
# an empty string if the creation was not successful.
117120
checkout::__create_worktree_from_branch() {
118121
local to_dir
119-
local existing_worktree
120122
local bare_dir
121123

122124
bare_dir=$(utils::get_bare_dir)
123125
to_dir=$(utils::branch_to_dir_name "${to}")
124126

125127
# Create worktree if does not exist
126-
if [ -z $(utils::get_branch_path "${to}") ]; then
128+
if [ -z "$(utils::get_branch_path "${to}")" ]; then
127129
# Created in the directory of the bare repository
128130
cd "${bare_dir}"
129131

libs/_utils.sh

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#!/usr/bin/env bash
2+
# shellcheck disable=SC2164
3+
14
#
25
# Miscellaneous utilities
36
#
@@ -37,15 +40,16 @@ utils::get_bare_dir() {
3740

3841

3942
utils::__store_last_checkedout() {
43+
local branch worktree_path info
4044
branch=$(git branch --show-current)
41-
worktree_path=$(utils::get_branch_path ${branch})
45+
worktree_path=$(utils::get_branch_path "${branch}")
4246
info="${worktree_path}\t[${branch}]"
43-
echo -e "${info}" > $(utils::get_bare_dir)/.lastcheckedout
47+
echo -e "${info}" > "$(utils::get_bare_dir)"/.lastcheckedout
4448
}
4549

4650

4751
# Open the current worktree in a enabled and set editor and
48-
# store a reference to the worktree directory in the repository's roots
52+
# store a reference to the worktree directory in the repository's root
4953
utils::open_editor_and_store_reference() {
5054
if [[ "${EDITOR:-}" ]] && [[ "${DISABLE_GIT_WORKTREE_EDITOR:-}" != "1" ]]; then
5155
utils::info "Opening in editor: ${EDITOR:-}"

0 commit comments

Comments
 (0)