|
| 1 | +import { mkdtemp, rm, writeFile } from "fs/promises"; |
| 2 | +import * as path from "path"; |
| 3 | +import * as os from "os"; |
| 4 | +import { execSync } from "child_process"; |
| 5 | + |
| 6 | +import { GIT_FETCH_SCRIPT } from "./gitStatus"; |
| 7 | + |
| 8 | +describe("GIT_FETCH_SCRIPT", () => { |
| 9 | + test("fetches when remote ref moves to a commit already present locally", async () => { |
| 10 | + const tempDir = await mkdtemp(path.join(os.tmpdir(), "mux-git-fetch-")); |
| 11 | + const originDir = path.join(tempDir, "origin.git"); |
| 12 | + const workspaceDir = path.join(tempDir, "workspace"); |
| 13 | + |
| 14 | + const run = (cmd: string, cwd?: string) => |
| 15 | + execSync(cmd, { cwd, stdio: "pipe" }).toString().trim(); |
| 16 | + |
| 17 | + try { |
| 18 | + // Initialize bare remote and clone it |
| 19 | + run(`git init --bare ${originDir}`); |
| 20 | + run(`git clone ${originDir} ${workspaceDir}`); |
| 21 | + |
| 22 | + // Basic git identity configuration |
| 23 | + run('git config user.email "test@example.com"', workspaceDir); |
| 24 | + run('git config user.name "Test User"', workspaceDir); |
| 25 | + run("git config commit.gpgsign false", workspaceDir); |
| 26 | + |
| 27 | + // Seed main with an initial commit |
| 28 | + await writeFile(path.join(workspaceDir, "README.md"), "init\n"); |
| 29 | + run("git add README.md", workspaceDir); |
| 30 | + run('git commit -m "init"', workspaceDir); |
| 31 | + run("git branch -M main", workspaceDir); |
| 32 | + run("git push -u origin main", workspaceDir); |
| 33 | + |
| 34 | + // Ensure remote HEAD points to main for deterministic primary branch detection |
| 35 | + run("git symbolic-ref HEAD refs/heads/main", originDir); |
| 36 | + |
| 37 | + // Create a commit on a feature branch (object exists locally) |
| 38 | + run("git checkout -b feature", workspaceDir); |
| 39 | + await writeFile(path.join(workspaceDir, "feature.txt"), "feature\n"); |
| 40 | + run("git add feature.txt", workspaceDir); |
| 41 | + run('git commit -m "feature"', workspaceDir); |
| 42 | + const featureSha = run("git rev-parse feature", workspaceDir); |
| 43 | + |
| 44 | + // Push the feature branch so the remote has the object but main stays old |
| 45 | + run("git push origin feature", workspaceDir); |
| 46 | + |
| 47 | + // Move remote main to the feature commit without updating local tracking ref |
| 48 | + run(`git update-ref refs/heads/main ${featureSha}`, originDir); |
| 49 | + |
| 50 | + const localBefore = run("git rev-parse origin/main", workspaceDir); |
| 51 | + expect(localBefore).not.toBe(featureSha); |
| 52 | + |
| 53 | + // Run the optimized fetch script (should update origin/main) |
| 54 | + run(GIT_FETCH_SCRIPT, workspaceDir); |
| 55 | + |
| 56 | + const localAfter = run("git rev-parse origin/main", workspaceDir); |
| 57 | + expect(localAfter).toBe(featureSha); |
| 58 | + } finally { |
| 59 | + await rm(tempDir, { recursive: true, force: true }); |
| 60 | + } |
| 61 | + }, 20000); |
| 62 | +}); |
0 commit comments