Skip to content

Commit

Permalink
create-turbo: automatic git configuration. (#4941)
Browse files Browse the repository at this point in the history
Not all environments where `create-turbo` is run will have a
preconfigured `git` config. This ensures that we always have an author
config for the initial commit.

It also reduces the number of processes executed to reduce the chances
of intermittent failures.

---------

Co-authored-by: Nathan Hammond <Nathan Hammond>
  • Loading branch information
nathanhammond committed May 15, 2023
1 parent 46b73a8 commit 8b1764d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 21 deletions.
26 changes: 14 additions & 12 deletions packages/create-turbo/__tests__/git.test.ts
Expand Up @@ -124,7 +124,6 @@ describe("git", () => {
const { root } = useFixture({ fixture: `git` });
const mockExecSync = jest
.spyOn(childProcess, "execSync")
.mockReturnValueOnce("git version 2.38.1")
.mockImplementationOnce(() => {
throw new Error(
"fatal: not a git repository (or any of the parent directories): .git"
Expand All @@ -139,11 +138,9 @@ describe("git", () => {
expect(result).toBe(true);

const calls = [
"git --version",
"git init",
"git checkout -b main",
"git add -A",
'git commit -m "test commit"',
'git commit --author="Turbobot <turbobot@vercel.com>" -am "test commit"',
];
expect(mockExecSync).toHaveBeenCalledTimes(calls.length + 2);
calls.forEach((call) => {
Expand All @@ -160,16 +157,15 @@ describe("git", () => {
});
const mockExecSync = jest
.spyOn(childProcess, "execSync")
.mockReturnValueOnce("git version 2.38.1")
.mockReturnValueOnce("true")
.mockReturnValue("success");

const result = tryGitInit(root, "test commit");
expect(result).toBe(false);

const calls = ["git --version"];
const calls: string[] = [];

// 1 call for git --version, 1 call for isInGitRepository
// 1 call for isInGitRepository
expect(mockExecSync).toHaveBeenCalledTimes(calls.length + 1);
calls.forEach((call) => {
expect(mockExecSync).toHaveBeenCalledWith(call, {
Expand All @@ -184,13 +180,21 @@ describe("git", () => {
const mockExecSync = jest
.spyOn(childProcess, "execSync")
.mockImplementationOnce(() => {
throw new Error("fatal: unknown command git");
throw new Error(
"fatal: not a git repository (or any of the parent directories): .git"
);
})
.mockImplementationOnce(() => {
throw new Error("abort: no repository found (.hg not found)");
})
.mockImplementationOnce(() => {
throw new Error("fatal: 128");
});

const result = tryGitInit(root, "test commit");
expect(result).toBe(false);

const calls = ["git --version"];
const calls: string[] = [GIT_REPO_COMMAND, HG_REPO_COMMAND, "git init"];

expect(mockExecSync).toHaveBeenCalledTimes(calls.length);
calls.forEach((call) => {
Expand All @@ -205,7 +209,6 @@ describe("git", () => {
const { root } = useFixture({ fixture: `git` });
const mockExecSync = jest
.spyOn(childProcess, "execSync")
.mockReturnValueOnce("git version 2.38.1")
.mockImplementationOnce(() => {
throw new Error(
"fatal: not a git repository (or any of the parent directories): .git"
Expand All @@ -224,10 +227,9 @@ describe("git", () => {
expect(result).toBe(false);

const calls = [
"git --version",
"git init",
"git checkout -b main",
"git add -A",
'git commit --author="Turbobot <turbobot@vercel.com>" -am "test commit"',
];

expect(mockExecSync).toHaveBeenCalledTimes(calls.length + 2);
Expand Down
20 changes: 11 additions & 9 deletions packages/create-turbo/src/utils/git.ts
Expand Up @@ -51,7 +51,6 @@ export function isInMercurialRepository(): boolean {
export function tryGitInit(root: string, message: string): boolean {
let didInit = false;
try {
execSync("git --version", { stdio: "ignore" });
if (isInGitRepository() || isInMercurialRepository()) {
return false;
}
Expand All @@ -61,10 +60,7 @@ export function tryGitInit(root: string, message: string): boolean {

execSync("git checkout -b main", { stdio: "ignore" });

execSync("git add -A", { stdio: "ignore" });
execSync(`git commit -m "${message}"`, {
stdio: "ignore",
});
gitCommit(message);
return true;
} catch (err) {
if (didInit) {
Expand All @@ -78,12 +74,18 @@ export function tryGitInit(root: string, message: string): boolean {

export function tryGitCommit(message: string): boolean {
try {
execSync("git add -A", { stdio: "ignore" });
execSync(`git commit -m "${message}"`, {
stdio: "ignore",
});
gitCommit(message);
return true;
} catch (err) {
return false;
}
}

function gitCommit(message: string) {
execSync(
`git commit --author="Turbobot <turbobot@vercel.com>" -am "${message}"`,
{
stdio: "ignore",
}
);
}

0 comments on commit 8b1764d

Please sign in to comment.