@howaboua/pi-subagent-review 0.1.51 → 0.1.52

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -29,12 +29,15 @@ The widget is UI-only and is cleared when the command finishes, fails, or is can
29
29
 
30
30
  The command chooses the base branch automatically:
31
31
 
32
+ - if the current branch has a configured local parent/upstream branch, it reviews against that branch
33
+ - if the current branch reflog says it was created from another local branch, it reviews against that branch
34
+ - if the branch was created from `HEAD`, it tries to infer the parent from the branch creation commit
32
35
  - if you are on a branch other than `main`, `master`, or `dev`, it reviews against `dev`
33
36
  - if no local `dev` exists, it falls back to `main`, then `master`
34
37
  - if you are on `dev`, it reviews against `main`, then `master`
35
38
  - if you are on `main` or `master`, it prefers `dev` when available
36
39
 
37
- This means you usually never need to specify the diff base manually.
40
+ This makes `/review` work better in git worktrees: a worktree branch created from another local branch is reviewed against that parent branch instead of blindly falling back to `dev`/`main`/`master`. If no parent can be detected, the old fallback rules still apply.
38
41
 
39
42
  ## User arguments
40
43
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@howaboua/pi-subagent-review",
3
- "version": "0.1.51",
3
+ "version": "0.1.52",
4
4
  "description": "Pi extension that adds /review via an isolated review subagent.",
5
5
  "license": "MIT",
