Skip to content

Commit

Permalink
tg-migrate-bases.sh: become POSIX compliant
Browse files Browse the repository at this point in the history
None of the POSIX specifications for the find utility allow the
`-mindepth` or `-maxdepth` primaries.

However the most recent POSIX specification allows the `-path`
primary (which has generally been supported everywhere that the
`-mindepth` and `-maxdepth` primaries were).

Replace use of `-mindepth` and `-maxdepth` with a substitute that
uses `-path` instead.  It should be almost as efficient.

Since `tg migrate-bases` is not expected to be used very often,
if ever, the minuscule performance hit (an extra fork or maybe two)
won't really matter and in return only POSIX-specified primaries
are used with the find utility.

Signed-off-by: Kyle J. McKay <[email protected]>
  • Loading branch information
mackyle committed Aug 24, 2021
1 parent 6575085 commit 125c162
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions tg-migrate-bases.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/bin/sh

# tg--migrate-bases -- migrate from old top-bases to new {top-bases}
# Copyright (C) 2017 Kyle J. McKay
# All rights reserved.
# Copyright (C) 2017,2021 Kyle J. McKay
# All rights reserved
# License GPLv2+

USAGE="\
Expand Down Expand Up @@ -258,12 +258,21 @@ else
headdir="$(cd "$git_dir" && pwd -P)"
fi

posix_find_worktrees_HEAD() (
# neither -mindepth nor -maxdepth are POSIX, grrrr
# however, POSIX has adopted -path
# $1 is the starting directory and the output lines,
# if any, will all start with "./"
cd "$1" &&
exec find . -path './HEAD' -prune -o -path '*/*/*/*' -prune -o -name HEAD -type f -print
) 2>/dev/null

# note that [ -n "$iowopt" ] will be true if linked worktrees are available
maybehaslw=
if
[ -n "$iowopt" ] &&
[ -d "$maindir/worktrees" ] &&
[ $(( $(find "$maindir/worktrees" -mindepth 2 -maxdepth 2 -type f -name HEAD -print 2>/dev/null | wc -l) )) -gt 0 ]
[ $(( $(posix_find_worktrees_HEAD "$maindir/worktrees" | wc -l) )) -gt 0 ]
then
maybehaslw=1
fi
Expand Down Expand Up @@ -304,9 +313,13 @@ process_one_symref "$headdir"
process_one_symref "$maindir"
# then each worktree (again duplicates will be automatically ignored)
if [ -n "$maybehaslw" ]; then
while read -r wktree && [ -n "$wktree" ]; do
process_one_symref "${wktree%/HEAD}"
while
read -r wktree &&
wktree="${wktree#./}" &&
[ -n "$wktree" ]
do
process_one_symref "$maindir/worktrees/${wktree%/HEAD}"
done <<-EOT
$(find "$maindir/worktrees" -mindepth 2 -maxdepth 2 -type f -name HEAD -print 2>/dev/null)
$(posix_find_worktrees_HEAD "$maindir/worktrees")
EOT
fi

0 comments on commit 125c162

Please sign in to comment.