Skip to content
This repository was archived by the owner on Dec 15, 2022. It is now read-only.

Get stagedness from git #1

Merged
merged 4 commits into from
Feb 2, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion lib/diff-hunk.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -67,14 +69,24 @@ export default class DiffHunk {
return diffHunk
}

async fromGitUtilsObject({hunk}) {
async fromGitUtilsObject({hunk, stagedLines}) {
if (!hunk) return;

this.header = hunk.header()

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)
}
}
Expand Down
19 changes: 17 additions & 2 deletions lib/file-diff.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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)
}
}
Expand Down
11 changes: 10 additions & 1 deletion lib/file-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
6 changes: 6 additions & 0 deletions lib/hunk-line.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,10 @@ export default class HunkLine {
if (line.newLineno() > 0) this.newLineNumber = line.newLineno()
this.emitChangeEvent()
}

static fromGitUtilsObject(obj) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like this isnt used. yar? nar?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const hunkLine = new HunkLine()
hunkLine.fromGitUtilsObject(obj)
return hunkLine
}
}
1 change: 0 additions & 1 deletion lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ module.exports = GitExperiment = {

openChangesPanel: () => {
if (this.changesPanel) {
this.gitIndex().updateRepository()
this.changesPanel.show()
}
else {
Expand Down