Skip to content

Commit b4aa626

Browse files
authored
fix: Don't delete existing .git when using --no-git flag (#12968)
## Why Closes #12967. Users who pass `--no-git` expect create-turbo to leave git alone, not initialize a repo or delete an existing `.git` directory. ## What Stops the `--no-git` path from doing any git work and removes the now-unused `.git` deletion helper. ## How Adds coverage that `--no-git` does not call the git init helper or spawn git commands, while preserving normal git initialization when the flag is not used. Verified with: - `pnpm --filter create-turbo test -- --runTestsByPath __tests__/git.test.ts __tests__/index.test.ts` - `pnpm --filter create-turbo check-types`
1 parent 7952b46 commit b4aa626

4 files changed

Lines changed: 6 additions & 73 deletions

File tree

packages/create-turbo/__tests__/git.test.ts

Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,7 @@ import type { SpawnSyncReturns } from "node:child_process";
44
import fs from "node:fs";
55
import { setupTestFixtures } from "@turbo/test-utils";
66
import { describe, it, expect, jest } from "@jest/globals";
7-
import {
8-
DEFAULT_IGNORE,
9-
tryGitInit,
10-
removeGitDirectory
11-
} from "../src/utils/git";
7+
import { DEFAULT_IGNORE, tryGitInit } from "../src/utils/git";
128

139
function spawnResult(status: number): SpawnSyncReturns<Buffer> {
1410
return {
@@ -255,41 +251,4 @@ describe("git", () => {
255251
}
256252
});
257253
});
258-
259-
describe("removeGitDirectory", () => {
260-
const { useFixture } = setupTestFixtures({
261-
directory: path.join(__dirname, "../"),
262-
options: { emptyFixture: true }
263-
});
264-
265-
it("attempts to remove .git directory", async () => {
266-
const { root } = useFixture({ fixture: `remove-git` });
267-
const mockRmSync = jest.spyOn(fs, "rmSync").mockReturnValue(undefined);
268-
269-
const result = removeGitDirectory(root);
270-
expect(result).toBe(true);
271-
272-
expect(mockRmSync).toHaveBeenCalledWith(path.join(root, ".git"), {
273-
recursive: true,
274-
force: true
275-
});
276-
mockRmSync.mockRestore();
277-
});
278-
279-
it("returns false on error", async () => {
280-
const { root } = useFixture({ fixture: `remove-git-error` });
281-
const mockRmSync = jest.spyOn(fs, "rmSync").mockImplementation(() => {
282-
throw new Error("Permission denied");
283-
});
284-
285-
const result = removeGitDirectory(root);
286-
expect(result).toBe(false);
287-
288-
expect(mockRmSync).toHaveBeenCalledWith(path.join(root, ".git"), {
289-
recursive: true,
290-
force: true
291-
});
292-
mockRmSync.mockRestore();
293-
});
294-
});
295254
});

packages/create-turbo/__tests__/index.test.ts

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ describe("create-turbo", () => {
292292
mockSpawnSync.mockRestore();
293293
});
294294

295-
it("does not initialize git and removes .git directory when --no-git flag is used", async () => {
295+
it("does not initialize git or remove .git directory when --no-git flag is used", async () => {
296296
const { root } = useFixture({ fixture: "create-turbo-no-git" });
297297
const packageManager = "npm";
298298

@@ -333,13 +333,7 @@ describe("create-turbo", () => {
333333
signal: null
334334
});
335335

336-
const mockTryGitInit = jest
337-
.spyOn(gitUtils, "tryGitInit")
338-
.mockReturnValue(true);
339-
340-
const mockRemoveGitDirectory = jest
341-
.spyOn(gitUtils, "removeGitDirectory")
342-
.mockReturnValue(true);
336+
const mockTryGitInit = jest.spyOn(gitUtils, "tryGitInit");
343337

344338
await create(root as CreateCommandArgument, {
345339
packageManager,
@@ -350,17 +344,16 @@ describe("create-turbo", () => {
350344
});
351345

352346
expect(mockTryGitInit).not.toHaveBeenCalled();
353-
expect(mockRemoveGitDirectory).toHaveBeenCalledWith(root);
347+
expect(mockSpawnSync).not.toHaveBeenCalled();
354348

355349
mockAvailablePackageManagers.mockRestore();
356350
mockCreateProject.mockRestore();
357351
mockGetWorkspaceDetails.mockRestore();
358352
mockSpawnSync.mockRestore();
359353
mockTryGitInit.mockRestore();
360-
mockRemoveGitDirectory.mockRestore();
361354
});
362355

363-
it("initializes git and does not remove .git directory when --no-git flag is not used", async () => {
356+
it("initializes git when --no-git flag is not used", async () => {
364357
const { root } = useFixture({ fixture: "create-turbo-with-git" });
365358
const packageManager = "npm";
366359

@@ -405,10 +398,6 @@ describe("create-turbo", () => {
405398
.spyOn(gitUtils, "tryGitInit")
406399
.mockReturnValue(true);
407400

408-
const mockRemoveGitDirectory = jest
409-
.spyOn(gitUtils, "removeGitDirectory")
410-
.mockReturnValue(true);
411-
412401
await create(root as CreateCommandArgument, {
413402
packageManager,
414403
skipInstall: true,
@@ -418,13 +407,11 @@ describe("create-turbo", () => {
418407
});
419408

420409
expect(mockTryGitInit).toHaveBeenCalledWith(root);
421-
expect(mockRemoveGitDirectory).not.toHaveBeenCalled();
422410

423411
mockAvailablePackageManagers.mockRestore();
424412
mockCreateProject.mockRestore();
425413
mockGetWorkspaceDetails.mockRestore();
426414
mockSpawnSync.mockRestore();
427415
mockTryGitInit.mockRestore();
428-
mockRemoveGitDirectory.mockRestore();
429416
});
430417
});

packages/create-turbo/src/commands/create/index.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
DownloadError,
1414
logger
1515
} from "@turbo/utils";
16-
import { tryGitInit, removeGitDirectory } from "../../utils/git";
16+
import { tryGitInit } from "../../utils/git";
1717
import { transforms } from "../../transforms";
1818
import { TransformError } from "../../transforms/errors";
1919
import { isDefaultExample } from "../../utils/is-default-example";
@@ -296,10 +296,6 @@ export async function create(
296296
if (tryGitInit(root)) {
297297
info("Initialized a git repository.");
298298
}
299-
} else {
300-
// User explicitly doesn't want git - remove any .git directory
301-
// (e.g., if the example template came with one)
302-
removeGitDirectory(root);
303299
}
304300

305301
opts.telemetry?.trackCommandStatus({ command: "create", status: "end" });

packages/create-turbo/src/utils/git.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,3 @@ export function tryGitInit(root: string): boolean {
108108
return false;
109109
}
110110
}
111-
112-
export function removeGitDirectory(root: string): boolean {
113-
try {
114-
rmSync(path.join(root, ".git"), { recursive: true, force: true });
115-
return true;
116-
} catch (_) {
117-
return false;
118-
}
119-
}

0 commit comments

Comments
 (0)