mirror of
https://github.com/danog/gift.git
synced 2024-11-30 04:19:37 +01:00
improve status parsing
This commit is contained in:
parent
f9abf4b277
commit
c905ba05e2
@ -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] != "?" }
|
||||
|
Loading…
Reference in New Issue
Block a user