Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"scripts": {
"build": "tsx scripts/build.ts",
"build:skills": "tsx scripts/build-skills.ts",
"test": "npm run test -w @diffity/git && npm run test -w @diffity/parser && npm run test -w @diffity/ui && npm run test:scripts",
"test": "npm run test -w @diffity/git && npm run test -w @diffity/github && npm run test -w @diffity/parser && npm run test -w @diffity/ui && npm run test:scripts",
"link-dev": "tsx scripts/link-dev.ts",
"dev": "tsx scripts/dev.ts",
"release:patch": "npm run build && tsx scripts/release.ts patch && npm publish -w packages/cli",
Expand Down
14 changes: 10 additions & 4 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import { createRequire } from 'node:module';
import open from 'open';
import pc from 'picocolors';
import { isGitRepo, isValidGitRef, getRepoRoot, getRepoName, normalizeRef, WORKING_TREE_REFS } from '@diffity/git';
import type { PrBase } from '@diffity/github';
import {
isGitHubPrUrl,
parseGitHubPrUrl,
checkoutPr,
getPrBaseRef,
getPrBase,
isCliInstalled,
isAuthenticated,
detectRemote,
Expand Down Expand Up @@ -100,6 +101,8 @@ range syntax (main..feature, main...feature) also work.`)
}
}

let prBase: PrBase | null = null;

if (refs.length === 1 && isGitHubPrUrl(refs[0])) {
const parsed = parseGitHubPrUrl(refs[0]);
if (!parsed) {
Expand Down Expand Up @@ -147,15 +150,14 @@ range syntax (main..feature, main...feature) also work.`)
process.exit(1);
}

let baseRef: string;
try {
baseRef = getPrBaseRef(parsed.number);
prBase = getPrBase(parsed.number);
} catch {
console.error(pc.red(`Error: Could not determine base branch for PR #${parsed.number}.`));
process.exit(1);
}

refs[0] = baseRef;
refs[0] = prBase.oid;
}

// --base/--compare flags take precedence over positional args
Expand Down Expand Up @@ -221,6 +223,10 @@ range syntax (main..feature, main...feature) also work.`)
description = 'Unstaged changes';
}

if (prBase) {
description = `Changes from ${prBase.name}`;
}

let effectiveRef: string;
if (refs.length > 0) {
effectiveRef = refs.length === 2 ? `${refs[0]}..${refs[1]}` : refs[0];
Expand Down
7 changes: 5 additions & 2 deletions packages/github/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@
},
"scripts": {
"build": "tsc",
"dev": "tsc --watch"
"dev": "tsc --watch",
"test": "vitest run",
"test:watch": "vitest"
},
"files": [
"dist"
],
"devDependencies": {
"@types/node": "^25.5.0",
"typescript": "^5.9.3"
"typescript": "^5.9.3",
"vitest": "^4.1.0"
}
}
4 changes: 2 additions & 2 deletions packages/github/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export type { GitHubRemote, GitHubDetails, PrComment, PushResult, PulledThread } from './types.js';
export type { GitHubRemote, GitHubDetails, PrBase, PrComment, PushResult, PulledThread } from './types.js';
export { detectRemote, fetchDetails, isCliInstalled, isAuthenticated } from './detection.js';
export { getFiles, getComments, getCommentCount, pushComments, pullComments } from './pr.js';
export { isGitHubPrUrl, parseGitHubPrUrl, checkoutPr, getPrBaseRef } from './pr-url.js';
export { isGitHubPrUrl, parseGitHubPrUrl, checkoutPr, getPrBase, parsePrBase } from './pr-url.js';
18 changes: 16 additions & 2 deletions packages/github/src/pr-url.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { exec } from './exec.js';
import type { PrBase } from './types.js';

const PR_URL_REGEX = /(?:https?:\/\/)?github\.com\/([^/]+)\/([^/]+)\/pull\/(\d+)/;

Expand All @@ -23,6 +24,19 @@ export function checkoutPr(prNumber: number): void {
exec(`gh pr checkout ${prNumber}`);
}

export function getPrBaseRef(prNumber: number): string {
return exec(`gh pr view ${prNumber} --json baseRefName --jq '.baseRefName'`);
export function parsePrBase(json: string): PrBase {
const { baseRefName, baseRefOid } = JSON.parse(json) as {
baseRefName?: string;
baseRefOid?: string;
};

if (!baseRefName || !baseRefOid) {
throw new Error('Pull request response is missing baseRefName or baseRefOid');
}

return { name: baseRefName, oid: baseRefOid };
}

export function getPrBase(prNumber: number): PrBase {
return parsePrBase(exec(`gh pr view ${prNumber} --json baseRefName,baseRefOid`));
}
10 changes: 10 additions & 0 deletions packages/github/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ export interface GitHubDetails {
commentCount: number;
}

export interface PrBase {
/** The base branch's name, for display. */
name: string;
/**
* The commit the pull request is based on. The diff must be taken from this, not from the
* local branch of the same name, which is usually behind the remote.
*/
oid: string;
}

export interface PulledThreadComment {
body: string;
authorName: string;
Expand Down
28 changes: 28 additions & 0 deletions packages/github/tests/pr-base.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { describe, it, expect } from 'vitest';
import { parsePrBase } from '../src/pr-url.js';

describe('parsePrBase', () => {
it('returns the base branch name and the commit it points at', () => {
const json = JSON.stringify({
baseRefName: 'master',
baseRefOid: '24e3eeab4c62927d05341e3eb9347c272fa7e3af',
});

expect(parsePrBase(json)).toEqual({
name: 'master',
oid: '24e3eeab4c62927d05341e3eb9347c272fa7e3af',
});
});

it('rejects a response without the oid, rather than diffing against a local branch', () => {
const json = JSON.stringify({ baseRefName: 'master' });

expect(() => parsePrBase(json)).toThrow(/baseRefName or baseRefOid/);
});

it('rejects a response without the branch name', () => {
const json = JSON.stringify({ baseRefOid: '24e3eeab4c' });

expect(() => parsePrBase(json)).toThrow(/baseRefName or baseRefOid/);
});
});