Skip to content

Commit

Permalink
Merge pull request #14 from cubehero/diff_parse_raw
Browse files Browse the repository at this point in the history
Diff parse raw
  • Loading branch information
notatestuser committed Mar 18, 2014
2 parents d6c4acf + 9287ab3 commit ea4e353
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 1 deletion.
25 changes: 25 additions & 0 deletions src/diff.coffee
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
_ = require 'underscore'
Blob = require './blob'

module.exports = class Diff
Expand Down Expand Up @@ -64,3 +65,27 @@ module.exports = class Diff

return diffs

# Public: Parse the raw diff format from the command output.
#
# text - String stdout of a `git diff` command.
#
# Returns Array of Diff.
@parse_raw: (repo, text) ->
lines = _.compact(text.split "\n")
diffs = []

for line in lines
line = line[1..-1] # get rid of leading ':'
line = line.replace(/\.\.\./g, '')
[a_mode, b_mode, a_sha, b_sha, status, a_path, b_path] = line.split(/\s/)
b_path = a_path unless b_path
new_file = status is 'M'
deleted_file = status is 'D'
renamed_file = status is 'R'

diffs.push new Diff(
repo, a_path, b_path, a_sha, b_sha, a_mode, b_mode,
new_file, deleted_file, null, renamed_file, null
)

return diffs
5 changes: 4 additions & 1 deletion src/repo.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,10 @@ module.exports = class Repo
@git "diff", options, _.flatten([commitA, commitB, "--", paths])
, (err, stdout, stderr) =>
return callback err if err
return callback err, Diff.parse(this, stdout)
if _.has(options, 'raw')
return callback err, Diff.parse_raw(this, stdout)
else
return callback err, Diff.parse(this, stdout)


# Public: Get the repository's remotes.
Expand Down
39 changes: 39 additions & 0 deletions test/diff.test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,45 @@ describe "Diff", ->
it "has a similarity_index of 0", ->
diff.similarity_index.should.eql 0

describe ".parse_raw", ->
describe "simple editing", ->
repo = fixtures.tagged
stdout = """
:100644 100644 95f6539... 0466f13... M file.txt
"""
diffs = Diff.parse_raw repo, stdout

it "is an Array of Diffs", ->
diffs.should.be.an.instanceof Array
diffs[0].should.be.an.instanceof Diff

it "has one diff", ->
diffs.should.have.lengthOf 1

describe "the first diff", ->
diff = diffs[0]

it "has the repo", ->
diff.repo.should.eql repo

for blob in ["a_blob", "b_blob"]
it "has a #{blob}", ->
diff[blob].should.be.an.instanceof Blob

for path in ["a_path", "b_path"]
it "has a #{path}", ->
diff[path].should.eql "file.txt"

it "has a b_mode", ->
diff.b_mode.should.eql "100644"

for change in ["new_file", "renamed_file", "deleted_file"]
it "#{change} is false", ->
diff[change].should.be.false

it "has a similarity_index of 0", ->
diff.similarity_index.should.eql 0

describe "delete a file", ->
repo = fixtures.branched
stdout = """
Expand Down

0 comments on commit ea4e353

Please sign in to comment.