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

create-turbo: automatic git configuration. #4941

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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" });
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is unnecessary; just a bonus place where weird errors can creep in.

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}"`,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hardcodes the automated commits to be from us: Turbobot <turbobot@vercel.com>

{
stdio: "ignore",
}
);
}
Comment on lines +84 to +91
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Factored out into a separate method so you get either the try behavior, or the boolean behavior as you see fit.