From 00ee06137ecda0bc00efce23797711a25990f10f Mon Sep 17 00:00:00 2001 From: peter schilling Date: Wed, 17 Dec 2025 07:32:37 -0800 Subject: [PATCH] Allow opening git commit view via URI scheme (#43341) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add support for `zed://git/commit/#` (**EDIT:** now changed to `zed://git/commit/?repo=`) URI scheme to access the git commit view implement parsing and handling of git commit URIs to navigate directly to commit views from external links. the main use case for me is to use OSC8 hyperlinks to link from a git sha into zed. this allows me e.g. to easily navigate from a terminal into zed **questions** - is this URI scheme appropriate? it was the first one i thought of, but wondering if `?ref=` might make more sense – the git/commit namespace was also an equally arbitrary choice
video demo showing navigation from zed's built in terminal https://github.com/user-attachments/assets/18ad7e64-6b39-44b2-a440-1a9eb71cd212
video demo showing navigation from ghostty to zed's commit view https://github.com/user-attachments/assets/1825e753-523f-4f98-b59c-7188ae2f5f19
Release Notes: - Added support for `zed://git/commit/?repo=` URI scheme to access the git commit view --------- Co-authored-by: Agus Zubiaga --- crates/zed/src/main.rs | 38 ++++++++++ crates/zed/src/zed/open_listener.rs | 107 ++++++++++++++++++++++++++++ 2 files changed, 145 insertions(+) diff --git a/crates/zed/src/main.rs b/crates/zed/src/main.rs index c8137a71c0f2a8524f6310d7cd711978ed833d1a..bd26812a1a3037e9d7fe0bf38c84c61143cc23e8 100644 --- a/crates/zed/src/main.rs +++ b/crates/zed/src/main.rs @@ -900,6 +900,44 @@ fn handle_open_request(request: OpenRequest, app_state: Arc, cx: &mut }) .detach_and_log_err(cx); } + OpenRequestKind::GitCommit { sha } => { + cx.spawn(async move |cx| { + let paths_with_position = + derive_paths_with_position(app_state.fs.as_ref(), request.open_paths).await; + let (workspace, _results) = open_paths_with_positions( + &paths_with_position, + &[], + app_state, + workspace::OpenOptions::default(), + cx, + ) + .await?; + + workspace + .update(cx, |workspace, window, cx| { + let Some(repo) = workspace.project().read(cx).active_repository(cx) + else { + log::error!("no active repository found for commit view"); + return Err(anyhow::anyhow!("no active repository found")); + }; + + git_ui::commit_view::CommitView::open( + sha, + repo.downgrade(), + workspace.weak_handle(), + None, + None, + window, + cx, + ); + Ok(()) + }) + .log_err(); + + anyhow::Ok(()) + }) + .detach_and_log_err(cx); + } } return; diff --git a/crates/zed/src/zed/open_listener.rs b/crates/zed/src/zed/open_listener.rs index 6352c20e5c0dcd0bd25063ca3a7bbcae87e48e3f..d61de0a291f3d3e7869225c0e07424cc3523f69b 100644 --- a/crates/zed/src/zed/open_listener.rs +++ b/crates/zed/src/zed/open_listener.rs @@ -58,6 +58,9 @@ pub enum OpenRequestKind { /// `None` opens settings without navigating to a specific path. setting_path: Option, }, + GitCommit { + sha: String, + }, } impl OpenRequest { @@ -110,6 +113,8 @@ impl OpenRequest { this.kind = Some(OpenRequestKind::Setting { setting_path: Some(setting_path.to_string()), }); + } else if let Some(commit_path) = url.strip_prefix("zed://git/commit/") { + this.parse_git_commit_url(commit_path)? } else if url.starts_with("ssh://") { this.parse_ssh_file_path(&url, cx)? } else if let Some(zed_link) = parse_zed_link(&url, cx) { @@ -138,6 +143,28 @@ impl OpenRequest { } } + fn parse_git_commit_url(&mut self, commit_path: &str) -> Result<()> { + // Format: ?repo= + let (sha, query) = commit_path + .split_once('?') + .context("invalid git commit url: missing query string")?; + anyhow::ensure!(!sha.is_empty(), "invalid git commit url: missing sha"); + + let repo = url::form_urlencoded::parse(query.as_bytes()) + .find_map(|(key, value)| (key == "repo").then_some(value)) + .filter(|s| !s.is_empty()) + .context("invalid git commit url: missing repo query parameter")? + .to_string(); + + self.open_paths.push(repo); + + self.kind = Some(OpenRequestKind::GitCommit { + sha: sha.to_string(), + }); + + Ok(()) + } + fn parse_ssh_file_path(&mut self, file: &str, cx: &App) -> Result<()> { let url = url::Url::parse(file)?; let host = url @@ -688,6 +715,86 @@ mod tests { assert_eq!(request.open_paths, vec!["/"]); } + #[gpui::test] + fn test_parse_git_commit_url(cx: &mut TestAppContext) { + let _app_state = init_test(cx); + + // Test basic git commit URL + let request = cx.update(|cx| { + OpenRequest::parse( + RawOpenRequest { + urls: vec!["zed://git/commit/abc123?repo=path/to/repo".into()], + ..Default::default() + }, + cx, + ) + .unwrap() + }); + + match request.kind.unwrap() { + OpenRequestKind::GitCommit { sha } => { + assert_eq!(sha, "abc123"); + } + _ => panic!("expected GitCommit variant"), + } + // Verify path was added to open_paths for workspace routing + assert_eq!(request.open_paths, vec!["path/to/repo"]); + + // Test with URL encoded path + let request = cx.update(|cx| { + OpenRequest::parse( + RawOpenRequest { + urls: vec!["zed://git/commit/def456?repo=path%20with%20spaces".into()], + ..Default::default() + }, + cx, + ) + .unwrap() + }); + + match request.kind.unwrap() { + OpenRequestKind::GitCommit { sha } => { + assert_eq!(sha, "def456"); + } + _ => panic!("expected GitCommit variant"), + } + assert_eq!(request.open_paths, vec!["path with spaces"]); + + // Test with empty path + cx.update(|cx| { + assert!( + OpenRequest::parse( + RawOpenRequest { + urls: vec!["zed://git/commit/abc123?repo=".into()], + ..Default::default() + }, + cx, + ) + .unwrap_err() + .to_string() + .contains("missing repo") + ); + }); + + // Test error case: missing SHA + let result = cx.update(|cx| { + OpenRequest::parse( + RawOpenRequest { + urls: vec!["zed://git/commit/abc123?foo=bar".into()], + ..Default::default() + }, + cx, + ) + }); + assert!(result.is_err()); + assert!( + result + .unwrap_err() + .to_string() + .contains("missing repo query parameter") + ); + } + #[gpui::test] async fn test_open_workspace_with_directory(cx: &mut TestAppContext) { let app_state = init_test(cx);