Git improvements (#24238)

- **Base diffs on uncommitted changes**
- **Show added files in project diff view**
- **Fix git panel optimism**

Release Notes:

- Git: update diffs to be relative to HEAD instead of the index; to pave
the way for showing which hunks are staged

---------

Co-authored-by: Cole <cole@zed.dev>
This commit is contained in:
Conrad Irwin 2025-02-04 23:09:41 -07:00 committed by GitHub
parent 22b7042b9e
commit 0963401a8d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 241 additions and 231 deletions

View file

@ -149,37 +149,32 @@ impl BufferChangeSetState {
) -> oneshot::Receiver<()> {
match diff_bases_change {
DiffBasesChange::SetIndex(index) => {
self.index_text = index.map(|mut text| {
text::LineEnding::normalize(&mut text);
Arc::new(text)
});
let mut index = index.unwrap_or_default();
text::LineEnding::normalize(&mut index);
self.index_text = Some(Arc::new(index));
self.index_changed = true;
}
DiffBasesChange::SetHead(head) => {
self.head_text = head.map(|mut text| {
text::LineEnding::normalize(&mut text);
Arc::new(text)
});
let mut head = head.unwrap_or_default();
text::LineEnding::normalize(&mut head);
self.head_text = Some(Arc::new(head));
self.head_changed = true;
}
DiffBasesChange::SetBoth(mut text) => {
if let Some(text) = text.as_mut() {
text::LineEnding::normalize(text);
}
self.head_text = text.map(Arc::new);
DiffBasesChange::SetBoth(text) => {
let mut text = text.unwrap_or_default();
text::LineEnding::normalize(&mut text);
self.head_text = Some(Arc::new(text));
self.index_text = self.head_text.clone();
self.head_changed = true;
self.index_changed = true;
}
DiffBasesChange::SetEach { index, head } => {
self.index_text = index.map(|mut text| {
text::LineEnding::normalize(&mut text);
Arc::new(text)
});
self.head_text = head.map(|mut text| {
text::LineEnding::normalize(&mut text);
Arc::new(text)
});
let mut index = index.unwrap_or_default();
text::LineEnding::normalize(&mut index);
let mut head = head.unwrap_or_default();
text::LineEnding::normalize(&mut head);
self.index_text = Some(Arc::new(index));
self.head_text = Some(Arc::new(head));
self.head_changed = true;
self.index_changed = true;
}

View file

@ -69,11 +69,13 @@ enum Message {
Unstage(GitRepo, Vec<RepoPath>),
}
pub enum Event {
RepositoriesUpdated,
pub enum GitEvent {
ActiveRepositoryChanged,
FileSystemUpdated,
GitStateUpdated,
}
impl EventEmitter<Event> for GitState {}
impl EventEmitter<GitEvent> for GitState {}
impl GitState {
pub fn new(
@ -103,7 +105,7 @@ impl GitState {
fn on_worktree_store_event(
&mut self,
worktree_store: Entity<WorktreeStore>,
_event: &WorktreeStoreEvent,
event: &WorktreeStoreEvent,
cx: &mut Context<'_, Self>,
) {
// TODO inspect the event
@ -172,7 +174,14 @@ impl GitState {
self.repositories = new_repositories;
self.active_index = new_active_index;
cx.emit(Event::RepositoriesUpdated);
match event {
WorktreeStoreEvent::WorktreeUpdatedGitRepositories(_) => {
cx.emit(GitEvent::GitStateUpdated);
}
_ => {
cx.emit(GitEvent::FileSystemUpdated);
}
}
}
pub fn all_repositories(&self) -> Vec<RepositoryHandle> {
@ -314,7 +323,7 @@ impl RepositoryHandle {
return;
};
git_state.active_index = Some(index);
cx.emit(Event::RepositoriesUpdated);
cx.emit(GitEvent::ActiveRepositoryChanged);
});
}