Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/collab/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,7 @@ impl Server {
.add_request_handler(forward_read_only_project_request::<proto::DownloadFileByPath>)
.add_request_handler(forward_read_only_project_request::<proto::GitGetBranches>)
.add_request_handler(forward_read_only_project_request::<proto::GetDefaultBranch>)
.add_request_handler(forward_read_only_project_request::<proto::BlameBufferAtRevision>)
.add_request_handler(forward_read_only_project_request::<proto::OpenUnstagedDiff>)
.add_request_handler(forward_read_only_project_request::<proto::OpenUncommittedDiff>)
.add_request_handler(forward_read_only_project_request::<proto::LspExtExpandMacro>)
Expand Down
64 changes: 63 additions & 1 deletion crates/collab/tests/integration/editor_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4214,7 +4214,10 @@ async fn test_git_blame_is_forwarded(cx_a: &mut TestAppContext, cx_b: &mut TestA
.into_iter()
.map(|(sha, message)| (sha.parse().unwrap(), message.into()))
.collect(),
tag_names: Default::default(),
tag_names: [("1b1b1b", vec!["v1.0.0".to_string()])]
.into_iter()
.map(|(sha, tag_names)| (sha.parse().unwrap(), tag_names))
.collect(),
};
client_a.fs().set_blame_for_repo(
Path::new(path!("/my-repo/.git")),
Expand Down Expand Up @@ -4300,6 +4303,10 @@ async fn test_git_blame_is_forwarded(cx_a: &mut TestAppContext, cx_b: &mut TestA
let details = blame.details_for_entry(*buffer, entry).unwrap();
assert_eq!(details.message, format!("message for idx-{}", idx));
}
assert_eq!(
blame.tag_names_for_entry(buffer_id_b, &blame_entry("1b1b1b", 0..1)),
vec![SharedString::from("v1.0.0")]
);
});
});

Expand Down Expand Up @@ -4375,6 +4382,61 @@ async fn test_git_blame_is_forwarded(cx_a: &mut TestAppContext, cx_b: &mut TestA
]
);
});

let revision = "0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d"
.parse::<git::Oid>()
.unwrap();
let content_at_revision = "old line1\nold line2\n";
let blame_at_revision = git::blame::Blame {
entries: vec![blame_entry("1b1b1b", 0..1), blame_entry("0d0d0d", 1..2)],
messages: [
("1b1b1b", "message for idx-0"),
("0d0d0d", "message for idx-1"),
]
.into_iter()
.map(|(sha, message)| (sha.parse().unwrap(), message.into()))
.collect(),
tag_names: [("0d0d0d", vec!["v1.0.0".to_string()])]
.into_iter()
.map(|(sha, tag_names)| (sha.parse().unwrap(), tag_names))
.collect(),
};
client_a
.fs()
.with_git_state(Path::new(path!("/my-repo/.git")), true, |state| {
state.refs.insert("HEAD".into(), revision.to_string());
state
.head_contents
.insert(repo_path("file.txt"), content_at_revision.to_string());
state
.blames_at_revision
.insert((repo_path("file.txt"), revision), blame_at_revision.clone());
})
.unwrap();

cx_a.executor().run_until_parked();
cx_b.executor().run_until_parked();

let repository_b = project_b.read_with(cx_b, |project, cx| {
project
.git_store()
.read(cx)
.repositories()
.values()
.next()
.unwrap()
.clone()
});

let (content, forwarded_blame) = repository_b
.update(cx_b, |repository, cx| {
repository.blame_buffer_at_revision(repo_path("file.txt"), revision, cx)
})
.await
.unwrap();

assert_eq!(content, content_at_revision);
assert_eq!(forwarded_blame, blame_at_revision);
}

