Skip to content

Commit a749fdd

Browse files
fix: git-stack
on /st fork from top of stack
1 parent 1d6cf13 commit a749fdd

3 files changed

Lines changed: 45 additions & 15 deletions

File tree

bin/git-stack

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -119,17 +119,25 @@ else
119119
done < <(get_existing_suffixes)
120120

121121
if [[ "$SUFFIX" == "st" ]]; then
122-
# From 'st', find first available single letter
123-
TARGET_SUFFIX=""
124-
for c in {a..z}; do
125-
if ! suffix_exists "$c" "${existing_suffixes[@]+"${existing_suffixes[@]}"}"; then
126-
TARGET_SUFFIX="$c"
127-
break
128-
fi
129-
done
130-
if [[ -z "$TARGET_SUFFIX" ]]; then
131-
echo "Error: Stack limit reached - all single-letter branches a-z exist" >&2
132-
exit 1
122+
# From 'st', find the largest existing suffix and increment from there
123+
if [[ ${#existing_suffixes[@]} -eq 0 ]]; then
124+
TARGET_SUFFIX="a"
125+
else
126+
max_suffix=$(printf '%s\n' "${existing_suffixes[@]}" | sort | tail -n 1)
127+
TARGET_SUFFIX=$(increment_suffix "$max_suffix")
128+
129+
# Safety limit to prevent infinite loops
130+
iterations=0
131+
max_iterations=1000
132+
133+
while suffix_exists "$TARGET_SUFFIX" "${existing_suffixes[@]}"; do
134+
TARGET_SUFFIX=$(increment_suffix "$TARGET_SUFFIX")
135+
iterations=$((iterations + 1))
136+
if [[ $iterations -ge $max_iterations ]]; then
137+
echo "Error: Could not find available suffix after $max_iterations attempts" >&2
138+
exit 1
139+
fi
140+
done
133141
fi
134142
else
135143
# Compute next_major (first char of current suffix + 1)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
source "$(dirname "$0")/utils.sh"
4+
5+
test_auto_from_st_with_gaps() {
6+
# Earlier branches (a, b) may have been merged. Only c and d remain.
7+
# From st, should increment from the largest (d) → e, not fill the gap at a.
8+
create_branch "ns/task/st" --checkout
9+
create_branch "ns/task/c"
10+
create_branch "ns/task/d"
11+
12+
local output exit_code=0
13+
output=$("$GIT_STACK" 2>&1) || exit_code=$?
14+
15+
assert_exit_code 0 "$exit_code"
16+
assert_on_branch "ns/task/e"
17+
assert_contains "$output" "ns/task/e"
18+
}
19+
20+
run_test test_auto_from_st_with_gaps "from st, increments from largest existing suffix (skips gaps)"

tests/git-stack/test_stack_limit.sh

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ set -euo pipefail
33
source "$(dirname "$0")/utils.sh"
44

55
test_stack_limit() {
6-
# From st, with all single letters a-z taken → should error
6+
# From st, with all single letters a-z taken → max is z,
7+
# increment_suffix("z") = "za", so it creates za (no longer an error)
78
create_branch "ns/task/st" --checkout
89

910
for c in {a..z}; do
@@ -13,8 +14,9 @@ test_stack_limit() {
1314
local output exit_code=0
1415
output=$("$GIT_STACK" 2>&1) || exit_code=$?
1516

16-
assert_exit_code 1 "$exit_code"
17-
assert_contains "$output" "Stack limit reached"
17+
assert_exit_code 0 "$exit_code"
18+
assert_on_branch "ns/task/za"
19+
assert_contains "$output" "ns/task/za"
1820
}
1921

20-
run_test test_stack_limit "stack limit from st when all single-letter branches exist"
22+
run_test test_stack_limit "from st with all a-z taken, wraps to za"

0 commit comments

Comments
 (0)