Skip to content

Commit 3ea6bc3

Browse files
fix: resolve bash escaping issues in close-issue command (#1425)
* fix: resolve bash escaping issues in close-issue command Fix double-escaping problem where command substitutions and character classes were being incorrectly escaped when executed through the dotfiles-commands plugin pipeline. Changes: - Extract worktree setup logic to external script - Replace problematic tr '[:upper:]' with awk/sed in dry-run - Avoid nested command substitutions in markdown bash blocks Root cause: Plugin execution pipeline was double-escaping special characters ($, [, ]), breaking bash syntax. External script bypasses this issue. Fixes #[issue-number-if-exists] * Update scripts/setup-issue-worktree.sh Co-authored-by: amazon-q-developer[bot] <208079219+amazon-q-developer[bot]@users.noreply.github.com> * Update scripts/setup-issue-worktree.sh Co-authored-by: amazon-q-developer[bot] <208079219+amazon-q-developer[bot]@users.noreply.github.com> * Update commands/close-issue.md Co-authored-by: amazon-q-developer[bot] <208079219+amazon-q-developer[bot]@users.noreply.github.com> * Update scripts/setup-issue-worktree.sh Co-authored-by: amazon-q-developer[bot] <208079219+amazon-q-developer[bot]@users.noreply.github.com> * Update commands/close-issue.md Co-authored-by: amazon-q-developer[bot] <208079219+amazon-q-developer[bot]@users.noreply.github.com> --------- Co-authored-by: amazon-q-developer[bot] <208079219+amazon-q-developer[bot]@users.noreply.github.com>
1 parent bef2816 commit 3ea6bc3

2 files changed

Lines changed: 86 additions & 17 deletions

File tree

commands/close-issue.md

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,9 @@ Fetch issue context from GitHub (safe read-only operation, runs in both normal a
212212
If dry-run mode is active, show the execution plan and exit:
213213

214214
!if [ "$DRY_RUN" = "true" ]; then
215-
! # Calculate planned values
216-
! ISSUE_SLUG=$(echo "$ISSUE_TITLE" | tr '[:upper:]' '[:lower:]' | tr ' ' '-' | tr -cd '[:alnum:]-' | cut -c1-50)
215+
! # Calculate planned values using simpler bash to avoid escaping issues
216+
! # Convert to lowercase and slugify
217+
! ISSUE_SLUG=$(echo "$ISSUE_TITLE" | awk '{print tolower($0)}' | sed 's/[^a-z0-9]/-/g' | sed 's/--*/-/g' | cut -c1-50)
217218
! # Ensure slug is not empty and has minimum length
218219
! if [ -z "$ISSUE_SLUG" ] || [ ${#ISSUE_SLUG} -lt 3 ]; then
219220
! ISSUE_SLUG="issue-implementation"
@@ -317,25 +318,41 @@ If dry-run mode is active, show the execution plan and exit:
317318

318319
Create isolated worktree or work in main repo based on configuration:
319320

320-
!# Calculate branch details
321-
!ISSUE_SLUG=$(echo "$ISSUE_TITLE" | tr '[:upper:]' '[:lower:]' | tr ' ' '-' | tr -cd '[:alnum:]-' | cut -c1-50)
322-
!if [ -z "$ISSUE_SLUG" ] || [ ${#ISSUE_SLUG} -lt 3 ]; then
323-
! ISSUE_SLUG="issue-implementation"
321+
!# Use external script to avoid bash escaping issues in command execution
322+
!SCRIPT_PATH="${DOTFILES_ROOT:-.}/scripts/setup-issue-worktree.sh"
323+
!if [ ! -f "$SCRIPT_PATH" ]; then
324+
! echo "Error: setup-issue-worktree.sh not found at $SCRIPT_PATH"
325+
! exit 1
324326
!fi
325-
!BRANCH_NAME="issue-${ISSUE_NUMBER}-${ISSUE_SLUG}"
326327
!
327-
!if [ "$USE_WORKTREE" = "true" ]; then
328-
! # Create isolated worktree (recommended for safety)
329-
! WORKTREE_PATH="$CONFIG_WORKTREE_BASE/issue-${ISSUE_NUMBER}"
330-
! echo "Creating worktree at: $WORKTREE_PATH"
331-
! git worktree add "$WORKTREE_PATH" -b "$BRANCH_NAME"
328+
!# Run the setup script
329+
# Run the setup script
330+
if ! SETUP_OUTPUT=$(bash "$SCRIPT_PATH" "$ISSUE_NUMBER" "$ISSUE_TITLE" "$CONFIG_WORKTREE_BASE" "$USE_WORKTREE"); then
331+
echo "Error: Failed to execute setup-issue-worktree.sh"
332+
exit 1
333+
fi
334+
echo "$SETUP_OUTPUT"
335+
!
336+
!# Extract variables from script output
337+
# Extract variables from script output
338+
WORKTREE_PATH=$(echo "$SETUP_OUTPUT" | grep "^WORKTREE_PATH=" | cut -d= -f2)
339+
BRANCH_NAME=$(echo "$SETUP_OUTPUT" | grep "^BRANCH_NAME=" | cut -d= -f2)
340+
341+
# Validate that required variables were extracted
342+
if [ -z "$BRANCH_NAME" ]; then
343+
echo "Error: Failed to extract BRANCH_NAME from setup script output"
344+
exit 1
345+
fi
346+
347+
if [ "$USE_WORKTREE" = "true" ] && [ -z "$WORKTREE_PATH" ]; then
348+
echo "Error: Failed to extract WORKTREE_PATH from setup script output"
349+
exit 1
350+
fi
351+
!
352+
!# Change to worktree if created
353+
!if [ "$USE_WORKTREE" = "true" ] && [ -n "$WORKTREE_PATH" ]; then
332354
! cd "$WORKTREE_PATH"
333355
! echo "Working in worktree: $WORKTREE_PATH"
334-
!else
335-
! # Work in main repo (faster, but less isolated)
336-
! echo "Working in main repo (no worktree)"
337-
! git checkout -b "$BRANCH_NAME"
338-
! echo "Created branch: $BRANCH_NAME"
339356
!fi
340357

341358
## Implementation

scripts/setup-issue-worktree.sh

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/usr/bin/env bash
2+
# Helper script to avoid bash escaping issues in close-issue command
3+
# Usage: setup-issue-worktree.sh <issue_number> <issue_title> <worktree_base> <use_worktree>
4+
5+
set -euo pipefail
6+
7+
# Validate required arguments
8+
if [ $# -lt 3 ]; then
9+
echo "Error: Missing required arguments" >&2
10+
echo "Usage: setup-issue-worktree.sh <issue_number> <issue_title> <worktree_base> [use_worktree]" >&2
11+
exit 1
12+
fi
13+
14+
ISSUE_NUMBER="$1"
15+
ISSUE_TITLE="$2"
16+
WORKTREE_BASE="$3"
17+
USE_WORKTREE="${4:-true}"
18+
19+
# Validate issue number is numeric
20+
if ! [[ "$ISSUE_NUMBER" =~ ^[0-9]+$ ]]; then
21+
echo "Error: Issue number must be numeric, got: $ISSUE_NUMBER" >&2
22+
exit 1
23+
fi
24+
25+
# Convert title to slug (avoiding escaping issues in markdown bash blocks)
26+
ISSUE_SLUG=$(echo "$ISSUE_TITLE" | tr '[:upper:]' '[:lower:]' | tr ' ' '-' | tr -cd '[:alnum:]-' | cut -c1-50)
27+
if [ -z "$ISSUE_SLUG" ] || [ ${#ISSUE_SLUG} -lt 3 ]; then
28+
ISSUE_SLUG="issue-implementation"
29+
fi
30+
31+
BRANCH_NAME="issue-${ISSUE_NUMBER}-${ISSUE_SLUG}"
32+
33+
if [ "$USE_WORKTREE" = "true" ]; then
34+
WORKTREE_PATH="$WORKTREE_BASE/issue-${ISSUE_NUMBER}"
35+
echo "Creating worktree at: $WORKTREE_PATH"
36+
if ! git worktree add "$WORKTREE_PATH" -b "$BRANCH_NAME" 2>/dev/null; then
37+
echo "Error: Failed to create worktree. This could be due to:" >&2
38+
echo " - Branch '$BRANCH_NAME' already exists" >&2
39+
echo " - Path '$WORKTREE_PATH' is already in use" >&2
40+
echo " - Insufficient permissions" >&2
41+
exit 1
42+
fi
43+
echo "WORKTREE_PATH=$WORKTREE_PATH"
44+
else
45+
echo "Working in main repo (no worktree)"
46+
if ! git checkout -b "$BRANCH_NAME" 2>/dev/null; then
47+
echo "Error: Failed to create branch '$BRANCH_NAME'. Branch may already exist." >&2
48+
exit 1
49+
fi
50+
fi
51+
52+
echo "BRANCH_NAME=$BRANCH_NAME"

0 commit comments

Comments
 (0)