Skip to content

Commit 6352c41

Browse files
committed
[Breaking] Remove RepositoryDoesNotExist and GitNotFound errors
Instead of synchronously stat'ing the path on ENOENT when it's possible that the caller doesn't care we'll leave it up to callers to choose whether to try to detect why the exec failed. Note that the pathExist approach was subject to race conditions and that there's no guarantee you'd ever be able to figure out exactly why the call failed
1 parent 808a682 commit 6352c41

File tree

5 files changed

+21
-58
lines changed

5 files changed

+21
-58
lines changed

docs/overview.md

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,11 @@
22

33
`dugite` is a wrapper on top of the Git command line interface, with some helpers to make it easy to consume in your Node applications. The important parts:
44

5-
- `GitProcess` - the core of the library - `GitProcess.exec` is how you
6-
interact with Git
7-
- `IGitResult` - the abstraction for a result returned by Git - contains
8-
exit code and standard output/error text
9-
- `IGitExecutionOptions` - additional overrides to change the behaviour
10-
of `GitProcess` (see [API extensibility](./api-extensibility.md) for
11-
more information)
12-
- `GitError` - a collection of known error codes that `dugite` can understand
13-
- `GitNotFoundErrorCode` and `RepositoryDoesNotExistErrorCode` - error codes
14-
that `dugite` will raise for exceptional scenarios when invoking Git - you
15-
should check for these in your code.
5+
- `GitProcess` - the core of the library - `GitProcess.exec` is how you
6+
interact with Git
7+
- `IGitResult` - the abstraction for a result returned by Git - contains
8+
exit code and standard output/error text
9+
- `IGitExecutionOptions` - additional overrides to change the behaviour
10+
of `GitProcess` (see [API extensibility](./api-extensibility.md) for
11+
more information)
12+
- `GitError` - a collection of known error codes that `dugite` can understand

lib/errors.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -169,15 +169,6 @@ export const GitErrorRegexes: { [regexp: string]: GitError } = {
169169
GitError.PathExistsButNotInRef,
170170
}
171171

172-
/**
173-
* The error code for when git cannot be found. This most likely indicates a
174-
* problem with dugite itself.
175-
*/
176-
export const GitNotFoundErrorCode = 'git-not-found-error'
177-
178-
/** The error code for when the path to a repository doesn't exist. */
179-
export const RepositoryDoesNotExistErrorCode = 'repository-does-not-exist-error'
180-
181172
export class ExecError extends Error {
182173
constructor(
183174
public readonly message: string,

lib/git-process.ts

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
1-
import * as fs from 'fs'
21
import { execFile, ExecFileOptions, spawn } from 'child_process'
3-
import {
4-
GitError,
5-
GitErrorRegexes,
6-
RepositoryDoesNotExistErrorCode,
7-
GitNotFoundErrorCode,
8-
ExecError,
9-
} from './errors'
2+
import { GitError, GitErrorRegexes, ExecError } from './errors'
103
import { ChildProcess } from 'child_process'
114

125
import { setupEnvironment } from './git-environment'
@@ -133,15 +126,6 @@ interface ErrorWithCode extends Error {
133126
}
134127

135128
export class GitProcess {
136-
private static pathExists(path: string): Boolean {
137-
try {
138-
fs.accessSync(path, (fs as any).F_OK)
139-
return true
140-
} catch {
141-
return false
142-
}
143-
}
144-
145129
/**
146130
* Execute a command and interact with the process outputs directly.
147131
*
@@ -235,19 +219,18 @@ export class GitProcess {
235219
// If the error's code is a string then it means the code isn't the
236220
// process's exit code but rather an error coming from Node's bowels,
237221
// e.g., ENOENT.
238-
let { message, code } = err
222+
let { message } = err
239223

240224
if (err.code === 'ENOENT') {
241-
if (GitProcess.pathExists(path) === false) {
242-
message = 'Unable to find path to repository on disk.'
243-
code = RepositoryDoesNotExistErrorCode
244-
} else {
245-
message = `Git could not be found at the expected path: '${gitLocation}'. This might be a problem with how the application is packaged, so confirm this folder hasn't been removed when packaging.`
246-
code = GitNotFoundErrorCode
247-
}
225+
message =
226+
`ENOENT: Git failed to execute. This typically means that ` +
227+
`the path provided doesn't exist or that the Git executable ` +
228+
`could not be found which could indicate a problem with the ` +
229+
`packaging of dugite. Verify that resolveGitBinary returns a ` +
230+
`valid path to the git binary.`
248231
}
249232

250-
reject(new ExecError(message, code, stdout, stderr, err))
233+
reject(new ExecError(message, err.code, stdout, stderr, err))
251234
}
252235
)
253236

lib/index.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
11
export { GitProcess, IGitResult, IGitExecutionOptions } from './git-process'
2-
export {
3-
GitError,
4-
RepositoryDoesNotExistErrorCode,
5-
GitNotFoundErrorCode,
6-
} from './errors'
2+
export { GitError } from './errors'
73
export * from './git-environment'

test/fast/git-process-test.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,7 @@ import * as path from 'path'
22
import * as fs from 'fs'
33
import * as crypto from 'crypto'
44

5-
import {
6-
GitProcess,
7-
GitError,
8-
RepositoryDoesNotExistErrorCode,
9-
} from '../../lib'
5+
import { GitProcess, GitError } from '../../lib'
106
import { ExecError, GitErrorRegexes } from '../../lib/errors'
117
import {
128
initialize,
@@ -289,8 +285,8 @@ describe('git-process', () => {
289285
error = e as Error
290286
}
291287

292-
expect(error!.message).toBe('Unable to find path to repository on disk.')
293-
expect((error as any).code).toBe(RepositoryDoesNotExistErrorCode)
288+
expect(error!.message).toContain('Git failed to execute.')
289+
expect((error as any).code).toBe('ENOENT')
294290
})
295291

296292
it('can parse HTTPS auth errors', () => {

0 commit comments

Comments
 (0)