-
Notifications
You must be signed in to change notification settings - Fork 790
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
1 parent
d8ee04f
commit b377829
Showing
4 changed files
with
71 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters