Skip to content

Commit

Permalink
fix: ensure git branch rename works on all git versions for `wrangler…
Browse files Browse the repository at this point in the history
… init`

Sometimes git is unable to rename a branch if it has no commits on it. So we now create an empty commit just before renaming.

Fixes #1228
  • Loading branch information
petebacondarwin committed Jun 15, 2022
1 parent d8ee04f commit b377829
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 17 deletions.
9 changes: 9 additions & 0 deletions .changeset/eighty-bags-destroy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"wrangler": patch
---

fix: ensure git branch rename works on all git versions for `wrangler init`

Sometimes git is unable to rename a branch if it has no commits on it. So we now create an empty commit just before renaming.

Fixes #1228
3 changes: 3 additions & 0 deletions packages/wrangler/src/__tests__/init.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,9 @@ describe("init", () => {
expect((await execa("git", ["branch", "--show-current"])).stdout).toEqual(
"main"
);
expect((await execa("git", ["log", "--oneline"])).stdout).toContain(
"Initial commit."
);
});

it("should not offer to initialize a git repo if it's already inside one", async () => {
Expand Down
53 changes: 53 additions & 0 deletions packages/wrangler/src/git-client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
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 to support earlier versions of git we cannot use
* `git init --initial-branch`.
* Instead, we rename the branch to `main` using `git branch -m`.
* But this can fail if there are no commits on the current branch so we create an artificial
* empty commit to guard against that.
*/
export async function initializeGit(cwd: string) {
await execa("git", ["init"], {
cwd,
});
await execa(
"git",
[
"commit",
"-m",
"Initial commit.",
"--allow-empty",
"--author='Wrangler <[email protected]>`",
],
{
cwd,
}
);
await execa("git", ["branch", "-m", "main"], {
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 b377829

Please sign in to comment.