-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathquery.c
159 lines (141 loc) · 2.65 KB
/
query.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
#include <u.h>
#include <libc.h>
#include "git.h"
#pragma varargck type "P" void
int fullpath;
int changes;
char *path[128];
int npath;
int
Pfmt(Fmt *f)
{
int i, n;
n = 0;
for(i = 0; i < npath; i++)
n += fmtprint(f, "%s/", path[i]);
return n;
}
void
showdir(Hash dh, char *dname, char m)
{
Dirent *p, *e;
Object *d;
path[npath++] = dname;
if((d = readobject(dh)) == nil)
sysfatal("bad hash %H", dh);
assert(d->type == GTree);
p = d->tree->ent;
e = p + d->tree->nent;
for(; p != e; p++){
if(p->modref)
continue;
if(p->mode & DMDIR)
showdir(p->h, p->name, m);
else
print("%c %P%s\n", m, p->name);
}
unref(d);
npath--;
}
void
show(Dirent *e, char m)
{
if(e->mode & DMDIR)
showdir(e->h, e->name, m);
else
print("- %P%s\n", e->name);
}
void
difftrees(Hash ah, Hash bh)
{
Dirent *ap, *bp, *ae, *be;
Object *a, *b;
int c;
if((a = readobject(ah)) == nil)
sysfatal("bad hash %H", ah);
if((b = readobject(bh)) == nil)
sysfatal("bad hash %H", bh);
if(a->type != b->type)
return;
switch(a->type){
case GCommit:
difftrees(a->commit->tree, b->commit->tree);
break;
case GTree:
ap = a->tree->ent;
ae = ap + a->tree->nent;
bp = b->tree->ent;
be = bp + b->tree->nent;
while(ap != ae && bp != be){
c = strcmp(ap->name, bp->name);
if(c == 0){
if(ap->mode == bp->mode && hasheq(&ap->h, &bp->h))
goto next;
if(ap->mode != bp->mode)
print("! %P%s\n", ap->name);
else if(!(ap->mode & DMDIR) || !(bp->mode & DMDIR))
print("@ %P%s\n", ap->name);
if((ap->mode & DMDIR) && (bp->mode & DMDIR)){
if(npath >= nelem(path))
sysfatal("path too deep");
path[npath++] = ap->name;
difftrees(ap->h, bp->h);
npath--;
}
next:
ap++;
bp++;
}else if(c < 0) {
show(ap, '-');
ap++;
}else if(c > 0){
show(bp, '+');
bp++;
}
}
for(; ap != ae; ap++)
show(ap, '-');
for(; bp != be; bp++)
show(bp, '+');
break;
}
unref(a);
unref(b);
}
void
usage(void)
{
fprint(2, "usage: %s [-pc] query\n", argv0);
exits("usage");
}
void
main(int argc, char **argv)
{
int i, j, n;
Hash *h;
char *p, *e;
char query[2048];
ARGBEGIN{
case 'p': fullpath++; break;
case 'c': changes++; break;
default: usage(); break;
}ARGEND;
gitinit();
fmtinstall('P', Pfmt);
p = query;
e = query + nelem(query);
for(i = 0; i < argc; i++)
p = seprint(p, e, "%s ", argv[i]);
if((n = resolverefs(&h, query)) == -1)
sysfatal("resolve %s: %r", argv[i]);
if(changes){
if(n != 2)
sysfatal("diff: need 2 commits, got %d", n);
difftrees(h[0], h[1]);
}else{
p = (fullpath ? "/mnt/git/object/" : "");
for(j = 0; j < n; j++)
print("%s%H\n", p, h[j]);
}
exits(nil);
}