Created
April 1, 2017 03:10
-
-
Save chemzqm/49bbfdba095a237be5b780b039aef80d to your computer and use it in GitHub Desktop.
Gti status by node
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
var exec = require('child_process').exec | |
var Branch = new Promise(function(resolve, reject) { | |
exec('git symbolic-ref -q HEAD | cut -c 12-', function (err, stdout) { | |
if (err) return reject(err) | |
if (stdout.length) return resolve(stdout.replace('\n', '')) | |
exec('git rev-parse --short HEAD', function (err, stdout) { | |
if (err) return reject(err) | |
resolve(':' + stdout.replace(/\n$/, '')) | |
}) | |
}) | |
}) | |
var Changed = new Promise(function(resolve, reject) { | |
exec('git diff --name-status | cut -c 1-2', function (err, stdout) { | |
if (err) return reject(err) | |
var out = stdout.replace(/\n$/, '') | |
var count = 0 | |
out.split('\n').forEach(function (line) { | |
if (/U/.test(line)) return | |
if (line.length) count++ | |
}) | |
resolve(count) | |
}) | |
}) | |
var Staged = new Promise(function(resolve, reject) { | |
exec('git diff --staged --name-status | cut -c 1-2', function (err, stdout) { | |
if (err) return reject(err) | |
var out = stdout.replace(/\n$/, '') | |
var conflicted = 0 | |
var staged = 0 | |
out.split('\n').forEach(function (line) { | |
if (!line.length) return | |
if (/U/.test(line)) { | |
conflicted++ | |
} else { | |
staged++ | |
} | |
}) | |
resolve([conflicted, staged]) | |
}) | |
}) | |
var Untracked = new Promise(function(resolve, reject) { | |
exec('git ls-files --others --exclude-standard', function (err, stdout) { | |
if (err) return reject(err) | |
var out = stdout.replace(/\n$/, '') | |
if (!out.length) return resolve(0) | |
resolve(stdout.split('\n').length) | |
}) | |
}) | |
Promise.all([Branch, Changed, Staged, Untracked]).then(function (arr) { | |
var branch = ' ' + arr[0] | |
var changed = arr[1] | |
var conflicted = arr[2][0] | |
var staged = arr[2][1] | |
var untracked = arr[3] | |
if (changed + conflicted + staged + untracked === 0) { | |
process.stdout.write(branch + ' ✓') | |
} else { | |
var res = '' | |
if (changed) res = res + '+' | |
if (conflicted) res = res + 'x' | |
if (staged) res = res + '●' | |
if (untracked) res = res + '…' | |
process.stdout.write(branch + ' ' + res) | |
} | |
}, function () { }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment