delete method

  1. @override
Future<void> delete(
  1. String locator
)
override

Removes the whole container.

Implementation

@override
Future<void> delete(String locator) async {
  final loc = await _locate(locator);
  _snapshots.remove(loc.repoKey);
  final snapshot = await _snapshot(loc);
  if (snapshot == null) return;

  final entry = snapshot.entryAt(loc.path);
  final paths = entry != null && entry.type == 'blob'
      ? [loc.path]
      : [
          for (final blob in snapshot.tree.blobsUnder(loc.path).keys)
            '${loc.path}/$blob',
        ];
  if (paths.isEmpty) return;

  await _wrap(locator, 'delete', () async {
    final tree = await _api.git.createTree(
      owner: loc.owner,
      repo: loc.repo,
      entries: [for (final path in paths) GitHubTreeEntry.deletion(path)],
      baseTreeSha: snapshot.treeSha,
    );
    final commit = await _api.git.createCommit(
      owner: loc.owner,
      repo: loc.repo,
      message: '${commitMessagePrefix}delete ${_basename(loc.path)}',
      treeSha: tree.sha,
      parentShas: [snapshot.commitSha],
      author: committer,
      committer: committer,
    );
    await _api.git.updateRef(
      owner: loc.owner,
      repo: loc.repo,
      ref: loc.refName,
      sha: commit.sha,
    );
    // Same reason as on a save: without this, a replica still serving the
    // pre-delete commit would report the document as present again.
    _recordAuthority(loc, commit.sha, tree.sha, snapshot.commitSha);
    _snapshots.remove(loc.repoKey);
    // The listing on disk still names what was just deleted, and offline it
    // would be served.
    await cache?.removeMeta(_snapshotKey(loc));
  });
}