-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.oak
245 lines (222 loc) · 6.53 KB
/
main.oak
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
{
println: println
default: default
identity: id
map: map
each: each
last: last
slice: slice
filter: filter
append: append
reverse: reverse
separate: separate
} := import('std')
{
join: join
split: split
padStart: padStart
endsWith?: endsWith?
trim: trim
} := import('str')
fmt := import('fmt')
math := import('math')
sort := import('sort')
fs := import('fs')
path := import('path')
datetime := import('datetime')
cli := import('cli')
Version := '1.0'
fn bold(s) '\x1b[1m' << s << '\x1b[0m'
fn boldyellow(s) '\x1b[1;33m' << s << '\x1b[0;0m'
fn red(s) '\x1b[0;31m' << s << '\x1b[0;0m'
fn gray(s) '\x1b[0;90m' << s << '\x1b[0;0m'
// We use the existence of ".git" as sign that we're in a git repo.
fn hasDotGit?(dir) {
gitHeadPath := path.join(dir, '.git')
fs.statFile(gitHeadPath) != ?
}
// I want to sort the list of git repositories by a rough reverse-chronological
// order of "when I last worked on them". This isn't something I can measure
// directly without checking the mtime of every file in the repo. But I use the
// mtime of `.git/index`, which changes when branches are switched or new
// commits are made, as a "reasonably good" proxy. If `.git/index` does not
// exist (e.g. for newly initialized repos), we fall back to `.git/HEAD` which
// always exists.
fn gitModTime(dir, pass) {
gitIndexPath := path.join(dir, '.git/index')
gitHeadPath := path.join(dir, '.git/HEAD')
with fs.statFile(gitIndexPath) fn(fileStat) if fileStat {
? -> with fs.statFile(gitHeadPath) fn(fileStat) {
pass(fileStat.mod)
}
_ -> pass(fileStat.mod)
}
}
fn gitBranch(dir, pass) {
with exec(
'sh'
['-c', fmt.format('cd {{0}} && git symbolic-ref --short HEAD', dir)]
''
) fn(evt) pass(evt.stdout |> trim())
}
// To check whether local is ahead of remote, we check whether head of local is
// an ancestor of (or the same as) the head of remote.
fn gitAllCommitsPushed(dir, pass) {
with exec(
'sh'
['-c', fmt.format('cd {{0}} && git merge-base --is-ancestor HEAD @{u}', dir)]
''
) fn(evt) pass(evt.status = 0)
}
fn gitStatus(dir, pass) {
with exec(
'sh'
['-c', fmt.format('cd {{0}} && git status --porcelain --short', dir)]
''
) fn(evt) pass(evt.stdout)
}
fn main(rootDir, options) {
if !options.color -> {
bold <- boldyellow <- red <- gray <- id
}
with fs.listFiles(rootDir) fn(files) {
[gitDirs, untrackedDirs] := files |>
filter(:dir) |>
map(fn(f) path.join(rootDir, f.name)) |>
separate(hasDotGit?)
gitProjects := gitDirs |> with map() fn(dir) {
name: path.split(dir) |> last()
dir: dir
}
untrackedProjects := untrackedDirs |> with map() fn(dir) {
name: path.split(dir) |> last()
}
// Each check for git metadata spawns a few OS processes. Doing that
// sequentially for many git repos is inefficient. So instead, we
// parallelize the spawning of all the processes in a wait group (we
// spawn all of them, and wait for all of them to complete).
//
// There are 4 * # of projects tasks to wait on, and when all of them
// have been completed, we proceed to the next step in `fn maybeRest`.
waits := 4 * len(gitProjects)
fn maybeRest() if waits = 0 -> {
// print git repos
projNames := gitProjects |> map(:name)
maxProjNameLen := math.max(0, projNames |> map(len)...)
projNameLenPad := padStart('', maxProjNameLen, ' ')
branches := gitProjects |> map(:branch)
maxBranchLen := math.max(0, branches |> map(len)...)
gitProjects |> sort.sort(:mtime) |> reverse() |> with each() fn(proj) {
fmt.printf(
if options.color {
true -> '{{0}} {{1}} {{2}}'
_ -> '{{0}} {{3}} {{1}} {{4}} {{2}}'
}
// always-used arguments
padStart(proj.name, maxProjNameLen, ' ') |> if proj.diff {
'' -> bold
_ -> boldyellow
}()
padStart(proj.branch, maxBranchLen, ' ') |> if proj.synced? {
true -> id
_ -> red
}()
datetime.format(proj.mtime) |> gray()
// no-color-only arguments
if proj.diff {
'' -> ' '
_ -> '*'
}
if proj.synced? {
true -> ' '
_ -> '!'
}
)
if options.files & proj.diff != '' -> {
proj.diff |>
split('\n') |>
filter(fn(line) line != '') |>
map(fn(line) projNameLenPad + ' ' + line) |>
join('\n') |>
println()
}
}
// print untracked projects
if untrackedProjects != [] -> {
println('Excluded (non-git) dirs:'
untrackedProjects |> map(:name) |> sort.sort() |> join(', '))
}
}
// we check for wait group completion before dispatching any tasks,
// because if a directory does not contain any git repos, this is the
// only time to check the wait group.
if gitProjects {
[] -> maybeRest()
_ -> gitProjects |> with each() fn(proj) {
with gitModTime(proj.dir) fn(mtime) {
proj.mtime := mtime
waits <- waits - 1
maybeRest()
}
with gitBranch(proj.dir) fn(branch) {
proj.branch := branch
waits <- waits - 1
maybeRest()
}
with gitAllCommitsPushed(proj.dir) fn(synced?) {
proj.synced? := synced?
waits <- waits - 1
maybeRest()
}
with gitStatus(proj.dir) fn(diff) {
proj.diff := diff
waits <- waits - 1
maybeRest()
}
}
}
}
}
// Fix cli.parseArgv() for running as a standalone binary
Cli := with cli.parseArgv() if {
args().1 |> default('') |> endsWith?('main.oak') -> args()
_ -> ['oak', 'superstat.oak'] |> append(args() |> slice(1))
}
if Cli.opts.version != ? | Cli.opts.v != ? -> {
fmt.printf('Superstat v{{0}}', Version)
exit(0)
}
if Cli.opts.help != ? | Cli.opts.h != ? -> {
println('Superstat: git status + diff every repo in a workspace.
Usage
superstat [rootDir] [options]
Options
--[h]elp Show this help message
--[v]ersion Print version information and exit
--no-[c]olor Use letters instead of colors to indicate info
--no-[f]iles Do not list non-committed file changes
Legend
[repo-name] [branch] [last-worked-on-timestamp]
[non-committed-changes]
- repo-name is yellow (or followed by "*") if there are non-committed
changes.
- branch is red (or followed by "!") if there are unsynced changes (if
local is ahead of remote).
- If no rootDir is provided, Superstat falls back to $SUPERSTAT_ROOT
if defined in the environment.
')
exit(0)
}
rootDir := Cli.verb |> default(env().SUPERSTAT_ROOT)
if rootDir = ? -> {
println('No root directory of git repositories provided.')
exit(1)
}
if fs.statFile(rootDir) = ? -> {
fmt.printf('Could not read "{{0}}".', rootDir)
exit(1)
}
main(path.resolve(rootDir), {
color: Cli.opts.'no-color' = ? & Cli.opts.c = ?
files: Cli.opts.'no-files' = ? & Cli.opts.f = ?
})