message: refactoring mutable-tree
diff: @@ -36,51 +36,50 @@ class MutableTree {
  }
  async insertPath(path, object, filemode, { allowCreate, allowUpdate }) {
  - let parts = path.split('/');
  - let here = this;
  -
  - while (parts.length > 1) {
  - let dirName = parts.shift();
  - let entry = here.entryByName(dirName);
  - if (!entry || !entry.isTree()) {
  - if (!allowCreate) {
  - throw new NotFound(`${path} does not exist`);
  - }
  - entry = here.insert(dirName, new MutableTree(here.repo, null), FILEMODE.TREE);
  - }
  - here = await entry.getTree();
  - }
  + let { tree, leaf, leafName } = await this.traverse(path, allowCreate);
  - let have = here.entryByName(parts[0]);
  -
  - if (!allowUpdate && have && have !== tombstone) {
  + if (!allowUpdate && leaf && leaf !== tombstone) {
  throw new OverwriteRejected(`Refusing to overwrite ${path}`);
  }
  - if (!allowCreate && (!have || have === tombstone)) {
  + if (!allowCreate && (!leaf || leaf === tombstone)) {
  throw new NotFound(`${path} does not exist`);
  }
  - return here.insert(parts[0], object, filemode);
  + return tree.insert(leafName, object, filemode);
  }
  async deletePath(path) {
  + let { tree, leaf, leafName } = await this.traverse(path, false);
  + if (!leaf || leaf === tombstone) {
  + throw new NotFound(`No such file ${path}`);
  + }
  + tree.overlay.set(leafName, tombstone);
  + }
  +
  + async traverse(path, allowCreate) {
  let parts = path.split('/');
  let here = this;
  +
  while (parts.length > 1) {
  let dirName = parts.shift();
  let entry = here.entryByName(dirName);
  if (!entry || !entry.isTree()) {
  - throw new NotFound(`No such directory ${path}`);
  + if (!allowCreate) {
  + throw new NotFound(`${path} does not exist`);
  }
  - here = await entry.getTree();
  + entry = here.insert(dirName, new MutableTree(here.repo, null), FILEMODE.TREE);
  }
  - let have = here.entryByName(parts[0]);
  - if (!have || have === tombstone) {
  - throw new NotFound(`No such file ${path}`);
  + here = await entry.getTree();
  }
  - here.overlay.set(parts[0], tombstone);
  +
  + return {
  + tree: here,
  + leaf: here.entryByName(parts[0]),
  + leafName: parts[0]
  + };
  }
  +
  async write(allowEmpty=false) {
  if (this.overlay.size === 0 && this.tree) {
  return this.tree;
  