Skip to content
This repository has been archived by the owner on Jul 23, 2019. It is now read-only.

Commit

Permalink
Merge pull request #178 from atom/base-path
Browse files Browse the repository at this point in the history
Expose basePath in WorkTree.prototype.entries
  • Loading branch information
Antonio Scandurra authored Apr 3, 2019
2 parents 0bafe4f + 3d0a73d commit c191c5f
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 4 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 9 additions & 3 deletions memo_core/src/epoch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub struct Epoch {
}

pub struct Cursor<'a> {
text_files: &'a HashMap<FileId, TextFile>,
epoch: &'a Epoch,
metadata_cursor: btree::Cursor<Metadata>,
parent_ref_cursor: btree::Cursor<ParentRefValue>,
child_ref_cursor: btree::Cursor<ChildRefValue>,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -1324,6 +1324,11 @@ impl<'a> Cursor<'a> {
}
}

pub fn base_path(&self) -> Result<Option<PathBuf>, 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);
Expand Down Expand Up @@ -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())
}
Expand Down
1 change: 1 addition & 0 deletions memo_js/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
3 changes: 2 additions & 1 deletion memo_js/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
4 changes: 4 additions & 0 deletions memo_js/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ struct Entry {
depth: usize,
name: String,
path: String,
#[serde(rename = "basePath")]
base_path: Option<String>,
status: memo::FileStatus,
visible: bool,
}
Expand Down Expand Up @@ -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,
});
Expand Down
67 changes: 67 additions & 0 deletions memo_js/test/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ suite("WorkTree", () => {
type: FileType.Directory,
name: "a",
path: "a",
basePath: "a",
status: FileStatus.Unchanged,
visible: true
},
Expand All @@ -117,6 +118,7 @@ suite("WorkTree", () => {
type: FileType.Text,
name: "e",
path: "e",
basePath: null,
status: FileStatus.New,
visible: true
},
Expand All @@ -125,6 +127,7 @@ suite("WorkTree", () => {
type: FileType.Text,
name: "f",
path: "f",
basePath: null,
status: FileStatus.New,
visible: true
}
Expand All @@ -137,6 +140,7 @@ suite("WorkTree", () => {
type: FileType.Directory,
name: "a",
path: "a",
basePath: "a",
status: FileStatus.Unchanged,
visible: true
},
Expand All @@ -145,6 +149,7 @@ suite("WorkTree", () => {
type: FileType.Directory,
name: "b",
path: "a/b",
basePath: "a/b",
status: FileStatus.Unchanged,
visible: true
},
Expand All @@ -153,6 +158,7 @@ suite("WorkTree", () => {
type: FileType.Text,
name: "c",
path: "a/b/c",
basePath: "a/b/c",
status: FileStatus.Modified,
visible: true
},
Expand All @@ -161,6 +167,7 @@ suite("WorkTree", () => {
type: FileType.Directory,
name: "d",
path: "a/b/d",
basePath: "a/b/d",
status: FileStatus.Removed,
visible: false
},
Expand All @@ -169,6 +176,7 @@ suite("WorkTree", () => {
type: FileType.Directory,
name: "x",
path: "a/b/x",
basePath: null,
status: FileStatus.New,
visible: true
},
Expand All @@ -177,6 +185,7 @@ suite("WorkTree", () => {
type: FileType.Text,
name: "e",
path: "e",
basePath: null,
status: FileStatus.New,
visible: true
},
Expand All @@ -185,6 +194,7 @@ suite("WorkTree", () => {
type: FileType.Text,
name: "f",
path: "f",
basePath: null,
status: FileStatus.New,
visible: true
}
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit c191c5f

Please sign in to comment.