-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
executable file
·145 lines (122 loc) · 3.65 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/env node
/**
* Copyright (c) 2012 Yahoo! Inc. All rights reserved.
* Copyrights licensed under the MIT License.
* See the accompanying LICENSE file for terms.
*/
/*jshint node:true */
'use strict';
var exec = require('child_process').exec,
Table = require('cli-table'),
colors = require('colors'),
CHUNK_RE = /^([* ]) (\S+)\s+([0-9a-f]+) (.+)$/,
REMOTE_RE = /^remotes\/([\w.\-]+)\/([\w.\-]+)/,
msglen,
branches = {}, //hash of local branch names[1], no values
remotes = {}, //hash of remote names[2], no values
meta = {}; //hash to look up git metadata by 'remote,branch'[3]
function index(star, ref, sha, msg) {
var branch,
remote,
parts = ref.match(REMOTE_RE);
if (parts) {
remote = parts[1];
branch = parts[2];
} else {
remote = 'local';
branch = ref;
branches[ref] = null; //local branches
}
if (!remotes.hasOwnProperty(remote)) {
remotes[remote] = null; //index all remotes
}
meta[[remote, branch].join()] = {star: star, sha: sha, msg: msg};
}
function getMeta(repo, branch) {
return meta[[repo, branch].join()];
}
function format(meta, local, prev) {
var out = [];
if (local === meta.sha) {
//this column/repo branch head sha is same as local one (1st column)
out.push('✔ '.green);
} else {
out.push(meta.sha);
//optionally show commit message unless it's the same as prev column
if (msglen && (prev !== meta.sha)) {
out.push(meta.msg.slice(0, msglen).grey);
}
}
return out.join(' ');
}
function map(remotes, branches) {
var title = ['branch'].concat(remotes),
style = {compact: true, 'padding-left': 1, 'padding-right': 2},
table = new Table({head: title, style: style}),
blank = {sha: '', msg: ''};
function perBranch(branch) {
var localmeta = getMeta('local', branch) || blank,
star = localmeta.star ? ' ' + localmeta.star : '',
row = [branch + star.blue],
lsha = '', //local, for ✔
psha = ''; //previous, for optional msg dedupe
function perRow(remote) {
var head = getMeta(remote, branch) || blank;
row.push(format(head, lsha, psha));
if (!lsha) {
lsha = localmeta.sha;
}
psha = head.sha;
}
remotes.forEach(perRow);
table.push(row);
}
branches.forEach(perBranch);
return table;
}
function disp(args) {
if (!process.stdout.isTTY) {
colors.mode = 'none';
}
return args.length > 2 ? args.pop() : 20;
}
function chunk(err, stdout, stderr) {
if (err) {
console.error(stderr);
return process.exit(err.code);
}
stdout.split('\n').forEach(function (str) {
var parts = str.match(CHUNK_RE);
if (parts) {
index(parts[1].trim(), parts[2], parts[3], parts[4]);
}
});
console.log(map(Object.keys(remotes), Object.keys(branches)).toString());
}
msglen = disp(process.argv);
exec('git branch -av', chunk);
/*
1. branches => {
arrowtests: null,
connect: null,
develop: null,
'develop-perf': null,
master: null,
testjs: null
}
2. remotes => { local: null, my: null, up: null }
3. meta => {
'local,arrowtests':
{ star: '',
sha: 'df4666f',
msg: 'tweak to use MOJITO_DIR instead of path.join etc' },
'local,connect':
{ star: '',
sha: '1b1e3b1',
msg: 'rm useless function wrapper, +lint/strict' },
'local,develop':
{ star: '*',
sha: '748e74b',
msg: 'Merge pull request #740 from isao/develop' },
...
*/