From c905ba05e2c6c70824d46f286042a3619ac2e151 Mon Sep 17 00:00:00 2001 From: Daniel Nagy Date: Wed, 13 Mar 2013 01:39:05 +0000 Subject: [PATCH] improve status parsing --- src/status.coffee | 49 ++++++++--------------------------------------- 1 file changed, 8 insertions(+), 41 deletions(-) diff --git a/src/status.coffee b/src/status.coffee index 2ebe241..fae71e2 100644 --- a/src/status.coffee +++ b/src/status.coffee @@ -4,27 +4,10 @@ # 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) -> @@ -32,26 +15,10 @@ S.Status = class Status # 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] != "?" }