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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"@rnx-kit/align-deps": "^3.4.5",
"@rnx-kit/lint-lockfile": "^0.1.0",
"@rnx-kit/oxlint-config": "^1.0.3",
"@rnx-kit/tools-git": "^0.1.1",
"@swc-node/register": "^1.11.1",
"@swc/core": "^1.15.8",
"@types/js-yaml": "^4.0.5",
Expand Down
61 changes: 2 additions & 59 deletions scripts/affected.mts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getBaseCommit, getChangedFiles, git } from "@rnx-kit/tools-git";
import yaml from "js-yaml";
import { Minimatch } from "minimatch";
import { spawnSync } from "node:child_process";
import * as fs from "node:fs";

type MatchChangedFiles = { "any-glob-to-any-file": string[] };
Expand All @@ -13,63 +13,6 @@ function clean(platforms: string[]): string[] {
return platforms.map((p) => p.toLowerCase()).sort();
}

/**
* Executes a Git command.
*/
function git(...args: string[]): string {
const { stderr, stdout } = spawnSync("git", args);
const message = stderr.toString().trim();
if (message) {
console.error(message);
}
return stdout.toString().trim();
}

/**
* Returns the default Git branch.
*/
function getDefaultBranch(): string {
if (process.env["CI"]) {
// CIs don't clone the repo, but use a different way to checkout a branch.
// This means that `origin/HEAD` is never created, which in turn means that
// we don't have a reliable way to get the default branch. For now, just
// return a hard-coded value.
return "origin/trunk";
}

const defaultBranch = git("rev-parse", "--abbrev-ref", "origin/HEAD");
if (!defaultBranch) {
throw new Error("Failed to determine default branch");
}
return defaultBranch;
}

/**
* Returns the commit from which this branch was forked.
*/
function getBaseCommit(targetBranch: string | undefined): string {
targetBranch =
!targetBranch || targetBranch.endsWith("/")
? getDefaultBranch()
: targetBranch;
const base = git("merge-base", "--fork-point", targetBranch);
if (!base) {
console.error("❌", "Failed to determine base commit");
}
return base;
}

/**
* Returns changed files since fork point.
*/
function getChangedFiles(since: string): string[] {
const changedFiles = git("diff", "--name-only", since);
if (!changedFiles) {
return [];
}
return changedFiles.split("\n");
}

/**
* Loads labels from Pull Request Labeler action configuration.
*/
Expand Down Expand Up @@ -105,7 +48,7 @@ function makeMatchers(): Record<string, Minimatch[]> {
function getAffectedPlatforms(targetBranch: string | undefined): string[] {
const platformMatchers = makeMatchers();

const baseCommit = getBaseCommit(targetBranch);
const baseCommit = getBaseCommit(targetBranch, "origin/trunk");
if (!baseCommit) {
// Match all platforms if we cannot find base commit
return clean(Object.keys(platformMatchers));
Expand Down
8 changes: 8 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2520,6 +2520,7 @@ __metadata:
"@rnx-kit/align-deps": "npm:^3.4.5"
"@rnx-kit/lint-lockfile": "npm:^0.1.0"
"@rnx-kit/oxlint-config": "npm:^1.0.3"
"@rnx-kit/tools-git": "npm:^0.1.1"
"@swc-node/register": "npm:^1.11.1"
"@swc/core": "npm:^1.15.8"
"@types/js-yaml": "npm:^4.0.5"
Expand Down Expand Up @@ -4550,6 +4551,13 @@ __metadata:
languageName: node
linkType: hard

"@rnx-kit/tools-git@npm:^0.1.1":
version: 0.1.1
resolution: "@rnx-kit/tools-git@npm:0.1.1"
checksum: 10c0/d672aa911d212a200cd593cf680fa316adc9cd221a26feac5f5fb055d5a4857d6c1f9356d8b027ea2a124eb7d24ba53416ccd373b0707a14f96f6586056658d7
languageName: node
linkType: hard

"@rnx-kit/tools-language@npm:^3.0.1":
version: 3.0.1
resolution: "@rnx-kit/tools-language@npm:3.0.1"
Expand Down
Loading