feat: rework window layout around panel stack (#36)
Rust / build (macos-latest, stable) (push) Canceled after 0s
Rust / build (ubuntu-latest, stable) (push) Canceled after 0s
Rust / build (windows-latest, stable) (push) Canceled after 0s

Reviewed-on: #36
This commit was merged in pull request #36.
This commit is contained in:
2026-11-24 03:42:54 +00:00
parent 81a46abbe5
commit a2abfb2f27
99 changed files with 6145 additions and 8891 deletions
+34 -27
View File
@@ -16,19 +16,24 @@ use ui::avatar::Avatar;
use ui::button::Button;
use ui::clipboard::ClipboardButton;
use ui::common::{Sizable as _, StyledExt as _, h_flex, v_flex};
use ui::empty_state::EmptyState;
use ui::icons::{Icon, IconRole};
use ui::input::InputState;
use ui::layout::panel::Panel;
use ui::scroll_area::ScrollArea;
use ui::spinner::Spinner;
use ui::tabs::{Tab, Tabs};
use ui::theme::ActiveTheme as _;
use utils::relative_time;
use super::dialogs;
use crate::issues::helpers::{comment_form, comments_section, pr_roots, sidebar_section};
use crate::app::AppRoot;
use crate::issues::helpers::{comment_form, comments_section, sidebar_section};
use crate::repo::commit_diff::{CommitDiffView, DiffPane};
use crate::shared::widgets::{empty_state, status_badge};
use crate::workspace::Workspace;
use crate::shared::event_list::commit_list;
use crate::shared::log_dropped;
use crate::shared::panels::open_detail;
use crate::shared::widgets::status_badge;
const NOT_FOUND: &str = "Pull request not found";
@@ -44,7 +49,7 @@ struct PrBinding {
}
pub struct PullRequestDetailView {
workspace: WeakEntity<Workspace>,
workspace: WeakEntity<AppRoot>,
store: Entity<RepoStore>,
pr_id: EventId,
update_dialog: Option<Entity<dialogs::update::UpdatePrDialog>>,
@@ -67,7 +72,7 @@ pub struct PullRequestDetailView {
impl PullRequestDetailView {
pub fn new(
workspace: WeakEntity<Workspace>,
workspace: WeakEntity<AppRoot>,
store: Entity<RepoStore>,
pr_id: EventId,
_window: &mut Window,
@@ -82,7 +87,7 @@ impl PullRequestDetailView {
});
cx.defer(move |cx| {
crate::shared::log_dropped(weak.update(cx, |this, cx| this.sync(cx)));
log_dropped(weak.update(cx, |this, cx| this.sync(cx)));
});
Self {
@@ -300,7 +305,7 @@ impl PullRequestDetailView {
};
let workspace = self.workspace.clone();
let commit_id = commit_id.to_owned();
crate::shared::panels::open_detail(
open_detail(
&workspace,
id,
move |window, cx| {
@@ -336,7 +341,7 @@ impl PullRequestDetailView {
]
})
.on_change(move |index, _, cx| {
crate::shared::log_dropped(weak.update(cx, |this, cx| {
log_dropped(weak.update(cx, |this, cx| {
this.active_tab = index;
cx.notify();
}));
@@ -346,10 +351,10 @@ impl PullRequestDetailView {
fn render_discussion(&mut self, cx: &mut Context<Self>) -> AnyElement {
if self.loading {
return centered_spinner();
return Spinner::centered("pr-loading");
}
if let Some(error) = self.error.clone() {
return empty_state(error);
return EmptyState::centered(error);
}
let (author, picture, age, root_id, pubkey) = {
let store = self.store.read(cx);
@@ -358,7 +363,7 @@ impl PullRequestDetailView {
.iter()
.find(|pr| pr.id == self.pr_id && pr.kind == Kind::GitPullRequest)
else {
return empty_state(NOT_FOUND);
return EmptyState::centered(NOT_FOUND);
};
let profile = self.directory.read(cx).profile(&root.pubkey, cx);
(
@@ -370,6 +375,8 @@ impl PullRequestDetailView {
)
};
let store = self.store.read(cx);
h_flex()
.size_full()
.min_h_0()
@@ -416,7 +423,7 @@ impl PullRequestDetailView {
.child(comment_form(
&self.store,
root_id,
pr_roots,
&store.pull_requests,
&self.comment_input,
"pr-comment",
cx,
@@ -426,7 +433,7 @@ impl PullRequestDetailView {
.child(sidebar_section(
&self.store,
root_id,
pr_roots,
&store.pull_requests,
true,
&self.directory,
cx,
@@ -436,29 +443,29 @@ impl PullRequestDetailView {
fn render_files(&self, _cx: &mut Context<Self>) -> AnyElement {
if self.loading {
return centered_spinner();
return Spinner::centered("pr-loading");
}
if let Some(error) = self.error.clone() {
return empty_state(error);
return EmptyState::centered(error);
}
self.pane.clone().into_any_element()
}
fn render_commits(&self, cx: &mut Context<Self>) -> AnyElement {
if self.loading {
return centered_spinner();
return Spinner::centered("pr-loading");
}
if let Some(error) = self.error.clone() {
return empty_state(error);
return EmptyState::centered(error);
}
if self.commits.is_empty() {
return empty_state("No commits found");
return EmptyState::centered("No commits found");
}
let commits = Arc::clone(&self.commits);
let worktree = self.worktree.clone();
let view = cx.entity().downgrade();
crate::shared::event_list::commit_list(
commit_list(
"pr-commits",
"pr-commits-scroll",
commits,
@@ -467,12 +474,11 @@ impl PullRequestDetailView {
move |commit, window, cx| {
if let Some(worktree) = worktree.clone() {
let id = commit.id.clone();
crate::shared::log_dropped(view.update(cx, |this, cx| {
log_dropped(view.update(cx, |this, cx| {
this.open_commit_diff(worktree, &id, window, cx);
}));
}
},
cx,
)
}
@@ -503,8 +509,9 @@ impl PullRequestDetailView {
.px_4()
.py_2()
.gap_2()
// The top right corner is the panel toolbar's.
.child(
h_flex().gap_2().child(status_badge(status)).child(
h_flex().gap_2().pr_16().child(status_badge(status)).child(
div()
.flex_1()
.min_w_0()
@@ -528,7 +535,7 @@ impl PullRequestDetailView {
.when(can_update, |this| {
this.child(Button::new("update-pr").label("Update").small().on_click(
move |_, _, cx| {
crate::shared::log_dropped(weak.update(cx, |this, cx| {
log_dropped(weak.update(cx, |this, cx| {
let root = this
.store
.read(cx)
@@ -577,6 +584,10 @@ impl Panel for PullRequestDetailView {
fn loading(&self, _cx: &App) -> bool {
self.loading
}
fn toolbar(&self, _cx: &App) -> bool {
true
}
}
impl Render for PullRequestDetailView {
@@ -595,7 +606,3 @@ impl Render for PullRequestDetailView {
})
}
}
fn centered_spinner() -> AnyElement {
crate::shared::widgets::centered_spinner("pr-loading")
}
@@ -4,14 +4,13 @@ use gpui::{
};
use nostr::prelude::Event;
use signed_state::RepoStore;
use ui::button::Button;
use ui::common::v_flex;
use ui::dialog::{Dialog, DialogHandle};
use ui::field::Field;
use ui::input::{Input, InputState};
use ui::theme::ActiveTheme as _;
use crate::shared::progress::DialogProgress;
use crate::shared::progress::{DialogProgress, dialog_submit_button};
pub struct UpdatePrDialog {
handle: DialogHandle,
@@ -80,14 +79,18 @@ impl Render for UpdatePrDialog {
let this = this.clone();
let busy = action_state.read(cx).busy;
vec![
Button::new("submit-update")
.label("Update pull request")
.loading(busy)
.disabled(busy)
.on_click(move |_, _, cx| {
crate::shared::log_dropped(this.update(cx, |this, cx| this.submit(cx)));
})
.into_any_element() as AnyElement,
dialog_submit_button(
"submit-update",
"Update pull request",
None,
busy,
&this,
|this, _window, cx| {
this.submit(cx);
Ok(())
},
)
.into_any_element() as AnyElement,
]
})
}
@@ -7,6 +7,7 @@ use ui::menu::MenuItem;
use utils::middle_truncate;
use super::NewPullRequestView;
use crate::repo::helpers::announcement_display_name;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PullRequestFilter {
@@ -77,8 +78,10 @@ pub fn source_menu_items(
.file_name()
.map(|name| name.to_string_lossy().into_owned())
.unwrap_or_else(|| path.display().to_string());
let target = view.clone();
let path_for_action = path.clone();
items.push(
MenuItem::new(truncate_label(&label))
.checked(active_path.as_ref() == Some(&path))
@@ -113,14 +116,12 @@ pub fn source_menu_items(
} else {
SharedString::from(format!("by {}", shorten_owner(&candidate.owner)))
};
let title = format!(
"{} — {}",
crate::repo::helpers::announcement_display_name(&candidate),
subtitle
);
let title = format!("{} — {}", announcement_display_name(&candidate), subtitle);
let target = view.clone();
let checked = active_fork == Some(candidate.addr());
let announcement = candidate;
items.push(
MenuItem::new(truncate_label(&title))
.checked(checked)
@@ -135,5 +136,6 @@ pub fn source_menu_items(
);
}
}
items
}
+14 -12
View File
@@ -29,13 +29,15 @@ use helpers::PullRequestFilter;
pub use new::NewPullRequestView;
pub use send_patch::SendPatchView;
use crate::app::AppRoot;
use crate::issues::helpers::{StatusCounts, filter_by_status};
use crate::repo::helpers::short_id;
use crate::shared::event_list::{EventList, EventRow};
use crate::workspace::Workspace;
use crate::shared::event_list::{EventHandler, EventList, EventRow, filter_toolbar};
use crate::shared::log_dropped;
use crate::shared::panels::open_detail;
pub struct PullRequestsView {
workspace: WeakEntity<Workspace>,
workspace: WeakEntity<AppRoot>,
store: Entity<RepoStore>,
filter: PullRequestFilter,
visible_prs: Vec<usize>,
@@ -49,7 +51,7 @@ pub struct PullRequestsView {
impl PullRequestsView {
pub fn new(
workspace: WeakEntity<Workspace>,
workspace: WeakEntity<AppRoot>,
store: Entity<RepoStore>,
cx: &mut Context<Self>,
) -> Self {
@@ -59,7 +61,7 @@ impl PullRequestsView {
});
cx.defer(move |cx| {
crate::shared::log_dropped(weak.update(cx, |this, cx| {
log_dropped(weak.update(cx, |this, cx| {
this.rebuild(cx);
}));
});
@@ -139,7 +141,7 @@ impl PullRequestsView {
let workspace_for_build = workspace.clone();
let store = self.store.clone();
crate::shared::panels::open_detail(
open_detail(
&workspace,
id.clone(),
move |window, cx| {
@@ -168,7 +170,7 @@ impl PullRequestsView {
let workspace_for_build = workspace.clone();
let store = self.store.clone();
crate::shared::panels::open_detail(
open_detail(
&workspace,
id.clone(),
move |window, cx| {
@@ -195,7 +197,7 @@ impl PullRequestsView {
let menu_open_weak = cx.entity().downgrade();
let patch_weak = cx.entity().downgrade();
crate::shared::event_list::filter_toolbar(
filter_toolbar(
cx,
[
Segmented::new("pr-filter", self.filter)
@@ -248,7 +250,7 @@ impl PullRequestsView {
}
})
.on_open_change(move |open, _, cx| {
crate::shared::log_dropped(menu_open_weak.update(cx, |this, cx| {
log_dropped(menu_open_weak.update(cx, |this, cx| {
this.new_pr_menu_open = open;
cx.notify();
}));
@@ -257,7 +259,7 @@ impl PullRequestsView {
vec![MenuItem::new("Send Patch").handler({
let weak = patch_weak.clone();
move |window, cx| {
crate::shared::log_dropped(weak.update(cx, |this, cx| {
log_dropped(weak.update(cx, |this, cx| {
this.open_send_patch(window, cx);
}));
}
@@ -302,14 +304,14 @@ impl Render for PullRequestsView {
let id_hex = pr.id.to_hex();
let status = store.read(cx).status_of(pr);
let on_click: crate::shared::event_list::EventHandler = {
let on_click: EventHandler = {
let workspace = workspace.clone();
let store = store.clone();
Rc::new(move |_, window, cx| {
let id = SharedString::from(format!("pr:{pr_id}"));
let workspace_for_build = workspace.clone();
let store = store.clone();
crate::shared::panels::open_detail(
open_detail(
&workspace,
id,
move |window, cx| {
+445 -63
View File
@@ -1,32 +1,38 @@
use std::path::PathBuf;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use gpui::prelude::FluentBuilder as _;
use gpui::{
AnyElement, App, AppContext as _, Context, Entity, InteractiveElement as _, IntoElement,
ParentElement as _, Render, SharedString, Styled as _, Subscription, UniformListScrollHandle,
WeakEntity, Window, div, px,
ParentElement as _, PathPromptOptions, Render, SharedString, Styled as _, Subscription,
UniformListScrollHandle, WeakEntity, Window, div, px,
};
use signed_state::{CheckoutsStore, RepoStore};
use ui::button::Button;
use nostr::prelude::Url;
use signed_core::Announcement;
use signed_git::{GitCache, Repo};
use signed_state::{Backend, CheckoutsStore, Mirrors, RepoListStore, RepoStore};
use ui::button::{Button, ButtonVariant};
use ui::combobox::{Combobox, ComboboxState};
use ui::common::{StyledExt as _, h_flex, v_flex};
use ui::empty_state::EmptyState;
use ui::icons::{Icon, IconRole};
use ui::input::{Input, InputState};
use ui::layout::panel::Panel;
use ui::menu::Menu;
use ui::spinner::Spinner;
use ui::tabs::{Tab, Tabs};
use super::helpers::{ForkCompare, truncate_label};
use crate::app::AppRoot;
use crate::repo::commit_diff::{CommitDiffView, DiffPane};
use crate::repo::helpers::announcement_display_name;
use crate::shared::event_list::commit_list;
use crate::shared::log_dropped;
use crate::shared::panels::{close_detail, open_detail};
use crate::shared::progress::error_row;
use crate::shared::widgets::empty_state;
use crate::workspace::Workspace;
mod source;
pub struct NewPullRequestView {
workspace: WeakEntity<Workspace>,
workspace: WeakEntity<AppRoot>,
panel_id: SharedString,
pub(super) store: Entity<RepoStore>,
pub(super) repo_path: Option<PathBuf>,
@@ -55,7 +61,7 @@ pub struct NewPullRequestView {
impl NewPullRequestView {
pub fn new(
workspace: WeakEntity<Workspace>,
workspace: WeakEntity<AppRoot>,
panel_id: SharedString,
store: Entity<RepoStore>,
window: &mut Window,
@@ -73,7 +79,7 @@ impl NewPullRequestView {
let weak = weak.clone();
move |value, _window, cx| {
if let Some(base) = value {
crate::shared::log_dropped(weak.update(cx, |this, cx| {
log_dropped(weak.update(cx, |this, cx| {
if this.base == base {
return;
}
@@ -94,7 +100,7 @@ impl NewPullRequestView {
let weak = weak.clone();
move |value, _window, cx| {
if let Some(compare) = value {
crate::shared::log_dropped(weak.update(cx, |this, cx| {
log_dropped(weak.update(cx, |this, cx| {
if this.compare == compare {
return;
}
@@ -110,7 +116,7 @@ impl NewPullRequestView {
});
window.defer(cx, move |window, cx| {
crate::shared::log_dropped(weak.update(cx, |this, cx| {
log_dropped(weak.update(cx, |this, cx| {
let Some(addr) = this.store.read(cx).addr().cloned() else {
return;
};
@@ -209,9 +215,7 @@ impl NewPullRequestView {
this.update(cx, |this, _cx| {
this.submitting = false;
})?;
crate::shared::log_dropped(
workspace.update(cx, |workspace, cx| workspace.close_detail(&panel_id, cx)),
);
close_detail(&workspace, &panel_id, cx);
Ok(())
});
self.push_task(task);
@@ -227,7 +231,7 @@ impl NewPullRequestView {
};
let workspace = self.workspace.clone();
let commit_id = commit_id.to_owned();
crate::shared::panels::open_detail(
open_detail(
&workspace,
id,
move |window, cx| {
@@ -270,9 +274,7 @@ impl NewPullRequestView {
let active_fork = self.fork.as_ref().map(|fork| fork.announcement.addr());
let can_submit = self.submit_enabled(cx);
let source_label = match &self.fork {
Some(fork) => truncate_label(&crate::repo::helpers::announcement_display_name(
&fork.announcement,
)),
Some(fork) => truncate_label(&announcement_display_name(&fork.announcement)),
None => self.repo_path.as_ref().map_or_else(
|| SharedString::from("No source"),
|path| truncate_label(&path.display().to_string()),
@@ -289,42 +291,45 @@ impl NewPullRequestView {
.child(
v_flex()
.gap_1()
.child(label("Merge Into"))
.child(div().text_xs().font_medium().child("Merge Into"))
.child(div().w(px(160.)).child(self.base_select.clone())),
)
.child(
v_flex()
.gap_1()
.child(label("Pull From"))
.child(div().text_xs().font_medium().child("Pull From"))
.child(div().w(px(160.)).child(self.compare_select.clone())),
)
.child(
v_flex().gap_1().child(label("Source")).child(
Menu::new("pr-source")
.open(self.source_open)
.on_change(move |open, _, cx| {
crate::shared::log_dropped(weak_menu.update(cx, |this, cx| {
this.source_open = open;
cx.notify();
}));
})
.trigger(move |_, _| {
Button::new("compare-source")
.label(source_label.clone())
.variant(ui::button::ButtonVariant::Secondary)
.into_any_element()
})
.items(move |_, _| {
super::helpers::source_menu_items(
source_view.clone(),
associated.clone(),
active_path.clone(),
candidates.clone(),
user,
active_fork.clone(),
)
}),
),
v_flex()
.gap_1()
.child(div().text_xs().font_medium().child("Source"))
.child(
Menu::new("pr-source")
.open(self.source_open)
.on_change(move |open, _, cx| {
log_dropped(weak_menu.update(cx, |this, cx| {
this.source_open = open;
cx.notify();
}));
})
.trigger(move |_, _| {
Button::new("compare-source")
.label(source_label.clone())
.variant(ButtonVariant::Secondary)
.into_any_element()
})
.items(move |_, _| {
super::helpers::source_menu_items(
source_view.clone(),
associated.clone(),
active_path.clone(),
candidates.clone(),
user,
active_fork.clone(),
)
}),
),
)
.when_some(refresh_fork, |this, fork| {
this.child(
@@ -343,9 +348,7 @@ impl NewPullRequestView {
.loading(self.submitting)
.disabled(!can_submit)
.on_click(move |_, window, cx| {
crate::shared::log_dropped(
weak.update(cx, |this, cx| this.submit(window, cx)),
);
log_dropped(weak.update(cx, |this, cx| this.submit(window, cx)));
}),
)
.into_any_element()
@@ -366,7 +369,7 @@ impl NewPullRequestView {
]
})
.on_change(move |index, _, cx| {
crate::shared::log_dropped(weak.update(cx, |this, cx| {
log_dropped(weak.update(cx, |this, cx| {
this.active_tab = index;
cx.notify();
}));
@@ -376,13 +379,13 @@ impl NewPullRequestView {
fn render_content(&self, cx: &mut Context<Self>) -> AnyElement {
if self.loading {
return crate::shared::widgets::centered_spinner("new-pr-loading");
return Spinner::centered("new-pr-loading");
}
if !self.has_source() {
return empty_state("Choose a local checkout or an announced fork to compare");
return EmptyState::centered("Choose a local checkout or an announced fork to compare");
}
if self.commits.is_none() && self.error.is_some() {
return empty_state("Nothing to compare");
return EmptyState::centered("Nothing to compare");
}
match self.active_tab {
0 => self.pane.clone().into_any_element(),
@@ -392,15 +395,15 @@ impl NewPullRequestView {
fn render_commits(&self, cx: &mut Context<Self>) -> AnyElement {
let Some(commits) = self.commits.as_ref() else {
return empty_state("No commits");
return EmptyState::centered("No commits");
};
if commits.is_empty() {
return empty_state("No commits between the branches");
return EmptyState::centered("No commits between the branches");
}
let commits = Arc::clone(commits);
let view = cx.entity().downgrade();
crate::shared::event_list::commit_list(
commit_list(
"new-pr-commits",
"new-pr-commits-scroll",
commits,
@@ -408,13 +411,383 @@ impl NewPullRequestView {
|_, _, _| None,
move |commit, window, cx| {
let id = commit.id.clone();
crate::shared::log_dropped(view.update(cx, |this, cx| {
log_dropped(view.update(cx, |this, cx| {
this.open_commit_diff(&id, window, cx);
}));
},
cx,
)
}
pub(super) fn has_source(&self) -> bool {
self.repo_path.is_some() || self.fork.is_some()
}
pub(super) fn work_path(&self) -> Option<PathBuf> {
match &self.fork {
Some(fork) => Some(fork.mirror_path.clone()),
None => self.repo_path.clone(),
}
}
pub(super) fn base_ref(&self) -> String {
match &self.fork {
Some(_) => ForkCompare::base_ref(&self.base),
None => self.base.to_string(),
}
}
pub(super) fn compare_ref(&self) -> String {
match &self.fork {
Some(fork) => fork.compare_ref(&self.compare),
None => self.compare.to_string(),
}
}
pub(crate) fn choose_checkout(&mut self, window: &mut Window, cx: &mut Context<Self>) {
let prompt = cx.prompt_for_paths(PathPromptOptions {
files: false,
directories: true,
multiple: false,
prompt: Some("Choose local checkout".into()),
});
let task: gpui::Task<Result<(), anyhow::Error>> =
cx.spawn_in(window, async move |this, cx| {
let picked = match prompt.await {
Ok(Ok(Some(mut paths))) => paths.pop(),
_ => None,
};
let Some(path) = picked else {
return Ok(());
};
this.update_in(cx, |this, window, cx| {
this.apply_folder_path(path, window, cx)
})?;
Ok(())
});
self.push_task(task);
}
pub(crate) fn apply_folder_path(
&mut self,
path: PathBuf,
window: &mut Window,
cx: &mut Context<Self>,
) {
self.fork_generation = self.fork_generation.wrapping_add(1);
let generation = self.fork_generation;
let path_string = path.to_string_lossy().to_string();
let task: gpui::Task<Result<(), anyhow::Error>> =
cx.spawn_in(window, async move |this, cx| {
let info = cx
.background_spawn({
let path = path.clone();
async move {
let repo = Repo::try_open(&path)?;
match repo.branches() {
Ok(branches) => Some((branches, repo.current_branch())),
Err(error) => {
log::warn!("failed to list branches: {error}");
None
}
}
}
})
.await;
this.update_in(cx, |this, window, cx| {
if generation != this.fork_generation {
return;
}
this.apply_checkout(path_string, info, window, cx);
})?;
Ok(())
});
self.push_task(task);
}
fn apply_checkout(
&mut self,
path: String,
info: Option<(Vec<String>, Option<String>)>,
window: &mut Window,
cx: &mut Context<Self>,
) {
self.fork = None;
let Some((branches, current)) = info else {
self.error = Some("The chosen folder is not a git repository".into());
self.repo_path = None;
self.branches.clear();
self.merge_base = None;
self.commits = None;
self.pane.update(cx, |pane, cx| pane.clear(cx));
cx.notify();
return;
};
if branches.is_empty() {
self.error = Some("The repository has no branches yet".into());
self.repo_path = None;
self.branches.clear();
self.merge_base = None;
self.commits = None;
self.pane.update(cx, |pane, cx| pane.clear(cx));
cx.notify();
return;
}
let announced = self.store.read(cx).head.clone();
let base = preferred_branch(&branches, announced);
let compare = current
.filter(|branch| branches.contains(branch))
.unwrap_or_else(|| base.clone());
self.repo_path = Some(PathBuf::from(&path));
self.error = None;
self.branches = branches.into_iter().map(SharedString::from).collect();
if let Some(addr) = self.store.read(cx).addr().cloned() {
CheckoutsStore::global(cx).update(cx, |store, cx| {
store.record(PathBuf::from(&path), addr, cx);
});
}
self.base = SharedString::from(base);
self.compare = SharedString::from(compare);
self.sync_selectors(window, cx);
self.reload_compare(cx);
}
fn sync_selectors(&mut self, window: &mut Window, cx: &mut Context<Self>) {
let options: Vec<(SharedString, SharedString)> = self
.branches
.iter()
.map(|branch| (branch.clone(), branch.clone()))
.collect();
let base = self.base.clone();
let compare = self.compare.clone();
self.base_select.update(cx, |state, cx| {
state.set_options(options.clone(), cx);
state.set_selected(Some(base), window, cx);
});
self.compare_select.update(cx, |state, cx| {
state.set_options(options, cx);
state.set_selected(Some(compare), window, cx);
});
}
pub(super) fn fork_candidates(&self, cx: &App) -> Vec<Announcement> {
let Some(addr) = self.store.read(cx).addr().cloned() else {
return Vec::new();
};
let euc = self
.store
.read(cx)
.announcement
.as_ref()
.and_then(|a| a.euc.clone());
let user = Backend::global(cx).read(cx).current_user();
let announcements = RepoListStore::global(cx).read(cx).announcements.clone();
Announcement::forks_in(&announcements, &addr, euc.as_deref(), user)
.into_iter()
.cloned()
.collect()
}
pub(crate) fn choose_fork(
&mut self,
announcement: Announcement,
window: &mut Window,
cx: &mut Context<Self>,
) {
let Some(base) = self.store.read(cx).addr().cloned() else {
return;
};
let mirror_path = Mirrors::path(&base);
let namespace = GitCache::fork_namespace(&announcement);
let clone_urls: Vec<Url> = announcement.clone.clone();
let base_clone_urls: Vec<Url> = self
.store
.read(cx)
.announcement
.as_ref()
.map(|a| a.clone.clone())
.unwrap_or_default();
self.fork_generation = self.fork_generation.wrapping_add(1);
let generation = self.fork_generation;
let handle = window.window_handle();
self.loading = true;
self.error = None;
cx.notify();
let task: gpui::Task<Result<(), anyhow::Error>> = cx.spawn(async move |this, cx| {
let result = cx
.background_spawn({
let namespace = namespace.clone();
let mirror_path = mirror_path.clone();
async move {
Mirrors::ensure(&base, &base_clone_urls)?;
let repo = Repo::open(&mirror_path)?;
repo.delete_refs_with_prefix("refs/fork")?;
repo.fetch_refs(
&clone_urls,
&format!("+refs/heads/*:refs/fork/{namespace}/*"),
)?;
let strip = |refs: Vec<String>, prefix: &str| {
let mut names: Vec<String> = refs
.into_iter()
.filter_map(|name| {
name.strip_prefix(prefix)
.map(|rest| rest.trim_start_matches('/').to_owned())
})
.filter(|name| !name.is_empty())
.collect();
names.sort();
names
};
let base_branches = strip(
repo.refs_with_prefix("refs/remotes/origin")?,
"refs/remotes/origin",
);
let compare_branches = strip(
repo.refs_with_prefix(&format!("refs/fork/{namespace}"))?,
&format!("refs/fork/{namespace}"),
);
Ok::<_, anyhow::Error>((base_branches, compare_branches))
}
})
.await;
if let Err(error) = cx.update_window(handle, |_, window, cx| {
this.update(cx, |this, cx| {
if generation != this.fork_generation {
return;
}
this.apply_fork(announcement, mirror_path, namespace, result, window, cx);
})
}) {
log::warn!("failed to apply fork: {error}");
}
Ok(())
});
self.push_task(task);
}
fn apply_fork(
&mut self,
announcement: Announcement,
mirror_path: PathBuf,
namespace: String,
result: Result<(Vec<String>, Vec<String>), anyhow::Error>,
window: &mut Window,
cx: &mut Context<Self>,
) {
self.loading = false;
let (base_branches, compare_branches) = match result {
Ok(branches) => branches,
Err(error) => {
self.error = Some(format!("Could not compare against the fork: {error}").into());
cx.notify();
return;
}
};
if compare_branches.is_empty() {
self.error = Some("The fork has no branches to compare".into());
cx.notify();
return;
}
if base_branches.is_empty() {
self.error = Some("Could not list the target repository's branches.".into());
cx.notify();
return;
}
let announced = self.store.read(cx).head.clone();
let base = preferred_branch(&base_branches, announced);
let compare = compare_branches
.iter()
.find(|branch| branch.as_str() == "main")
.map(|branch| SharedString::from(branch.clone()))
.unwrap_or_else(|| SharedString::from(compare_branches[0].clone()));
self.fork = Some(ForkCompare {
announcement,
namespace,
mirror_path,
});
self.error = None;
self.base = SharedString::from(base);
self.compare = compare;
self.branches = base_branches.into_iter().map(SharedString::from).collect();
self.sync_selectors(window, cx);
self.reload_compare(cx);
}
pub(super) fn reload_compare(&mut self, cx: &mut Context<Self>) {
let Some(repo_path) = self.work_path() else {
return;
};
let base = self.base_ref();
let compare = self.compare_ref();
let base_name = self.base.to_string();
let compare_name = self.compare.to_string();
self.loading = true;
self.error = None;
self.compare_generation += 1;
let generation = self.compare_generation;
cx.notify();
if base == compare {
self.loading = false;
self.merge_base = None;
self.commits = None;
self.pane.update(cx, |pane, cx| pane.clear(cx));
self.error = Some("Choose different base and compare branches".into());
cx.notify();
return;
}
let task: gpui::Task<Result<(), anyhow::Error>> = cx.spawn(async move |this, cx| {
let result = cx
.background_spawn({
async move {
let repo = Repo::open(Path::new(&repo_path))?;
let merge_base = repo.merge_base(&base, &compare)?.ok_or_else(|| {
anyhow::anyhow!(
"{base_name} and {compare_name} share no common ancestor"
)
})?;
let commits = repo.commit_range(&merge_base, &compare)?;
let diff = repo.range_diff(&merge_base, &compare)?;
Ok::<_, anyhow::Error>((merge_base, commits, diff))
}
})
.await;
this.update(cx, |this, cx| {
if generation != this.compare_generation {
return;
}
this.loading = false;
match result {
Ok((merge_base, commits, diff)) => {
this.merge_base = Some(merge_base);
this.commits = Some(Arc::new(commits));
this.pane.update(cx, |pane, cx| pane.set_diff(diff, cx));
}
Err(error) => {
this.merge_base = None;
this.commits = None;
this.pane.update(cx, |pane, cx| pane.clear(cx));
this.error = Some(error.to_string().into());
}
}
cx.notify();
})?;
Ok(())
});
self.push_task(task);
}
}
impl Panel for NewPullRequestView {
@@ -462,6 +835,15 @@ impl Render for NewPullRequestView {
}
}
fn label(text: &'static str) -> AnyElement {
div().text_xs().font_medium().child(text).into_any_element()
/// The announced head when listed, else `main`, else the first branch.
fn preferred_branch(branches: &[String], announced: Option<String>) -> String {
announced
.filter(|branch| branches.contains(branch))
.or_else(|| {
branches
.iter()
.find(|branch| branch.as_str() == "main")
.cloned()
})
.unwrap_or_else(|| branches[0].clone())
}
@@ -1,397 +0,0 @@
use std::path::{Path, PathBuf};
use std::sync::Arc;
use gpui::{App, AppContext as _, Context, PathPromptOptions, SharedString, Window};
use nostr::prelude::Url;
use signed_core::Announcement;
use signed_git::{GitCache, Repo};
use signed_state::{Backend, CheckoutsStore, Mirrors, RepoListStore};
use super::NewPullRequestView;
use crate::pull_requests::helpers::ForkCompare;
/// The announced head when listed, else `main`, else the first branch.
fn preferred_branch(branches: &[String], announced: Option<String>) -> String {
announced
.filter(|branch| branches.contains(branch))
.or_else(|| {
branches
.iter()
.find(|branch| branch.as_str() == "main")
.cloned()
})
.unwrap_or_else(|| branches[0].clone())
}
impl NewPullRequestView {
pub(super) fn has_source(&self) -> bool {
self.repo_path.is_some() || self.fork.is_some()
}
pub(super) fn work_path(&self) -> Option<PathBuf> {
match &self.fork {
Some(fork) => Some(fork.mirror_path.clone()),
None => self.repo_path.clone(),
}
}
pub(super) fn base_ref(&self) -> String {
match &self.fork {
Some(_) => ForkCompare::base_ref(&self.base),
None => self.base.to_string(),
}
}
pub(super) fn compare_ref(&self) -> String {
match &self.fork {
Some(fork) => fork.compare_ref(&self.compare),
None => self.compare.to_string(),
}
}
pub(crate) fn choose_checkout(&mut self, window: &mut Window, cx: &mut Context<Self>) {
let prompt = cx.prompt_for_paths(PathPromptOptions {
files: false,
directories: true,
multiple: false,
prompt: Some("Choose local checkout".into()),
});
let task: gpui::Task<Result<(), anyhow::Error>> =
cx.spawn_in(window, async move |this, cx| {
let picked = match prompt.await {
Ok(Ok(Some(mut paths))) => paths.pop(),
_ => None,
};
let Some(path) = picked else {
return Ok(());
};
this.update_in(cx, |this, window, cx| {
this.apply_folder_path(path, window, cx)
})?;
Ok(())
});
self.push_task(task);
}
pub(crate) fn apply_folder_path(
&mut self,
path: PathBuf,
window: &mut Window,
cx: &mut Context<Self>,
) {
self.fork_generation = self.fork_generation.wrapping_add(1);
let generation = self.fork_generation;
let path_string = path.to_string_lossy().to_string();
let task: gpui::Task<Result<(), anyhow::Error>> =
cx.spawn_in(window, async move |this, cx| {
let info = cx
.background_spawn({
let path = path.clone();
async move {
let repo = Repo::try_open(&path)?;
match repo.branches() {
Ok(branches) => Some((branches, repo.current_branch())),
Err(error) => {
log::warn!("failed to list branches: {error}");
None
}
}
}
})
.await;
this.update_in(cx, |this, window, cx| {
if generation != this.fork_generation {
return;
}
this.apply_checkout(path_string, info, window, cx);
})?;
Ok(())
});
self.push_task(task);
}
fn apply_checkout(
&mut self,
path: String,
info: Option<(Vec<String>, Option<String>)>,
window: &mut Window,
cx: &mut Context<Self>,
) {
self.fork = None;
let Some((branches, current)) = info else {
self.error = Some("The chosen folder is not a git repository".into());
self.repo_path = None;
self.branches.clear();
self.merge_base = None;
self.commits = None;
self.pane.update(cx, |pane, cx| pane.clear(cx));
cx.notify();
return;
};
if branches.is_empty() {
self.error = Some("The repository has no branches yet".into());
self.repo_path = None;
self.branches.clear();
self.merge_base = None;
self.commits = None;
self.pane.update(cx, |pane, cx| pane.clear(cx));
cx.notify();
return;
}
let announced = self.store.read(cx).head.clone();
let base = preferred_branch(&branches, announced);
let compare = current
.filter(|branch| branches.contains(branch))
.unwrap_or_else(|| base.clone());
self.repo_path = Some(PathBuf::from(&path));
self.error = None;
self.branches = branches.into_iter().map(SharedString::from).collect();
if let Some(addr) = self.store.read(cx).addr().cloned() {
CheckoutsStore::global(cx).update(cx, |store, cx| {
store.record(PathBuf::from(&path), addr, cx);
});
}
self.base = SharedString::from(base);
self.compare = SharedString::from(compare);
self.sync_selectors(window, cx);
self.reload_compare(cx);
}
fn sync_selectors(&mut self, window: &mut Window, cx: &mut Context<Self>) {
let options: Vec<(SharedString, SharedString)> = self
.branches
.iter()
.map(|branch| (branch.clone(), branch.clone()))
.collect();
let base = self.base.clone();
let compare = self.compare.clone();
self.base_select.update(cx, |state, cx| {
state.set_options(options.clone(), cx);
state.set_selected(Some(base), window, cx);
});
self.compare_select.update(cx, |state, cx| {
state.set_options(options, cx);
state.set_selected(Some(compare), window, cx);
});
}
pub(super) fn fork_candidates(&self, cx: &App) -> Vec<Announcement> {
let Some(addr) = self.store.read(cx).addr().cloned() else {
return Vec::new();
};
let euc = self
.store
.read(cx)
.announcement
.as_ref()
.and_then(|a| a.euc.clone());
let user = Backend::global(cx).read(cx).current_user();
let announcements = RepoListStore::global(cx).read(cx).announcements.clone();
Announcement::forks_in(&announcements, &addr, euc.as_deref(), user)
.into_iter()
.cloned()
.collect()
}
pub(crate) fn choose_fork(
&mut self,
announcement: Announcement,
window: &mut Window,
cx: &mut Context<Self>,
) {
let Some(base) = self.store.read(cx).addr().cloned() else {
return;
};
let mirror_path = Mirrors::path(&base);
let namespace = GitCache::fork_namespace(&announcement);
let clone_urls: Vec<Url> = announcement.clone.clone();
let base_clone_urls: Vec<Url> = self
.store
.read(cx)
.announcement
.as_ref()
.map(|a| a.clone.clone())
.unwrap_or_default();
self.fork_generation = self.fork_generation.wrapping_add(1);
let generation = self.fork_generation;
let handle = window.window_handle();
self.loading = true;
self.error = None;
cx.notify();
let task: gpui::Task<Result<(), anyhow::Error>> = cx.spawn(async move |this, cx| {
let result = cx
.background_spawn({
let namespace = namespace.clone();
let mirror_path = mirror_path.clone();
async move {
Mirrors::ensure(&base, &base_clone_urls)?;
let repo = Repo::open(&mirror_path)?;
repo.delete_refs_with_prefix("refs/fork")?;
repo.fetch_refs(
&clone_urls,
&format!("+refs/heads/*:refs/fork/{namespace}/*"),
)?;
let strip = |refs: Vec<String>, prefix: &str| {
let mut names: Vec<String> = refs
.into_iter()
.filter_map(|name| {
name.strip_prefix(prefix)
.map(|rest| rest.trim_start_matches('/').to_owned())
})
.filter(|name| !name.is_empty())
.collect();
names.sort();
names
};
let base_branches = strip(
repo.refs_with_prefix("refs/remotes/origin")?,
"refs/remotes/origin",
);
let compare_branches = strip(
repo.refs_with_prefix(&format!("refs/fork/{namespace}"))?,
&format!("refs/fork/{namespace}"),
);
Ok::<_, anyhow::Error>((base_branches, compare_branches))
}
})
.await;
if let Err(error) = cx.update_window(handle, |_, window, cx| {
this.update(cx, |this, cx| {
if generation != this.fork_generation {
return;
}
this.apply_fork(announcement, mirror_path, namespace, result, window, cx);
})
}) {
log::warn!("failed to apply fork: {error}");
}
Ok(())
});
self.push_task(task);
}
fn apply_fork(
&mut self,
announcement: Announcement,
mirror_path: PathBuf,
namespace: String,
result: Result<(Vec<String>, Vec<String>), anyhow::Error>,
window: &mut Window,
cx: &mut Context<Self>,
) {
self.loading = false;
let (base_branches, compare_branches) = match result {
Ok(branches) => branches,
Err(error) => {
self.error = Some(format!("Could not compare against the fork: {error}").into());
cx.notify();
return;
}
};
if compare_branches.is_empty() {
self.error = Some("The fork has no branches to compare".into());
cx.notify();
return;
}
if base_branches.is_empty() {
self.error = Some("Could not list the target repository's branches.".into());
cx.notify();
return;
}
let announced = self.store.read(cx).head.clone();
let base = preferred_branch(&base_branches, announced);
let compare = compare_branches
.iter()
.find(|branch| branch.as_str() == "main")
.map(|branch| SharedString::from(branch.clone()))
.unwrap_or_else(|| SharedString::from(compare_branches[0].clone()));
self.fork = Some(ForkCompare {
announcement,
namespace,
mirror_path,
});
self.error = None;
self.base = SharedString::from(base);
self.compare = compare;
self.branches = base_branches.into_iter().map(SharedString::from).collect();
self.sync_selectors(window, cx);
self.reload_compare(cx);
}
pub(super) fn reload_compare(&mut self, cx: &mut Context<Self>) {
let Some(repo_path) = self.work_path() else {
return;
};
let base = self.base_ref();
let compare = self.compare_ref();
let base_name = self.base.to_string();
let compare_name = self.compare.to_string();
self.loading = true;
self.error = None;
self.compare_generation += 1;
let generation = self.compare_generation;
cx.notify();
if base == compare {
self.loading = false;
self.merge_base = None;
self.commits = None;
self.pane.update(cx, |pane, cx| pane.clear(cx));
self.error = Some("Choose different base and compare branches".into());
cx.notify();
return;
}
let task: gpui::Task<Result<(), anyhow::Error>> = cx.spawn(async move |this, cx| {
let result = cx
.background_spawn({
async move {
let repo = Repo::open(Path::new(&repo_path))?;
let merge_base = repo.merge_base(&base, &compare)?.ok_or_else(|| {
anyhow::anyhow!(
"{base_name} and {compare_name} share no common ancestor"
)
})?;
let commits = repo.commit_range(&merge_base, &compare)?;
let diff = repo.range_diff(&merge_base, &compare)?;
Ok::<_, anyhow::Error>((merge_base, commits, diff))
}
})
.await;
this.update(cx, |this, cx| {
if generation != this.compare_generation {
return;
}
this.loading = false;
match result {
Ok((merge_base, commits, diff)) => {
this.merge_base = Some(merge_base);
this.commits = Some(Arc::new(commits));
this.pane.update(cx, |pane, cx| pane.set_diff(diff, cx));
}
Err(error) => {
this.merge_base = None;
this.commits = None;
this.pane.update(cx, |pane, cx| pane.clear(cx));
this.error = Some(error.to_string().into());
}
}
cx.notify();
})?;
Ok(())
});
self.push_task(task);
}
}
@@ -11,11 +11,13 @@ use ui::input::{Input, InputState};
use ui::layout::panel::Panel;
use ui::theme::ActiveTheme as _;
use crate::app::AppRoot;
use crate::shared::log_dropped;
use crate::shared::panels::close_detail;
use crate::shared::progress::error_row;
use crate::workspace::Workspace;
pub struct SendPatchView {
workspace: WeakEntity<Workspace>,
workspace: WeakEntity<AppRoot>,
panel_id: SharedString,
store: Entity<RepoStore>,
subject: Entity<InputState>,
@@ -27,7 +29,7 @@ pub struct SendPatchView {
impl SendPatchView {
pub fn new(
workspace: WeakEntity<Workspace>,
workspace: WeakEntity<AppRoot>,
panel_id: SharedString,
store: Entity<RepoStore>,
_window: &mut Window,
@@ -82,11 +84,7 @@ impl SendPatchView {
cx.spawn(async move |this, cx| match task.await {
Ok(()) => {
this.update(cx, |this, cx| {
let panel_id = this.panel_id.clone();
crate::shared::log_dropped(
this.workspace
.update(cx, |workspace, cx| workspace.close_detail(&panel_id, cx)),
);
close_detail(&this.workspace, &this.panel_id, cx)
})
.log_err();
}
@@ -122,7 +120,7 @@ impl SendPatchView {
.loading(self.submitting)
.disabled(!can_submit)
.on_click(move |_, _, cx| {
crate::shared::log_dropped(weak.update(cx, |this, cx| this.submit(cx)));
log_dropped(weak.update(cx, |this, cx| this.submit(cx)));
}),
)
.into_any_element()