Skip to content

Commit 94de7c1

Browse files
ajaymathurNoviny
authored andcommitted
Catch errors from git being absent and continue on as best possible (#173)
* update to the latest lock file * handle git errors in commit phase * Update serious-snakes-reply.md * Update README.md * Update soft-teachers-help.md * Update package.json
1 parent f63b652 commit 94de7c1

9 files changed

Lines changed: 240 additions & 8677 deletions

File tree

‎.changeset/serious-snakes-reply.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@changesets/errors': minor
3+
---
4+
5+
Create package with `GitError`

‎.changeset/soft-teachers-help.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@changesets/cli': patch
3+
---
4+
5+
Catch errors from git being absent and continue on as best possible

‎packages/errors/README.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# @changesets/errors
2+
3+
Error classes for Changesets.

‎packages/errors/package.json‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "@changesets/errors",
3+
"version": "0.0.0",
4+
"description": "Error classes for @changesets",
5+
"main": "dist/errors.cjs.js",
6+
"module": "dist/errors.esm.js",
7+
"license": "MIT",
8+
"repository": "https://github.com/changesets/changesets/tree/master/packages/errors"
9+
}

‎packages/errors/src/index.test.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { GitError } from "./index";
2+
3+
describe("Error classes", () => {
4+
describe("GitError class", () => {
5+
let error: GitError;
6+
beforeEach(() => {
7+
error = new GitError(1, "Operation failed");
8+
});
9+
it("should be an instance of error", () => {
10+
expect(error).toBeInstanceOf(Error);
11+
});
12+
it("should include error message and exit code in error", () => {
13+
expect(error.toString()).toMatch(/(Operation failed).*(1)/);
14+
});
15+
});
16+
});

‎packages/errors/src/index.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export class GitError extends Error {
2+
constructor(exitCode: number, message: string) {
3+
super(`Error: Git - ${message}, exit code: ${exitCode}`);
4+
}
5+
}

‎packages/git/package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"repository": "https://github.com/changesets/changesets/tree/master/packages/git",
99
"dependencies": {
1010
"@babel/runtime": "^7.4.4",
11+
"@changesets/errors": "^0.0.0",
1112
"@changesets/types": "^0.3.0",
1213
"get-workspaces": "^0.5.0",
1314
"pkg-dir": "^4.1.0",

‎packages/git/src/index.ts‎

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import spawn from "spawndamnit";
22
import path from "path";
33
import getWorkspaces from "get-workspaces";
44
import pkgDir from "pkg-dir";
5+
import { GitError } from "@changesets/errors";
56
import { Workspace } from "@changesets/types";
67

78
// TODO: Currently getting this working, need to decide how to
@@ -16,6 +17,8 @@ async function getProjectDirectory(cwd: string) {
1617

1718
async function getMasterRef(cwd: string) {
1819
const gitCmd = await spawn("git", ["rev-parse", "master"], { cwd });
20+
if (gitCmd.code !== 0)
21+
throw new GitError(gitCmd.code, gitCmd.stderr.toString());
1922
return gitCmd.stdout
2023
.toString()
2124
.trim()
@@ -82,25 +85,30 @@ async function getChangedChangesetFilesSinceMaster(
8285
cwd: string,
8386
fullPath = false
8487
): Promise<Array<string>> {
85-
const ref = await getMasterRef(cwd);
86-
// First we need to find the commit where we diverged from `ref` at using `git merge-base`
87-
let cmd = await spawn("git", ["merge-base", ref, "HEAD"], { cwd });
88-
// Now we can find which files we added
89-
cmd = await spawn(
90-
"git",
91-
["diff", "--name-only", "--diff-filter=d", "master"],
92-
{ cwd }
93-
);
94-
95-
let tester = /.changeset\/[^/]+\.md$/;
96-
97-
const files = cmd.stdout
98-
.toString()
99-
.trim()
100-
.split("\n")
101-
.filter(file => tester.test(file));
102-
if (!fullPath) return files;
103-
return files.map(file => path.resolve(cwd, file));
88+
try {
89+
const ref = await getMasterRef(cwd);
90+
// First we need to find the commit where we diverged from `ref` at using `git merge-base`
91+
let cmd = await spawn("git", ["merge-base", ref, "HEAD"], { cwd });
92+
// Now we can find which files we added
93+
cmd = await spawn(
94+
"git",
95+
["diff", "--name-only", "--diff-filter=d", "master"],
96+
{ cwd }
97+
);
98+
99+
let tester = /.changeset\/[^/]+\.md$/;
100+
101+
const files = cmd.stdout
102+
.toString()
103+
.trim()
104+
.split("\n")
105+
.filter(file => tester.test(file));
106+
if (!fullPath) return files;
107+
return files.map(file => path.resolve(cwd, file));
108+
} catch (err) {
109+
if (err instanceof GitError) return [];
110+
throw err;
111+
}
104112
}
105113

106114
async function getChangedPackagesSinceCommit(commitHash: string, cwd: string) {
@@ -140,8 +148,13 @@ async function getChangedPackagesSinceCommit(commitHash: string, cwd: string) {
140148
//
141149
// Don't use this function in master branch as it returns nothing in that case.
142150
async function getChangedPackagesSinceMaster(cwd: string) {
143-
const masterRef = await getMasterRef(cwd);
144-
return getChangedPackagesSinceCommit(masterRef, cwd);
151+
try {
152+
const masterRef = await getMasterRef(cwd);
153+
return getChangedPackagesSinceCommit(masterRef, cwd);
154+
} catch (err) {
155+
if (err instanceof GitError) return [];
156+
throw err;
157+
}
145158
}
146159

147160
export {

0 commit comments

Comments
 (0)