Skip to content

Commit

Permalink
fix: support all git versions for wrangler init (#1265)
Browse files Browse the repository at this point in the history
If `git` does not support the `--initial-branch` argument then just fallback to the default initial branch name.

We tried to be more clever about this but there are two many weird corner cases with different git versions on different architectures.
Now we do our best, with recent versions of git, to ensure that the branch is called `main` but otherwise just make sure we don't crash.

Fixes #1228
  • Loading branch information
petebacondarwin authored Jun 20, 2022
1 parent 9074990 commit e322475
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 17 deletions.
12 changes: 12 additions & 0 deletions .changeset/eighty-bags-destroy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
"wrangler": patch
---

fix: support all git versions for `wrangler init`

If `git` does not support the `--initial-branch` argument then just fallback to the default initial branch name.

We tried to be more clever about this but there are two many weird corner cases with different git versions on different architectures.
Now we do our best, with recent versions of git, to ensure that the branch is called `main` but otherwise just make sure we don't crash.

Fixes #1228
42 changes: 42 additions & 0 deletions packages/wrangler/src/git-client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { execa } from "execa";
import { findUp } from "find-up";

/**
* Check whether the given current working directory is within a git repository
* by looking for a `.git` directory in this or an ancestor directory.
*/
export async function isInsideGitRepo(cwd: string) {
const res = await findUp(".git", { cwd, type: "directory" });
return res !== undefined;
}

/**
* Check whether git is installed by trying to run it.
*/
export async function isGitInstalled() {
try {
return (await execa("git", ["--version"])).exitCode === 0;
} catch (err) {
return false;
}
}

/**
* Initialize a new Worker project with a git repository.
*
* We want the branch to be called `main` but earlier versions of git do not support `--initial-branch`.
* If that is the case then we just fallback to the default initial branch name.
*/
export async function initializeGit(cwd: string) {
try {
// Try to create the repository with the HEAD branch of `main`.
await execa("git", ["init", "--initial-branch", "main"], {
cwd,
});
} catch {
// Unable to create the repo with a HEAD branch name, so just fall back to the default.
await execa("git", ["init"], {
cwd,
});
}
}
23 changes: 6 additions & 17 deletions packages/wrangler/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { setTimeout } from "node:timers/promises";
import TOML from "@iarna/toml";
import chalk from "chalk";
import { watch } from "chokidar";
import { execa } from "execa";
import { findUp } from "find-up";
import getPort from "get-port";
import { render } from "ink";
Expand All @@ -23,6 +22,7 @@ import { getVarsForDev } from "./dev/dev-vars";
import { confirm, prompt, select } from "./dialogs";
import { getEntry } from "./entry";
import { DeprecationError } from "./errors";
import { initializeGit, isGitInstalled, isInsideGitRepo } from "./git-client";
import {
getKVNamespaceId,
listKVNamespaces,
Expand Down Expand Up @@ -433,26 +433,15 @@ function createCLIParser(argv: string[]) {

const yesFlag = args.yes ?? false;

const isInsideGitProject = Boolean(
await findUp(".git", { cwd: creationDirectory, type: "directory" })
);
let isGitInstalled;
try {
isGitInstalled = (await execa("git", ["--version"])).exitCode === 0;
} catch (err) {
isGitInstalled = false;
}
if (!isInsideGitProject && isGitInstalled) {
if (
!(await isInsideGitRepo(creationDirectory)) &&
(await isGitInstalled())
) {
const shouldInitGit =
yesFlag ||
(await confirm("Would you like to use git to manage this Worker?"));
if (shouldInitGit) {
await execa("git", ["init"], {
cwd: creationDirectory,
});
await execa("git", ["branch", "-m", "main"], {
cwd: creationDirectory,
});
await initializeGit(creationDirectory);
await writeFile(
path.join(creationDirectory, ".gitignore"),
readFileSync(path.join(__dirname, "../templates/gitignore"))
Expand Down

0 comments on commit e322475

Please sign in to comment.