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
89 changes: 89 additions & 0 deletions apps/server/src/vcs/GitVcsDriverCore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2414,6 +2414,95 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
}),
);

it.effect("counts Changes from the copy of main a branch was cut from", () =>
Effect.gen(function* () {
const upstream = yield* makeTmpDir();
yield* initRepoWithCommit(upstream);
yield* git(upstream, ["branch", "-M", "main"]);
const fork = yield* makeTmpDir();
yield* git(fork, ["clone", upstream, "."]);
yield* git(fork, ["config", "user.email", "test@test.com"]);
yield* git(fork, ["config", "user.name", "Test"]);
yield* writeTextFile(fork, "fork.txt", "one\ntwo\nthree\n");
yield* git(fork, ["add", "fork.txt"]);
yield* git(fork, ["commit", "-m", "fork-only work"]);
yield* writeTextFile(upstream, "upstream.txt", "upstream\n");
yield* git(upstream, ["add", "upstream.txt"]);
yield* git(upstream, ["commit", "-m", "upstream-only work"]);
const cwd = yield* makeTmpDir();
yield* git(cwd, ["clone", upstream, "."]);
yield* git(cwd, ["config", "user.email", "test@test.com"]);
yield* git(cwd, ["config", "user.name", "Test"]);
yield* git(cwd, ["remote", "add", "fork", fork]);
yield* git(cwd, ["fetch", "fork"]);
// Local main follows the fork, so both copies of main can be the base.
yield* git(cwd, ["branch", "--set-upstream-to", "fork/main", "main"]);
const driver = yield* GitVcsDriver.GitVcsDriver;
const changes = Effect.map(
driver.statusDetailsLocal(cwd, { includeBranchChanges: true }),
(status) => status.branchChanges,
);

yield* git(cwd, ["switch", "-c", "from-fork", "fork/main"]);
yield* writeTextFile(cwd, "from-fork.txt", "fork feature\n");
yield* git(cwd, ["add", "from-fork.txt"]);
yield* git(cwd, ["commit", "-m", "work on the fork"]);
assert.deepStrictEqual(yield* changes, {
baseRef: "fork/main",
insertions: 1,
deletions: 0,
});

yield* git(cwd, ["switch", "-c", "from-origin", "origin/main"]);
yield* writeTextFile(cwd, "from-origin.txt", "origin\nfeature\n");
yield* git(cwd, ["add", "from-origin.txt"]);
yield* git(cwd, ["commit", "-m", "work on upstream"]);
assert.deepStrictEqual(yield* changes, {
baseRef: "origin/main",
insertions: 2,
deletions: 0,
});

yield* git(cwd, ["switch", "main"]);
yield* git(cwd, ["reset", "--hard", "fork/main"]);
yield* writeTextFile(cwd, "local.txt", "local\n");
assert.deepStrictEqual(yield* changes, {
baseRef: "fork/main",
insertions: 1,
deletions: 0,
});
}),
);

it.effect("never counts Changes from a pull request author's copy of main", () =>
Effect.gen(function* () {
const upstream = yield* makeTmpDir();
yield* initRepoWithCommit(upstream);
yield* git(upstream, ["branch", "-M", "main"]);
const author = yield* makeTmpDir();
yield* git(author, ["clone", upstream, "."]);
yield* git(author, ["config", "user.email", "test@test.com"]);
yield* git(author, ["config", "user.name", "Test"]);
yield* writeTextFile(author, "pr.txt", "pull\nrequest\n");
yield* git(author, ["add", "pr.txt"]);
yield* git(author, ["commit", "-m", "pull request work"]);
const cwd = yield* makeTmpDir();
yield* git(cwd, ["clone", upstream, "."]);
// How a checkout of a pull request opened from the author's main looks.
yield* git(cwd, ["remote", "add", "author", author]);
yield* git(cwd, ["fetch", "author", "+refs/heads/main:refs/remotes/author/main"]);
yield* git(cwd, ["switch", "-c", "t3code/pr-42/main", "author/main"]);
const driver = yield* GitVcsDriver.GitVcsDriver;

const status = yield* driver.statusDetailsLocal(cwd, { includeBranchChanges: true });
assert.deepStrictEqual(status.branchChanges, {
baseRef: "origin/main",
insertions: 2,
deletions: 0,
});
}),
);

