@@ -10,8 +10,8 @@ pub use crate::remote::*;
use anyhow::{Context as _, Result};
pub use git2 as libgit;
use gpui::{Action, actions};
+pub use repository::RemoteCommandOutput;
pub use repository::WORK_DIRECTORY_REPO_PATH;
-pub use repository::{GitCommandOutput, RemoteCommandOutput};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use std::ffi::OsStr;
@@ -132,32 +132,6 @@ pub struct RemoteCommandOutput {
pub stderr: String,
}
-#[derive(Debug, Clone)]
-pub struct GitCommandOutput {
- pub stdout: String,
- pub stderr: String,
-}
-
-impl std::fmt::Display for GitCommandOutput {
- fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
- let stderr = self.stderr.trim();
- let message = if stderr.is_empty() {
- self.stdout.trim()
- } else {
- stderr
- };
- write!(f, "{}", message)
- }
-}
-
-impl std::error::Error for GitCommandOutput {}
-
-impl GitCommandOutput {
- pub fn is_empty(&self) -> bool {
- self.stdout.is_empty() && self.stderr.is_empty()
- }
-}
-
impl RemoteCommandOutput {
pub fn is_empty(&self) -> bool {
self.stdout.is_empty() && self.stderr.is_empty()
@@ -1103,18 +1077,10 @@ impl GitRepository for RealGitRepository {
.spawn(async move {
let branch = branch.await?;
- match GitBinary::new(git_binary_path, working_directory?, executor)
- .run_with_output(&["checkout", &branch])
- .await
- {
- Ok(_) => anyhow::Ok(()),
- Err(e) => {
- if let Some(git_error) = e.downcast_ref::<GitBinaryCommandError>() {
- anyhow::bail!("{}", git_error.stderr.trim());
- }
- Err(e)
- }
- }
+ GitBinary::new(git_binary_path, working_directory?, executor)
+ .run(&["checkout", &branch])
+ .await?;
+ anyhow::Ok(())
})
.boxed()
}
@@ -1138,18 +1104,10 @@ impl GitRepository for RealGitRepository {
self.executor
.spawn(async move {
- match GitBinary::new(git_binary_path, working_directory?, executor)
- .run_with_output(&["branch", "-m", &branch, &new_name])
- .await
- {
- Ok(_) => Ok(()),
- Err(e) => {
- if let Some(git_error) = e.downcast_ref::<GitBinaryCommandError>() {
- anyhow::bail!("{}", git_error.stderr.trim());
- }
- Err(e)
- }
- }
+ GitBinary::new(git_binary_path, working_directory?, executor)
+ .run(&["branch", "-m", &branch, &new_name])
+ .await?;
+ anyhow::Ok(())
})
.boxed()
}
@@ -1865,31 +1823,6 @@ impl GitBinary {
Ok(stdout)
}
- pub async fn run_with_output<S>(
- &self,
- args: impl IntoIterator<Item = S>,
- ) -> Result<GitCommandOutput>
- where
- S: AsRef<OsStr>,
- {
- let mut command = self.build_command(args);
- let output = command.output().await?;
-
- let stdout = String::from_utf8_lossy(&output.stdout).to_string();
- let stderr = String::from_utf8_lossy(&output.stderr).to_string();
-
- if !output.status.success() {
- return Err(GitBinaryCommandError {
- stdout: stdout.clone(),
- stderr: stderr.clone(),
- status: output.status,
- }
- .into());
- }
-
- Ok(GitCommandOutput { stdout, stderr })
- }
-
/// Returns the result of the command without trimming the trailing newline.
pub async fn run_raw<S>(&self, args: impl IntoIterator<Item = S>) -> Result<String>
where
@@ -244,37 +244,25 @@ impl BranchListDelegate {
let new_branch_name = new_branch_name.to_string().replace(' ', "-");
cx.spawn(async move |_, cx| {
if let Some(based_branch) = from_branch {
- match repo
- .update(cx, |repo, _| repo.change_branch(based_branch.to_string()))?
- .await
- {
- Ok(Ok(_)) => {}
- Ok(Err(error)) => return Err(error),
- Err(_) => return Err(anyhow::anyhow!("Operation was canceled")),
- }
+ repo.update(cx, |repo, _| repo.change_branch(based_branch.to_string()))?
+ .await??;
}
- match repo
- .update(cx, |repo, _| repo.create_branch(new_branch_name.clone()))?
- .await
- {
- Ok(Ok(_)) => {}
- Ok(Err(error)) => return Err(error),
- Err(_) => return Err(anyhow::anyhow!("Operation was canceled")),
- }
-
- match repo
- .update(cx, |repo, _| repo.change_branch(new_branch_name))?
- .await
- {
- Ok(Ok(_)) => {}
- Ok(Err(error)) => return Err(error),
- Err(_) => return Err(anyhow::anyhow!("Operation was canceled")),
- }
+ repo.update(cx, |repo, _| {
+ repo.create_branch(new_branch_name.to_string())
+ })?
+ .await??;
+ repo.update(cx, |repo, _| {
+ repo.change_branch(new_branch_name.to_string())
+ })?
+ .await??;
Ok(())
})
- .detach_and_prompt_err("Failed to create branch", window, cx, |_, _, _| None);
+ .detach_and_prompt_err("Failed to create branch", window, cx, |e, _, _| {
+ Some(e.to_string())
+ });
+ cx.emit(DismissEvent);
}
}
@@ -425,8 +413,6 @@ impl PickerDelegate for BranchListDelegate {
cx.spawn_in(window, {
let branch = entry.branch.clone();
async move |picker, cx| {
- let branch_name = branch.name().to_string();
-
let branch_change_task = picker.update(cx, |this, cx| {
let repo = this
.delegate
@@ -438,31 +424,23 @@ impl PickerDelegate for BranchListDelegate {
let mut cx = cx.to_async();
anyhow::Ok(async move {
- repo.update(&mut cx, |repo, _| repo.change_branch(branch_name))?
- .await?
+ repo.update(&mut cx, |repo, _| {
+ repo.change_branch(branch.name().to_string())
+ })?
+ .await?
})
})??;
- match branch_change_task.await {
- Ok(_) => {
- let _ = picker.update(cx, |_, cx| {
- cx.emit(DismissEvent);
- anyhow::Ok(())
- })?;
- }
- Err(error) => {
- let _ = picker.update(cx, |_, cx| {
- cx.emit(DismissEvent);
- anyhow::Ok(())
- })?;
- return Err(error);
- }
- }
+ branch_change_task.await?;
+
+ picker.update(cx, |_, cx| {
+ cx.emit(DismissEvent);
- anyhow::Ok(())
+ anyhow::Ok(())
+ })
}
})
- .detach_and_prompt_err("Failed to switch branch", window, cx, |_, _, _| None);
+ .detach_and_prompt_err("Failed to change branch", window, cx, |_, _, _| None);
}
fn dismissed(&mut self, _: &mut Window, cx: &mut Context<Picker<Self>>) {