Public: anyone can read this; the crew can change it.
Update from parent merges when the fork has diverged #5
mason/forge:mason/4-update-from-parent-merges-when-the-fork → main 3 commits fork
6 files
changed +289
−60
docs/APPS.md +4 −2
| @@ -382,8 +382,10 @@ outbox with a sweep behind the wake-up: | ||
| 382 | 382 | Forks follow their parents. A fork's default branch is a copy of the |
| 383 | 383 | parent's until someone commits on it, and while that is so the parent |
| 384 | 384 | moving is the fork moving — fast-forward only, as pushes land, a mirror's |
| 385 | sync included. A fork with commits of its own on that branch is left as it | |
| 386 | is; `Update from parent` on its page (or `POST …/sync`) says so. This is | |
| 385 | sync included. A fork with commits of its own on that branch is left alone | |
| 386 | by that automatic following; asked by hand, `Update from parent` on its | |
| 387 | page (or `POST …/sync`) merges the parent in with a merge commit made on | |
| 388 | behalf of whoever asked, and refuses whole when that would conflict. This is | |
| 387 | 389 | what lets one person's fork stand between the crew and the outside world: |
| 388 | 390 | the crew proposes into it, the person merges there, and proposes the |
| 389 | 391 | result onward as one pull (see `docs/ORIGINS.md`, "a fork of a mirror"). |
src/actions/forks.ts +91 −14
| @@ -1,8 +1,10 @@ | ||
| 1 | 1 | /** |
| 2 | 2 | * Forks that follow their parent, asked for by hand. The following itself |
| 3 | * happens as pushes land (`core/following.ts`); this is the same step on | |
| 4 | * request, with the answer when it cannot be done — and `forkDrift` is what | |
| 5 | * a surface says before either happens. | |
| 3 | * happens as pushes land (`core/following.ts`), fast-forward only; this is | |
| 4 | * the same step on request — and asked for by hand it goes further, merging | |
| 5 | * the parent in when the fork has commits of its own, which is the one thing | |
| 6 | * automatic following will never do. `forkDrift` is what a surface says | |
| 7 | * before either happens. | |
| 6 | 8 | */ |
| 7 | 9 | |
| 8 | 10 | import type { App } from "../app.ts"; |
| @@ -10,16 +12,29 @@ import { fastForwardFork, type ForkSync } from "../core/following.ts"; | ||
| 10 | 12 | import { getRepoById, type Repo } from "../core/repos.ts"; |
| 11 | 13 | import type { User } from "../core/users.ts"; |
| 12 | 14 | import { divergence } from "../git/branches.ts"; |
| 13 | import { resolveOid } from "../git/merge.ts"; | |
| 15 | import { fetchInto, merge, mergeBase, resolveOid } from "../git/merge.ts"; | |
| 14 | 16 | import { repoDir } from "../git/repos.ts"; |
| 15 | import { err, type Result } from "../kernel/result.ts"; | |
| 17 | import { err, ok, type Result } from "../kernel/result.ts"; | |
| 16 | 18 | import { requireWriter, type AccessError } from "./access.ts"; |
| 17 | 19 | |
| 18 | 20 | export type { ForkSync } from "../core/following.ts"; |
| 19 | 21 | |
| 20 | export type SyncForkError = AccessError | "not_a_fork" | "diverged"; | |
| 22 | export type SyncForkError = | |
| 23 | | AccessError | |
| 24 | | "not_a_fork" | |
| 25 | /** The merge would have to be resolved by a person, in a work tree. */ | |
| 26 | | "conflict" | |
| 27 | /** Git refused the merge for some other reason; nothing was written. */ | |
| 28 | | "merge_failed"; | |
| 21 | 29 | |
| 22 | /** Bring a fork's default branch up to its parent's, if nothing was committed on it. */ | |
| 30 | /** | |
| 31 | * Bring a fork's default branch up to its parent's. A fork with nothing of | |
| 32 | * its own fast-forwards; one that has been committed on takes the parent as | |
| 33 | * a merge, made here on behalf of the person who asked, so that the answer | |
| 34 | * to "update from parent" is the same sentence either way. A merge that | |
| 35 | * would conflict is refused whole: the fork is left exactly as it was, for | |
| 36 | * a work tree and a person to reconcile. | |
| 37 | */ | |
| 23 | 38 | export async function syncFork( |
| 24 | 39 | app: App, |
| 25 | 40 | actor: User | undefined, |
| @@ -32,7 +47,9 @@ export async function syncFork( | ||
| 32 | 47 | ? undefined |
| 33 | 48 | : getRepoById(app.db, fork.forkParentId); |
| 34 | 49 | if (parent === undefined) return err("not_a_fork"); |
| 35 | return fastForwardFork(app, fork, parent, writer.value.id); | |
| 50 | const followed = await fastForwardFork(app, fork, parent, writer.value.id); | |
| 51 | if (followed.ok) return followed; | |
| 52 | return mergeParent(app, fork, parent, writer.value); | |
| 36 | 53 | } |
| 37 | 54 | |
| 38 | 55 | export interface ForkDrift { |
| @@ -44,12 +61,12 @@ export interface ForkDrift { | ||
| 44 | 61 | |
| 45 | 62 | /** |
| 46 | 63 | * How far a fork's default branch sits from its parent's — `behind` is what |
| 47 | * an update would bring, `ahead` is what makes one impossible. A read, not a | |
| 48 | * mutation: no authorization, because the caller has already established it | |
| 49 | * may see the fork. The parent's objects reach the fork through its | |
| 50 | * alternates, so one `rev-list --left-right --count` in the fork answers | |
| 51 | * both halves; when it cannot (a detached fork, an empty branch) both are | |
| 52 | * null and a surface says nothing rather than guessing. | |
| 64 | * an update would bring, `ahead` is what makes one a merge rather than a | |
| 65 | * fast-forward. A read, not a mutation: no authorization, because the caller | |
| 66 | * has already established it may see the fork. The parent's objects reach the | |
| 67 | * fork through its alternates, so one `rev-list --left-right --count` in the | |
| 68 | * fork answers both halves; when it cannot (a detached fork, an empty branch) | |
| 69 | * both are null and a surface says nothing rather than guessing. | |
| 53 | 70 | */ |
| 54 | 71 | export async function forkDrift( |
| 55 | 72 | app: App, |
| @@ -67,3 +84,63 @@ export async function forkDrift( | ||
| 67 | 84 | return { ahead: null, behind: null }; |
| 68 | 85 | return divergence(forkDir, base, head); |
| 69 | 86 | } |
| 87 | ||
| 88 | /** | |
| 89 | * The parent's default branch merged into the fork's, when the fork has | |
| 90 | * commits of its own in the way. Plumbing in the bare repo, like every other | |
| 91 | * merge here: the tree is computed with `merge-tree`, the commit records both | |
| 92 | * tips, and the branch moves only if it has not moved under us. The parent's | |
| 93 | * tip is fetched in first, so the merge commit's second parent is an object | |
| 94 | * the fork owns rather than one it borrows. | |
| 95 | */ | |
| 96 | async function mergeParent( | |
| 97 | app: App, | |
| 98 | fork: Repo, | |
| 99 | parent: Repo, | |
| 100 | actor: User, | |
| 101 | ): Promise<Result<ForkSync, "conflict" | "merge_failed">> { | |
| 102 | const parentDir = repoDir(app.reposRoot, parent.ownerName, parent.name); | |
| 103 | const forkDir = repoDir(app.reposRoot, fork.ownerName, fork.name); | |
| 104 | const ref = `refs/heads/${fork.defaultBranch}`; | |
| 105 | const from = await resolveOid(forkDir, ref); | |
| 106 | const base = await resolveOid( | |
| 107 | parentDir, | |
| 108 | `refs/heads/${parent.defaultBranch}`, | |
| 109 | ); | |
| 110 | if (from === undefined || base === undefined) return err("merge_failed"); | |
| 111 | // A fork that is only ahead has the parent's tip in its history already: | |
| 112 | // nothing to take, and no empty merge commit to make. | |
| 113 | if ((await mergeBase(forkDir, from, base)) === base) | |
| 114 | return ok({ updated: false, from, to: from }); | |
| 115 | const tip = await fetchInto( | |
| 116 | forkDir, | |
| 117 | parentDir, | |
| 118 | `refs/heads/${parent.defaultBranch}`, | |
| 119 | ); | |
| 120 | if (tip === undefined) return err("merge_failed"); | |
| 121 | const merged = await merge(forkDir, { | |
| 122 | baseBranch: fork.defaultBranch, | |
| 123 | baseOid: from, | |
| 124 | headOid: tip, | |
| 125 | strategy: "merge", | |
| 126 | message: | |
| 127 | `Merge ${parent.ownerName}/${parent.name} into ${fork.defaultBranch}\n\n` + | |
| 128 | `Updated from the parent by ${actor.username}.`, | |
| 129 | author: { | |
| 130 | name: actor.displayName || actor.username, | |
| 131 | email: actor.email, | |
| 132 | }, | |
| 133 | }); | |
| 134 | if (!merged.ok) | |
| 135 | return err(merged.error.kind === "conflict" ? "conflict" : "merge_failed"); | |
| 136 | app.bus.append({ | |
| 137 | kind: "push", | |
| 138 | repoId: fork.id, | |
| 139 | actorId: actor.id, | |
| 140 | payload: { | |
| 141 | updates: [{ ref, oldOid: from, newOid: merged.value.mergeOid }], | |
| 142 | followed: `${parent.ownerName}/${parent.name}`, | |
| 143 | }, | |
| 144 | }); | |
| 145 | return ok({ updated: true, from, to: merged.value.mergeOid }); | |
| 146 | } |
src/http/api.ts +3 −1
| @@ -2284,7 +2284,9 @@ export function apiRoutes(router: Router<Handler>): void { | ||
| 2284 | 2284 | if (repo.origin === null && repo.forkParentId !== null) { |
| 2285 | 2285 | const followed = await syncFork(ctx.app, ctx.auth.user, repo); |
| 2286 | 2286 | if (!followed.ok) |
| 2287 | return followed.error === "diverged" || followed.error === "not_a_fork" | |
| 2287 | return followed.error === "conflict" || | |
| 2288 | followed.error === "merge_failed" || | |
| 2289 | followed.error === "not_a_fork" | |
| 2288 | 2290 | ? json({ error: followed.error }, 409) |
| 2289 | 2291 | : respondAccess(followed.error, 404); |
| 2290 | 2292 | return json(followed.value); |
src/web/pages/repo.ts +15 −14
| @@ -86,8 +86,8 @@ function forkParent(ctx: Ctx, repo: Repo): Repo | undefined { | ||
| 86 | 86 | |
| 87 | 87 | /** |
| 88 | 88 | * How far the fork has slipped from its parent, in a phrase. A fork with |
| 89 | * commits of its own says so: an update would be refused, so the sentence | |
| 90 | * has to explain the missing button. | |
| 89 | * commits of its own says so — the update is still offered, but it will be | |
| 90 | * a merge rather than a fast-forward, and the sentence is what says which. | |
| 91 | 91 | */ |
| 92 | 92 | function driftNote(drift: ForkDrift | undefined): string | undefined { |
| 93 | 93 | if (drift === undefined || drift.ahead === null || drift.behind === null) |
| @@ -97,18 +97,17 @@ function driftNote(drift: ForkDrift | undefined): string | undefined { | ||
| 97 | 97 | if (ahead === 0) |
| 98 | 98 | return behind === 0 ? "up to date" : `${commits(behind)} behind`; |
| 99 | 99 | if (behind === 0) return `${commits(ahead)} of its own`; |
| 100 | return `${commits(ahead)} of its own, ${behind} behind — merge by hand`; | |
| 100 | return `${commits(ahead)} of its own, ${behind} behind — merges`; | |
| 101 | 101 | } |
| 102 | 102 | |
| 103 | 103 | /** |
| 104 | * Offer the update only when it would fast-forward: something to take, and | |
| 105 | * nothing of the fork's own in the way. Unknown drift keeps the button, since | |
| 106 | * the action itself is the thing that decides. | |
| 104 | * Offer the update whenever there is something to take: level is nothing to | |
| 105 | * do, and commits of the fork's own are merged rather than refused. Unknown | |
| 106 | * drift keeps the button, since the action itself is the thing that decides. | |
| 107 | 107 | */ |
| 108 | 108 | function updatable(drift: ForkDrift | undefined): boolean { |
| 109 | if (drift === undefined || drift.ahead === null || drift.behind === null) | |
| 110 | return true; | |
| 111 | return drift.behind > 0 && drift.ahead === 0; | |
| 109 | if (drift === undefined || drift.behind === null) return true; | |
| 110 | return drift.behind > 0; | |
| 112 | 111 | } |
| 113 | 112 | |
| 114 | 113 | /** |
| @@ -216,11 +215,13 @@ export async function repoSyncSubmit(ctx: Ctx): Promise<Response> { | ||
| 216 | 215 | return flashTo(ctx, repoBase(repo), { |
| 217 | 216 | tone: "danger", |
| 218 | 217 | message: |
| 219 | followed.error === "diverged" | |
| 220 | ? `${repo.defaultBranch} has commits of its own; merge the parent's by hand.` | |
| 221 | : followed.error === "not_a_fork" | |
| 222 | ? "This repository has no parent to follow." | |
| 223 | : "You need write access to update this fork.", | |
| 218 | followed.error === "conflict" | |
| 219 | ? `${repo.defaultBranch} and the parent changed the same lines; merge it by hand.` | |
| 220 | : followed.error === "merge_failed" | |
| 221 | ? "The parent could not be merged into this fork." | |
| 222 | : followed.error === "not_a_fork" | |
| 223 | ? "This repository has no parent to follow." | |
| 224 | : "You need write access to update this fork.", | |
| 224 | 225 | }); |
| 225 | 226 | return flashTo(ctx, repoBase(repo), { |
| 226 | 227 | message: followed.value.updated |
test/crew.test.ts +8 −2
| @@ -539,9 +539,15 @@ echo '{"type":"result","subtype":"success","is_error":false,"result":"Added CREW | ||
| 539 | 539 | runs("agent").find((r) => r.reason === "assigned" && r.state === "ok"), |
| 540 | 540 | ); |
| 541 | 541 | expect(built.summary).toMatch(/^opened pull #\d+/); |
| 542 | // Only the pull's own checks: the seeding push declared the check and so | |
| 543 | // was checked too, against a tree with no CREW.md at all, and that run's | |
| 544 | // verdict is not the one this test is about. | |
| 545 | const number = built.summary.match(/#(\d+)/)?.[1]; | |
| 546 | const pullChecks = () => | |
| 547 | runs("check").filter((r) => r.env === `pull-${number}`); | |
| 542 | 548 | // The forge checks the pull: no daffodils yet. |
| 543 | 549 | const failed = await until(() => |
| 544 | runs("check").find((r) => r.state === "failed"), | |
| 550 | pullChecks().find((r) => r.state === "failed"), | |
| 545 | 551 | ); |
| 546 | 552 | expect(failed.summary).toBe("check failed: check exited with code 1"); |
| 547 | 553 | // …and the agent hears about it, with the log, and answers. |
| @@ -556,7 +562,7 @@ echo '{"type":"result","subtype":"success","is_error":false,"result":"Added CREW | ||
| 556 | 562 | expect(prompt).toContain("check exited with code 1"); |
| 557 | 563 | expect(prompt).toContain("The end of the check log:"); |
| 558 | 564 | const passed = await until(() => |
| 559 | runs("check").find((r) => r.state === "ok"), | |
| 565 | pullChecks().find((r) => r.state === "ok"), | |
| 560 | 566 | ); |
| 561 | 567 | expect(passed.summary).toMatch(/^check passed/); |
| 562 | 568 | }, 120_000); |
test/forks.test.ts +168 −27
| @@ -80,6 +80,35 @@ describe("forks", () => { | ||
| 80 | 80 | .then((j) => (j as { token: string }).token); |
| 81 | 81 | } |
| 82 | 82 | |
| 83 | /** Fork alice/app for someone, cloned into their own work tree. */ | |
| 84 | async function forkOf(username: string, token: string): Promise<string> { | |
| 85 | const res = await fetch(`${base}/api/v1/repos/alice/app/forks`, { | |
| 86 | method: "POST", | |
| 87 | headers: { | |
| 88 | authorization: `Bearer ${token}`, | |
| 89 | "content-type": "application/json", | |
| 90 | }, | |
| 91 | body: "{}", | |
| 92 | }); | |
| 93 | expect(res.status).toBe(201); | |
| 94 | const remote = `${base.replace("http://", `http://${username}:${token}@`)}/${username}/app.git`; | |
| 95 | await gitOk(["clone", remote, `${username}-app`], work); | |
| 96 | return join(work, `${username}-app`); | |
| 97 | } | |
| 98 | ||
| 99 | /** One commit on a clone's main, pushed. */ | |
| 100 | async function commitTo( | |
| 101 | dir: string, | |
| 102 | file: string, | |
| 103 | body: string, | |
| 104 | message: string, | |
| 105 | ): Promise<void> { | |
| 106 | await Bun.write(join(dir, file), body); | |
| 107 | await gitOk(["add", "."], dir); | |
| 108 | await gitOk(["commit", "-m", message], dir); | |
| 109 | await gitOk(["push", "origin", "main"], dir); | |
| 110 | } | |
| 111 | ||
| 83 | 112 | beforeAll(async () => { |
| 84 | 113 | dataDir = await mkdtemp(join(tmpdir(), "forge-fork-")); |
| 85 | 114 | work = await mkdtemp(join(tmpdir(), "forge-fork-work-")); |
| @@ -220,30 +249,21 @@ describe("forks", () => { | ||
| 220 | 249 | expect(answer.updated).toBe(false); |
| 221 | 250 | expect(answer.to).toBe(aliceTip); |
| 222 | 251 | |
| 223 | // Bob committed on his fork's main: it is his to reconcile, and he is told. | |
| 224 | const bobBefore = ( | |
| 252 | // Bob committed on his fork's main, so alice's push did not carry it | |
| 253 | // along: following is fast-forward only, and a merge is his to ask for. | |
| 254 | const bobTip = ( | |
| 225 | 255 | await gitOk( |
| 226 | 256 | ["-C", repoDir(repos, "bob", "app"), "rev-parse", "main"], |
| 227 | 257 | work, |
| 228 | 258 | ) |
| 229 | 259 | ).trim(); |
| 230 | const refused = await fetch(`${base}/api/v1/repos/bob/app/sync`, { | |
| 231 | method: "POST", | |
| 232 | headers: as(bobToken), | |
| 233 | body: "{}", | |
| 234 | }); | |
| 235 | expect(refused.status).toBe(409); | |
| 236 | expect(((await refused.json()) as { error: string }).error).toBe( | |
| 237 | "diverged", | |
| 238 | ); | |
| 260 | expect(bobTip).not.toBe(aliceTip); | |
| 239 | 261 | expect( |
| 240 | ( | |
| 241 | await gitOk( | |
| 242 | ["-C", repoDir(repos, "bob", "app"), "rev-parse", "main"], | |
| 243 | work, | |
| 244 | ) | |
| 245 | ).trim(), | |
| 246 | ).toBe(bobBefore); | |
| 262 | await gitOk( | |
| 263 | ["-C", repoDir(repos, "bob", "app"), "log", "--oneline", "main"], | |
| 264 | work, | |
| 265 | ), | |
| 266 | ).not.toContain("alice moves on"); | |
| 247 | 267 | }); |
| 248 | 268 | |
| 249 | 269 | test("the About rail says how far a fork is from its parent", async () => { |
| @@ -282,36 +302,157 @@ describe("forks", () => { | ||
| 282 | 302 | expect(current).toContain("up to date"); |
| 283 | 303 | expect(current).not.toContain("Update from parent"); |
| 284 | 304 | |
| 285 | // Alice moves on with nothing following her, which is how a fork ends up | |
| 286 | // behind: the page counts the commit it is missing and offers the update. | |
| 287 | unfollow(); | |
| 305 | // Alice moves on. A live push carries dan's untouched fork with it, so to | |
| 306 | // see the state a fork reaches when its parent moved while nothing was | |
| 307 | // listening, his copy is wound back to where it stood — which is what the | |
| 308 | // page counts, and what it offers the update for. | |
| 309 | const danDir = repoDir(join(dataDir, "repos"), "dan", "app"); | |
| 310 | const stood = await gitOk(["-C", danDir, "rev-parse", "main"], work); | |
| 288 | 311 | const dir = join(work, "app"); |
| 289 | 312 | await Bun.write(join(dir, "LATER.md"), "later\n"); |
| 290 | 313 | await gitOk(["add", "."], dir); |
| 291 | 314 | await gitOk(["commit", "-m", "alice again"], dir); |
| 292 | 315 | await gitOk(["push", "origin", "main"], dir); |
| 293 | unfollow = followParents(app); | |
| 316 | const moved = await gitOk( | |
| 317 | ["-C", repoDir(join(dataDir, "repos"), "alice", "app"), "rev-parse", "main"], | |
| 318 | work, | |
| 319 | ); | |
| 320 | const deadline = Date.now() + 5_000; | |
| 321 | while ( | |
| 322 | Date.now() < deadline && | |
| 323 | (await gitOk(["-C", danDir, "rev-parse", "main"], work)) !== moved | |
| 324 | ) | |
| 325 | await Bun.sleep(50); | |
| 326 | await gitOk(["-C", danDir, "update-ref", "refs/heads/main", stood], work); | |
| 294 | 327 | |
| 295 | 328 | const behind = await rail("/dan/app", danCookie); |
| 296 | 329 | expect(behind).toContain("1 commit behind"); |
| 297 | 330 | expect(behind).toContain("Update from parent"); |
| 298 | 331 | |
| 299 | // Bob committed on his fork and alice pushed twice since: an update would | |
| 300 | // be refused, so the page says why instead of offering one. | |
| 332 | // Bob committed on his fork and alice pushed twice since: the update is | |
| 333 | // still offered, and the page says it will arrive as a merge. | |
| 301 | 334 | const diverged = await rail("/bob/app", await signIn("bob")); |
| 302 | 335 | expect(diverged).toContain("1 commit of its own"); |
| 303 | 336 | expect(diverged).toContain("2 behind"); |
| 304 | expect(diverged).not.toContain("Update from parent"); | |
| 337 | expect(diverged).toContain("merges"); | |
| 338 | expect(diverged).toContain("Update from parent"); | |
| 339 | }); | |
| 340 | ||
| 305 | 341 | test("the fork count on the repository page opens the list of forks", async () => { |
| 306 | 342 | const home = await (await fetch(`${base}/alice/app`)).text(); |
| 307 | 343 | expect(home).toContain('href="/alice/app/forks"'); |
| 308 | expect(home).toContain("2 forks"); | |
| 344 | expect(home).toContain("3 forks"); | |
| 309 | 345 | const listed = await (await fetch(`${base}/alice/app/forks`)).text(); |
| 310 | expect(listed).toContain("2 forks"); | |
| 346 | expect(listed).toContain("3 forks"); | |
| 311 | 347 | expect(listed).toContain('href="/bob/app"'); |
| 312 | 348 | expect(listed).toContain('href="/carol/app"'); |
| 313 | 349 | }); |
| 314 | 350 | |
| 351 | test("a diverged fork takes the parent as a merge commit", async () => { | |
| 352 | const repos = join(dataDir, "repos"); | |
| 353 | const erinToken = await mkUser("erin"); | |
| 354 | const fork = await forkOf("erin", erinToken); | |
| 355 | ||
| 356 | // Erin commits on her fork, then alice moves on: neither tip is an | |
| 357 | // ancestor of the other, so following left the fork where it was. | |
| 358 | await commitTo(fork, "erin.txt", "erin's work\n", "erin work"); | |
| 359 | const asErin = { | |
| 360 | authorization: `Bearer ${erinToken}`, | |
| 361 | "content-type": "application/json", | |
| 362 | }; | |
| 363 | // Only ahead, for the moment: nothing to take, and no merge commit made. | |
| 364 | const level = await fetch(`${base}/api/v1/repos/erin/app/sync`, { | |
| 365 | method: "POST", | |
| 366 | headers: asErin, | |
| 367 | body: "{}", | |
| 368 | }); | |
| 369 | expect(level.status).toBe(200); | |
| 370 | expect(((await level.json()) as { updated: boolean }).updated).toBe(false); | |
| 371 | ||
| 372 | await commitTo(join(work, "app"), "SHIP.md", "shipping\n", "alice ships"); | |
| 373 | const forkDir = repoDir(repos, "erin", "app"); | |
| 374 | const before = await gitOk(["-C", forkDir, "rev-parse", "main"], work); | |
| 375 | const aliceTip = await gitOk( | |
| 376 | ["-C", repoDir(repos, "alice", "app"), "rev-parse", "main"], | |
| 377 | work, | |
| 378 | ); | |
| 379 | expect(before).not.toBe(aliceTip); | |
| 380 | ||
| 381 | const res = await fetch(`${base}/api/v1/repos/erin/app/sync`, { | |
| 382 | method: "POST", | |
| 383 | headers: asErin, | |
| 384 | body: "{}", | |
| 385 | }); | |
| 386 | expect(res.status).toBe(200); | |
| 387 | const synced = (await res.json()) as { | |
| 388 | updated: boolean; | |
| 389 | from: string; | |
| 390 | to: string; | |
| 391 | }; | |
| 392 | expect(synced.updated).toBe(true); | |
| 393 | expect(synced.from).toBe(before); | |
| 394 | ||
| 395 | // The tip is a merge of both sides, made by the person who asked… | |
| 396 | const tip = await gitOk(["-C", forkDir, "rev-parse", "main"], work); | |
| 397 | expect(tip).toBe(synced.to); | |
| 398 | expect( | |
| 399 | (await gitOk(["-C", forkDir, "rev-list", "--parents", "-n1", tip], work)) | |
| 400 | .split(" ") | |
| 401 | .slice(1), | |
| 402 | ).toEqual([before, aliceTip]); | |
| 403 | expect( | |
| 404 | await gitOk(["-C", forkDir, "log", "-1", "--format=%an <%ae>%n%s"], work), | |
| 405 | ).toContain("erin <[email protected]>"); | |
| 406 | // …carrying both trees, and nothing of alice's is missing any more. | |
| 407 | const files = await gitOk( | |
| 408 | ["-C", forkDir, "ls-tree", "--name-only", tip], | |
| 409 | work, | |
| 410 | ); | |
| 411 | expect(files).toContain("erin.txt"); | |
| 412 | expect(files).toContain("SHIP.md"); | |
| 413 | expect( | |
| 414 | await gitCode( | |
| 415 | ["-C", forkDir, "merge-base", "--is-ancestor", aliceTip, tip], | |
| 416 | work, | |
| 417 | ), | |
| 418 | ).toBe(0); | |
| 419 | }); | |
| 420 | ||
| 421 | test("a fork that would conflict is refused and left untouched", async () => { | |
| 422 | const repos = join(dataDir, "repos"); | |
| 423 | const frankToken = await mkUser("frank"); | |
| 424 | const fork = await forkOf("frank", frankToken); | |
| 425 | ||
| 426 | // Both sides rewrite the same line of the same file. | |
| 427 | await commitTo(fork, "README.md", "# frank's\n", "frank edits the readme"); | |
| 428 | await commitTo( | |
| 429 | join(work, "app"), | |
| 430 | "README.md", | |
| 431 | "# alice's\n", | |
| 432 | "alice edits the readme", | |
| 433 | ); | |
| 434 | const forkDir = repoDir(repos, "frank", "app"); | |
| 435 | const before = await gitOk(["-C", forkDir, "rev-parse", "main"], work); | |
| 436 | ||
| 437 | const res = await fetch(`${base}/api/v1/repos/frank/app/sync`, { | |
| 438 | method: "POST", | |
| 439 | headers: { | |
| 440 | authorization: `Bearer ${frankToken}`, | |
| 441 | "content-type": "application/json", | |
| 442 | }, | |
| 443 | body: "{}", | |
| 444 | }); | |
| 445 | expect(res.status).toBe(409); | |
| 446 | expect(((await res.json()) as { error: string }).error).toBe("conflict"); | |
| 447 | // Nothing was written: the branch, and the file on it, are as they were. | |
| 448 | expect(await gitOk(["-C", forkDir, "rev-parse", "main"], work)).toBe( | |
| 449 | before, | |
| 450 | ); | |
| 451 | expect(await gitOk(["-C", forkDir, "show", "main:README.md"], work)).toBe( | |
| 452 | "# frank's", | |
| 453 | ); | |
| 454 | }); | |
| 455 | ||
| 315 | 456 | test("a stranger cannot fork a private repo", async () => { |
| 316 | 457 | await fetch(`${base}/api/v1/repos`, { |
| 317 | 458 | method: "POST", |
new above
offline