| @@ -0,0 +1,682 @@ |
| 1 | + //! T3 Code's git actions in the thread details panel: one quick action |
| 2 | + //! that fits how the branch stands (Commit, Commit & push, Commit, push & |
| 3 | + //! PR, Push, Push & create PR, Pull, Create PR), a menu with Commit, Push |
| 4 | + //! and Create PR, the commit dialog, and the confirmation before pushing |
| 5 | + //! to the default branch. Commit messages and pull requests are written by |
| 6 | + //! the text generation model when left empty. |
| 7 | + |
| 8 | + use std::path::{Path, PathBuf}; |
| 9 | + use std::time::Instant; |
| 10 | + |
| 11 | + use gpui_kit::component::{ |
| 12 | + Icon, IconName, WindowExt as _, checkbox::Checkbox, h_flex, |
| 13 | + input::EditorState, |
| 14 | + menu::{DropdownMenu as _, PopupMenuItem}, |
| 15 | + notification::Notification, |
| 16 | + tooltip::Tooltip, |
| 17 | + v_flex, |
| 18 | + }; |
| 19 | + use gpui_kit::prelude::FluentBuilder as _; |
| 20 | + use gpui_kit::*; |
| 21 | + |
| 22 | + use super::Workspace; |
| 23 | + use crate::git::{self, GitStatus, StatusFile}; |
| 24 | + use crate::ui::icons::Lucide; |
| 25 | + use crate::ui::metal::{self, MetalButton, MetalStyled as _}; |
| 26 | + |
| 27 | + /// What a git action does. |
| 28 | + #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 29 | + pub enum GitAction { |
| 30 | + Commit, |
| 31 | + CommitPush, |
| 32 | + CommitPushPr, |
| 33 | + Push, |
| 34 | + PushPr, |
| 35 | + CreatePr, |
| 36 | + Pull, |
| 37 | + } |
| 38 | + |
| 39 | + impl GitAction { |
| 40 | + fn label(self) -> &'static str { |
| 41 | + match self { |
| 42 | + GitAction::Commit => "Commit", |
| 43 | + GitAction::CommitPush => "Commit & push", |
| 44 | + GitAction::CommitPushPr => "Commit, push & PR", |
| 45 | + GitAction::Push => "Push", |
| 46 | + GitAction::PushPr => "Push & create PR", |
| 47 | + GitAction::CreatePr => "Create PR", |
| 48 | + GitAction::Pull => "Pull", |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + fn icon(self) -> Icon { |
| 53 | + match self { |
| 54 | + GitAction::Commit => Icon::new(Lucide::GitCommitHorizontal), |
| 55 | + GitAction::Push | GitAction::CommitPush => Icon::new(Lucide::CloudUpload), |
| 56 | + GitAction::Pull => Icon::new(Lucide::CloudDownload), |
| 57 | + GitAction::CommitPushPr | GitAction::PushPr | GitAction::CreatePr => Icon::new(Lucide::GitPullRequest), |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + fn commits(self) -> bool { |
| 62 | + matches!(self, GitAction::Commit | GitAction::CommitPush | GitAction::CommitPushPr) |
| 63 | + } |
| 64 | + |
| 65 | + fn pushes(self) -> bool { |
| 66 | + matches!(self, GitAction::CommitPush | GitAction::CommitPushPr | GitAction::Push | GitAction::PushPr) |
| 67 | + } |
| 68 | + |
| 69 | + fn opens_pr(self) -> bool { |
| 70 | + matches!(self, GitAction::CommitPushPr | GitAction::PushPr | GitAction::CreatePr) |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + /// The quick action: something to run, or a label with why there's |
| 75 | + /// nothing to run. |
| 76 | + #[derive(Clone, Debug, PartialEq)] |
| 77 | + pub enum Quick { |
| 78 | + Run(GitAction), |
| 79 | + Hint { label: &'static str, hint: &'static str }, |
| 80 | + } |
| 81 | + |
| 82 | + /// T3 Code's choice of quick action for how the branch stands. |
| 83 | + pub fn quick_action(status: &GitStatus, pr_open: bool) -> Quick { |
| 84 | + let hint = |label, hint| Quick::Hint { label, hint }; |
| 85 | + if status.detached() { |
| 86 | + return hint("Commit", "Create and checkout a ref before pushing or opening a PR."); |
| 87 | + } |
| 88 | + let pr_or_default = pr_open || status.on_default(); |
| 89 | + if status.has_changes() { |
| 90 | + return Quick::Run(match () { |
| 91 | + _ if status.upstream.is_none() && !status.has_origin => GitAction::Commit, |
| 92 | + _ if pr_or_default => GitAction::CommitPush, |
| 93 | + _ => GitAction::CommitPushPr, |
| 94 | + }); |
| 95 | + } |
| 96 | + if status.upstream.is_none() { |
| 97 | + if !status.has_origin { |
| 98 | + return hint("Push", "Add an \"origin\" remote before pushing."); |
| 99 | + } |
| 100 | + if status.ahead_of_default == 0 && pr_open { |
| 101 | + return hint("Commit", "Branch is up to date. No action needed."); |
| 102 | + } |
| 103 | + if status.ahead_of_default == 0 { |
| 104 | + return hint("Push", "No local commits to push."); |
| 105 | + } |
| 106 | + return Quick::Run(if pr_or_default { GitAction::Push } else { GitAction::PushPr }); |
| 107 | + } |
| 108 | + if status.ahead > 0 && status.behind > 0 { |
| 109 | + return hint("Sync ref", "Branch has diverged from upstream. Rebase/merge first."); |
| 110 | + } |
| 111 | + if status.behind > 0 { |
| 112 | + return Quick::Run(GitAction::Pull); |
| 113 | + } |
| 114 | + if status.ahead > 0 { |
| 115 | + return Quick::Run(if pr_or_default { GitAction::Push } else { GitAction::PushPr }); |
| 116 | + } |
| 117 | + if pr_open { |
| 118 | + return hint("Commit", "Branch is up to date. No action needed."); |
| 119 | + } |
| 120 | + if status.ahead_of_default > 0 && !status.on_default() { |
| 121 | + return Quick::Run(GitAction::CreatePr); |
| 122 | + } |
| 123 | + hint("Commit", "Branch is up to date. No action needed.") |
| 124 | + } |
| 125 | + |
| 126 | + /// Why a menu action can't run now, as T3 Code says it. |
| 127 | + fn disabled_reason(action: GitAction, status: &GitStatus, pr_open: bool) -> Option<&'static str> { |
| 128 | + match action { |
| 129 | + GitAction::Commit if !status.has_changes() => Some("Worktree is clean. Make changes before committing."), |
| 130 | + GitAction::Push if status.detached() => Some("Detached HEAD: check out a branch before pushing."), |
| 131 | + GitAction::Push if status.has_changes() => Some("Commit or stash local changes before pushing."), |
| 132 | + GitAction::Push if status.behind > 0 => Some("Branch is behind upstream. Pull/rebase before pushing."), |
| 133 | + GitAction::Push if !status.has_origin => Some("Add an \"origin\" remote before pushing."), |
| 134 | + GitAction::Push if status.upstream.is_some() && status.ahead == 0 || status.upstream.is_none() && status.ahead_of_default == 0 => { |
| 135 | + Some("No local commits to push.") |
| 136 | + } |
| 137 | + GitAction::CreatePr if status.detached() => Some("Detached HEAD: check out a branch before creating a PR."), |
| 138 | + GitAction::CreatePr if status.has_changes() => Some("Commit local changes before creating a PR."), |
| 139 | + GitAction::CreatePr if !status.has_origin => Some("Add an \"origin\" remote before creating a PR."), |
| 140 | + GitAction::CreatePr if status.ahead_of_default == 0 => Some("No local commits to include in a PR."), |
| 141 | + GitAction::CreatePr if status.behind > 0 => Some("Branch is behind upstream. Pull/rebase before creating a PR."), |
| 142 | + GitAction::CreatePr if pr_open => Some("A pull request is already open."), |
| 143 | + _ => None, |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + /// What a finished action reports. |
| 148 | + struct Outcome { |
| 149 | + title: String, |
| 150 | + url: Option<String>, |
| 151 | + } |
| 152 | + |
| 153 | + /// Runs an action in `path`: commits (with `message`, or a generated one, |
| 154 | + /// on a new feature branch when asked), pushes and opens a pull request. |
| 155 | + /// Blocks; run it in the background. |
| 156 | + fn run( |
| 157 | + path: &Path, |
| 158 | + action: GitAction, |
| 159 | + files: Option<Vec<String>>, |
| 160 | + message: String, |
| 161 | + feature_branch: bool, |
| 162 | + ) -> Result<Outcome, String> { |
| 163 | + let mut title = String::new(); |
| 164 | + if action == GitAction::Pull { |
| 165 | + let status = git::status(path).unwrap_or_default(); |
| 166 | + let before = git::git(path, &["rev-parse", "HEAD"]).unwrap_or_default(); |
| 167 | + git::git(path, &["pull", "--ff-only"])?; |
| 168 | + let after = git::git(path, &["rev-parse", "HEAD"]).unwrap_or_default(); |
| 169 | + let branch = status.branch.unwrap_or_default(); |
| 170 | + return Ok(Outcome { |
| 171 | + title: if before == after { |
| 172 | + "Already up to date".into() |
| 173 | + } else { |
| 174 | + format!("Pulled: updated {branch} from {}", status.upstream.unwrap_or_default()) |
| 175 | + }, |
| 176 | + url: None, |
| 177 | + }); |
| 178 | + } |
| 179 | + if action.commits() { |
| 180 | + if feature_branch { |
| 181 | + let fragment = crate::worktree::sanitize_fragment(message.lines().next().unwrap_or("update")); |
| 182 | + let mut branch = format!("feature/{fragment}"); |
| 183 | + for n in 2.. { |
| 184 | + if git::git(path, &["rev-parse", "--verify", "-q", &format!("refs/heads/{branch}")]).is_err() { |
| 185 | + break; |
| 186 | + } |
| 187 | + branch = format!("feature/{fragment}-{n}"); |
| 188 | + } |
| 189 | + git::git(path, &["switch", "-c", &branch])?; |
| 190 | + } |
| 191 | + match &files { |
| 192 | + Some(files) => { |
| 193 | + let mut args = vec!["add", "-A", "--"]; |
| 194 | + args.extend(files.iter().map(String::as_str)); |
| 195 | + git::git(path, &args)?; |
| 196 | + } |
| 197 | + None => { |
| 198 | + git::git(path, &["add", "-A"])?; |
| 199 | + } |
| 200 | + } |
| 201 | + let message = if message.trim().is_empty() { |
| 202 | + let (summary, patch) = git::staged(path); |
| 203 | + let branch = git::status(path).and_then(|status| status.branch); |
| 204 | + crate::textgen::commit_message(branch.as_deref(), &summary, &patch).unwrap_or_else(|| "Update files".into()) |
| 205 | + } else { |
| 206 | + message |
| 207 | + }; |
| 208 | + git::git(path, &["commit", "-q", "-m", &message])?; |
| 209 | + let sha = git::git(path, &["rev-parse", "--short", "HEAD"])?.trim().to_string(); |
| 210 | + title = format!("Committed {sha}"); |
| 211 | + } |
| 212 | + if action.pushes() || action.opens_pr() { |
| 213 | + let status = git::status(path).unwrap_or_default(); |
| 214 | + let branch = status.branch.clone().ok_or("Detached HEAD: check out a branch before pushing.")?; |
| 215 | + if action.pushes() || status.upstream.is_none() { |
| 216 | + if status.upstream.is_none() { |
| 217 | + git::git(path, &["push", "-q", "-u", "origin", &branch])?; |
| 218 | + } else { |
| 219 | + git::git(path, &["push", "-q"])?; |
| 220 | + } |
| 221 | + let sha = git::git(path, &["rev-parse", "--short", "HEAD"])?.trim().to_string(); |
| 222 | + title = format!("Pushed {sha} to {branch}"); |
| 223 | + } |
| 224 | + if action.opens_pr() { |
| 225 | + let gh = crate::ui::workspace::find_program(&["gh"]).ok_or("Install the GitHub CLI (gh) to create pull requests.")?; |
| 226 | + let base = status.default_branch.clone().unwrap_or_else(|| "main".into()); |
| 227 | + let base_ref = format!("origin/{base}"); |
| 228 | + let commits = git::git(path, &["log", "--oneline", &format!("{base_ref}..HEAD")]).unwrap_or_default(); |
| 229 | + let stat = git::git(path, &["diff", "--stat", &format!("{base_ref}...HEAD")]).unwrap_or_default(); |
| 230 | + let patch = git::git(path, &["diff", &format!("{base_ref}...HEAD")]).unwrap_or_default(); |
| 231 | + let mut command = std::process::Command::new(gh); |
| 232 | + command.current_dir(path).args(["pr", "create", "--base", &base, "--head", &branch]); |
| 233 | + match crate::textgen::pull_request_content(&base, &branch, &commits, &stat, &patch) { |
| 234 | + Some((pr_title, body)) => command.args(["--title", &pr_title, "--body", &body]), |
| 235 | + None => command.arg("--fill"), |
| 236 | + }; |
| 237 | + let output = command.stdin(std::process::Stdio::null()).output().map_err(|err| err.to_string())?; |
| 238 | + if !output.status.success() { |
| 239 | + return Err(String::from_utf8_lossy(&output.stderr).trim().to_string()); |
| 240 | + } |
| 241 | + let url = String::from_utf8_lossy(&output.stdout).lines().rev().find(|line| line.starts_with("http")).map(str::to_string); |
| 242 | + let number = url.as_deref().and_then(|url| url.rsplit('/').next()).unwrap_or_default().to_string(); |
| 243 | + return Ok(Outcome { title: format!("Created PR #{number}"), url }); |
| 244 | + } |
| 245 | + } |
| 246 | + Ok(Outcome { title: if title.is_empty() { "Done".into() } else { title }, url: None }) |
| 247 | + } |
| 248 | + |
| 249 | + impl Workspace { |
| 250 | + /// The folder the git actions work in: the open thread's worktree, or |
| 251 | + /// its project's checkout. `None` for a thread on another device. |
| 252 | + fn git_path(&self, cx: &App) -> Option<PathBuf> { |
| 253 | + let project = self.active_project()?; |
| 254 | + if project.environment.is_some() { |
| 255 | + return None; |
| 256 | + } |
| 257 | + Some(self.active_worktree(cx).unwrap_or_else(|| project.path.clone())) |
| 258 | + } |
| 259 | + |
| 260 | + /// Starts an action, after asking first when it would push to the |
| 261 | + /// default branch. |
| 262 | + pub(super) fn git_action(&mut self, action: GitAction, window: &mut Window, cx: &mut Context<Self>) { |
| 263 | + let Some((_, status)) = self.git_status.clone() else { return }; |
| 264 | + if action == GitAction::Commit { |
| 265 | + self.open_commit_dialog(status, window, cx); |
| 266 | + return; |
| 267 | + } |
| 268 | + if (action.pushes() || action.opens_pr()) && status.on_default() { |
| 269 | + self.confirm_default_branch(action, &status, None, window, cx); |
| 270 | + return; |
| 271 | + } |
| 272 | + self.start_git_action(action, None, String::new(), false, window, cx); |
| 273 | + } |
| 274 | + |
| 275 | + fn start_git_action( |
| 276 | + &mut self, |
| 277 | + action: GitAction, |
| 278 | + files: Option<Vec<String>>, |
| 279 | + message: String, |
| 280 | + feature_branch: bool, |
| 281 | + window: &mut Window, |
| 282 | + cx: &mut Context<Self>, |
| 283 | + ) { |
| 284 | + let Some(path) = self.git_path(cx) else { return }; |
| 285 | + let status = match action { |
| 286 | + GitAction::Pull => "Pulling latest changes...", |
| 287 | + _ if action.commits() && message.trim().is_empty() => "Generating commit message...", |
| 288 | + _ if action.commits() => "Committing...", |
| 289 | + _ if action.pushes() => "Pushing...", |
| 290 | + _ => "Creating pull request...", |
| 291 | + }; |
| 292 | + self.git_running = Some((status, Instant::now())); |
| 293 | + cx.notify(); |
| 294 | + let task = cx.background_executor().spawn({ |
| 295 | + let path = path.clone(); |
| 296 | + async move { run(&path, action, files, message, feature_branch) } |
| 297 | + }); |
| 298 | + cx.spawn_in(window, async move |this, cx| { |
| 299 | + let result = task.await; |
| 300 | + let _ = this.update_in(cx, |this, window, cx| { |
| 301 | + this.git_running = None; |
| 302 | + match result { |
| 303 | + Ok(outcome) => { |
| 304 | + let mut notification = Notification::success(outcome.title.clone()); |
| 305 | + if let Some(url) = outcome.url { |
| 306 | + notification = notification.content(move |_, _, _| { |
| 307 | + let url = url.clone(); |
| 308 | + MetalButton::new("view-pr") |
| 309 | + .compact() |
| 310 | + .label("View PR") |
| 311 | + .on_click(move |_, _, _| crate::ui::workspace::open_url(&url)) |
| 312 | + .into_any_element() |
| 313 | + }); |
| 314 | + } |
| 315 | + window.push_notification(notification, cx); |
| 316 | + } |
| 317 | + Err(err) => window.push_notification(Notification::error(err).title("Action failed"), cx), |
| 318 | + } |
| 319 | + this.poll_git(cx); |
| 320 | + cx.notify(); |
| 321 | + }); |
| 322 | + }) |
| 323 | + .detach(); |
| 324 | + } |
| 325 | + |
| 326 | + /// T3 Code's question before pushing to the default branch: go on, or |
| 327 | + /// check out a feature branch and run the same action there. |
| 328 | + fn confirm_default_branch( |
| 329 | + &mut self, |
| 330 | + action: GitAction, |
| 331 | + status: &GitStatus, |
| 332 | + commit: Option<(Option<Vec<String>>, String)>, |
| 333 | + window: &mut Window, |
| 334 | + cx: &mut Context<Self>, |
| 335 | + ) { |
| 336 | + let branch = status.branch.clone().unwrap_or_default(); |
| 337 | + let with_commit = action.commits() || commit.is_some(); |
| 338 | + let (title, description, continue_label): (&str, String, String) = match (action.opens_pr(), with_commit) { |
| 339 | + (false, true) => ( |
| 340 | + "Commit & push to default ref?", |
| 341 | + "This action will commit and push changes".into(), |
| 342 | + format!("Commit & push to {branch}"), |
| 343 | + ), |
| 344 | + (false, false) => ("Push to default ref?", "This action will push local commits".into(), format!("Push to {branch}")), |
| 345 | + (true, true) => ( |
| 346 | + "Commit, push & create PR from default ref?", |
| 347 | + "This action will commit, push, and create a PR".into(), |
| 348 | + "Commit, push & create PR".into(), |
| 349 | + ), |
| 350 | + (true, false) => ( |
| 351 | + "Push & create PR from default ref?", |
| 352 | + "This action will push local commits and create a PR".into(), |
| 353 | + "Push & create PR".into(), |
| 354 | + ), |
| 355 | + }; |
| 356 | + let description = |
| 357 | + format!("{description} on \"{branch}\". You can continue on this ref or create a feature ref and run the same action there."); |
| 358 | + let workspace = cx.entity().downgrade(); |
| 359 | + let (files, message) = commit.unwrap_or((None, String::new())); |
| 360 | + window.open_dialog(cx, move |dialog, _, cx| { |
| 361 | + let p = metal::palette(cx); |
| 362 | + let (stay, feature) = (workspace.clone(), workspace.clone()); |
| 363 | + let (stay_files, feature_files) = (files.clone(), files.clone()); |
| 364 | + let (stay_message, feature_message) = (message.clone(), message.clone()); |
| 365 | + dialog |
| 366 | + .w(px(576.)) |
| 367 | + .title(title) |
| 368 | + .child(div().text_size(px(13.)).text_color(p.ink2).child(description.clone())) |
| 369 | + .footer( |
| 370 | + h_flex() |
| 371 | + .w_full() |
| 372 | + .justify_end() |
| 373 | + .gap(px(8.)) |
| 374 | + .child(MetalButton::new("default-abort").compact().label("Abort").on_click(|_, window, cx| window.close_dialog(cx))) |
| 375 | + .child(MetalButton::new("default-continue").compact().label(continue_label.clone()).on_click( |
| 376 | + move |_, window, cx| { |
| 377 | + window.close_dialog(cx); |
| 378 | + let (files, message) = (stay_files.clone(), stay_message.clone()); |
| 379 | + let _ = stay.update(cx, |this, cx| this.start_git_action(action, files, message, false, window, cx)); |
| 380 | + }, |
| 381 | + )) |
| 382 | + .child( |
| 383 | + MetalButton::new("default-feature") |
| 384 | + .primary() |
| 385 | + .compact() |
| 386 | + .label("Check out feature branch & continue") |
| 387 | + .on_click(move |_, window, cx| { |
| 388 | + window.close_dialog(cx); |
| 389 | + let (files, message) = (feature_files.clone(), feature_message.clone()); |
| 390 | + let _ = feature.update(cx, |this, cx| this.start_git_action(action, files, message, true, window, cx)); |
| 391 | + }), |
| 392 | + ), |
| 393 | + ) |
| 394 | + }); |
| 395 | + } |
| 396 | + |
| 397 | + /// T3 Code's commit dialog: the branch, the files (pick which with |
| 398 | + /// Edit), and an optional message. |
| 399 | + fn open_commit_dialog(&mut self, status: GitStatus, window: &mut Window, cx: &mut Context<Self>) { |
| 400 | + let workspace = cx.entity().downgrade(); |
| 401 | + let dialog = cx.new(|cx| CommitDialog::new(status, workspace, window, cx)); |
| 402 | + window.open_dialog(cx, move |modal, _, _| { |
| 403 | + let dialog = dialog.clone(); |
| 404 | + modal.w(px(560.)).title("Commit changes").child(dialog) |
| 405 | + }); |
| 406 | + } |
| 407 | + |
| 408 | + /// The git actions' row in the thread details panel: the quick action |
| 409 | + /// and the menu beside it, or what's running and for how long. |
| 410 | + pub(super) fn render_git_actions(&self, cx: &mut Context<Self>) -> AnyElement { |
| 411 | + let p = metal::palette(cx); |
| 412 | + let Some((_, status)) = self.git_status.clone() else { |
| 413 | + return super::Workspace::panel_row(Lucide::GitCommitHorizontal, "Commit", p).text_color(p.ink3).into_any_element(); |
| 414 | + }; |
| 415 | + let pr_open = self.active.as_ref().and_then(|session| session.read(cx).pull_request.clone()).is_some_and(|pr| pr.is_open()); |
| 416 | + if let Some((label, started)) = self.git_running { |
| 417 | + return h_flex() |
| 418 | + .h(px(32.)) |
| 419 | + .px(px(10.)) |
| 420 | + .gap(px(10.)) |
| 421 | + .text_size(px(13.)) |
| 422 | + .text_color(p.ink2) |
| 423 | + .child(metal::led("git-running", metal::Led::Live, true, p)) |
| 424 | + .child(div().flex_1().min_w_0().truncate().child(label)) |
| 425 | + .child(div().text_size(px(12.)).text_color(p.ink3).child(elapsed(started))) |
| 426 | + .into_any_element(); |
| 427 | + } |
| 428 | + let quick = quick_action(&status, pr_open); |
| 429 | + let (icon, label, hint, action) = match &quick { |
| 430 | + Quick::Run(action) => (action.icon(), action.label(), None, Some(*action)), |
| 431 | + Quick::Hint { label, hint } => { |
| 432 | + let icon = match *label { |
| 433 | + "Commit" => Icon::new(Lucide::GitCommitHorizontal), |
| 434 | + "Push" => Icon::new(Lucide::CloudUpload), |
| 435 | + _ => Icon::new(IconName::Info), |
| 436 | + }; |
| 437 | + (icon, *label, Some(*hint), None) |
| 438 | + } |
| 439 | + }; |
| 440 | + let workspace = cx.entity().downgrade(); |
| 441 | + let menu_status = status.clone(); |
| 442 | + h_flex() |
| 443 | + .w_full() |
| 444 | + .rounded(px(10.)) |
| 445 | + .hover(|style| style.bg(p.row_hover)) |
| 446 | + .child( |
| 447 | + h_flex() |
| 448 | + .id("git-quick-action") |
| 449 | + .flex_1() |
| 450 | + .min_w_0() |
| 451 | + .h(px(32.)) |
| 452 | + .px(px(10.)) |
| 453 | + .gap(px(10.)) |
| 454 | + .text_size(px(13.)) |
| 455 | + .font_weight(FontWeight::MEDIUM) |
| 456 | + .text_color(if action.is_some() { p.ink.opacity(0.8) } else { p.ink3 }) |
| 457 | + .cursor_pointer() |
| 458 | + .child(icon.size(px(16.)).text_color(p.ink3)) |
| 459 | + .child(div().truncate().child(label)) |
| 460 | + .when_some(hint, |this, hint| { |
| 461 | + this.tooltip(move |window, cx| Tooltip::new(hint).build(window, cx)) |
| 462 | + .on_click(move |_, window, cx| window.push_notification(Notification::info(hint).title(label), cx)) |
| 463 | + }) |
| 464 | + .when_some(action, |this, action| { |
| 465 | + this.on_click(cx.listener(move |this, _, window, cx| this.git_action(action, window, cx))) |
| 466 | + }), |
| 467 | + ) |
| 468 | + .child(div().w(px(1.)).h(px(16.)).bg(p.rule)) |
| 469 | + .child( |
| 470 | + MetalButton::new("git-action-options") |
| 471 | + .ghost() |
| 472 | + .mini() |
| 473 | + .w(px(32.)) |
| 474 | + .h(px(32.)) |
| 475 | + .icon(IconName::ChevronDown) |
| 476 | + .tooltip("Git action options") |
| 477 | + .dropdown_menu_with_anchor(Anchor::TopRight, move |mut menu, _, _| { |
| 478 | + for action in [GitAction::Commit, GitAction::Push, GitAction::CreatePr] { |
| 479 | + if action != GitAction::Commit && !menu_status.has_origin { |
| 480 | + continue; |
| 481 | + } |
| 482 | + if action == GitAction::CreatePr && pr_open { |
| 483 | + continue; |
| 484 | + } |
| 485 | + let reason = disabled_reason(action, &menu_status, pr_open); |
| 486 | + let workspace = workspace.clone(); |
| 487 | + menu = menu.item( |
| 488 | + PopupMenuItem::new(action.label()) |
| 489 | + .icon(action.icon()) |
| 490 | + .disabled(reason.is_some()) |
| 491 | + .on_click(move |_, window, cx| { |
| 492 | + let _ = workspace.update(cx, |this, cx| this.git_action(action, window, cx)); |
| 493 | + }), |
| 494 | + ); |
| 495 | + } |
| 496 | + if menu_status.detached() { |
| 497 | + menu = menu.separator().label("Detached HEAD: create and check out a branch to enable push and pull request actions."); |
| 498 | + } else if menu_status.behind > 0 { |
| 499 | + menu = menu.separator().label("Behind upstream. Pull/rebase first."); |
| 500 | + } |
| 501 | + menu |
| 502 | + }), |
| 503 | + ) |
| 504 | + .into_any_element() |
| 505 | + } |
| 506 | + } |
| 507 | + |
| 508 | + fn elapsed(started: Instant) -> String { |
| 509 | + let secs = started.elapsed().as_secs(); |
| 510 | + if secs < 60 { format!("{secs}s") } else { format!("{}m {}s", secs / 60, secs % 60) } |
| 511 | + } |
| 512 | + |
| 513 | + /// The commit dialog's state. |
| 514 | + struct CommitDialog { |
| 515 | + status: GitStatus, |
| 516 | + selected: Vec<bool>, |
| 517 | + editing: bool, |
| 518 | + message: Entity<EditorState>, |
| 519 | + workspace: WeakEntity<Workspace>, |
| 520 | + } |
| 521 | + |
| 522 | + impl CommitDialog { |
| 523 | + fn new(status: GitStatus, workspace: WeakEntity<Workspace>, window: &mut Window, cx: &mut Context<Self>) -> Self { |
| 524 | + let message = cx.new(|cx| { |
| 525 | + crate::ui::markdown_input::composer_state(EditorState::new(window, cx), false).placeholder("Leave empty to auto-generate") |
| 526 | + }); |
| 527 | + let selected = vec![true; status.files.len()]; |
| 528 | + Self { status, selected, editing: false, message, workspace } |
| 529 | + } |
| 530 | + |
| 531 | + fn picked(&self) -> Vec<&StatusFile> { |
| 532 | + self.status.files.iter().zip(&self.selected).filter(|(_, on)| **on).map(|(file, _)| file).collect() |
| 533 | + } |
| 534 | + |
| 535 | + fn commit(&mut self, feature_branch: bool, window: &mut Window, cx: &mut Context<Self>) { |
| 536 | + let all = self.selected.iter().all(|on| *on); |
| 537 | + let files = (!all).then(|| self.picked().into_iter().map(|file| file.path.clone()).collect::<Vec<_>>()); |
| 538 | + let message = self.message.read(cx).value().to_string(); |
| 539 | + window.close_dialog(cx); |
| 540 | + let _ = self.workspace.update(cx, |this, cx| { |
| 541 | + this.start_git_action(GitAction::Commit, files, message, feature_branch, window, cx) |
| 542 | + }); |
| 543 | + } |
| 544 | + } |
| 545 | + |
| 546 | + impl Render for CommitDialog { |
| 547 | + fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement { |
| 548 | + let p = metal::palette(cx); |
| 549 | + let picked = self.picked(); |
| 550 | + let (added, removed) = picked.iter().fold((0, 0), |(a, r), file| (a + file.added.unwrap_or(0), r + file.removed.unwrap_or(0))); |
| 551 | + let count = picked.len(); |
| 552 | + let total = self.status.files.len(); |
| 553 | + let on_default = self.status.on_default(); |
| 554 | + let all = self.selected.iter().all(|on| *on); |
| 555 | + v_flex() |
| 556 | + .gap(px(12.)) |
| 557 | + .child( |
| 558 | + div() |
| 559 | + .text_size(px(13.)) |
| 560 | + .text_color(p.ink2) |
| 561 | + .child("Review and confirm your commit. Leave the message blank to auto-generate one."), |
| 562 | + ) |
| 563 | + .child( |
| 564 | + v_flex() |
| 565 | + .p(px(12.)) |
| 566 | + .gap(px(8.)) |
| 567 | + .rounded(px(14.)) |
| 568 | + .well(p) |
| 569 | + .text_size(px(13.)) |
| 570 | + .child( |
| 571 | + h_flex() |
| 572 | + .gap(px(8.)) |
| 573 | + .child(div().w(px(48.)).text_color(p.ink3).child("Branch")) |
| 574 | + .child(div().flex_1().truncate().text_color(p.ink).child(self.status.branch.clone().unwrap_or_else(|| "(detached HEAD)".into()))) |
| 575 | + .when(on_default, |this| this.child(div().text_size(px(12.)).text_color(p.amber_ink).child("Default branch"))), |
| 576 | + ) |
| 577 | + .child( |
| 578 | + h_flex() |
| 579 | + .gap(px(8.)) |
| 580 | + .child(div().w(px(48.)).text_color(p.ink3).child("Files")) |
| 581 | + .child(div().flex_1().text_color(p.ink2).when(count < total, |this| this.child(format!("({count} of {total})")))) |
| 582 | + .when(self.editing, |this| { |
| 583 | + this.child( |
| 584 | + Checkbox::new("commit-all") |
| 585 | + .checked(all) |
| 586 | + .on_click(cx.listener(move |this, _, _, cx| { |
| 587 | + let on = !all; |
| 588 | + this.selected.iter_mut().for_each(|selected| *selected = on); |
| 589 | + cx.notify(); |
| 590 | + })), |
| 591 | + ) |
| 592 | + }) |
| 593 | + .child( |
| 594 | + MetalButton::new("commit-edit-files") |
| 595 | + .ghost() |
| 596 | + .mini() |
| 597 | + .label(if self.editing { "Done" } else { "Edit" }) |
| 598 | + .on_click(cx.listener(|this, _, _, cx| { |
| 599 | + this.editing = !this.editing; |
| 600 | + cx.notify(); |
| 601 | + })), |
| 602 | + ), |
| 603 | + ) |
| 604 | + .child( |
| 605 | + v_flex() |
| 606 | + .id("commit-files") |
| 607 | + .max_h(px(176.)) |
| 608 | + .overflow_y_scroll() |
| 609 | + .font_family(metal::MONO) |
| 610 | + .text_size(px(12.)) |
| 611 | + .when(total == 0, |this| this.child(div().text_color(p.ink3).child("none"))) |
| 612 | + .children(self.status.files.iter().enumerate().map(|(ix, file)| { |
| 613 | + let on = self.selected[ix]; |
| 614 | + h_flex() |
| 615 | + .gap(px(8.)) |
| 616 | + .py(px(2.)) |
| 617 | + .when(self.editing, |this| { |
| 618 | + this.child(Checkbox::new(("commit-file", ix)).checked(on).on_click(cx.listener(move |this, _, _, cx| { |
| 619 | + this.selected[ix] = !this.selected[ix]; |
| 620 | + cx.notify(); |
| 621 | + }))) |
| 622 | + }) |
| 623 | + .child(div().flex_1().min_w_0().truncate().text_color(if on { p.ink } else { p.ink3 }).child(file.path.clone())) |
| 624 | + .child(if on { |
| 625 | + h_flex() |
| 626 | + .gap(px(4.)) |
| 627 | + .child(div().text_color(p.green_ink).child(format!("+{}", file.added.unwrap_or(0)))) |
| 628 | + .child(div().text_color(p.red).child(format!("-{}", file.removed.unwrap_or(0)))) |
| 629 | + .into_any_element() |
| 630 | + } else { |
| 631 | + div().text_color(p.ink3).child("Excluded").into_any_element() |
| 632 | + }) |
| 633 | + })), |
| 634 | + ) |
| 635 | + .child( |
| 636 | + h_flex() |
| 637 | + .justify_end() |
| 638 | + .gap(px(4.)) |
| 639 | + .font_family(metal::MONO) |
| 640 | + .text_size(px(12.)) |
| 641 | + .child(div().text_color(p.green_ink).child(format!("+{added}"))) |
| 642 | + .child(div().text_color(p.ink3).child("/")) |
| 643 | + .child(div().text_color(p.red).child(format!("-{removed}"))), |
| 644 | + ), |
| 645 | + ) |
| 646 | + .child( |
| 647 | + v_flex() |
| 648 | + .gap(px(6.)) |
| 649 | + .child(div().text_size(px(12.)).font_weight(FontWeight::MEDIUM).text_color(p.ink2).child("Commit message (optional)")) |
| 650 | + .child( |
| 651 | + div() |
| 652 | + .px(px(10.)) |
| 653 | + .py(px(6.)) |
| 654 | + .rounded(px(10.)) |
| 655 | + .well(p) |
| 656 | + .text_size(px(13.)) |
| 657 | + .child(crate::ui::markdown_input::MarkdownInput::new(&self.message).rows(3, 8)), |
| 658 | + ), |
| 659 | + ) |
| 660 | + .child( |
| 661 | + h_flex() |
| 662 | + .justify_end() |
| 663 | + .gap(px(8.)) |
| 664 | + .child(MetalButton::new("commit-cancel").compact().label("Cancel").on_click(|_, window, cx| window.close_dialog(cx))) |
| 665 | + .child( |
| 666 | + MetalButton::new("commit-new-branch") |
| 667 | + .compact() |
| 668 | + .label("Commit on new branch") |
| 669 | + .disabled(count == 0) |
| 670 | + .on_click(cx.listener(|this, _, window, cx| this.commit(true, window, cx))), |
| 671 | + ) |
| 672 | + .child( |
| 673 | + MetalButton::new("commit-confirm") |
| 674 | + .primary() |
| 675 | + .compact() |
| 676 | + .label("Commit") |
| 677 | + .disabled(count == 0) |
| 678 | + .on_click(cx.listener(|this, _, window, cx| this.commit(false, window, cx))), |
| 679 | + ), |
| 680 | + ) |
| 681 | + } |
| 682 | + } |