diff --git a/lib/diff-hunk.js b/lib/diff-hunk.js index 0d62245aca..4b8dd91f8c 100644 --- a/lib/diff-hunk.js +++ b/lib/diff-hunk.js @@ -2,6 +2,8 @@ import HunkLine from './hunk-line' +import _ from 'underscore-contrib' + // DiffHunk contains diff information for a single hunk within a single file. It // holds a list of HunkLine objects. export default class DiffHunk { @@ -67,7 +69,7 @@ export default class DiffHunk { return diffHunk } - async fromGitUtilsObject({hunk}) { + async fromGitUtilsObject({hunk, stagedLines}) { if (!hunk) return; this.header = hunk.header() @@ -75,6 +77,16 @@ export default class DiffHunk { for (let line of (await hunk.lines())) { let hunkLine = new HunkLine() hunkLine.fromGitUtilsObject({line: line}) + const staged = Boolean(_.find(stagedLines, line => { + return line.getOldLineNumber() === hunkLine.getOldLineNumber() && + line.getNewLineNumber() === hunkLine.getNewLineNumber() && + line.getLineOrigin() === hunkLine.getLineOrigin() + })) + if (staged) { + console.log('staged:') + console.log(hunkLine.getContent()) + } + hunkLine.setIsStaged(staged) this.lines.push(hunkLine) } } diff --git a/lib/file-diff.js b/lib/file-diff.js index 778c84240a..f12e8fd663 100644 --- a/lib/file-diff.js +++ b/lib/file-diff.js @@ -2,6 +2,7 @@ import path from 'path' import DiffHunk from './diff-hunk' +import HunkLine from './hunk-line' import {createObjectsFromString} from './common' // FileDiff contains diff information for a single file. It holds a list of @@ -130,7 +131,7 @@ export default class FileDiff { return fileDiff } - async fromGitUtilsObject({diff}) { + async fromGitUtilsObject({diff, stagedDiff}) { if (!diff) return; this.oldPathName = diff.oldFile().path() @@ -141,9 +142,23 @@ export default class FileDiff { this.untracked = diff.isUntracked() this.deleted = diff.isDeleted() + let stagedLines = [] + if (stagedDiff) { + // TODO: This all happens sequentially which is a bit of a bummer. + const hunks = await stagedDiff.hunks() + for (const hunk of hunks) { + const lines = await hunk.lines() + stagedLines = stagedLines.concat(lines) + } + } + + stagedLines = stagedLines + .map(line => HunkLine.fromGitUtilsObject({line})) + .filter(line => line.isChanged()) + for (let hunk of (await diff.hunks())) { let diffHunk = new DiffHunk() - diffHunk.fromGitUtilsObject({hunk: hunk}) + diffHunk.fromGitUtilsObject({hunk, stagedLines}) this.hunks.push(diffHunk) } } diff --git a/lib/file-list.js b/lib/file-list.js index 1d169a8fa7..6fd8988c2e 100644 --- a/lib/file-list.js +++ b/lib/file-list.js @@ -32,10 +32,19 @@ export default class FileList { // FIXME: for now, we need to get the stati for the diff stuff to work. :/ this.gitService.getStatuses() let diffs = await this.gitService.getDiffs('all') + // TODO: It's a bummer these two lines happen sequentially + const stagedDiffs = await this.gitService.getDiffs('staged') + + const diffsByName = {} + for (const diff of stagedDiffs) { + // TODO: Old path is probably not always right. + diffsByName[diff.oldFile().path()] = diff + } for(let diff of diffs) { let fileDiff = new FileDiff() - fileDiff.fromGitUtilsObject({diff: diff}) + const stagedDiff = diffsByName[diff.oldFile().path()] + fileDiff.fromGitUtilsObject({diff, stagedDiff}) this.files.push(fileDiff) } this.emitter.emit('did-change') diff --git a/lib/hunk-line.js b/lib/hunk-line.js index 954427e37e..46e5436320 100644 --- a/lib/hunk-line.js +++ b/lib/hunk-line.js @@ -91,4 +91,10 @@ export default class HunkLine { if (line.newLineno() > 0) this.newLineNumber = line.newLineno() this.emitChangeEvent() } + + static fromGitUtilsObject(obj) { + const hunkLine = new HunkLine() + hunkLine.fromGitUtilsObject(obj) + return hunkLine + } } diff --git a/lib/main.js b/lib/main.js index 9e36b24517..49649d9adc 100644 --- a/lib/main.js +++ b/lib/main.js @@ -66,7 +66,6 @@ module.exports = GitExperiment = { openChangesPanel: () => { if (this.changesPanel) { - this.gitIndex().updateRepository() this.changesPanel.show() } else {