Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
git_panel: Add error treatment when trying to push from an outdated branch (#47296)
Wagner Almeida3c2f78251463
diff --git a/crates/git_ui_core/src/notifications.rs b/crates/git_ui_core/src/notifications.rsindex 5fc29326078..fc86d512be6 100644--- a/crates/git_ui_core/src/notifications.rs+++ b/crates/git_ui_core/src/notifications.rs@@ -33,6 +33,22 @@ pub fn open_output(workspace.add_item_to_center(Box::new(editor), window, cx);}+fn is_non_fast_forward_error(error_message: &str) -> bool {+ (error_message.contains("[rejected]")+ && (error_message.contains("(non-fast-forward)")+ || error_message.contains("(fetch first)")))+ || (error_message.contains("Updates were rejected")+ && (error_message.contains("behind") || error_message.contains("remote")))+}++fn git_error_toast_title(action: &str, message: &str) -> String {+ if action == "push" && is_non_fast_forward_error(message) {+ "Push rejected: fetch and integrate remote changes before pushing again".to_string()+ } else {+ format!("git {action} failed")+ }+}+pub fn show_error_toast(workspace: Entity<Workspace>,action: impl Into<SharedString>,@@ -50,7 +66,8 @@ pub fn show_error_toast(cx.defer(move |cx| {workspace.update(cx, |workspace, cx| {let workspace_weak = cx.weak_entity();- let toast = StatusToast::new(format!("git {} failed", action), cx, |this, _cx| {+ let title = git_error_toast_title(&action, &message);+ let toast = StatusToast::new(title, cx, |this, _cx| {this.icon(Icon::new(IconName::XCircle).size(IconSize::Small)@@ -90,6 +107,83 @@ fn format_git_error_toast_message(error: &anyhow::Error) -> String {mod tests {use super::*;+ #[test]+ fn test_is_non_fast_forward_error_standard_format() {+ let error = "error: failed to push some refs to 'origin'\n\+ To https://github.com/user/repo.git\n\+ ! [rejected] main -> main (non-fast-forward)";+ assert!(is_non_fast_forward_error(error));+ }++ #[test]+ fn test_is_non_fast_forward_error_behind_variant() {+ let error = "Updates were rejected because the tip of your current branch is behind\n\+ its remote counterpart.";+ assert!(is_non_fast_forward_error(error));+ }++ #[test]+ fn test_is_non_fast_forward_error_remote_variant() {+ let error = "Updates were rejected because the remote contains work that you do\n\+ not have locally.";+ assert!(is_non_fast_forward_error(error));+ }++ #[test]+ fn test_is_non_fast_forward_error_combined_patterns() {+ let error = "! [rejected] main -> main (non-fast-forward)\n\+ error: failed to push some refs to 'https://github.com/user/repo.git'\n\+ hint: Updates were rejected because the remote contains work that you do\n\+ hint: not have locally.";+ assert!(is_non_fast_forward_error(error));+ }++ #[test]+ fn test_is_non_fast_forward_error_false_positives() {+ assert!(!is_non_fast_forward_error("error: failed to push"));++ assert!(!is_non_fast_forward_error("fatal: Authentication failed"));++ assert!(!is_non_fast_forward_error(+ "error: RPC failed; curl 56 OpenSSL"+ ));++ assert!(!is_non_fast_forward_error("[rejected]"));+ assert!(!is_non_fast_forward_error("Updates were rejected"));+ assert!(!is_non_fast_forward_error("(non-fast-forward)"));++ assert!(!is_non_fast_forward_error("fatal: could not read Username"));+ }++ #[test]+ fn test_is_non_fast_forward_error_edge_cases() {+ assert!(!is_non_fast_forward_error(""));++ assert!(!is_non_fast_forward_error(" "));+ assert!(!is_non_fast_forward_error("\n\n"));++ assert!(!is_non_fast_forward_error("random error message"));++ assert!(!is_non_fast_forward_error(+ "! [REJECTED] main -> main (NON-FAST-FORWARD)"+ ));+ }++ #[test]+ fn test_is_non_fast_forward_error_multiline_real_world() {+ let error = "+ To https://github.com/user/repo.git+ ! [rejected] feature-branch -> feature-branch (non-fast-forward)+ error: failed to push some refs to 'https://github.com/user/repo.git'+ hint: Updates were rejected because the remote contains work that you do+ hint: not have locally. This is usually caused by another repository pushing+ hint: to the same ref. You may want to first integrate the remote changes+ hint: (e.g., 'git pull ...') before pushing again.+ hint: See the 'Note about fast-forwards' in 'git push --help' for details.+ ";+ assert!(is_non_fast_forward_error(error));+ }+#[test]fn test_format_git_error_toast_message_prefers_raw_rpc_message() {let rpc_error = RpcError::from_proto(