#[gpui::test(iterations = 30)]
Expand Down
4 changes: 4 additions & 0 deletions crates/editor/src/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -876,6 +876,10 @@ actions!(
ToggleGitBlameInline,
/// Opens the git commit for the blame at cursor.
OpenGitBlameCommit,
/// Opens a blame of the file at the revision of the blame entry at cursor.
BlameRevision,
/// Opens a blame of the file at the revision preceding the blame entry at cursor.
BlamePreviousRevision,
/// Toggles the diagnostics panel.
ToggleDiagnostics,
/// Toggles indent guides display.
Expand Down
11 changes: 8 additions & 3 deletions crates/editor/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ pub use element::{
CursorLayout, EditorElement, HighlightedRange, HighlightedRangeLine, PointForPosition,
file_status_label_color, render_breadcrumb_text,
};
pub use git::blame::BlameRenderer;
pub use git::blame::{BlameRenderer, GitBlame};
pub use git::{
DiffHunkDelegate, ResolvedDiffHunk, ResolvedDiffHunks, RestoreOnlyDiffHunkDelegate,
RestoreOnlyUnstagedDiffHunkDelegate, UncommittedDiffHunkDelegate, render_diff_hunk_controls,
Expand Down Expand Up @@ -158,7 +158,7 @@ use futures::{
future::{self, Shared},
};
use fuzzy::{StringMatch, StringMatchCandidate};
use git::blame::{GitBlame, GlobalBlameRenderer};
use git::blame::GlobalBlameRenderer;
use gpui::{
Action, Animation, AnimationExt, AnyElement, App, AppContext, AsyncWindowContext,
AvailableSpace, Background, Bounds, ClickEvent, ClipboardEntry, ClipboardItem, Context,
Expand Down Expand Up @@ -3330,7 +3330,12 @@ impl Editor {
cx.notify();
return;
}
if self.show_git_blame_gutter {
if self.show_git_blame_gutter
&& !self
.blame
.as_ref()
.is_some_and(|blame| blame.read(cx).is_static())
{
self.show_git_blame_gutter = false;
cx.notify();
return;
Expand Down
6 changes: 5 additions & 1 deletion crates/editor/src/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,11 @@ impl EditorElement {
register_action(editor, window, Editor::copy_file_location);
register_action(editor, window, Editor::toggle_git_blame);
register_action(editor, window, Editor::toggle_git_blame_inline);
register_action(editor, window, Editor::open_git_blame_commit);
if editor.read(cx).blame().is_some() {
register_action(editor, window, Editor::open_git_blame_commit);
register_action(editor, window, Editor::blame_revision);
register_action(editor, window, Editor::blame_previous_revision);
}
register_action(editor, window, Editor::toggle_selected_diff_hunks);
register_action(editor, window, Editor::toggle_staged_selected_diff_hunks);
register_action(editor, window, Editor::stage_and_next);
Expand Down
133 changes: 126 additions & 7 deletions crates/editor/src/git.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
pub(super) mod blame;

use super::*;
use ::git::{Restore, blame::BlameEntry, commit::ParsedCommitMessage, status::FileStatus};
use ::git::{
Oid, Restore, blame::BlameEntry, commit::ParsedCommitMessage, repository::RepoPath,
status::FileStatus,
};
use buffer_diff::{BufferDiff, DiffHunkStatus, DiffHunkStatusKind};
use project::git_store::Repository;

#[derive(Clone)]
pub struct ResolvedDiffHunk {
Expand Down Expand Up @@ -2145,15 +2149,36 @@ impl Editor {
})
}

pub fn set_blame(
&mut self,
blame: Entity<GitBlame>,
window: &mut Window,
cx: &mut Context<Self>,
) {
self.blame_subscription = Some(cx.observe_in(&blame, window, |_, _, _, cx| cx.notify()));
self.blame = Some(blame);
self.show_git_blame_gutter = true;
cx.notify();
}

fn start_git_blame(
&mut self,
user_triggered: bool,
window: &mut Window,
cx: &mut Context<Self>,
) {
if self
.blame
.as_ref()
.is_some_and(|blame| blame.read(cx).is_static())
{
return;
}
if let Some(project) = self.project() {
if let Some(buffer) = self.buffer().read(cx).as_singleton()
&& buffer.read(cx).file().is_none()
&& buffer.read(cx).file().is_none_or(|file| {
matches!(file.disk_state(), language::DiskState::Historic { .. })
})
{
return;
}
Expand Down Expand Up @@ -2248,7 +2273,19 @@ impl Editor {
window: &mut Window,
cx: &mut Context<Self>,
) -> Option<()> {
let blame = self.blame.as_ref()?;
let (blame_entry, repo) = self.blame_entry_at_cursor(window, cx)?;
let renderer = cx.global::<GlobalBlameRenderer>().0.clone();
let workspace = self.workspace()?.downgrade();
renderer.open_blame_commit(blame_entry, repo, workspace, window, cx);
None
}

fn blame_entry_at_cursor(
&mut self,
window: &mut Window,
cx: &mut Context<Self>,
) -> Option<(BlameEntry, Entity<Repository>)> {
let blame = self.blame.clone()?;
let snapshot = self.snapshot(window, cx);
let cursor = self
.selections
Expand All @@ -2269,11 +2306,93 @@ impl Editor {
.next()
})
.flatten()?;
let repository = blame.read(cx).repository(cx, buffer.remote_id())?;
Some((blame_entry, repository))
}

pub(super) fn blame_revision(
&mut self,
_: &BlameRevision,
window: &mut Window,
cx: &mut Context<Self>,
) {
let Some((blame_entry, repository)) = self.blame_entry_at_cursor(window, cx) else {
self.show_blame_revision_toast("No blame entry for this line", cx);
return;
};
if blame_entry.sha.is_zero() {
self.show_blame_revision_toast("Cannot blame revision: the line is not committed", cx);
return;
}
if self
.blame
.as_ref()
.is_some_and(|blame| blame.read(cx).highlighted_sha() == Some(blame_entry.sha))
{
self.show_blame_revision_toast("Already blaming at this revision", cx);
return;
}
let Some(path) = RepoPath::new(&blame_entry.filename).log_err() else {
return;
};
self.open_blame_revision(path, blame_entry.sha, repository, window, cx);
}

pub(super) fn blame_previous_revision(
&mut self,
_: &BlamePreviousRevision,
window: &mut Window,
cx: &mut Context<Self>,
) {
let Some((blame_entry, repository)) = self.blame_entry_at_cursor(window, cx) else {
self.show_blame_revision_toast("No blame entry for this line", cx);
return;
};
let Some((revision, filename)) = blame_entry.previous_sha_and_filename() else {
self.show_blame_revision_toast("No previous revision for this line", cx);
return;
};
let Some(path) = RepoPath::new(filename).log_err() else {
return;
};
self.open_blame_revision(path, revision, repository, window, cx);
}

fn open_blame_revision(
&mut self,
path: RepoPath,
revision: Oid,
repository: Entity<Repository>,
window: &mut Window,
cx: &mut Context<Self>,
) {
let Some(workspace) = self.workspace() else {
return;
};
let renderer = cx.global::<GlobalBlameRenderer>().0.clone();
let repo = blame.read(cx).repository(cx, buffer.remote_id())?;
let workspace = self.workspace()?.downgrade();
renderer.open_blame_commit(blame_entry, repo, workspace, window, cx);
None
renderer.open_blame_revision(
path,
revision,
repository,
workspace.downgrade(),
window,
cx,
);
}

fn show_blame_revision_toast(&self, message: &str, cx: &mut Context<Self>) {
struct BlameRevisionToast;
if let Some(workspace) = self.workspace() {
workspace.update(cx, |workspace, cx| {
workspace.show_toast(
Toast::new(
NotificationId::unique::<BlameRevisionToast>(),
message.to_owned(),
),
cx,
);
});
}
}

fn has_blame_entries(&self, cx: &App) -> bool {
Expand Down
Loading
Loading