message: refactor indexer for readability
diff: @@ -49,8 +49,6 @@ module.exports = class Indexer {
  await Promise.all(branches.map(branch => this._updateBranch(branch, realTime)));
  }
  -
  -
  async _ensureRepo() {
  if (!this.repo) {
  this.repo = await Repository.open(this.repoPath);
  @@ -147,15 +145,29 @@ module.exports = class Indexer {
  if (newTree) {
  for (let newEntry of newTree.entries()) {
  let name = newEntry.name();
  - let oldEntry;
  seen.set(name, true);
  + await this._indexEntry(branch, name, oldTree, newEntry, bulkOps);
  + }
  + }
  + if (oldTree) {
  + for (let oldEntry of oldTree.entries()) {
  + let name = oldEntry.name();
  + if (!seen.get(name)) {
  + await this._deleteEntry(branch, oldEntry, bulkOps);
  + }
  + }
  + }
  + }
  +
  + async _indexEntry(branch, name, oldTree, newEntry, bulkOps) {
  + let oldEntry;
  if (oldTree) {
  oldEntry = safeEntryByName(oldTree, name);
  if (oldEntry && oldEntry.id().equal(newEntry.id())) {
  // We can prune whole subtrees when we find an identical
  // entry. Which is kinda the point of Git's data
  // structure in the first place.
  - continue;
  + return;
  }
  }
  if (newEntry.isTree()) {
  @@ -176,11 +188,8 @@ module.exports = class Indexer {
  }, (await newEntry.getBlob()).content().toString('utf8'));
  }
  }
  - }
  - if (oldTree) {
  - for (let oldEntry of oldTree.entries()) {
  - let name = oldEntry.name();
  - if (!seen.get(name)) {
  +
  + async _deleteEntry(branch, oldEntry, bulkOps) {
  if (oldEntry.isTree()) {
  await this._indexTree(branch, await oldEntry.getTree(), null, bulkOps);
  } else {
  @@ -194,9 +203,6 @@ module.exports = class Indexer {
  });
  }
  }
  - }
  - }
  - }
  // 1. Index the branch into newIndex.
  // 2. Update the canonical elasticsearch alias for the branch to point at newIndex
  