Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
refactor(check-deps): improve git ref handling and rename options
- Rename defaultBranch to baseBranch (more accurate terminology)
- Add --remote option (default: 'origin') for customizing remote name
- Default base branch is now ${remote}/main instead of local 'main'
- Remove misleading fallback logic in getMergeBase that could give wrong results
- Rename getGitDiff to getManifestGitDiff for clarity
- Remove unnecessary try/catch in getManifestGitDiff (git diff returns 0 for no changes)
  • Loading branch information
cryptodev-2s committed Dec 10, 2025
commit e9c96d017568153518cb0d21526ed6cda5e5e8ca
73 changes: 34 additions & 39 deletions src/check-dependency-bumps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,23 +46,23 @@ async function getStdoutFromCommand(
* @param projectRoot - Working directory for the command.
* @returns The git diff output.
*/
async function getGitDiff(
async function getManifestGitDiff(
fromRef: string,
toRef: string,
projectRoot: string,
): Promise<string> {
try {
return await getStdoutFromCommand(
'git',
['diff', '-U9999', fromRef, toRef, '--', '**/package.json'],
projectRoot,
);
} catch (error: any) {
if (error.exitCode === 1 && error.stdout === '') {
return '';
}
throw error;
}
return await getStdoutFromCommand(
'git',
[
'diff',
'-U9999', // Show maximum context to ensure full dependency lists are visible
fromRef,
toRef,
'--',
'**/package.json',
],
projectRoot,
);
}

/**
Expand Down Expand Up @@ -240,29 +240,21 @@ async function getCurrentBranchName(projectRoot: string): Promise<string> {
}

/**
* Gets the merge base between HEAD and the default branch.
* Gets the merge base between HEAD and the base branch.
*
* @param defaultBranch - The default branch name.
* @param baseBranch - The base branch reference (e.g., 'origin/main', 'upstream/develop').
* @param projectRoot - Working directory for the command.
* @returns The merge base commit SHA.
*/
async function getMergeBase(
defaultBranch: string,
baseBranch: string,
projectRoot: string,
): Promise<string> {
try {
return await getStdoutFromCommand(
'git',
['merge-base', 'HEAD', defaultBranch],
projectRoot,
);
} catch {
return await getStdoutFromCommand(
'git',
['merge-base', 'HEAD', `origin/${defaultBranch}`],
projectRoot,
);
}
return await getStdoutFromCommand(
'git',
['merge-base', 'HEAD', baseBranch],
projectRoot,
);
}

/**
Expand Down Expand Up @@ -310,9 +302,10 @@ async function resolveRepositoryUrl(
*
* @param options - Options.
* @param options.projectRoot - Root directory containing packages.
* @param options.fromRef - Starting git ref (defaults to merge base with default branch).
* @param options.fromRef - Starting git ref (defaults to merge base with base branch).
* @param options.toRef - Ending git ref (defaults to HEAD).
* @param options.defaultBranch - Default branch name.
* @param options.remote - Remote name (defaults to 'origin').
* @param options.baseBranch - Base branch reference (defaults to '<remote>/main').
* @param options.fix - Whether to add missing changelog entries.
* @param options.prNumber - PR number to include when adding entries.
* @param options.repoUrl - Repository URL override.
Expand All @@ -324,34 +317,36 @@ export async function checkDependencyBumps({
projectRoot,
fromRef,
toRef = 'HEAD',
defaultBranch = 'main',
remote = 'origin',
baseBranch,
fix = false,
prNumber,
repoUrl,
stdout,
stderr,
}: CheckDependencyBumpsOptions): Promise<PackageChanges> {
const actualBaseBranch = baseBranch ?? `${remote}/main`;
let actualFromRef = fromRef ?? '';

if (!actualFromRef) {
const currentBranch = await getCurrentBranchName(projectRoot);
stdout.write(`\n📌 Current branch: ${currentBranch}\n`);
stdout.write(`\n📌 Current branch: '${currentBranch}'\n`);

if (currentBranch === defaultBranch) {
if (currentBranch === actualBaseBranch) {
stdout.write(
`⚠️ You are on the ${defaultBranch} branch. Provide --from or switch to a feature branch.\n`,
`⚠️ You are on the ${actualBaseBranch} branch. Provide --from or switch to a feature branch.\n`,
);
return {};
}

try {
actualFromRef = await getMergeBase(defaultBranch, projectRoot);
actualFromRef = await getMergeBase(actualBaseBranch, projectRoot);
stdout.write(
`📍 Comparing against merge base with ${defaultBranch}: ${actualFromRef.substring(0, 8)}...\n`,
`📍 Comparing against merge base with ${actualBaseBranch}: ${actualFromRef.substring(0, 8)}...\n`,
);
} catch {
stderr.write(
`❌ Could not find merge base with ${defaultBranch}. Provide --from or --default-branch.\n`,
`❌ Could not find merge base with ${actualBaseBranch}. Provide --from or --base-branch.\n`,
);
return {};
}
Expand All @@ -364,7 +359,7 @@ export async function checkDependencyBumps({
)} to ${toRef}...\n\n`,
);

const diff = await getGitDiff(actualFromRef, toRef, projectRoot);
const diff = await getManifestGitDiff(actualFromRef, toRef, projectRoot);
if (!diff) {
stdout.write('No package.json changes found.\n');
return {};
Expand Down
18 changes: 13 additions & 5 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ async function saveChangelog(
type CheckDependencyBumpsCommandOptions = {
from?: string;
to?: string;
defaultBranch?: string;
remote?: string;
baseBranch?: string;
fix?: boolean;
pr?: string;
repo?: string;
Expand Down Expand Up @@ -398,11 +399,17 @@ async function main() {
type: 'string',
default: 'HEAD',
})
.option('default-branch', {
.option('remote', {
alias: 'r',
describe:
'The remote name to use when auto-detecting the base branch.',
default: 'origin',
type: 'string',
})
.option('base-branch', {
alias: 'b',
describe:
'The name of the default branch to compare against when auto-detecting.',
default: 'main',
'The base branch reference to compare against (defaults to <remote>/main).',
type: 'string',
})
.option('fix', {
Expand Down Expand Up @@ -526,7 +533,8 @@ async function main() {
projectRoot: resolvedRoot,
fromRef: checkDepsArgs.from,
toRef: checkDepsArgs.to,
defaultBranch: checkDepsArgs.defaultBranch,
remote: checkDepsArgs.remote,
baseBranch: checkDepsArgs.baseBranch,
fix: checkDepsArgs.fix,
prNumber: checkDepsArgs.pr,
repoUrl: checkDepsArgs.repo,
Expand Down
8 changes: 5 additions & 3 deletions src/dependency-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,14 @@ export type PackageChanges = Record<string, PackageInfo>;
export type CheckDependencyBumpsOptions = {
/** Root directory containing packages. */
projectRoot: string;
/** Starting git reference (defaults to merge base with default branch). */
/** Starting git reference (defaults to merge base with base branch). */
fromRef?: string;
/** Ending git reference (defaults to HEAD). */
toRef?: string;
/** Default branch name for auto-detection. */
defaultBranch?: string;
/** Remote name for auto-detection (defaults to 'origin'). */
remote?: string;
/** Base branch reference for auto-detection (defaults to '<remote>/main'). */
baseBranch?: string;
/** Whether to automatically add missing changelog entries. */
fix?: boolean;
/** PR number to include in changelog entries. */
Expand Down