Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions bin/wt
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ worktree_base() {

list_worktrees() {
local target="${1:-}"
git worktree list --porcelain | awk -v target="$target" '
git worktree list --porcelain | TARGET="$target" awk '
BEGIN { target = ENVIRON["TARGET"] }
/^worktree / { path = substr($0, 10) }
/^branch / {
branch = substr($0, 8);
Expand Down Expand Up @@ -149,13 +150,7 @@ create_worktree() {

get_worktree_path() {
local branch="$1"
git worktree list --porcelain | awk -v b="$branch" '
/^worktree / { path = substr($0, 10) }
/^branch / {
br = substr($0, 8); sub("refs/heads/", "", br)
if (br == b) { print path; exit }
}
'
list_worktrees "$branch"
}

# Get or create a worktree for <branch>, print its path
Expand Down
19 changes: 19 additions & 0 deletions tests/awk_injection.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env bats

setup() {
source "${BATS_TEST_DIRNAME}/../bin/wt"
git() {
if [ "$1" = "worktree" ] && [ "$2" = "list" ]; then
printf 'worktree /path/to/wt\nHEAD abc123\nbranch refs/heads/branch\\name\n\n'
return 0
fi
command git "$@"
}
export -f git
}

@test "get_worktree_path with backslashes in branch name" {
run get_worktree_path 'branch\name'
[ "$status" -eq 0 ]
[ "$output" = "/path/to/wt" ]
}