Skip to content

Commit

Permalink
Catch errors from git being absent and continue on as best possible (#…
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
ajaymathur authored and Noviny committed Oct 4, 2019
1 parent f63b652 commit 94de7c1
Show file tree
Hide file tree
Showing 9 changed files with 240 additions and 8,677 deletions.
5 changes: 5 additions & 0 deletions .changeset/serious-snakes-reply.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@changesets/errors': minor
---

Create package with `GitError`
5 changes: 5 additions & 0 deletions .changeset/soft-teachers-help.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@changesets/cli': patch
---

Catch errors from git being absent and continue on as best possible
3 changes: 3 additions & 0 deletions packages/errors/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# @changesets/errors

Error classes for Changesets.
9 changes: 9 additions & 0 deletions packages/errors/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "@changesets/errors",
"version": "0.0.0",
"description": "Error classes for @changesets",
"main": "dist/errors.cjs.js",
"module": "dist/errors.esm.js",
"license": "MIT",
"repository": "https://github.com/changesets/changesets/tree/master/packages/errors"
}
16 changes: 16 additions & 0 deletions packages/errors/src/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { GitError } from "./index";

describe("Error classes", () => {
describe("GitError class", () => {
let error: GitError;
beforeEach(() => {
error = new GitError(1, "Operation failed");
});
it("should be an instance of error", () => {
expect(error).toBeInstanceOf(Error);
});
it("should include error message and exit code in error", () => {
expect(error.toString()).toMatch(/(Operation failed).*(1)/);
});
});
});
5 changes: 5 additions & 0 deletions packages/errors/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export class GitError extends Error {
constructor(exitCode: number, message: string) {
super(`Error: Git - ${message}, exit code: ${exitCode}`);
}
}
1 change: 1 addition & 0 deletions packages/git/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"repository": "https://github.com/changesets/changesets/tree/master/packages/git",
"dependencies": {
"@babel/runtime": "^7.4.4",
"@changesets/errors": "^0.0.0",
"@changesets/types": "^0.3.0",
"get-workspaces": "^0.5.0",
"pkg-dir": "^4.1.0",
Expand Down
55 changes: 34 additions & 21 deletions packages/git/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import spawn from "spawndamnit";
import path from "path";
import getWorkspaces from "get-workspaces";
import pkgDir from "pkg-dir";
import { GitError } from "@changesets/errors";
import { Workspace } from "@changesets/types";

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

async function getMasterRef(cwd: string) {
const gitCmd = await spawn("git", ["rev-parse", "master"], { cwd });
if (gitCmd.code !== 0)
throw new GitError(gitCmd.code, gitCmd.stderr.toString());
return gitCmd.stdout
.toString()
.trim()
Expand Down Expand Up @@ -82,25 +85,30 @@ async function getChangedChangesetFilesSinceMaster(
cwd: string,
fullPath = false
): Promise<Array<string>> {
const ref = await getMasterRef(cwd);
// First we need to find the commit where we diverged from `ref` at using `git merge-base`
let cmd = await spawn("git", ["merge-base", ref, "HEAD"], { cwd });
// Now we can find which files we added
cmd = await spawn(
"git",
["diff", "--name-only", "--diff-filter=d", "master"],
{ cwd }
);

let tester = /.changeset\/[^/]+\.md$/;

const files = cmd.stdout
.toString()
.trim()
.split("\n")
.filter(file => tester.test(file));
if (!fullPath) return files;
return files.map(file => path.resolve(cwd, file));
try {
const ref = await getMasterRef(cwd);
// First we need to find the commit where we diverged from `ref` at using `git merge-base`
let cmd = await spawn("git", ["merge-base", ref, "HEAD"], { cwd });
// Now we can find which files we added
cmd = await spawn(
"git",
["diff", "--name-only", "--diff-filter=d", "master"],
{ cwd }
);

let tester = /.changeset\/[^/]+\.md$/;

const files = cmd.stdout
.toString()
.trim()
.split("\n")
.filter(file => tester.test(file));
if (!fullPath) return files;
return files.map(file => path.resolve(cwd, file));
} catch (err) {
if (err instanceof GitError) return [];
throw err;
}
}

async function getChangedPackagesSinceCommit(commitHash: string, cwd: string) {
Expand Down Expand Up @@ -140,8 +148,13 @@ async function getChangedPackagesSinceCommit(commitHash: string, cwd: string) {
//
// Don't use this function in master branch as it returns nothing in that case.
async function getChangedPackagesSinceMaster(cwd: string) {
const masterRef = await getMasterRef(cwd);
return getChangedPackagesSinceCommit(masterRef, cwd);
try {
const masterRef = await getMasterRef(cwd);
return getChangedPackagesSinceCommit(masterRef, cwd);
} catch (err) {
if (err instanceof GitError) return [];
throw err;
}
}

export {
Expand Down

0 comments on commit 94de7c1

Please sign in to comment.