Skip to content

Commit 6ff8325

Browse files
authored
feat: moved branch scope detection to scripts (#159)
1 parent 570605d commit 6ff8325

8 files changed

Lines changed: 85 additions & 29 deletions

File tree

skills/create-branch/SKILL.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,15 @@ Run:
2525
git rev-parse --abbrev-ref HEAD
2626
```
2727

28-
**If the result is NOT `main`, `master`, or `develop`:** the session is already on a feature branch. Skip silently — return control to the caller without any output.
28+
Compare the result against the base branch detected by:
2929

30-
**If the result is `main`, `master`, or `develop` (or `HEAD` for detached state):** proceed to Step 2.
30+
```!
31+
bash scripts/detect-base-branch.sh
32+
```
33+
34+
**If the current branch is NOT the base branch (and not `HEAD`):** the session is already on a feature branch. Skip silently — return control to the caller without any output.
35+
36+
**If the current branch IS the base branch (or `HEAD` for detached state):** proceed to Step 2.
3137

3238
## Step 2: Infer branch name
3339

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../shared/scripts/detect-base-branch.sh

skills/rebase/SKILL.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,21 @@ Rebase the current feature branch onto the latest base branch to keep it up-to-d
1515

1616
Run these checks in order. If any fail, inform the user and stop.
1717

18-
### Detect current branch
18+
### Detect current branch and base branch
1919

2020
```bash
2121
git rev-parse --abbrev-ref HEAD
2222
```
2323

2424
If the result is `main`, `master`, or `develop` — inform the user they're already on the base branch and stop.
2525

26-
### Detect base branch
26+
Detect the base branch:
2727

28-
Check for `main`, `master`, `develop` (in that order). Use the first one that exists. If none exist, inform the user and stop.
28+
```!
29+
bash scripts/detect-base-branch.sh
30+
```
31+
32+
If the script exits with an error, inform the user no base branch was found and stop.
2933

3034
### Check for uncommitted changes
3135

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../shared/scripts/detect-base-branch.sh

skills/review/SKILL.md

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -27,32 +27,16 @@ Parse the review scope above for optional file paths or directories.
2727

2828
**If no paths provided:**
2929

30-
1. Detect current branch:
30+
Run the scope detection script:
3131

32-
```bash
33-
git rev-parse --abbrev-ref HEAD
34-
```
32+
```!
33+
bash scripts/detect-review-scope.sh
34+
```
3535

36-
2. Detect default branch:
37-
38-
```bash
39-
git symbolic-ref refs/remotes/origin/HEAD 2>/dev/null | sed 's|refs/remotes/origin/||'
40-
```
41-
42-
Fallback: check for `main`, then `master`.
43-
44-
3. **If on a feature branch** (current branch differs from the default branch):
45-
- Get changed files: `git diff <default-branch>...HEAD --name-only`
46-
- Include uncommitted changes: `git diff --name-only` and `git diff --cached --name-only`
47-
- Deduplicate the combined file list
48-
- Announce scope summary: number of changed files, which areas of the codebase are affected
49-
- Proceed to Step 2 — the user invoked `/review`, intent is clear
50-
51-
4. **If on the default branch:**
52-
- Tell the user: "You're on `<branch>`. No branch diff available."
53-
- Use **AskUserQuestion**: "What would you like to review?" with options:
54-
- **Specify files or directories**: accept paths from the user
55-
- **Review entire project**: no scope constraint
36+
- **If `SCOPE=branch`**: use the listed files as review scope. Announce scope summary: number of changed files, which areas of the codebase are affected. Proceed to Step 2.
37+
- **If `SCOPE=default`**: tell the user: "You're on `<CURRENT_BRANCH>`. No branch diff available." Use **AskUserQuestion**: "What would you like to review?" with options:
38+
- **Specify files or directories**: accept paths from the user
39+
- **Review entire project**: no scope constraint
5640

5741
## Step 2 — Run Reviews
5842

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../shared/scripts/detect-review-scope.sh
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/env bash
2+
# Detect the default/base branch of the repository.
3+
# Checks main, master, develop in order. Outputs the first one found.
4+
# Exits 1 if none exist.
5+
6+
for branch in main master develop; do
7+
if git rev-parse --verify "$branch" &>/dev/null ||
8+
git rev-parse --verify "origin/$branch" &>/dev/null; then
9+
echo "$branch"
10+
exit 0
11+
fi
12+
done
13+
14+
echo "ERROR: no base branch found (checked main, master, develop)" >&2
15+
exit 1
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/usr/bin/env bash
2+
# Detect review scope: current branch, base branch, and changed files.
3+
# Output format (one section per line):
4+
# CURRENT_BRANCH=<branch>
5+
# BASE_BRANCH=<branch>
6+
# SCOPE=branch|default
7+
# FILES (one per line after the FILES header)
8+
#
9+
# Usage: detect-review-scope.sh
10+
11+
set -euo pipefail
12+
13+
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
14+
echo "CURRENT_BRANCH=$CURRENT_BRANCH"
15+
16+
# Detect base branch
17+
BASE_BRANCH=""
18+
DEFAULT_REF=$(git symbolic-ref refs/remotes/origin/HEAD 2>/dev/null | sed 's|refs/remotes/origin/||' || true)
19+
if [ -n "$DEFAULT_REF" ]; then
20+
BASE_BRANCH="$DEFAULT_REF"
21+
else
22+
for branch in main master develop; do
23+
if git rev-parse --verify "$branch" &>/dev/null ||
24+
git rev-parse --verify "origin/$branch" &>/dev/null; then
25+
BASE_BRANCH="$branch"
26+
break
27+
fi
28+
done
29+
fi
30+
echo "BASE_BRANCH=$BASE_BRANCH"
31+
32+
# Determine scope
33+
if [ "$CURRENT_BRANCH" = "$BASE_BRANCH" ] || [ -z "$BASE_BRANCH" ]; then
34+
echo "SCOPE=default"
35+
else
36+
echo "SCOPE=branch"
37+
echo "FILES"
38+
# Combine branch diff + uncommitted + staged, deduplicate
39+
{
40+
git diff "$BASE_BRANCH"...HEAD --name-only 2>/dev/null
41+
git diff --name-only 2>/dev/null
42+
git diff --cached --name-only 2>/dev/null
43+
} | sort -u
44+
fi

0 commit comments

Comments
 (0)