932 lines
40 KiB
Rust
932 lines
40 KiB
Rust
use std::path::{Path, PathBuf};
|
|
use std::rc::Rc;
|
|
|
|
use assets::CustomIconName;
|
|
use dock::{BasePanel, DockArea, DockPlacement, Panel, PanelEvent, panel_handle};
|
|
use gpui::prelude::*;
|
|
use gpui::{
|
|
AnyElement, App, Context, Entity, EventEmitter, FocusHandle, Focusable, PathPromptOptions,
|
|
Pixels, Render, SharedString, Size, WeakEntity, Window, div, px, size,
|
|
};
|
|
use gpui_component::alert::Alert;
|
|
use gpui_component::button::{Button, ButtonVariants};
|
|
use gpui_component::checkbox::Checkbox;
|
|
use gpui_component::dialog::{DialogDescription, DialogFooter, DialogHeader, DialogTitle};
|
|
use gpui_component::form::{field, v_form};
|
|
use gpui_component::input::{Input, InputState, Textarea, TextareaState};
|
|
use gpui_component::scroll::Scrollbar;
|
|
use gpui_component::{
|
|
ActiveTheme, Disableable, Icon, IconName, VirtualListScrollHandle, WindowExt, h_flex, v_flex,
|
|
v_virtual_list,
|
|
};
|
|
use nostr::prelude::{EventId, Kind};
|
|
use signed_core::{RepoStatus, activity_subject};
|
|
use signed_git::{format_patch_between, merge_base, patch_applies};
|
|
use signed_state::{GitStore, ProfileStore, RepoStore};
|
|
use signed_ui::image_cache::{MAX_IMAGES, image_cache};
|
|
use signed_ui::{SegmentButton, UserAvatar, placeholder, status_badge};
|
|
use utils::relative_time;
|
|
|
|
use super::pull_request_detail::PullRequestDetailView;
|
|
|
|
/// Height of one pull request row in the virtual list; same layout as an
|
|
/// issue row.
|
|
const PR_ROW_HEIGHT: f32 = 73.;
|
|
|
|
/// Status filter of the pull request list, chosen via the header's filter
|
|
/// buttons.
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
enum PullRequestFilter {
|
|
/// Every pull request, regardless of status.
|
|
All,
|
|
/// Pull requests whose resolved status is [`RepoStatus::Open`].
|
|
Open,
|
|
/// Pull requests whose resolved status is [`RepoStatus::Closed`].
|
|
Closed,
|
|
/// Pull requests whose resolved status is [`RepoStatus::Draft`].
|
|
Draft,
|
|
/// Pull requests whose resolved status is [`RepoStatus::Applied`]
|
|
/// (i.e. merged).
|
|
Merged,
|
|
}
|
|
|
|
impl PullRequestFilter {
|
|
/// Whether a pull request with `status` is included by this filter.
|
|
fn matches(self, status: RepoStatus) -> bool {
|
|
match self {
|
|
Self::All => true,
|
|
Self::Open => status == RepoStatus::Open,
|
|
Self::Closed => status == RepoStatus::Closed,
|
|
Self::Draft => status == RepoStatus::Draft,
|
|
Self::Merged => status == RepoStatus::Applied,
|
|
}
|
|
}
|
|
}
|
|
|
|
pub struct PullRequestsView {
|
|
focus_handle: FocusHandle,
|
|
/// Dock area the detail panels are added to.
|
|
dock_area: WeakEntity<DockArea>,
|
|
/// Repo store holding the pull requests and their statuses.
|
|
store: Entity<RepoStore>,
|
|
/// Display name of the repository, for the panel title.
|
|
repo_name: SharedString,
|
|
/// Filter selected in the header filter buttons.
|
|
filter: PullRequestFilter,
|
|
/// Per-row heights of the virtual list.
|
|
item_sizes: Rc<Vec<Size<Pixels>>>,
|
|
/// Number of rows [`Self::item_sizes`] was built for (the filtered
|
|
/// pull request count); rebuilt on change.
|
|
pr_len: usize,
|
|
/// Indices into the store's `pull_requests` matching [`Self::filter`]
|
|
/// (root PR events only; updates are revisions of the root); the
|
|
/// virtual list renders this slice. Rebuilt only when the store
|
|
/// version or the filter changes, keyed by [`Self::cache_key`].
|
|
visible_prs: Vec<usize>,
|
|
/// Header counts `(total, open, closed, draft, merged)` of the root
|
|
/// pull requests only (revisions are not separate PRs), rebuilt with
|
|
/// [`Self::visible_prs`].
|
|
counts: (usize, usize, usize, usize, usize),
|
|
/// Store version and filter the cached rows/counts were built from.
|
|
cache_key: Option<(u64, PullRequestFilter)>,
|
|
/// Virtual list state of the pull requests list.
|
|
scroll_handle: VirtualListScrollHandle,
|
|
}
|
|
|
|
impl PullRequestsView {
|
|
pub fn new(
|
|
dock_area: WeakEntity<DockArea>,
|
|
store: Entity<RepoStore>,
|
|
repo_name: SharedString,
|
|
_window: &mut Window,
|
|
cx: &mut Context<Self>,
|
|
) -> Self {
|
|
Self {
|
|
focus_handle: cx.focus_handle(),
|
|
dock_area,
|
|
store,
|
|
repo_name,
|
|
filter: PullRequestFilter::Open,
|
|
item_sizes: Rc::new(Vec::new()),
|
|
pr_len: 0,
|
|
visible_prs: Vec::new(),
|
|
counts: (0, 0, 0, 0, 0),
|
|
cache_key: None,
|
|
scroll_handle: VirtualListScrollHandle::new(),
|
|
}
|
|
}
|
|
|
|
/// Open the detail panel of `pr_id` at the bottom of the dock area.
|
|
fn open_pull_request_detail(
|
|
&mut self,
|
|
pr_id: EventId,
|
|
window: &mut Window,
|
|
cx: &mut Context<Self>,
|
|
) {
|
|
let Some(dock_area) = self.dock_area.upgrade() else {
|
|
return;
|
|
};
|
|
|
|
let panel = cx.new(|cx| {
|
|
PullRequestDetailView::new(
|
|
self.dock_area.clone(),
|
|
self.store.clone(),
|
|
pr_id,
|
|
window,
|
|
cx,
|
|
)
|
|
});
|
|
|
|
dock_area.update(cx, |dock_area, cx| {
|
|
dock_area.add_panel_view(panel_handle(panel), DockPlacement::Center, None, window, cx);
|
|
});
|
|
}
|
|
|
|
/// Render one row of the pull request list; `ix` is the row index and
|
|
/// `pr_ix` the index of the pull request in the store's `pull_requests`.
|
|
fn render_row(&self, ix: usize, pr_ix: usize, cx: &mut Context<Self>) -> AnyElement {
|
|
let pr = &self.store.read(cx).pull_requests[pr_ix];
|
|
let pr_id = pr.id;
|
|
let title = activity_subject(pr);
|
|
let id_hex = pr.id.to_hex();
|
|
|
|
let age = relative_time(pr.created_at);
|
|
let status = self.store.read(cx).status_of(pr);
|
|
|
|
let profile = ProfileStore::global(cx).read(cx).get(&pr.pubkey);
|
|
let author = profile.name();
|
|
let picture = profile.picture();
|
|
|
|
h_flex()
|
|
.id(ix)
|
|
.w_full()
|
|
.gap_4()
|
|
.px_4()
|
|
.py_2()
|
|
.border_b_1()
|
|
.border_color(cx.theme().border)
|
|
.items_start()
|
|
.on_click(cx.listener(move |this, _event, window, cx| {
|
|
this.open_pull_request_detail(pr_id, window, cx);
|
|
}))
|
|
.child(status_badge(status, cx))
|
|
.child(
|
|
v_flex()
|
|
.flex_1()
|
|
.child(
|
|
div()
|
|
.h_8()
|
|
.min_w_0()
|
|
.text_ellipsis()
|
|
.whitespace_nowrap()
|
|
.line_clamp(1)
|
|
.text_sm()
|
|
.child(title),
|
|
)
|
|
.child(
|
|
h_flex()
|
|
.h_6()
|
|
.gap_2()
|
|
.text_xs()
|
|
.child(
|
|
h_flex()
|
|
.gap_1()
|
|
.child(UserAvatar::new(author.clone()).picture(picture))
|
|
.child(div().child(author)),
|
|
)
|
|
.child(SharedString::from("opened"))
|
|
.child(
|
|
div()
|
|
.text_color(cx.theme().muted_foreground)
|
|
.child(SharedString::from(&id_hex[..8])),
|
|
)
|
|
.child(SharedString::from(age)),
|
|
),
|
|
)
|
|
.hover(|this| this.bg(cx.theme().list_hover))
|
|
.into_any_element()
|
|
}
|
|
|
|
fn render_header(&self, cx: &mut Context<Self>) -> AnyElement {
|
|
// Counts of the last list rebuild (`render` rebuilds first when the
|
|
// store version or filter changed, so this is never stale).
|
|
let (total, open, closed, draft, merged) = self.counts;
|
|
|
|
h_flex()
|
|
.px_4()
|
|
.w_full()
|
|
.gap_3()
|
|
.border_b_1()
|
|
.border_color(cx.theme().border)
|
|
.bg(cx.theme().muted.opacity(0.5))
|
|
.child(
|
|
h_flex()
|
|
.h_12()
|
|
.gap_2()
|
|
.child(
|
|
SegmentButton::new("all", "All")
|
|
.icon(Icon::new(CustomIconName::GitPullRequest))
|
|
.count(total)
|
|
.selected(self.filter == PullRequestFilter::All)
|
|
.on_click(cx.listener(|this, _event, _window, cx| {
|
|
this.filter = PullRequestFilter::All;
|
|
cx.notify();
|
|
})),
|
|
)
|
|
.child(
|
|
SegmentButton::new("open", "Open")
|
|
.icon(Icon::new(CustomIconName::GitPullRequest))
|
|
.count(open)
|
|
.selected(self.filter == PullRequestFilter::Open)
|
|
.on_click(cx.listener(|this, _event, _window, cx| {
|
|
this.filter = PullRequestFilter::Open;
|
|
cx.notify();
|
|
})),
|
|
)
|
|
.child(
|
|
SegmentButton::new("closed", "Closed")
|
|
.icon(Icon::new(CustomIconName::GitPullRequestClosed))
|
|
.count(closed)
|
|
.selected(self.filter == PullRequestFilter::Closed)
|
|
.on_click(cx.listener(|this, _event, _window, cx| {
|
|
this.filter = PullRequestFilter::Closed;
|
|
cx.notify();
|
|
})),
|
|
)
|
|
.child(
|
|
SegmentButton::new("draft", "Draft")
|
|
.icon(Icon::new(CustomIconName::GitPullRequestDraft))
|
|
.count(draft)
|
|
.selected(self.filter == PullRequestFilter::Draft)
|
|
.on_click(cx.listener(|this, _event, _window, cx| {
|
|
this.filter = PullRequestFilter::Draft;
|
|
cx.notify();
|
|
})),
|
|
)
|
|
.child(
|
|
SegmentButton::new("merged", "Merged")
|
|
.icon(Icon::new(CustomIconName::GitPullRequestMerged))
|
|
.count(merged)
|
|
.selected(self.filter == PullRequestFilter::Merged)
|
|
.on_click(cx.listener(|this, _event, _window, cx| {
|
|
this.filter = PullRequestFilter::Merged;
|
|
cx.notify();
|
|
})),
|
|
),
|
|
)
|
|
.child(div().flex_1())
|
|
.child(
|
|
SegmentButton::new("new-pr", "New pull request")
|
|
.icon(Icon::new(CustomIconName::CirclePlus))
|
|
.primary()
|
|
.on_click(cx.listener(|this, _event, window, cx| {
|
|
open_new_pull_request_dialog(this.store.clone(), window, cx);
|
|
})),
|
|
)
|
|
.into_any_element()
|
|
}
|
|
}
|
|
|
|
/// A patch series generated from a local repository, with the metadata
|
|
/// derived from it.
|
|
struct GeneratedPatch {
|
|
/// The `git format-patch` series (fills the patch textarea).
|
|
patch: String,
|
|
/// The merge base with the target branch, as hex.
|
|
merge_base: Option<String>,
|
|
}
|
|
|
|
/// State of the new pull request dialog, so the async generation, the
|
|
/// apply check and the draft checkbox re-render.
|
|
#[derive(Default)]
|
|
struct NewPullRequestDialogState {
|
|
draft: bool,
|
|
/// The last generated patch series; its merge base is reused at submit
|
|
/// only while the patch textarea is unchanged.
|
|
generated: Option<GeneratedPatch>,
|
|
/// Result of the pre-publish applicability check against the app's
|
|
/// mirror clone of the target repository.
|
|
apply_check: Option<Result<(), String>>,
|
|
/// A patch generation is in flight.
|
|
generating: bool,
|
|
/// Error of the last generation attempt.
|
|
error: Option<SharedString>,
|
|
}
|
|
|
|
impl NewPullRequestDialogState {
|
|
/// Text and whether it is good news, for the line under the patch field.
|
|
fn apply_check_message(&self) -> Option<(SharedString, bool)> {
|
|
match &self.apply_check {
|
|
Some(Ok(())) => Some((
|
|
"Applies cleanly to the repository's default branch".into(),
|
|
true,
|
|
)),
|
|
Some(Err(error)) => Some((
|
|
format!("May not apply cleanly to the repository's default branch: {error}").into(),
|
|
false,
|
|
)),
|
|
None => None,
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Open the "new pull request" dialog: a title, an optional description,
|
|
/// an optional branch name and a patch input that submit through
|
|
/// [`RepoStore::open_pull_request`] when confirmed. The patch can either be
|
|
/// pasted, or generated from a local checkout: pick a repository, a source
|
|
/// and a target branch, and the app runs `git format-patch` itself and
|
|
/// checks the series against the app's mirror clone of the target.
|
|
pub(super) fn open_new_pull_request_dialog(
|
|
store: Entity<RepoStore>,
|
|
window: &mut Window,
|
|
cx: &mut App,
|
|
) {
|
|
let subject = cx.new(|cx| InputState::new(window, cx).placeholder("Pull request title"));
|
|
let description =
|
|
cx.new(|cx| TextareaState::new(window, cx).placeholder("Describe the change..."));
|
|
let branch = cx.new(|cx| InputState::new(window, cx).placeholder("Branch name (optional)"));
|
|
let repo_path = cx.new(|cx| InputState::new(window, cx).placeholder("Pick a local checkout…"));
|
|
let source = cx.new(|cx| InputState::new(window, cx).placeholder("Source branch"));
|
|
let target = cx.new(|cx| InputState::new(window, cx).placeholder("Target branch"));
|
|
let patch = cx
|
|
.new(|cx| TextareaState::new(window, cx).placeholder("Paste `git format-patch` output..."));
|
|
let state = cx.new(|_| NewPullRequestDialogState::default());
|
|
|
|
window.open_dialog(cx, move |dialog, _window, _cx| {
|
|
let subject = subject.clone();
|
|
let description = description.clone();
|
|
let branch = branch.clone();
|
|
let repo_path = repo_path.clone();
|
|
let source = source.clone();
|
|
let target = target.clone();
|
|
let patch = patch.clone();
|
|
let store = store.clone();
|
|
let state = state.clone();
|
|
|
|
dialog
|
|
.width(px(560.))
|
|
.margin_top(px(50.))
|
|
.content(move |body, _window, cx| {
|
|
let generating = state.read(cx).generating;
|
|
let draft = state.read(cx).draft;
|
|
let error = state.read(cx).error.clone();
|
|
let apply_check = state.read(cx).apply_check_message();
|
|
body.child(
|
|
DialogHeader::new()
|
|
.child(DialogTitle::new().child("New pull request"))
|
|
.child(
|
|
DialogDescription::new().child(
|
|
"Propose a change with the output of `git format-patch`.",
|
|
),
|
|
),
|
|
)
|
|
.child(
|
|
v_form()
|
|
.child(
|
|
field()
|
|
.label("Title")
|
|
.required(true)
|
|
.child(Input::new(&subject)),
|
|
)
|
|
.child(
|
|
field()
|
|
.label("Description")
|
|
.child(Textarea::new(&description).h(px(96.))),
|
|
)
|
|
.child(
|
|
field()
|
|
.label("Local repository")
|
|
.description(
|
|
"Generate the patch from a local checkout; leave empty to paste it",
|
|
)
|
|
.child(
|
|
h_flex()
|
|
.gap_1()
|
|
.items_center()
|
|
.child(
|
|
div()
|
|
.flex_1()
|
|
.child(Input::new(&repo_path).disabled(true)),
|
|
)
|
|
.child(
|
|
Button::new("choose-checkout")
|
|
.icon(IconName::FolderOpen)
|
|
.ghost()
|
|
.tooltip("Choose local checkout")
|
|
.on_click({
|
|
let repo_path = repo_path.clone();
|
|
let source = source.clone();
|
|
let target = target.clone();
|
|
let patch = patch.clone();
|
|
let branch = branch.clone();
|
|
let state = state.clone();
|
|
let store = store.clone();
|
|
move |_ev, window, cx| {
|
|
choose_local_repo(
|
|
&repo_path,
|
|
&source,
|
|
&target,
|
|
&patch,
|
|
&branch,
|
|
&state,
|
|
&store,
|
|
window,
|
|
cx,
|
|
);
|
|
}
|
|
}),
|
|
)
|
|
.child(
|
|
Button::new("generate-patch")
|
|
.ghost()
|
|
.label("Generate")
|
|
.tooltip(
|
|
"Generate the patch from the local checkout",
|
|
)
|
|
.loading(generating)
|
|
.disabled(generating)
|
|
.on_click({
|
|
let repo_path = repo_path.clone();
|
|
let source = source.clone();
|
|
let target = target.clone();
|
|
let patch = patch.clone();
|
|
let branch = branch.clone();
|
|
let state = state.clone();
|
|
let store = store.clone();
|
|
move |_ev, window, cx| {
|
|
let path =
|
|
repo_path.read(cx).value().to_string();
|
|
let source =
|
|
source.read(cx).value().to_string();
|
|
let target =
|
|
target.read(cx).value().to_string();
|
|
if !path.is_empty()
|
|
&& !source.is_empty()
|
|
&& !target.is_empty()
|
|
{
|
|
generate_patch(
|
|
&state,
|
|
&patch,
|
|
&branch,
|
|
path,
|
|
source,
|
|
target,
|
|
&store,
|
|
window,
|
|
cx,
|
|
);
|
|
}
|
|
}
|
|
}),
|
|
),
|
|
),
|
|
)
|
|
.child(
|
|
field()
|
|
.label("Source branch")
|
|
.child(Input::new(&source)),
|
|
)
|
|
.child(
|
|
field()
|
|
.label("Target branch")
|
|
.child(Input::new(&target)),
|
|
)
|
|
.child(
|
|
field()
|
|
.label("Branch")
|
|
.description("Optional: the branch the change is proposed from")
|
|
.child(Input::new(&branch)),
|
|
)
|
|
.child(
|
|
field().label("Patch").child(
|
|
v_flex()
|
|
.gap_1()
|
|
.child(Textarea::new(&patch).h(px(140.)))
|
|
.when_some(apply_check, |this, (message, ok)| {
|
|
this.child(
|
|
div()
|
|
.text_xs()
|
|
.text_color(if ok {
|
|
cx.theme().success
|
|
} else {
|
|
cx.theme().warning
|
|
})
|
|
.child(message),
|
|
)
|
|
})
|
|
.when_some(error, |this, message| {
|
|
this.child(
|
|
div()
|
|
.text_xs()
|
|
.text_color(cx.theme().danger)
|
|
.child(message),
|
|
)
|
|
}),
|
|
),
|
|
)
|
|
.child(
|
|
field().child(
|
|
Checkbox::new("pr-draft")
|
|
.label("Create as draft")
|
|
.checked(draft)
|
|
.on_click({
|
|
let state = state.clone();
|
|
move |checked, _window, cx| {
|
|
state.update(cx, |state, _| state.draft = *checked);
|
|
}
|
|
}),
|
|
),
|
|
),
|
|
)
|
|
.child(
|
|
DialogFooter::new().justify_end().child(
|
|
Button::new("submit")
|
|
.primary()
|
|
.label("Create pull request")
|
|
.tooltip("Create pull request")
|
|
.loading(generating)
|
|
.disabled(generating)
|
|
.on_click({
|
|
let subject = subject.clone();
|
|
let description = description.clone();
|
|
let branch = branch.clone();
|
|
let patch = patch.clone();
|
|
let repo_path = repo_path.clone();
|
|
let store = store.clone();
|
|
let state = state.clone();
|
|
|
|
move |_event, window, cx| {
|
|
if state.read(cx).generating {
|
|
return;
|
|
}
|
|
let subject = subject.read(cx).value().to_string();
|
|
let description = description.read(cx).value().to_string();
|
|
let branch = branch.read(cx).value().to_string();
|
|
let patch = patch.read(cx).value().to_string();
|
|
let subject = (!subject.is_empty()).then_some(subject);
|
|
let branch = (!branch.is_empty()).then_some(branch);
|
|
let draft = state.read(cx).draft;
|
|
// The generated merge base stays valid
|
|
// only while the patch is unchanged; an
|
|
// edited patch falls back to none.
|
|
let merge_base = state
|
|
.read(cx)
|
|
.generated
|
|
.as_ref()
|
|
.filter(|generated| generated.patch == patch)
|
|
.and_then(|generated| generated.merge_base.clone());
|
|
// The checkout (when set) is where the
|
|
// tip commit is pushed from, so other
|
|
// clients can fetch it.
|
|
let repo_path = repo_path.read(cx).value().to_string();
|
|
let push_from = (!repo_path.is_empty())
|
|
.then(|| PathBuf::from(repo_path));
|
|
|
|
store.update(cx, |store, cx| {
|
|
store.open_pull_request(
|
|
subject,
|
|
description,
|
|
branch,
|
|
patch,
|
|
draft,
|
|
merge_base,
|
|
push_from,
|
|
cx,
|
|
);
|
|
});
|
|
|
|
window.close_dialog(cx);
|
|
}
|
|
}),
|
|
),
|
|
)
|
|
})
|
|
});
|
|
}
|
|
|
|
/// Prompt for a local checkout, fill the source/target defaults (the
|
|
/// checkout's current branch and the repository's announced HEAD) and
|
|
/// generate the patch series right away.
|
|
#[allow(clippy::too_many_arguments)]
|
|
fn choose_local_repo(
|
|
repo_path: &Entity<InputState>,
|
|
source: &Entity<InputState>,
|
|
target: &Entity<InputState>,
|
|
patch: &Entity<TextareaState>,
|
|
branch: &Entity<InputState>,
|
|
state: &Entity<NewPullRequestDialogState>,
|
|
store: &Entity<RepoStore>,
|
|
window: &mut Window,
|
|
cx: &mut App,
|
|
) {
|
|
let handle = window.window_handle();
|
|
let repo_path = repo_path.clone();
|
|
let source = source.clone();
|
|
let target = target.clone();
|
|
let patch = patch.clone();
|
|
let branch = branch.clone();
|
|
let state = state.clone();
|
|
let store = store.clone();
|
|
// The announced HEAD branch is the natural target default.
|
|
let target_default = store.read(cx).head.clone().unwrap_or_default();
|
|
|
|
let prompt = cx.prompt_for_paths(PathPromptOptions {
|
|
files: false,
|
|
directories: true,
|
|
multiple: false,
|
|
prompt: Some("Choose local checkout".into()),
|
|
});
|
|
|
|
cx.spawn(async move |cx| {
|
|
if let Ok(Ok(Some(mut paths))) = prompt.await
|
|
&& let Some(path) = paths.pop()
|
|
{
|
|
let path = path.to_string_lossy().to_string();
|
|
|
|
// The checkout's current branch is the source default; resolve
|
|
// it off the UI thread.
|
|
let current = cx
|
|
.background_executor()
|
|
.spawn({
|
|
let path = path.clone();
|
|
async move {
|
|
gix::open(Path::new(&path))
|
|
.ok()
|
|
.and_then(|repo| signed_git::current_branch(&repo).ok().flatten())
|
|
}
|
|
})
|
|
.await;
|
|
|
|
let _ = handle.update(cx, |_, window, cx| {
|
|
repo_path.update(cx, |input, cx| {
|
|
input.set_value(path.clone(), window, cx);
|
|
});
|
|
source.update(cx, |input, cx| {
|
|
input.set_value(current.clone().unwrap_or_default(), window, cx);
|
|
});
|
|
target.update(cx, |input, cx| {
|
|
input.set_value(target_default.clone(), window, cx);
|
|
});
|
|
|
|
if let Some(current) = current
|
|
&& !current.is_empty()
|
|
&& !target_default.is_empty()
|
|
{
|
|
generate_patch(
|
|
&state,
|
|
&patch,
|
|
&branch,
|
|
path,
|
|
current,
|
|
target_default,
|
|
&store,
|
|
window,
|
|
cx,
|
|
);
|
|
}
|
|
});
|
|
}
|
|
})
|
|
.detach();
|
|
}
|
|
|
|
/// Generate the patch series `source..target` of the local checkout at
|
|
/// `repo_path`, fill the patch textarea and record the merge base and the
|
|
/// pre-publish applicability check in `state`.
|
|
#[allow(clippy::too_many_arguments)]
|
|
fn generate_patch(
|
|
state: &Entity<NewPullRequestDialogState>,
|
|
patch_input: &Entity<TextareaState>,
|
|
branch_input: &Entity<InputState>,
|
|
repo_path: String,
|
|
source: String,
|
|
target: String,
|
|
store: &Entity<RepoStore>,
|
|
window: &mut Window,
|
|
cx: &mut App,
|
|
) {
|
|
state.update(cx, |state, cx| {
|
|
state.generating = true;
|
|
state.error = None;
|
|
state.apply_check = None;
|
|
cx.notify();
|
|
});
|
|
|
|
let cache = GitStore::global(cx).cache().clone();
|
|
let (addr, clone_urls) = {
|
|
let store = store.read(cx);
|
|
(
|
|
store.addr().clone(),
|
|
store
|
|
.announcement
|
|
.as_ref()
|
|
.map(|a| {
|
|
a.clone
|
|
.iter()
|
|
.map(ToString::to_string)
|
|
.collect::<Vec<String>>()
|
|
})
|
|
.unwrap_or_default(),
|
|
)
|
|
};
|
|
|
|
let handle = window.window_handle();
|
|
let state = state.clone();
|
|
let patch_input = patch_input.clone();
|
|
let branch_input = branch_input.clone();
|
|
|
|
let task = cx.spawn(async move |cx| {
|
|
// The branch-name tag defaults to the source branch; keep a copy
|
|
// for the UI update after the background generation moves it.
|
|
let source_label = source.clone();
|
|
let generated = cx
|
|
.background_executor()
|
|
.spawn(async move {
|
|
let base =
|
|
merge_base(Path::new(&repo_path), &source, &target)?.ok_or_else(|| {
|
|
anyhow::anyhow!("{source} and {target} share no common ancestor")
|
|
})?;
|
|
let patch = format_patch_between(Path::new(&repo_path), &base, &source)?;
|
|
// Best-effort: does the series apply to the current default
|
|
// branch of the app's mirror clone of the target repository?
|
|
let check = cache
|
|
.ensure_clone(&addr, &clone_urls)
|
|
.ok()
|
|
.and_then(|repo| repo.workdir().map(|workdir| workdir.to_path_buf()))
|
|
.map(|workdir| patch_applies(&workdir, &patch).map_err(|e| e.to_string()));
|
|
Ok::<_, anyhow::Error>((patch, Some(base), check))
|
|
})
|
|
.await;
|
|
|
|
let _ = handle.update(cx, |_, window, cx| match generated {
|
|
Ok((patch, merge_base, check)) => {
|
|
patch_input.update(cx, |input, cx| {
|
|
input.set_value(patch.clone(), window, cx);
|
|
});
|
|
// The branch-name tag defaults to the source branch.
|
|
if branch_input.read(cx).value().is_empty() {
|
|
branch_input.update(cx, |input, cx| {
|
|
input.set_value(source_label.clone(), window, cx);
|
|
});
|
|
}
|
|
state.update(cx, |state, cx| {
|
|
state.generating = false;
|
|
state.generated = Some(GeneratedPatch { patch, merge_base });
|
|
state.apply_check = check;
|
|
cx.notify();
|
|
});
|
|
}
|
|
Err(error) => state.update(cx, |state, cx| {
|
|
state.generating = false;
|
|
state.error = Some(error.to_string().into());
|
|
cx.notify();
|
|
}),
|
|
});
|
|
});
|
|
task.detach();
|
|
}
|
|
|
|
impl BasePanel for PullRequestsView {
|
|
fn panel_name(&self) -> &'static str {
|
|
"pull-requests"
|
|
}
|
|
}
|
|
|
|
impl Panel for PullRequestsView {
|
|
fn title(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
|
|
div().child(SharedString::from(format!(
|
|
"{}/pull-requests",
|
|
self.repo_name
|
|
)))
|
|
}
|
|
}
|
|
|
|
impl EventEmitter<PanelEvent> for PullRequestsView {}
|
|
|
|
impl Focusable for PullRequestsView {
|
|
fn focus_handle(&self, _cx: &App) -> FocusHandle {
|
|
self.focus_handle.clone()
|
|
}
|
|
}
|
|
|
|
impl Render for PullRequestsView {
|
|
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
|
|
let filter = self.filter;
|
|
|
|
// Rebuild the filtered rows and header counts only when the store
|
|
// refreshed or the filter changed; other renders reuse the cache.
|
|
let version = self.store.read(cx).version();
|
|
if self.cache_key != Some((version, filter)) {
|
|
let store = self.store.read(cx);
|
|
let mut counts = (0usize, 0usize, 0usize, 0usize, 0usize);
|
|
self.visible_prs = store
|
|
.pull_requests
|
|
.iter()
|
|
.enumerate()
|
|
.filter_map(|(ix, pr)| {
|
|
// Kind-30620 patches are revisions of a root PR (NIP-34),
|
|
// not separate pull requests: count only root events, or
|
|
// the header counts inflate with every revision (which
|
|
// also default to `Open` in `status_of`).
|
|
if pr.kind != Kind::GitPullRequest {
|
|
return None;
|
|
}
|
|
let status = store.status_of(pr);
|
|
counts.0 += 1;
|
|
match status {
|
|
RepoStatus::Open => counts.1 += 1,
|
|
RepoStatus::Closed => counts.2 += 1,
|
|
RepoStatus::Draft => counts.3 += 1,
|
|
RepoStatus::Applied => counts.4 += 1,
|
|
}
|
|
filter.matches(status).then_some(ix)
|
|
})
|
|
.collect();
|
|
self.counts = counts;
|
|
self.cache_key = Some((version, filter));
|
|
}
|
|
|
|
let count = self.visible_prs.len();
|
|
|
|
// The virtual list's item count comes from `item_sizes`; rebuild it
|
|
// whenever the filtered pull request count changes.
|
|
if count != self.pr_len {
|
|
self.pr_len = count;
|
|
self.item_sizes = Rc::new(vec![size(px(0.), px(PR_ROW_HEIGHT)); count]);
|
|
}
|
|
|
|
let sizes = self.item_sizes.clone();
|
|
let scroll_handle = self.scroll_handle.clone();
|
|
let view = cx.entity().clone();
|
|
|
|
// Non-fatal warnings and errors of the last action (e.g. creating
|
|
// or updating a PR), shown as dismissible banners above the list.
|
|
let (last_error, last_warning) = {
|
|
let store = self.store.read(cx);
|
|
(store.last_error.clone(), store.last_warning.clone())
|
|
};
|
|
|
|
v_flex()
|
|
.size_full()
|
|
.image_cache(image_cache("pull-requests", MAX_IMAGES))
|
|
.child(self.render_header(cx))
|
|
.when_some(last_warning, |this, warning| {
|
|
this.child(Alert::warning("pr-warning", warning).banner().on_close({
|
|
let store = self.store.clone();
|
|
move |_event, _window, cx| {
|
|
store.update(cx, |store, _| store.last_warning = None);
|
|
}
|
|
}))
|
|
})
|
|
.when_some(last_error, |this, error| {
|
|
this.child(Alert::error("pr-error", error).banner().on_close({
|
|
let store = self.store.clone();
|
|
move |_event, _window, cx| {
|
|
store.update(cx, |store, _| store.last_error = None);
|
|
}
|
|
}))
|
|
})
|
|
.child(
|
|
v_flex()
|
|
.relative()
|
|
.flex_1()
|
|
.min_h_0()
|
|
.w_full()
|
|
.when(count > 0, |this| {
|
|
this.child(
|
|
v_virtual_list(view, "prl", sizes, move |this, range, _window, cx| {
|
|
range
|
|
.map(|ix| {
|
|
let pr_ix = this.visible_prs[ix];
|
|
this.render_row(ix, pr_ix, cx)
|
|
})
|
|
.collect()
|
|
})
|
|
.track_scroll(&scroll_handle)
|
|
.size_full(),
|
|
)
|
|
.child(
|
|
div()
|
|
.absolute()
|
|
.top_0()
|
|
.left_0()
|
|
.right_0()
|
|
.bottom_0()
|
|
.child(Scrollbar::vertical(&scroll_handle)),
|
|
)
|
|
})
|
|
.when(count == 0, |this| {
|
|
let message = match filter {
|
|
PullRequestFilter::All => "No pull requests",
|
|
PullRequestFilter::Open => "No open pull requests",
|
|
PullRequestFilter::Closed => "No closed pull requests",
|
|
PullRequestFilter::Draft => "No draft pull requests",
|
|
PullRequestFilter::Merged => "No merged pull requests",
|
|
};
|
|
this.child(placeholder(message, cx))
|
|
}),
|
|
)
|
|
.into_any_element()
|
|
}
|
|
}
|