5 typedef struct Objbuf Objbuf;
24 else if(e->mode & DMDIR)
26 else if(e->mode & 0111)
33 entcmp(void *pa, void *pb)
35 char abuf[256], bbuf[256], *ae, *be;
41 * If the files have the same name, they're equal.
42 * Otherwise, If they're trees, they sort as thoug
43 * there was a trailing slash.
47 if(strcmp(a->name, b->name) == 0)
50 ae = seprint(abuf, abuf + sizeof(abuf) - 1, a->name);
51 be = seprint(bbuf, bbuf + sizeof(bbuf) - 1, b->name);
56 return strcmp(abuf, bbuf);
60 bwrite(void *p, void *buf, int nbuf)
62 return Bwrite(p, buf, nbuf);
66 objbytes(void *p, void *buf, int nbuf)
76 r = (nbuf < r) ? nbuf : r;
77 memcpy(buf, b->hdr, r);
82 if(b->off < b->ndat + b->nhdr){
86 r = (nbuf < r) ? nbuf : r;
87 memcpy(s + n, b->dat + o, r);
95 writeobj(Hash *h, char *hdr, int nhdr, char *dat, int ndat)
97 Objbuf b = {.off=0, .hdr=hdr, .nhdr=nhdr, .dat=dat, .ndat=ndat};
103 st = sha1((uchar*)hdr, nhdr, nil, nil);
104 st = sha1((uchar*)dat, ndat, nil, st);
105 sha1(nil, 0, h->h, st);
107 snprint(s, sizeof(s), "%H", *h);
108 fd = create(".git/objects", OREAD, DMDIR|0755);
110 snprint(o, sizeof(o), ".git/objects/%c%c", s[0], s[1]);
111 fd = create(o, OREAD, DMDIR | 0755);
113 snprint(o, sizeof(o), ".git/objects/%c%c/%s", s[0], s[1], s + 2);
114 if(readobject(*h) == nil){
115 if((f = Bopen(o, OWRITE)) == nil)
116 sysfatal("could not open %s: %r", o);
117 if(deflatezlib(f, bwrite, &b, objbytes, 9, 0) == -1)
118 sysfatal("could not write %s: %r", o);
124 writetree(Dirent *ent, int nent, Hash *h)
126 char *t, *txt, *etxt, hdr[128];
130 t = emalloc((16+256+20) * nent);
132 etxt = t + (16+256+20) * nent;
134 /* sqeeze out deleted entries */
137 for(d = ent; d != ent + nent; d++)
142 qsort(ent, nent, sizeof(Dirent), entcmp);
143 for(d = ent; d != ent + nent; d++){
144 if(strlen(d->name) >= 255)
145 sysfatal("overly long filename: %s", d->name);
146 t = seprint(t, etxt, "%o %s", gitmode(d), d->name) + 1;
147 memcpy(t, d->h.h, sizeof(d->h.h));
150 nhdr = snprint(hdr, sizeof(hdr), "%T %lld", GTree, (vlong)(t - txt)) + 1;
151 writeobj(h, hdr, nhdr, txt, t - txt);
157 blobify(Dir *d, char *path, int *mode, Hash *bh)
162 if((d->mode & DMDIR) != 0)
163 sysfatal("not file: %s", path);
165 nh = snprint(h, sizeof(h), "%T %lld", GBlob, d->length) + 1;
166 if((f = open(path, OREAD)) == -1)
167 sysfatal("could not open %s: %r", path);
168 buf = emalloc(d->length);
169 if(readn(f, buf, d->length) != d->length)
170 sysfatal("could not read blob %s: %r", path);
171 writeobj(bh, h, nh, buf, d->length);
182 /* Explicitly removed. */
183 snprint(ipath, sizeof(ipath), ".git/index9/removed/%s", path);
184 if(strstr(cleanname(ipath), ".git/index9/removed") != ipath)
185 sysfatal("path %s leaves index", ipath);
187 if(d != nil && d->qid.type != QTDIR){
192 /* Explicitly added. */
193 snprint(ipath, sizeof(ipath), ".git/index9/tracked/%s", path);
194 if(strstr(cleanname(ipath), ".git/index9/tracked") != ipath)
195 sysfatal("path %s leaves index", ipath);
196 if(access(ipath, AEXIST) == 0)
203 pathelt(char *buf, int nbuf, char *p, int *isdir)
210 while(*p && *p != '/' && b != buf + nbuf)
213 *isdir = (*p == '/');
218 dirent(Dirent **ent, int *nent, char *name)
222 for(d = *ent; d != *ent + *nent; d++)
223 if(d->name && strcmp(d->name, name) == 0)
226 *ent = erealloc(*ent, *nent * sizeof(Dirent));
227 d = *ent + (*nent - 1);
228 memset(d, 0, sizeof(*d));
229 d->name = estrdup(name);
234 treeify(Object *t, char **path, char **epath, int off, Hash *h)
236 int r, n, ne, nsub, nent, isdir;
245 nent = t->tree->nent;
246 ent = eamalloc(nent, sizeof(*ent));
247 sub = eamalloc((epath - path), sizeof(Object*));
248 memcpy(ent, t->tree->ent, nent*sizeof(*ent));
249 for(p = path; p != epath; p = ep){
250 ne = pathelt(elt, sizeof(elt), *p + off, &isdir);
251 for(ep = p; ep != epath; ep++){
252 if(strncmp(elt, *ep + off, ne) != 0)
254 if((*ep)[off+ne] != '\0' && (*ep)[off+ne] != '/')
257 e = dirent(&ent, &nent, elt);
259 sysfatal("symlinks may not be modified: %s", *path);
261 sysfatal("submodules may not be modified: %s", *path);
263 e->mode = DMDIR | 0755;
264 sub[nsub] = readobject(e->h);
265 if(sub[nsub] == nil || sub[nsub]->type != GTree)
266 sub[nsub] = emptydir();
268 * if after processing deletions, a tree is empty,
269 * mark it for removal from the parent.
271 * Note, it is still written to the object store,
272 * but this is fine -- and ensures that an empty
273 * repository will continue to work.
275 n = treeify(sub[nsub], p, ep, off + ne + 1, &e->h);
282 if(d != nil && tracked(*p))
283 blobify(d, *p, &e->mode, &e->h);
290 werrstr("%.*s: empty directory", off, *path);
294 r = writetree(ent, nent, h);
302 mkcommit(Hash *c, char *msg, char *name, char *email, vlong date, Hash *parents, int nparents, Hash tree)
309 fmtprint(&f, "tree %H\n", tree);
310 for(i = 0; i < nparents; i++)
311 fmtprint(&f, "parent %H\n", parents[i]);
312 fmtprint(&f, "author %s <%s> %lld +0000\n", name, email, date);
313 fmtprint(&f, "committer %s <%s> %lld +0000\n", name, email, date);
315 fmtprint(&f, "%s", msg);
319 nh = snprint(h, sizeof(h), "%T %d", GCommit, ns) + 1;
320 writeobj(c, h, nh, s, ns);
330 if(resolveref(&h, "HEAD") == -1)
332 if((c = readobject(h)) == nil || c->type != GCommit)
333 sysfatal("could not read HEAD %H", h);
334 if((t = readobject(c->commit->tree)) == nil)
335 sysfatal("could not read tree for commit %H", h);
342 fprint(2, "usage: %s -n name -e email -m message -d date [files...]\n", argv0);
347 main(int argc, char **argv)
349 Hash th, ch, parents[Maxparents];
350 char *msg, *name, *email, *dstr, cwd[1024];
351 int i, r, ncwd, nparents;
356 if(access(".git", AEXIST) != 0)
357 sysfatal("could not find git repo: %r");
358 if(getwd(cwd, sizeof(cwd)) == nil)
359 sysfatal("getcwd: %r");
369 case 'm': msg = EARGF(usage()); break;
370 case 'n': name = EARGF(usage()); break;
371 case 'e': email = EARGF(usage()); break;
372 case 'd': dstr = EARGF(usage()); break;
374 if(nparents >= Maxparents)
375 sysfatal("too many parents");
376 if(resolveref(&parents[nparents++], EARGF(usage())) == -1)
377 sysfatal("invalid parent: %r");
384 sysfatal("missing message");
386 sysfatal("missing name");
388 sysfatal("missing email");
390 date=strtoll(dstr, &dstr, 10);
391 if(strlen(dstr) != 0)
392 sysfatal("could not parse date %s", dstr);
394 if(msg == nil || name == nil)
396 for(i = 0; i < argc; i++){
398 if(*argv[i] == '/' && strncmp(argv[i], cwd, ncwd) == 0)
400 while(*argv[i] == '/')
405 r = treeify(t, argv, argv + argc, 0, &th);
407 sysfatal("could not commit: %r\n");
408 mkcommit(&ch, msg, name, email, date, parents, nparents, th);