You can cancel long-running Git operations (like clone) by passing an AbortSignal in the options object of the exec call. This is achieved using the standard AbortController API.
Manual Cancellation
Create an AbortController and pass its signal to exec. You can then call controller.abort() to stop the process.
Automatic Timeout
Use AbortSignal.timeout(ms) to automatically cancel the operation if it exceeds a specific duration.
Note: When a process is cancelled, the exec promise will reject, and you should handle the error in a try/catch block.
import { exec } from 'dugite'
// Manual cancellation
const controller = new AbortController()
const resultPromise = exec(['clone', 'https://github.com/example/repo'], '/path/to/dir', {
signal: controller.signal,
})
// Cancel if needed
controller.abort()
try {
const result = await resultPromise
} catch (error) {
// Handle cancellation
}
// Automatic timeout cancellation
const result = await exec(['clone', 'https://github.com/example/repo'], '/path/to/dir', {
signal: AbortSignal.timeout(5000), // Auto-cancel after 5 seconds
})