Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support all git versions for wrangler init #1265

Merged
merged 1 commit into from
Jun 20, 2022
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
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