parseGitStatusPorcelain function
Parses git status --porcelain output (the git watcher seam's
fixture format) into counts + branch (from the ## branch header;
a detached HEAD reports a null branch — dirty counts only). Pure —
the host runs git.
Implementation
StatusLineGit parseGitStatusPorcelain(String output) {
var staged = 0;
var unstaged = 0;
var untracked = 0;
String? branch;
for (final line in output.split('\n')) {
if (line.isEmpty) continue;
if (line.startsWith('## ')) {
final header = line.substring(3);
// An initial repo and a detached HEAD both report branch-less
// rather than adopting their sentence as a name (see
// [_isBranchlessGitHeader]).
if (_isBranchlessGitHeader(header)) continue;
final dot = header.indexOf('...');
final bracket = header.indexOf('[');
branch = header
.substring(0, dot < 0 ? (bracket < 0 ? header.length : bracket) : dot)
.trim();
continue;
}
if (line.length < 2) continue;
final x = line[0];
final y = line[1];
// `!` = ignored files (only reported with --ignored): they are not
// untracked work, so they never count toward `?N`.
if (x == '?') {
untracked++;
} else if (x == '!') {
continue;
} else {
if (x != ' ') staged++;
if (y != ' ') unstaged++;
}
}
return StatusLineGit(
branch: branch,
staged: staged,
unstaged: unstaged,
untracked: untracked,
);
}