|
33 | 33 | # the opening `"` of a quoted path (which would leave a trailing `path"` in |
34 | 34 | # NCOMMAND). The pattern re-anchors on `git`, so multi-`-C` chains (e.g. |
35 | 35 | # `git -C /a -C /b push`) need a second pass to collapse the residual `-C`. |
36 | | -NCOMMAND=$(echo "$COMMAND" | sed -E 's/(^|\s|&&\s*)git[[:space:]]+-C[[:space:]]+"[^"]+"/\1git/g; s/(^|\s|&&\s*)git[[:space:]]+-C[[:space:]]+[^"[:space:]][^[:space:]]*/\1git/g') |
37 | | -NCOMMAND=$(echo "$NCOMMAND" | sed -E 's/(^|\s|&&\s*)git[[:space:]]+-C[[:space:]]+"[^"]+"/\1git/g; s/(^|\s|&&\s*)git[[:space:]]+-C[[:space:]]+[^"[:space:]][^[:space:]]*/\1git/g') |
| 36 | +NCOMMAND=$(echo "$COMMAND" | sed -E 's/(^|[[:space:]]|&&[[:space:]]*)git[[:space:]]+-C[[:space:]]+"[^"]+"/\1git/g; s/(^|[[:space:]]|&&[[:space:]]*)git[[:space:]]+-C[[:space:]]+[^"[:space:]][^[:space:]]*/\1git/g') |
| 37 | +NCOMMAND=$(echo "$NCOMMAND" | sed -E 's/(^|[[:space:]]|&&[[:space:]]*)git[[:space:]]+-C[[:space:]]+"[^"]+"/\1git/g; s/(^|[[:space:]]|&&[[:space:]]*)git[[:space:]]+-C[[:space:]]+[^"[:space:]][^[:space:]]*/\1git/g') |
38 | 38 |
|
39 | 39 | deny() { |
40 | 40 | local reason="$1" |
@@ -117,11 +117,16 @@ detect_work_dir() { |
117 | 117 | # `git -C` is the explicit git-level override and wins over any ambient cd prefix, |
118 | 118 | # so check it first (e.g. `cd /tmp && git -C /worktree push` targets /worktree). |
119 | 119 | # Greedy `.*-C` anchors on the LAST `-C` in the chosen segment. |
120 | | - if echo "$search_str" | grep -qE 'git\s+([^&|;]*\s)?-C\s+'; then |
121 | | - work_dir=$(echo "$search_str" | sed -nE 's/.*-C[[:space:]]+"([^"]+)".*/\1/p;t;s/.*-C[[:space:]]+([^[:space:]]+).*/\1/p') |
| 120 | + # Two separate sed invocations (quoted path first, then unquoted fallback) instead |
| 121 | + # of a single `;t;s` chain — BSD sed parses chained s/// after `t` as a label. |
| 122 | + if echo "$search_str" | grep -qE 'git[[:space:]]+([^&|;]*[[:space:]])?-C[[:space:]]+'; then |
| 123 | + work_dir=$(echo "$search_str" | sed -nE 's/.*-C[[:space:]]+"([^"]+)".*/\1/p') |
| 124 | + if [ -z "$work_dir" ]; then |
| 125 | + work_dir=$(echo "$search_str" | sed -nE 's/.*-C[[:space:]]+([^[:space:]]+).*/\1/p') |
| 126 | + fi |
122 | 127 | fi |
123 | | - if [ -z "$work_dir" ] && echo "$COMMAND" | grep -qE '^\s*cd\s+'; then |
124 | | - work_dir=$(echo "$COMMAND" | sed -nE 's/^\s*cd\s+"?([^"&]+)"?\s*&&.*/\1/p') |
| 128 | + if [ -z "$work_dir" ] && echo "$COMMAND" | grep -qE '^[[:space:]]*cd[[:space:]]+'; then |
| 129 | + work_dir=$(echo "$COMMAND" | sed -nE 's/^[[:space:]]*cd[[:space:]]+"?([^"&]+)"?[[:space:]]*&&.*/\1/p') |
125 | 130 | fi |
126 | 131 | # Trim trailing whitespace |
127 | 132 | work_dir="${work_dir%"${work_dir##*[![:space:]]}"}" |
|
0 commit comments