From 3d0a73d0f69415ca1ad5430f8bd86851b5a25815 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Wed, 3 Apr 2019 09:07:05 +0200 Subject: [PATCH] Expose basePath in WorkTree.prototype.entries --- Cargo.lock | 2 ++ memo_core/src/epoch.rs | 12 ++++++-- memo_js/README.md | 1 + memo_js/src/index.ts | 3 +- memo_js/src/lib.rs | 4 +++ memo_js/test/tests.ts | 67 ++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 85 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 613b44b3..a78dc5aa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,3 +1,5 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. [[package]] name = "aho-corasick" version = "0.6.4" diff --git a/memo_core/src/epoch.rs b/memo_core/src/epoch.rs index a211fdfc..e65a7412 100644 --- a/memo_core/src/epoch.rs +++ b/memo_core/src/epoch.rs @@ -38,7 +38,7 @@ pub struct Epoch { } pub struct Cursor<'a> { - text_files: &'a HashMap, + epoch: &'a Epoch, metadata_cursor: btree::Cursor, parent_ref_cursor: btree::Cursor, child_ref_cursor: btree::Cursor, @@ -231,7 +231,7 @@ impl Epoch { let parent_ref_cursor = self.parent_refs.cursor(); let child_ref_cursor = self.child_refs.cursor(); let mut cursor = Cursor { - text_files: &self.text_files, + epoch: &self, metadata_cursor, parent_ref_cursor, child_ref_cursor, @@ -1324,6 +1324,11 @@ impl<'a> Cursor<'a> { } } + pub fn base_path(&self) -> Result, Error> { + let metadata = self.metadata_cursor.item().ok_or(Error::CursorExhausted)?; + Ok(self.epoch.base_path(metadata.file_id)) + } + fn descend_into(&mut self, parent_visible: bool, dir_id: FileId) -> bool { let mut child_ref_cursor = self.child_ref_cursor.clone(); child_ref_cursor.seek(&dir_id, SeekBias::Left); @@ -1363,7 +1368,8 @@ impl<'a> Cursor<'a> { } fn is_modified_file(&self, file_id: FileId) -> bool { - self.text_files + self.epoch + .text_files .get(&file_id) .map_or(false, |f| f.is_modified()) } diff --git a/memo_js/README.md b/memo_js/README.md index 9baf4fe1..72b47af6 100644 --- a/memo_js/README.md +++ b/memo_js/README.md @@ -87,6 +87,7 @@ Each returned entry has the following fields: - `depth`: The length of the path leading to this entry. - `name`: The entry's name. - `path`: The entry's path. +- `basePath`: The entry's original path at the beginning of the commit. - `type`: The type of this file (`"File"` or `"Directory"`) - `status`: How this path has changed since the base commit (`"New"`, `"Renamed"`, `"Removed"`, `"Modified"`, `"RenamedAndModified"`, or `"Unchanged"`) - `visible`: Whether or not this file is currently visible (not deleted). diff --git a/memo_js/src/index.ts b/memo_js/src/index.ts index d100eaf7..34dbb966 100644 --- a/memo_js/src/index.ts +++ b/memo_js/src/index.ts @@ -65,7 +65,8 @@ export interface Entry { readonly depth: number; readonly type: FileType; readonly name: string; - readonly path: string; + readonly path: Path; + readonly basePath: Path | null; readonly status: FileStatus; readonly visible: boolean; } diff --git a/memo_js/src/lib.rs b/memo_js/src/lib.rs index e77e34b2..4977cc71 100644 --- a/memo_js/src/lib.rs +++ b/memo_js/src/lib.rs @@ -72,6 +72,8 @@ struct Entry { depth: usize, name: String, path: String, + #[serde(rename = "basePath")] + base_path: Option, status: memo::FileStatus, visible: bool, } @@ -366,11 +368,13 @@ impl WorkTree { let mut descend = false; if show_deleted || entry.status != memo::FileStatus::Removed { let path = cursor.path().unwrap(); + let base_path = cursor.base_path().unwrap(); entries.push(Entry { file_type: entry.file_type, depth: entry.depth, name: entry.name.to_string_lossy().into_owned(), path: path.to_string_lossy().into_owned(), + base_path: base_path.map(|p| p.to_string_lossy().into_owned()), status: entry.status, visible: entry.visible, }); diff --git a/memo_js/test/tests.ts b/memo_js/test/tests.ts index ba841566..2beaa78a 100644 --- a/memo_js/test/tests.ts +++ b/memo_js/test/tests.ts @@ -109,6 +109,7 @@ suite("WorkTree", () => { type: FileType.Directory, name: "a", path: "a", + basePath: "a", status: FileStatus.Unchanged, visible: true }, @@ -117,6 +118,7 @@ suite("WorkTree", () => { type: FileType.Text, name: "e", path: "e", + basePath: null, status: FileStatus.New, visible: true }, @@ -125,6 +127,7 @@ suite("WorkTree", () => { type: FileType.Text, name: "f", path: "f", + basePath: null, status: FileStatus.New, visible: true } @@ -137,6 +140,7 @@ suite("WorkTree", () => { type: FileType.Directory, name: "a", path: "a", + basePath: "a", status: FileStatus.Unchanged, visible: true }, @@ -145,6 +149,7 @@ suite("WorkTree", () => { type: FileType.Directory, name: "b", path: "a/b", + basePath: "a/b", status: FileStatus.Unchanged, visible: true }, @@ -153,6 +158,7 @@ suite("WorkTree", () => { type: FileType.Text, name: "c", path: "a/b/c", + basePath: "a/b/c", status: FileStatus.Modified, visible: true }, @@ -161,6 +167,7 @@ suite("WorkTree", () => { type: FileType.Directory, name: "d", path: "a/b/d", + basePath: "a/b/d", status: FileStatus.Removed, visible: false }, @@ -169,6 +176,7 @@ suite("WorkTree", () => { type: FileType.Directory, name: "x", path: "a/b/x", + basePath: null, status: FileStatus.New, visible: true }, @@ -177,6 +185,7 @@ suite("WorkTree", () => { type: FileType.Text, name: "e", path: "e", + basePath: null, status: FileStatus.New, visible: true }, @@ -185,6 +194,7 @@ suite("WorkTree", () => { type: FileType.Text, name: "f", path: "f", + basePath: null, status: FileStatus.New, visible: true } @@ -217,6 +227,63 @@ suite("WorkTree", () => { assert.strictEqual(tree1.head(), null); }); + test("base path", async () => { + const OID_0 = "0".repeat(40); + + const git = new TestGitProvider(); + git.commit(OID_0, [ + { depth: 1, name: "a", type: FileType.Directory }, + { depth: 2, name: "b", type: FileType.Directory }, + { depth: 3, name: "c", type: FileType.Text, text: "oid0 base text" }, + { depth: 3, name: "d", type: FileType.Directory } + ]); + + const [tree, initOps] = await WorkTree.create(uuid(), OID_0, [], git); + await collectOps(initOps); + + tree.rename("a/b/c", "e"); + tree.remove("a/b/d"); + tree.createFile("f", FileType.Text); + assert.deepStrictEqual(tree.entries(), [ + { + depth: 1, + name: "a", + basePath: "a", + path: "a", + status: FileStatus.Unchanged, + type: FileType.Directory, + visible: true + }, + { + depth: 2, + name: "b", + path: "a/b", + basePath: "a/b", + status: FileStatus.Unchanged, + type: FileType.Directory, + visible: true + }, + { + depth: 1, + name: "e", + path: "e", + basePath: "a/b/c", + status: FileStatus.Renamed, + type: FileType.Text, + visible: true + }, + { + depth: 1, + name: "f", + path: "f", + basePath: null, + status: FileStatus.New, + type: FileType.Text, + visible: true + } + ]); + }); + test("selections", async () => { const OID = "0".repeat(40); const git = new TestGitProvider();