Skip to content

Commit fb58ae9

Browse files
fix: resolve unbound MAIN_REPO/PRE_MERGE_SHA on --resume and remove dead archive phase (merge worktree-20260323-193735)
2 parents 05c28a9 + 41b9034 commit fb58ae9

File tree

2 files changed

+19
-98
lines changed

2 files changed

+19
-98
lines changed

plugins/dso/scripts/merge-to-main.sh

Lines changed: 17 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -47,24 +47,6 @@ WORKTREE_DIR="$REPO_ROOT"
4747
_SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
4848
: "${CLAUDE_PLUGIN_ROOT:?CLAUDE_PLUGIN_ROOT must be set}"
4949

50-
# --- Count closed tickets using a single awk pass over all .md files ---
51-
# Usage: _count_closed_tickets <tickets_dir>
52-
# Returns: integer count of .md files whose YAML front-matter contains "status: closed"
53-
_count_closed_tickets() {
54-
local dir="$1"
55-
local _result
56-
_result=$(find "$dir" -maxdepth 1 -name "*.md" -type f -print0 \
57-
| xargs -0 awk '
58-
FILENAME != _prev {
59-
if (_prev != "" && _found) count++
60-
_prev=FILENAME; _found=0; _n=0
61-
}
62-
/^---$/ { _n++; if(_n==2) nextfile }
63-
_n==1 && /^status:[[:space:]]*closed/ { _found=1 }
64-
END { if (_prev != "" && _found) count++; print count+0 }
65-
' 2>/dev/null)
66-
echo "${_result:-0}"
67-
}
6850

6951
# --- State file helpers (resumable merge support) ---
7052

@@ -917,6 +899,22 @@ fi
917899
_state_init
918900
trap '_sigurg_handler' URG
919901

902+
# --- Resolve MAIN_REPO early so all phases can use it (including --resume) ---
903+
# MAIN_REPO is the path to the main (non-worktree) checkout. Phases like
904+
# validate and ci_trigger need it, but it was previously set only inside
905+
# _phase_sync — causing unbound variable errors on --resume.
906+
MAIN_REPO=$(dirname "$(git rev-parse --git-common-dir)")
907+
if [ -z "$MAIN_REPO" ]; then
908+
echo "ERROR: Could not determine main repo path."
909+
exit 1
910+
fi
911+
912+
# PRE_MERGE_SHA: set to current HEAD as a safe default. _phase_merge overwrites
913+
# it with the actual pre-merge SHA before merging. On --resume (merge already
914+
# done), this default is stale but _phase_ci_trigger handles empty/stale values
915+
# gracefully via git diff error suppression (2>/dev/null).
916+
PRE_MERGE_SHA=$(git -C "$MAIN_REPO" rev-parse HEAD 2>/dev/null || echo "")
917+
920918
# =============================================================================
921919
# Phase functions — each wraps a sequential phase with state recording
922920
# =============================================================================
@@ -1230,38 +1228,10 @@ _phase_push() {
12301228
fi
12311229
}
12321230

1233-
# --- 4.5) Archive closed tickets if count exceeds threshold ---
1231+
# --- 4.5) Archive phase (no-op — archive-closed-tickets.sh removed in v3 cleanup) ---
12341232
_phase_archive() {
12351233
_CURRENT_PHASE="archive"
12361234
_state_write_phase "archive"
1237-
1238-
_ARCHIVE_SCRIPT="$_SCRIPT_DIR/archive-closed-tickets.sh"
1239-
if [ ! -f "$_ARCHIVE_SCRIPT" ]; then
1240-
_ARCHIVE_SCRIPT="$MAIN_REPO/scripts/archive-closed-tickets.sh"
1241-
fi
1242-
if [ -f "$_ARCHIVE_SCRIPT" ]; then
1243-
_CLOSED_COUNT=$(_count_closed_tickets "$TICKETS_DIR")
1244-
if [ "$_CLOSED_COUNT" -gt 100 ]; then
1245-
echo "Archiving $_CLOSED_COUNT closed ticket(s)..."
1246-
_ARCHIVE_OUT=$(TICKETS_DIR="$TICKETS_DIR" bash "$_ARCHIVE_SCRIPT" 2>&1)
1247-
echo "$_ARCHIVE_OUT"
1248-
# Commit archived tickets if any were moved
1249-
if echo "$_ARCHIVE_OUT" | grep -qE '^Archived [1-9]'; then
1250-
git add "$TICKETS_DIR"/archive/ 2>/dev/null || true
1251-
git add -u "$TICKETS_DIR"/ 2>/dev/null || true
1252-
if ! git diff --cached --quiet 2>/dev/null; then
1253-
git commit -m "chore: archive closed tickets [skip ci]" --quiet
1254-
git push --quiet 2>&1 || echo "WARNING: Push of archive commit failed — retry with git push."
1255-
echo "OK: Archived tickets committed and pushed."
1256-
fi
1257-
fi
1258-
else
1259-
echo "INFO: $_CLOSED_COUNT closed ticket(s) — below threshold (100), skipping archive."
1260-
fi
1261-
else
1262-
echo "INFO: archive-closed-tickets.sh not found — skipping archive step."
1263-
fi
1264-
12651235
_state_mark_complete "archive"
12661236
}
12671237

tests/scripts/test-merge-to-main.sh

Lines changed: 2 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -146,59 +146,10 @@ assert_ne "test_parallel_validation_fmt_exit_code_captured" "0" "$_FMT_RC"
146146
assert_ne "test_parallel_validation_lint_exit_code_captured" "0" "$_LINT_RC"
147147

148148
# =============================================================================
149-
# Test: _count_closed_tickets() function exists in merge-to-main.sh
149+
# Test: _count_closed_tickets() removed (v2 cleanup — v3 uses ticket list)
150150
# =============================================================================
151151
HAS_COUNT_FUNCTION=$(grep -c '_count_closed_tickets' "$MERGE_SCRIPT" || true)
152-
assert_ne "test_count_closed_tickets_function_exists" "0" "$HAS_COUNT_FUNCTION"
153-
154-
# =============================================================================
155-
# Test: _count_closed_tickets() uses single awk pass (not -exec awk per file)
156-
# =============================================================================
157-
HAS_EXEC_AWK=$(grep -c '\-exec awk' "$MERGE_SCRIPT" || true)
158-
assert_eq "test_count_closed_tickets_no_exec_awk" "0" "$HAS_EXEC_AWK"
159-
160-
# =============================================================================
161-
# Test: _count_closed_tickets() returns correct count for temp dir fixture
162-
# Create a temp dir with 2 open + 3 closed tickets; assert count == 3
163-
# =============================================================================
164-
_TC_TMPDIR=$(mktemp -d)
165-
trap 'rm -rf "$_TC_TMPDIR"' EXIT
166-
167-
# Helper to write a minimal markdown ticket with given status
168-
_write_ticket() {
169-
local file="$1" status="$2"
170-
printf -- '---\nstatus: %s\ntitle: Ticket %s\n---\nBody text.\n' "$status" "$status" > "$file"
171-
}
172-
173-
_write_ticket "$_TC_TMPDIR/open1.md" "open"
174-
_write_ticket "$_TC_TMPDIR/open2.md" "open"
175-
_write_ticket "$_TC_TMPDIR/closed1.md" "closed"
176-
_write_ticket "$_TC_TMPDIR/closed2.md" "closed"
177-
_write_ticket "$_TC_TMPDIR/closed3.md" "closed"
178-
179-
# Source the function under test by extracting and eval-ing just the function
180-
# definition from merge-to-main.sh (avoid executing the full script).
181-
_FN_BODY=$(awk '/^_count_closed_tickets\(\)/{found=1} found{print; if(/^\}$/){exit}}' "$MERGE_SCRIPT")
182-
if [[ -z "$_FN_BODY" ]]; then
183-
# Function not yet defined — RED: mark both behavioural tests as failing
184-
assert_eq "test_count_closed_tickets_correct_count" "3" "FUNCTION_NOT_FOUND"
185-
assert_eq "test_count_closed_tickets_open_not_counted" "0" "FUNCTION_NOT_FOUND"
186-
else
187-
eval "$_FN_BODY"
188-
_GOT_COUNT=$(_count_closed_tickets "$_TC_TMPDIR")
189-
assert_eq "test_count_closed_tickets_correct_count" "3" "$_GOT_COUNT"
190-
191-
# Also verify open tickets are NOT counted (open-only dir)
192-
_OPEN_TMPDIR=$(mktemp -d)
193-
trap 'rm -rf "$_OPEN_TMPDIR"' EXIT
194-
_write_ticket "$_OPEN_TMPDIR/open1.md" "open"
195-
_write_ticket "$_OPEN_TMPDIR/open2.md" "open"
196-
_GOT_OPEN_COUNT=$(_count_closed_tickets "$_OPEN_TMPDIR")
197-
assert_eq "test_count_closed_tickets_open_not_counted" "0" "$_GOT_OPEN_COUNT"
198-
rm -rf "$_OPEN_TMPDIR"
199-
fi
200-
201-
rm -rf "$_TC_TMPDIR"
152+
assert_eq "test_count_closed_tickets_removed" "0" "$HAS_COUNT_FUNCTION"
202153

203154
# =============================================================================
204155
# State file helper function tests

0 commit comments

Comments
 (0)