Skip to content

Commit

Permalink
Skip COMMIT_EDITMSG contents when opening the file
Browse files Browse the repository at this point in the history
Zed does not use its contents, but only saves into this file before committing.
  • Loading branch information
SomeoneToIgnore committed Feb 3, 2025
1 parent 8d20e94 commit f9dc0dd
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 23 deletions.
5 changes: 1 addition & 4 deletions crates/git_ui/src/git_panel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ fn commit_message_buffer(
worktree_id: worktree.read(cx).id(),
path: Arc::from(relative_path),
},
true,
cx,
)
})
Expand Down Expand Up @@ -248,10 +249,6 @@ impl GitPanel {
.detach();

let commit_editor = cx.new(|cx| commit_message_editor(None, window, cx));
commit_editor.update(cx, |editor, cx| {
editor.clear(window, cx);
});

let scroll_handle = UniformListScrollHandle::new();

cx.subscribe_in(
Expand Down
16 changes: 12 additions & 4 deletions crates/project/src/buffer_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ impl RemoteBufferStore {
&self,
path: Arc<Path>,
worktree: Entity<Worktree>,
skip_file_contents: bool,
cx: &mut Context<BufferStore>,
) -> Task<Result<Entity<Buffer>>> {
let worktree_id = worktree.read(cx).id().to_proto();
Expand All @@ -341,6 +342,7 @@ impl RemoteBufferStore {
project_id,
worktree_id,
path: path_string,
skip_file_contents,
})
.await?;
let buffer_id = BufferId::new(response.buffer_id)?;
Expand Down Expand Up @@ -786,10 +788,11 @@ impl LocalBufferStore {
&self,
path: Arc<Path>,
worktree: Entity<Worktree>,
skip_file_contents: bool,
cx: &mut Context<BufferStore>,
) -> Task<Result<Entity<Buffer>>> {
let load_buffer = worktree.update(cx, |worktree, cx| {
let load_file = worktree.load_file(path.as_ref(), cx);
let load_file = worktree.load_file(path.as_ref(), skip_file_contents, cx);
let reservation = cx.reserve_entity();
let buffer_id = BufferId::from(reservation.entity_id().as_non_zero_u64());
cx.spawn(move |_, mut cx| async move {
Expand Down Expand Up @@ -973,6 +976,7 @@ impl BufferStore {
pub fn open_buffer(
&mut self,
project_path: ProjectPath,
skip_file_contents: bool,
cx: &mut Context<Self>,
) -> Task<Result<Entity<Buffer>>> {
if let Some(buffer) = self.get_by_path(&project_path, cx) {
Expand All @@ -991,8 +995,12 @@ impl BufferStore {
return Task::ready(Err(anyhow!("no such worktree")));
};
let load_buffer = match &self.state {
BufferStoreState::Local(this) => this.open_buffer(path, worktree, cx),
BufferStoreState::Remote(this) => this.open_buffer(path, worktree, cx),
BufferStoreState::Local(this) => {
this.open_buffer(path, worktree, skip_file_contents, cx)
}
BufferStoreState::Remote(this) => {
this.open_buffer(path, worktree, skip_file_contents, cx)
}
};

entry
Expand Down Expand Up @@ -1485,7 +1493,7 @@ impl BufferStore {
let buffers = this.update(&mut cx, |this, cx| {
project_paths
.into_iter()
.map(|project_path| this.open_buffer(project_path, cx))
.map(|project_path| this.open_buffer(project_path, false, cx))
.collect::<Vec<_>>()
})?;
for buffer_task in buffers {
Expand Down
2 changes: 1 addition & 1 deletion crates/project/src/lsp_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5616,7 +5616,7 @@ impl LspStore {
lsp_store
.update(&mut cx, |lsp_store, cx| {
lsp_store.buffer_store().update(cx, |buffer_store, cx| {
buffer_store.open_buffer(project_path, cx)
buffer_store.open_buffer(project_path, false, cx)
})
})?
.await
Expand Down
42 changes: 34 additions & 8 deletions crates/project/src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1933,7 +1933,21 @@ impl Project {
}

self.buffer_store.update(cx, |buffer_store, cx| {
buffer_store.open_buffer(path.into(), cx)
buffer_store.open_buffer(path.into(), false, cx)
})
}

pub fn open_buffer_without_contents(
&mut self,
path: impl Into<ProjectPath>,
cx: &mut Context<Self>,
) -> Task<Result<Entity<Buffer>>> {
if self.is_disconnected(cx) {
return Task::ready(Err(anyhow!(ErrorCode::Disconnected)));
}

self.buffer_store.update(cx, |buffer_store, cx| {
buffer_store.open_buffer(path.into(), true, cx)
})
}

Expand Down Expand Up @@ -3937,14 +3951,25 @@ impl Project {
) -> Result<proto::OpenBufferResponse> {
let peer_id = envelope.original_sender_id()?;
let worktree_id = WorktreeId::from_proto(envelope.payload.worktree_id);
let skip_file_contents = envelope.payload.skip_file_contents;
let open_buffer = this.update(&mut cx, |this, cx| {
this.open_buffer(
ProjectPath {
worktree_id,
path: PathBuf::from(envelope.payload.path).into(),
},
cx,
)
if skip_file_contents {
this.open_buffer_without_contents(
ProjectPath {
worktree_id,
path: PathBuf::from(envelope.payload.path).into(),
},
cx,
)
} else {
this.open_buffer(
ProjectPath {
worktree_id,
path: PathBuf::from(envelope.payload.path).into(),
},
cx,
)
}
})?;

let buffer = open_buffer.await?;
Expand Down Expand Up @@ -4091,6 +4116,7 @@ impl Project {
worktree_id: worktree.read(cx).id(),
path: Arc::from(relative_path),
},
true,
cx,
)
})
Expand Down
1 change: 1 addition & 0 deletions crates/proto/proto/zed.proto
Original file line number Diff line number Diff line change
Expand Up @@ -795,6 +795,7 @@ message OpenBufferByPath {
uint64 project_id = 1;
uint64 worktree_id = 2;
string path = 3;
bool skip_file_contents = 4;
}

message OpenBufferById {
Expand Down
3 changes: 3 additions & 0 deletions crates/remote_server/src/headless_project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,7 @@ impl HeadlessProject {
worktree_id,
path: PathBuf::from(message.payload.path).into(),
},
message.payload.skip_file_contents,
cx,
)
});
Expand Down Expand Up @@ -487,6 +488,7 @@ impl HeadlessProject {
worktree_id: worktree.read(cx).id(),
path: path.into(),
},
false,
cx,
)
});
Expand Down Expand Up @@ -749,6 +751,7 @@ impl HeadlessProject {
worktree_id: worktree.read(cx).id(),
path: Arc::from(relative_path),
},
true,
cx,
)
})
Expand Down
22 changes: 18 additions & 4 deletions crates/worktree/src/worktree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -862,9 +862,14 @@ impl Worktree {
}
}

pub fn load_file(&self, path: &Path, cx: &Context<Worktree>) -> Task<Result<LoadedFile>> {
pub fn load_file(
&self,
path: &Path,
skip_file_contents: bool,
cx: &Context<Worktree>,
) -> Task<Result<LoadedFile>> {
match self {
Worktree::Local(this) => this.load_file(path, cx),
Worktree::Local(this) => this.load_file(path, skip_file_contents, cx),
Worktree::Remote(_) => {
Task::ready(Err(anyhow!("remote worktrees can't yet load files")))
}
Expand Down Expand Up @@ -1582,7 +1587,12 @@ impl LocalWorktree {
})
}

fn load_file(&self, path: &Path, cx: &Context<Worktree>) -> Task<Result<LoadedFile>> {
fn load_file(
&self,
path: &Path,
skip_file_contents: bool,
cx: &Context<Worktree>,
) -> Task<Result<LoadedFile>> {
let path = Arc::from(path);
let abs_path = self.absolutize(&path);
let fs = self.fs.clone();
Expand All @@ -1591,7 +1601,11 @@ impl LocalWorktree {

cx.spawn(|this, _cx| async move {
let abs_path = abs_path?;
let text = fs.load(&abs_path).await?;
let text = if skip_file_contents {
String::new()
} else {
fs.load(&abs_path).await?
};

let worktree = this
.upgrade()
Expand Down
4 changes: 2 additions & 2 deletions crates/worktree/src/worktree_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ async fn test_open_gitignored_files(cx: &mut TestAppContext) {
let prev_read_dir_count = fs.read_dir_call_count();
let loaded = tree
.update(cx, |tree, cx| {
tree.load_file("one/node_modules/b/b1.js".as_ref(), cx)
tree.load_file("one/node_modules/b/b1.js".as_ref(), false, cx)
})
.await
.unwrap();
Expand Down Expand Up @@ -507,7 +507,7 @@ async fn test_open_gitignored_files(cx: &mut TestAppContext) {
let prev_read_dir_count = fs.read_dir_call_count();
let loaded = tree
.update(cx, |tree, cx| {
tree.load_file("one/node_modules/a/a2.js".as_ref(), cx)
tree.load_file("one/node_modules/a/a2.js".as_ref(), false, cx)
})
.await
.unwrap();
Expand Down

0 comments on commit f9dc0dd

Please sign in to comment.