Repository navigation
Expand file tree
/
Copy pathcopy-worktreeinclude.sh
More file actions
109 lines (93 loc) · 3.28 KB
/
Copy pathcopy-worktreeinclude.sh
File metadata and controls
109 lines (93 loc) · 3.28 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/env bash
set -uo pipefail
export PATH="/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin:${PATH:-}"
fail() {
echo "error: $*" >&2
if [ -n "${HERDR_BIN_PATH:-}" ]; then
"$HERDR_BIN_PATH" notification show "worktreeinclude-local" --body "$*" >/dev/null 2>&1 || true
fi
exit 1
}
if [ $# -eq 2 ]; then
source_root=$1
target=$2
else
command -v jq >/dev/null || fail "jq is required"
event_json=${HERDR_PLUGIN_EVENT_JSON:-}
[ -n "$event_json" ] || fail "HERDR_PLUGIN_EVENT_JSON is empty"
source_root=$(jq -r '.data.workspace.worktree.repo_root // empty' <<<"$event_json")
target=$(jq -r '.data.worktree.path // empty' <<<"$event_json")
fi
[ -d "$source_root" ] && [ -d "$target" ] || fail "not a directory: '$source_root' or '$target'"
source_root=$(cd "$source_root" && pwd -P)
target=$(cd "$target" && pwd -P)
tmp=$(mktemp -d) || fail "mktemp failed"
trap 'rm -rf "$tmp"' EXIT
patterns=$tmp/patterns
for include_file in .worktreeinclude .worktreeinclude.local; do
if [ -f "$source_root/$include_file" ]; then
cat "$source_root/$include_file"
echo
fi
done >"$patterns"
if [ ! -s "$patterns" ]; then
echo "no .worktreeinclude or .worktreeinclude.local in $source_root"
exit 0
fi
case $(uname -s) in
Darwin) cp_cmd=(cp -cRPp) ;;
*) cp_cmd=(cp -RPp --reflink=auto) ;;
esac
"${cp_cmd[@]}" "$patterns" "$tmp/probe" 2>/dev/null || cp_cmd=(cp -RPp)
other_worktrees=$(git -C "$source_root" worktree list --porcelain | sed -n 's/^worktree //p' | grep -vFx "$source_root")
overlaps_other_worktree() {
local path=$source_root/$1 worktree
while IFS= read -r worktree; do
[ -n "$worktree" ] || continue
case "$worktree/" in "$path"/*) return 0 ;; esac
case "$path/" in "$worktree"/*) return 0 ;; esac
done <<<"$other_worktrees"
return 1
}
list_included_and_gitignored() {
git -C "$source_root" ls-files -z --others --ignored --exclude-from="$patterns" "$@" |
git -C "$source_root" check-ignore -z --stdin
}
copied=0
skipped=0
failed=0
copy_path() {
local relative=${1%/}
local destination=$target/$relative
if overlaps_other_worktree "$relative"; then
echo "skipped (worktree): $relative"
skipped=$((skipped + 1))
elif [ -e "$destination" ] || [ -L "$destination" ]; then
echo "skipped (exists): $relative"
skipped=$((skipped + 1))
elif mkdir -p "$(dirname "$destination")" && "${cp_cmd[@]}" "$source_root/$relative" "$destination"; then
echo "copied: $relative"
copied=$((copied + 1))
else
echo "failed: $relative" >&2
failed=$((failed + 1))
fi
}
# ponytail: a wholly ignored directory is copied as one unit, so a !negation
# inside it has no effect; expand such directories per file if that matters.
files_pathspec=(.)
last_directory=
while IFS= read -r -d '' entry; do
case $entry in */) ;; *) continue ;; esac
if [ -n "$last_directory" ]; then
case $entry in "$last_directory"*) continue ;; esac
fi
last_directory=$entry
files_pathspec+=(":(exclude,literal)$entry")
copy_path "$entry"
done < <(list_included_and_gitignored --directory)
while IFS= read -r -d '' entry; do
copy_path "$entry"
done < <(list_included_and_gitignored -- "${files_pathspec[@]}")
echo "done: copied $copied, skipped $skipped, failed $failed ($source_root -> $target)"
[ "$failed" -eq 0 ] || fail "$failed path(s) failed to copy into $target"