forked from oridb/git9
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathref.c
447 lines (400 loc) · 6.97 KB
/
ref.c
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
#include <u.h>
#include <libc.h>
#include <ctype.h>
#include "git.h"
typedef struct Eval Eval;
typedef struct XObject XObject;
struct Eval {
char *str;
char *p;
Object **stk;
int nstk;
int stksz;
};
struct XObject {
Object *obj;
Object *mark;
XObject *queue;
XObject *next;
};
void
eatspace(Eval *ev)
{
while(isspace(ev->p[0]))
ev->p++;
}
int
objdatecmp(void *pa, void *pb)
{
Object *a, *b;
a = *(Object**)pa;
b = *(Object**)pb;
assert(a->type == GCommit && b->type == GCommit);
if(a->commit->mtime == b->commit->mtime)
return 0;
else if(a->commit->mtime < b->commit->mtime)
return -1;
else
return 1;
}
void
push(Eval *ev, Object *o)
{
if(ev->nstk == ev->stksz){
ev->stksz = 2*ev->stksz + 1;
ev->stk = erealloc(ev->stk, ev->stksz*sizeof(Object*));
}
ev->stk[ev->nstk++] = o;
}
Object*
pop(Eval *ev)
{
if(ev->nstk == 0)
sysfatal("stack underflow");
return ev->stk[--ev->nstk];
}
Object*
peek(Eval *ev)
{
if(ev->nstk == 0)
sysfatal("stack underflow");
return ev->stk[ev->nstk - 1];
}
int
isword(char e)
{
return isalnum(e) || e == '/' || e == '-' || e == '_' || e == '.';
}
int
word(Eval *ev, char *b, int nb)
{
char *p, *e;
int n;
p = ev->p;
for(e = p; isword(*e) && strncmp(e, "..", 2) != 0; e++)
/* nothing */;
/* 1 for nul terminator */
n = e - p + 1;
if(n >= nb)
n = nb;
snprint(b, n, "%s", p);
ev->p = e;
return n > 0;
}
int
take(Eval *ev, char *m)
{
int l;
l = strlen(m);
if(strncmp(ev->p, m, l) != 0)
return 0;
ev->p += l;
return 1;
}
XObject*
hnode(XObject *ht[], Object *o)
{
XObject *h;
int hh;
hh = o->hash.h[0] & 0xff;
for(h = ht[hh]; h; h = h->next)
if(hasheq(&o->hash, &h->obj->hash))
return h;
h = malloc(sizeof(*h));
h->obj = o;
h->mark = nil;
h->queue = nil;
h->next = ht[hh];
ht[hh] = h;
return h;
}
Object*
ancestor(Object *a, Object *b)
{
Object *o, *p, *r;
XObject *ht[256];
XObject *h, *q, *q1, *q2;
int i;
if(a == b)
return a;
if(a == nil || b == nil)
return nil;
r = nil;
memset(ht, 0, sizeof(ht));
q1 = nil;
h = hnode(ht, a);
h->mark = a;
h->queue = q1;
q1 = h;
h = hnode(ht, b);
h->mark = b;
h->queue = q1;
q1 = h;
while(1){
q2 = nil;
while(q = q1){
q1 = q->queue;
q->queue = nil;
o = q->obj;
for(i = 0; i < o->commit->nparent; i++){
p = readobject(o->commit->parent[i]);
if(p == nil)
goto err;
h = hnode(ht, p);
if(h->mark != nil){
if(h->mark != q->mark){
r = h->obj;
goto done;
}
} else {
h->mark = q->mark;
h->queue = q2;
q2 = h;
}
}
}
if(q2 == nil){
err:
werrstr("no common ancestor");
break;
}
q1 = q2;
}
done:
for(i=0; i<nelem(ht); i++){
while(h = ht[i]){
ht[i] = h->next;
free(h);
}
}
return r;
}
int
lca(Eval *ev)
{
Object *a, *b, *o;
if(ev->nstk < 2){
werrstr("ancestor needs 2 objects");
return -1;
}
a = pop(ev);
b = pop(ev);
o = ancestor(a, b);
if(o == nil)
return -1;
push(ev, o);
return 0;
}
int
parent(Eval *ev)
{
Object *o, *p;
o = pop(ev);
/* Special case: first commit has no parent. */
if(o->commit->nparent == 0)
p = emptydir();
else if ((p = readobject(o->commit->parent[0])) == nil){
werrstr("no parent for %H", o->hash);
return -1;
}
push(ev, p);
return 0;
}
int
unwind(Eval *ev, Object **obj, int *idx, int nobj, Object **p, Objset *set, int keep)
{
int i;
for(i = nobj; i >= 0; i--){
idx[i]++;
if(keep && !oshas(set, obj[i])){
push(ev, obj[i]);
osadd(set, obj[i]);
}else{
osadd(set, obj[i]);
}
if(idx[i] < obj[i]->commit->nparent){
*p = obj[i];
return i;
}
}
return -1;
}
int
range(Eval *ev)
{
Object *a, *b, *p, **all;
int nall, *idx, mark;
Objset keep, skip;
b = pop(ev);
a = pop(ev);
if(a->type != GCommit || b->type != GCommit){
werrstr("non-commit object in range");
return -1;
}
p = b;
all = nil;
idx = nil;
nall = 0;
mark = ev->nstk;
osinit(&keep);
osinit(&skip);
while(1){
all = erealloc(all, (nall + 1)*sizeof(Object*));
idx = erealloc(idx, (nall + 1)*sizeof(int));
all[nall] = p;
idx[nall] = 0;
if(p == a)
if((nall = unwind(ev, all, idx, nall, &p, &keep, 1)) == -1)
break;
else if(p->commit->nparent == 0)
if((nall = unwind(ev, all, idx, nall, &p, &skip, 0)) == -1)
break;
else if(oshas(&keep, p))
if((nall = unwind(ev, all, idx, nall, &p, &keep, 1)) == -1)
break;
else if(oshas(&skip, p))
if((nall = unwind(ev, all, idx, nall, &p, &skip, 0)) == -1)
break;
if(p->commit->nparent == 0)
break;
if((p = readobject(p->commit->parent[idx[nall]])) == nil)
sysfatal("bad commit %H", p->commit->parent[idx[nall]]);
nall++;
}
free(all);
qsort(ev->stk + mark, ev->nstk - mark, sizeof(Object*), objdatecmp);
return 0;
}
int
readref(Hash *h, char *ref)
{
static char *try[] = {"", "refs/", "refs/heads/", "refs/remotes/", "refs/tags/", nil};
char buf[256], s[256], **pfx;
int r, f, n;
/* TODO: support hash prefixes */
if((r = hparse(h, ref)) != -1)
return r;
if(strcmp(ref, "HEAD") == 0){
snprint(buf, sizeof(buf), ".git/HEAD");
if((f = open(buf, OREAD)) == -1)
return -1;
if((n = readn(f, s, sizeof(s) - 1))== -1)
return -1;
s[n] = 0;
strip(s);
r = hparse(h, s);
goto found;
}
for(pfx = try; *pfx; pfx++){
snprint(buf, sizeof(buf), ".git/%s%s", *pfx, ref);
if((f = open(buf, OREAD)) == -1)
continue;
if((n = readn(f, s, sizeof(s) - 1)) == -1)
continue;
s[n] = 0;
strip(s);
r = hparse(h, s);
close(f);
goto found;
}
return -1;
found:
if(r == -1 && strstr(s, "ref: ") == s)
r = readref(h, s + strlen("ref: "));
return r;
}
int
evalpostfix(Eval *ev)
{
char name[256];
Object *o;
Hash h;
eatspace(ev);
if(!word(ev, name, sizeof(name))){
werrstr("expected name in expression");
return -1;
}
if(readref(&h, name) == -1){
werrstr("invalid ref %s", name);
return -1;
}else if((o = readobject(h)) == nil){
werrstr("invalid ref %s (hash %H)", name, h);
return -1;
}
push(ev, o);
while(1){
eatspace(ev);
switch(ev->p[0]){
case '^':
case '~':
ev->p++;
if(parent(ev) == -1)
return -1;
break;
case '@':
ev->p++;
if(lca(ev) == -1)
return -1;
break;
default:
goto done;
break;
}
}
done:
return 0;
}
int
evalexpr(Eval *ev, char *ref)
{
memset(ev, 0, sizeof(*ev));
ev->str = ref;
ev->p = ref;
while(1){
if(evalpostfix(ev) == -1)
return -1;
if(ev->p[0] == '\0')
return 0;
else if(take(ev, ":") || take(ev, "..")){
if(evalpostfix(ev) == -1)
return -1;
if(ev->p[0] != '\0'){
werrstr("junk at end of expression");
return -1;
}
return range(ev);
}
}
}
int
resolverefs(Hash **r, char *ref)
{
Eval ev;
Hash *h;
int i;
if(evalexpr(&ev, ref) == -1){
free(ev.stk);
return -1;
}
h = emalloc(ev.nstk*sizeof(Hash));
for(i = 0; i < ev.nstk; i++)
h[i] = ev.stk[i]->hash;
*r = h;
return ev.nstk;
}
int
resolveref(Hash *r, char *ref)
{
Eval ev;
if(evalexpr(&ev, ref) == -1){
free(ev.stk);
return -1;
}
if(ev.nstk != 1){
werrstr("ambiguous ref expr");
free(ev.stk);
return -1;
}
*r = ev.stk[0]->hash;
return 0;
}