({
fs: _fs,
dir,
gitdir = join(dir, '.git'),
filepath,
ref,
cache = {},
})
| 29 | * |
| 30 | */ |
| 31 | export async function resetIndex({ |
| 32 | fs: _fs, |
| 33 | dir, |
| 34 | gitdir = join(dir, '.git'), |
| 35 | filepath, |
| 36 | ref, |
| 37 | cache = {}, |
| 38 | }) { |
| 39 | try { |
| 40 | assertParameter('fs', _fs) |
| 41 | assertParameter('gitdir', gitdir) |
| 42 | assertParameter('filepath', filepath) |
| 43 | |
| 44 | const fs = new FileSystem(_fs) |
| 45 | const updatedGitdir = await discoverGitdir({ fsp: fs, dotgit: gitdir }) |
| 46 | |
| 47 | let oid |
| 48 | let workdirOid |
| 49 | |
| 50 | try { |
| 51 | // Resolve commit |
| 52 | oid = await GitRefManager.resolve({ |
| 53 | fs, |
| 54 | gitdir: updatedGitdir, |
| 55 | ref: ref || 'HEAD', |
| 56 | }) |
| 57 | } catch (e) { |
| 58 | if (ref) { |
| 59 | // Only throw the error if a ref is explicitly provided |
| 60 | throw e |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | // Not having an oid at this point means `resetIndex()` was called without explicit `ref` on a new git |
| 65 | // repository. If that happens, we can skip resolving the file path. |
| 66 | if (oid) { |
| 67 | try { |
| 68 | // Resolve blob |
| 69 | oid = await resolveFilepath({ |
| 70 | fs, |
| 71 | cache, |
| 72 | gitdir: updatedGitdir, |
| 73 | oid, |
| 74 | filepath, |
| 75 | }) |
| 76 | } catch (e) { |
| 77 | // This means we're resetting the file to a "deleted" state |
| 78 | oid = null |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | // For files that aren't in the workdir use zeros |
| 83 | let stats = { |
| 84 | ctime: new Date(0), |
| 85 | mtime: new Date(0), |
| 86 | dev: 0, |
| 87 | ino: 0, |
| 88 | mode: 0, |
no test coverage detected
searching dependent graphs…