-
Notifications
You must be signed in to change notification settings - Fork 736
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: support all git versions for
wrangler init
(#1265)
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
1 parent
9074990
commit e322475
Showing
3 changed files
with
60 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,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 |
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,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, | ||
}); | ||
} | ||
} |
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