1
0
mirror of https://github.com/danog/gift.git synced 2024-11-30 04:19:37 +01:00

Merge pull request #14 from cubehero/diff_parse_raw

Diff parse raw
This commit is contained in:
Luke Plaster 2014-03-18 13:46:07 +01:00
commit ea4e353f68
3 changed files with 68 additions and 1 deletions

View File

@ -1,3 +1,4 @@
_ = require 'underscore'
Blob = require './blob'
module.exports = class Diff
@ -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

View File

@ -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.

View File

@ -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 = """