mirror of
https://github.com/atom/atom.git
synced 2026-02-02 18:55:17 -05:00
Added getDiffStats.
This commit is contained in:
@@ -436,4 +436,21 @@ describe('GitRepositoryAsync', () => {
|
||||
expect(behind).toBe(0)
|
||||
})
|
||||
})
|
||||
|
||||
describe('.getDiffStats(path)', () => {
|
||||
let workingDirectory
|
||||
beforeEach(() => {
|
||||
workingDirectory = copyRepository()
|
||||
repo = GitRepositoryAsync.open(workingDirectory)
|
||||
})
|
||||
|
||||
it('returns the diff stat', async () => {
|
||||
const filePath = path.join(workingDirectory, 'a.txt')
|
||||
fs.writeFileSync(filePath, 'change')
|
||||
|
||||
const {added, deleted} = await repo.getDiffStats('a.txt')
|
||||
expect(added).toBe(1)
|
||||
expect(deleted).toBe(0)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -391,6 +391,44 @@ export default class GitRepositoryAsync {
|
||||
return (statusBit & deletedStatusFlags) > 0
|
||||
}
|
||||
|
||||
// Retrieving Diffs
|
||||
// ================
|
||||
// Public: Retrieves the number of lines added and removed to a path.
|
||||
//
|
||||
// This compares the working directory contents of the path to the `HEAD`
|
||||
// version.
|
||||
//
|
||||
// * `path` The {String} path to check.
|
||||
//
|
||||
// Returns a {Promise} which resolves to an {Object} with the following keys:
|
||||
// * `added` The {Number} of added lines.
|
||||
// * `deleted` The {Number} of deleted lines.
|
||||
getDiffStats (_path) {
|
||||
return this.repoPromise
|
||||
.then(repo => Promise.all([repo, repo.getHeadCommit()]))
|
||||
.then(([repo, headCommit]) => Promise.all([repo, headCommit.getTree()]))
|
||||
.then(([repo, tree]) => {
|
||||
const options = new Git.DiffOptions()
|
||||
options.pathspec = _path
|
||||
return Git.Diff.treeToWorkdir(repo, tree, options)
|
||||
})
|
||||
.then(diff => diff.patches()) // :: Array<Patch>
|
||||
.then(patches => Promise.all(patches.map(p => p.hunks()))) // :: Array<Array<Hunk>>
|
||||
.then(hunks => Promise.all(_.flatten(hunks).map(h => h.lines()))) // :: Array<Array<Line>>
|
||||
.then(lines => {
|
||||
const stats = {added: 0, deleted: 0}
|
||||
for (const line of _.flatten(lines)) {
|
||||
const origin = line.origin()
|
||||
if (origin === Git.Diff.LINE.ADDITION) {
|
||||
stats.added++
|
||||
} else if (origin === Git.Diff.LINE.DELETION) {
|
||||
stats.deleted++
|
||||
}
|
||||
}
|
||||
return stats
|
||||
})
|
||||
}
|
||||
|
||||
// Checking Out
|
||||
// ============
|
||||
|
||||
|
||||
Reference in New Issue
Block a user