-
-
Notifications
You must be signed in to change notification settings - Fork 320
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for
git count-objects
as git.countObjects
- Loading branch information
Showing
9 changed files
with
141 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'simple-git': minor | ||
--- | ||
|
||
Add support for parsing `count-objects` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import type { SimpleGitApi } from '../simple-git-api'; | ||
import type { SimpleGit } from '../../../typings'; | ||
import { asCamelCase, asNumber, LineParser, parseStringResponse } from '../utils'; | ||
|
||
export interface CountObjectsResult { | ||
count: number; | ||
size: number; | ||
inPack: number; | ||
packs: number; | ||
sizePack: number; | ||
prunePackable: number; | ||
garbage: number; | ||
sizeGarbage: number; | ||
} | ||
|
||
function countObjectsResponse(): CountObjectsResult { | ||
return { | ||
count: 0, | ||
garbage: 0, | ||
inPack: 0, | ||
packs: 0, | ||
prunePackable: 0, | ||
size: 0, | ||
sizeGarbage: 0, | ||
sizePack: 0, | ||
}; | ||
} | ||
|
||
const parser: LineParser<CountObjectsResult> = new LineParser( | ||
/([a-z-]+): (\d+)$/, | ||
(result, [key, value]) => { | ||
const property = asCamelCase(key); | ||
if (result.hasOwnProperty(property)) { | ||
result[property as keyof typeof result] = asNumber(value); | ||
} | ||
} | ||
); | ||
|
||
export default function (): Pick<SimpleGit, 'countObjects'> { | ||
return { | ||
countObjects(this: SimpleGitApi) { | ||
return this._runTask({ | ||
commands: ['count-objects', '--verbose'], | ||
format: 'utf-8', | ||
parser(stdOut: string) { | ||
return parseStringResponse(countObjectsResponse(), [parser], stdOut); | ||
}, | ||
}); | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { closeWithSuccess, like, newSimpleGit } from './__fixtures__'; | ||
import { CountObjectsResult } from '../../typings'; | ||
|
||
const COUNT_OBJ_RESPONSE = ` | ||
count: 323 | ||
size: 7920 | ||
in-pack: 8134 | ||
packs: 1 | ||
size-pack: 3916 | ||
prune-packable: 0 | ||
garbage: 0 | ||
size-garbage: 0 | ||
`; | ||
|
||
describe('count-objects', () => { | ||
it('gets the repo object counts', async () => { | ||
const task = newSimpleGit().countObjects(); | ||
await closeWithSuccess(COUNT_OBJ_RESPONSE); | ||
const objects = await task; | ||
|
||
expect(objects).toEqual( | ||
like({ | ||
count: 323, | ||
size: 7920, | ||
inPack: 8134, | ||
packs: 1, | ||
sizePack: 3916, | ||
}) | ||
); | ||
}); | ||
|
||
it('ignores unknown properties', async () => { | ||
const task = newSimpleGit().countObjects(); | ||
await closeWithSuccess('foo: 123'); | ||
expect(await task).not.toHaveProperty('foo'); | ||
}); | ||
|
||
it('ignores invalid values', async () => { | ||
const task = newSimpleGit().countObjects(); | ||
await closeWithSuccess('packs: error'); | ||
expect(await task).toHaveProperty('packs', 0); | ||
}); | ||
|
||
it.each<[string, keyof CountObjectsResult, number]>([ | ||
['prune-packable', 'prunePackable', 100], | ||
['garbage', 'garbage', 101], | ||
['size-garbage', 'sizeGarbage', 102], | ||
])('parses %s property', async (key, asKey, value) => { | ||
const task = newSimpleGit().countObjects(); | ||
await closeWithSuccess(`${key}: ${value}`); | ||
|
||
expect(await task).toEqual(like({ [asKey]: value })); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters