Skip to content

Commit

Permalink
improve status parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
nagy committed Mar 13, 2013
1 parent f9abf4b commit c905ba0
Showing 1 changed file with 8 additions and 41 deletions.
49 changes: 8 additions & 41 deletions src/status.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -4,54 +4,21 @@
# callback - Receives `(err, status)`
#
module.exports = S = (repo, callback) ->
repo.git "status", (err, stdout, stderr) ->
repo.git "status --porcelain", (err, stdout, stderr) ->
status = new Status repo
status.parse stdout
return callback err, status


BEGIN_STAGED = [
"# Changes to be committed:"
]
BEGIN_UNSTAGED = [
"# Changed but not updated:"
"# Changes not staged for commit:"
]
BEGIN_UNTRACKED = [
"# Untracked files:"
]
FILE = /^#\s+([^\s]+)[:]\s+(.+)$/
TYPES =
added: "A"
modified: "M"
deleted: "D"
callback err, status

S.Status = class Status
constructor: (@repo) ->

# Internal: Parse the status from stdout of a `git status` command.
parse: (text) ->
@files = {}
@clean = true
state = null
@clean = text.length == 0
for line in text.split("\n")
if ~BEGIN_STAGED.indexOf(line)
state = "staged"
@clean = false
else if ~BEGIN_UNSTAGED.indexOf(line)
state = "unstaged"
@clean = false
else if ~BEGIN_UNTRACKED.indexOf(line)
state = "untracked"
@clean = false
else if state && match = FILE.exec(line)
file = match[2]
data = switch state
when "staged" then {staged: true, tracked: true}
when "unstaged" then {staged: false, tracked: true}
data.type = TYPES[match[1]]
@files[file] = data
else if state == "untracked" && (match = /^#\s+([^\s]+)$/.exec(line))
file = match[1]
@files[file] = {tracked: false}
return
if line.length == 0
continue
file = line.substr 3
type = line.substr 0,2
@files[file] = { type: type.trim(), staged: (line[0] != " " and line[0] != "?" ) , tracked: line[0] != "?" }

0 comments on commit c905ba0

Please sign in to comment.