it.effect("reports non-repository directories without failing", () =>
Effect.gen(function* () {
const cwd = yield* makeTmpDir();
Expand Down
91 changes: 74 additions & 17 deletions apps/server/src/vcs/GitVcsDriverCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1677,6 +1677,55 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
return remoteName;
});

// The remote the local `branchName` tracks, such as the fork a local main follows.
// A remote fetched for someone else's pull request is never it.
const readTrackedRemoteName = Effect.fn("readTrackedRemoteName")(function* (
cwd: string,
branchName: string,
) {
const ref = `refs/heads/${branchName}`;
const stdout = yield* runGitStdout(
"GitVcsDriver.readTrackedRemoteName",
cwd,
["for-each-ref", "--format=%(refname) %(upstream:remotename)", ref],
true,
);
const remoteName = stdout
.split("\n")
.map((line) => line.trim().split(" "))
.find(([refName]) => refName === ref)?.[1];
return remoteName && remoteName !== "." ? remoteName : null;
});

// The copy that leaves the fewest of `refName`'s commits outside it is the one
// the branch was cut from. Ties keep the earlier copy.
const closestCopy = Effect.fn("closestCopy")(function* (
cwd: string,
refName: string,
copies: readonly [string, ...string[]],
) {
let closest = copies[0];
let fewestOutside = Number.POSITIVE_INFINITY;
for (const copy of copies) {
const stdout = yield* runGitStdout(
"GitVcsDriver.closestCopy",
cwd,
["rev-list", "--count", `${copy}..refs/heads/${refName}`],
true,
);
const outside = Number.parseInt(stdout.trim(), 10);
if (outside < fewestOutside) {
closest = copy;
fewestOutside = outside;
}
}
return closest;
});

// Picks the base branch name from gh-merge-base, the default branch, then main or
// master, and returns the primary remote's copy, else the local branch. When the local
// base tracks another remote, as with a fork beside its upstream, the copy the branch
// was cut from wins, so Changes holds only the branch's commits.
// `allowRemoteOfCurrent` lets the review diff compare the default branch with its remote copy.
const resolveBaseBranchForNoUpstream = Effect.fn("resolveBaseBranchForNoUpstream")(function* (
cwd: string,
Expand Down Expand Up @@ -1716,34 +1765,42 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
if (normalizedCandidate.length === 0) {
continue;
}
if (normalizedCandidate === refName) {
if (
options?.allowRemoteOfCurrent &&
primaryRemoteName &&
(yield* remoteBranchExists({
cwd,
remoteName: primaryRemoteName,
refName: normalizedCandidate,
}))
) {
return `${primaryRemoteName}/${normalizedCandidate}`;
}
const isCurrent = normalizedCandidate === refName;
if (isCurrent && !options?.allowRemoteOfCurrent) {
continue;
}

if (
const primaryCopy =
primaryRemoteName &&
(yield* remoteBranchExists({
cwd,
remoteName: primaryRemoteName,
refName: normalizedCandidate,
}))
) {
return `${primaryRemoteName}/${normalizedCandidate}`;
? `${primaryRemoteName}/${normalizedCandidate}`
: null;
if (primaryCopy === null && !isCurrent && (yield* branchExists(cwd, normalizedCandidate))) {
return normalizedCandidate;
}

if (yield* branchExists(cwd, normalizedCandidate)) {
return normalizedCandidate;
const copies = primaryCopy === null ? [] : [primaryCopy];
const trackedRemoteName = yield* readTrackedRemoteName(cwd, normalizedCandidate);
if (
trackedRemoteName !== null &&
trackedRemoteName !== primaryRemoteName &&
(yield* remoteBranchExists({
cwd,
remoteName: trackedRemoteName,
refName: normalizedCandidate,
}))
) {
copies.push(`${trackedRemoteName}/${normalizedCandidate}`);
}
const [firstCopy, ...otherCopies] = copies;
if (firstCopy !== undefined) {
return otherCopies.length === 0
? firstCopy
: yield* closestCopy(cwd, refName, [firstCopy, ...otherCopies]);
}
}

Expand Down
Loading