Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Diff parse raw #14

Merged
merged 3 commits into from
Mar 18, 2014
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
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
31 changes: 26 additions & 5 deletions src/repo.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -128,17 +128,38 @@ module.exports = class Repo
# commitA - A Commit or String commit id.
# commitB - A Commit or String commit id.
# paths - A list of String paths to restrict the difference to (optional).
# options - An object of options to pass to git diff (optional)
# callback - A Function which receives `(err, diffs)`.
#
diff: (commitA, commitB, paths, callback) ->
[callback, paths] = [paths, callback] if !callback
paths ?= []
# Possible forms of the method:
#
# diff(commitA, commitB, callback)
# diff(commitA, commitB, paths, callback)
# diff(commitA, commitB, options, callback)
# diff(commitA, commitB, paths, options, callback)
#
diff: (commitA, commitB) ->
[paths, options] = [[], {}]
if arguments.length is 3
callback = arguments[2]
else if arguments.length is 4
callback = arguments[3]
if arguments[2] instanceof Array
paths = arguments[2]
else if arguments[2] instanceof Object
options = arguments[2]
else if arguments.length is 5
[paths, options, callback] = arguments.slice(2)

commitA = commitA.id if _.isObject(commitA)
commitB = commitB.id if _.isObject(commitB)
@git "diff", {}, _.flatten([commitA, commitB, "--", paths])
@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