6
6
  "files": [
package/src/review.ts CHANGED
@@ -16,16 +16,11 @@ async function gitString(pi: ExtensionAPI, cwd: string, args: string[], timeout
16
16
  return result.stdout.trim();
17
17
  }
18
18
 
19
- async function hasLocalBranch(pi: ExtensionAPI, cwd: string, branch: "main" | "master"): Promise<boolean> {
19
+ async function hasLocalBranch(pi: ExtensionAPI, cwd: string, branch: string): Promise<boolean> {
20
20
  const result = await runGit(pi, cwd, ["show-ref", "--verify", "--quiet", `refs/heads/${branch}`]);
21
21
  return result.code === 0;
22
22
  }
23
23
 
24
- async function hasLocalDevBranch(pi: ExtensionAPI, cwd: string): Promise<boolean> {
25
- const result = await runGit(pi, cwd, ["show-ref", "--verify", "--quiet", "refs/heads/dev"]);
26
- return result.code === 0;
27
- }
28
-
29
24
  async function hasTrackedChangesAgainst(pi: ExtensionAPI, cwd: string, revision: string): Promise<boolean> {
30
25
  const result = await runGit(pi, cwd, ["diff", "--quiet", revision]);
31
26
  if (result.code === 0) return false;
@@ -33,29 +28,97 @@ async function hasTrackedChangesAgainst(pi: ExtensionAPI, cwd: string, revision:
33
28
  throw new Error(result.stderr.trim() || `git diff --quiet ${revision} failed with exit code ${result.code}`);
34
29
  }
35
30
 
31
+ async function firstExistingBranch(pi: ExtensionAPI, cwd: string, branches: string[]): Promise<string | undefined> {
32
+ for (const branch of branches) {
33
+ if (await hasLocalBranch(pi, cwd, branch)) return branch;
34
+ }
35
+ return undefined;
36
+ }
37
+
38
+ function normalizeLocalBranchRef(ref: string): string | undefined {
39
+ const trimmed = ref.trim();
40
+ if (!trimmed || trimmed === "HEAD") return undefined;
41
+ if (trimmed.startsWith("refs/heads/")) return trimmed.slice("refs/heads/".length);
42
+ if (trimmed.startsWith("heads/")) return trimmed.slice("heads/".length);
43
+ return trimmed.replace(/^\/+/, "");
44
+ }
45
+
46
+ async function getConfiguredParentBranch(pi: ExtensionAPI, cwd: string, currentBranch: string): Promise<string | undefined> {
47
+ const remote = await runGit(pi, cwd, ["config", "--get", `branch.${currentBranch}.remote`]);
48
+ if (remote.code === 0 && remote.stdout.trim() && remote.stdout.trim() !== ".") return undefined;
49
+
50
+ const merge = await runGit(pi, cwd, ["config", "--get", `branch.${currentBranch}.merge`]);
51
+ if (merge.code !== 0) return undefined;
52
+ const branch = normalizeLocalBranchRef(merge.stdout.trim());
53
+ if (!branch || branch === currentBranch) return undefined;
54
+ return (await hasLocalBranch(pi, cwd, branch)) ? branch : undefined;
55
+ }
56
+
57
+ async function inferParentBranchFromCreationCommit(pi: ExtensionAPI, cwd: string, currentBranch: string, commit: string): Promise<string | undefined> {
58
+ const result = await runGit(pi, cwd, ["for-each-ref", "--contains", commit, "--format=%(refname:short)%00%(objectname)", "refs/heads"]);
59
+ if (result.code !== 0) return undefined;
60
+
61
+ const candidates = result.stdout
62
+ .split("\n")
63
+ .map((line) => {
64
+ const [branch, tip] = line.split("\0");
65
+ return { branch: branch ?? "", tip: tip ?? "" };
66
+ })
67
+ .filter((candidate) => candidate.branch && candidate.branch !== currentBranch && candidate.tip === commit);
68
+
69
+ return candidates.length === 1 ? candidates[0]?.branch : undefined;
70
+ }
71
+
72
+ async function getReflogParentBranch(pi: ExtensionAPI, cwd: string, currentBranch: string): Promise<string | undefined> {
73
+ const result = await runGit(pi, cwd, ["reflog", "show", "--format=%H%x00%gs", `refs/heads/${currentBranch}`]);
74
+ if (result.code !== 0) return undefined;
75
+
76
+ for (const line of result.stdout.split("\n")) {
77
+ const [commit, subject] = line.split("\0");
78
+ const copyMatch = (subject ?? "").match(/^Branch: copied refs\/heads\/(.+) to refs\/heads\/.+$/);
79
+ if (copyMatch) {
80
+ const candidate = normalizeLocalBranchRef(copyMatch[1] ?? "");
81
+ if (candidate && candidate !== currentBranch && await hasLocalBranch(pi, cwd, candidate)) return candidate;
82
+ continue;
83
+ }
84
+
85
+ const match = (subject ?? "").match(/^branch: Created from (.+)$/);
86
+ if (!match) continue;
87
+
88
+ const createdFrom = match[1] ?? "";
89
+ const candidate = normalizeLocalBranchRef(createdFrom);
90
+ if (candidate && candidate !== currentBranch && await hasLocalBranch(pi, cwd, candidate)) return candidate;
91
+ if (/^HEAD(?:[~^].*)?$/.test(createdFrom.trim()) && commit) return inferParentBranchFromCreationCommit(pi, cwd, currentBranch, commit);
92
+ }
93
+
94
+ return undefined;
95
+ }
96
+
97
+ async function detectParentBranch(pi: ExtensionAPI, cwd: string, currentBranch: string): Promise<string | undefined> {
98
+ if (!currentBranch) return undefined;
99
+ return (await getConfiguredParentBranch(pi, cwd, currentBranch)) ?? (await getReflogParentBranch(pi, cwd, currentBranch));
100
+ }
101
+
102
+ async function detectFallbackBaseBranch(pi: ExtensionAPI, cwd: string, currentBranch: string): Promise<string | undefined> {
103
+ const hasDev = await hasLocalBranch(pi, cwd, "dev");
104
+ const hasMain = await hasLocalBranch(pi, cwd, "main");
105
+ const hasMaster = await hasLocalBranch(pi, cwd, "master");
106
+
107
+ if (currentBranch === "dev") return (await firstExistingBranch(pi, cwd, ["main", "master"]));
108
+ if (currentBranch !== "main" && currentBranch !== "master") return (await firstExistingBranch(pi, cwd, ["dev", "main", "master"]));
109
+ if (hasDev) return "dev";
110
+ if (currentBranch === "main" && hasMaster) return "master";
111
+ if (currentBranch === "master" && hasMain) return "main";
112
+ return currentBranch || undefined;
113
+ }
114
+
36
115
  export async function detectReviewContext(pi: ExtensionAPI, cwd: string): Promise<ReviewContext> {
37
116
  const repoRoot = await gitString(pi, cwd, ["rev-parse", "--show-toplevel"]);
38
117
  const currentBranch = await gitString(pi, repoRoot, ["branch", "--show-current"]);
39
- const [hasDev, hasMain, hasMaster] = await Promise.all([
40
- hasLocalDevBranch(pi, repoRoot),
41
- hasLocalBranch(pi, repoRoot, "main"),
42
- hasLocalBranch(pi, repoRoot, "master"),
43
- ]);
44
-
45
- let baseBranch: "main" | "master" | "dev" | undefined;
46
- if (currentBranch === "dev") {
47
- if (hasMain) baseBranch = "main";
48
- else if (hasMaster) baseBranch = "master";
49
- } else if (currentBranch !== "main" && currentBranch !== "master") {
50
- if (hasDev) baseBranch = "dev";
51
- else if (hasMain) baseBranch = "main";
52
- else if (hasMaster) baseBranch = "master";
53
- } else {
54
- if (hasDev) baseBranch = "dev";
55
- else if (currentBranch === "main" && hasMaster) baseBranch = "master";
56
- else if (currentBranch === "master" && hasMain) baseBranch = "main";
57
- else baseBranch = currentBranch as "main" | "master";
58
- }
118
+
119
+ const parentBranch = await detectParentBranch(pi, repoRoot, currentBranch);
120
+ const baseBranch = parentBranch ?? (await detectFallbackBaseBranch(pi, repoRoot, currentBranch));
121
+ const baseSource = parentBranch ? "parent" : "fallback";
59
122
 
60
123
  if (!baseBranch) {
61
124
  const branches = await gitString(pi, repoRoot, ["for-each-ref", "--format=%(refname:short)", "refs/heads"]);
@@ -74,6 +137,7 @@ export async function detectReviewContext(pi: ExtensionAPI, cwd: string): Promis
74
137
  repoRoot,
75
138
  currentRef,
76
139
  baseBranch,
140
+ baseSource,
77
141
  mergeBase,
78
142
  baseTip,
79
143
  status,
@@ -94,7 +158,7 @@ export function buildReviewTask(review: ReviewContext, extraFocus: string, conve
94
158
  const sections = [
95
159
  `Repository root: ${review.repoRoot}`,
96
160
  `Current ref: ${review.currentRef}`,
97
- `Chosen local base branch: ${review.baseBranch}`,
161
+ `Chosen local base branch: ${review.baseBranch} (${review.baseSource === "parent" ? "detected parent branch" : "fallback branch"})`,
98
162
  `Base branch tip (short SHA): ${review.baseTip}`,
99
163
  `Merge base (${review.baseBranch}, HEAD): ${review.mergeBase}`,
100
164
  "",
@@ -152,9 +216,16 @@ export function buildReviewTask(review: ReviewContext, extraFocus: string, conve
152
216
  return sections.join("\n");
153
217
  }
154
218
 
219
+ function markdownCodeSpan(value: string): string {
220
+ const longestBacktickRun = Math.max(0, ...Array.from(value.matchAll(/`+/g), (match) => match[0].length));
221
+ const delimiter = "`".repeat(longestBacktickRun + 1);
222
+ const padding = value.startsWith("`") || value.endsWith("`") ? " " : "";
223
+ return `${delimiter}${padding}${value}${padding}${delimiter}`;
224
+ }
225
+
155
226
  export function buildReviewUserMessage(review: ReviewContext, findings: string): string {
156
227
  return [
157
- `Review findings from /${REVIEW_COMMAND} against local base branch \`${review.baseBranch}\` in \`${review.repoRoot}\` (merge base \`${review.mergeBase.slice(0, 12)}\`):`,
228
+ `Review findings from /${REVIEW_COMMAND} against local base branch ${markdownCodeSpan(review.baseBranch)} (${review.baseSource === "parent" ? "detected parent branch" : "fallback branch"}) in ${markdownCodeSpan(review.repoRoot)} (merge base ${markdownCodeSpan(review.mergeBase.slice(0, 12))}):`,
158
229
  "",
159
230
  findings.trim() || "No actionable issues found.",
160
231
  "",
package/src/types.ts CHANGED
@@ -60,7 +60,8 @@ export interface ChildRunDetails {
60
60
  export interface ReviewContext {
61
61
  repoRoot: string;
62
62
  currentRef: string;
63
- baseBranch: "main" | "master" | "dev";
63
+ baseBranch: string;
64
+ baseSource: "parent" | "fallback";
64
65
  mergeBase: string;
65
66
  baseTip: string;
66
67
  status: string;