Skip to content

Commit

Permalink
fix(friendshipper): remove unnecessary arg char length counting in re…
Browse files Browse the repository at this point in the history
…vert
  • Loading branch information
rudoi committed Oct 11, 2024
1 parent 59f3d1f commit 00f1db8
Showing 1 changed file with 2 additions and 82 deletions.
84 changes: 2 additions & 82 deletions friendshipper/src-tauri/src/repo/operations/revert.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use anyhow::{anyhow, bail};
use anyhow::anyhow;
use axum::extract::State;
use axum::{async_trait, Json};
use std::fs;
use std::io::Write;
use tempfile::NamedTempFile;
use tokio::sync::oneshot::error::RecvError;
use tracing::info;
use tracing::{error, instrument};
use tracing::instrument;

use crate::engine::EngineProvider;
use crate::state::AppState;
Expand Down Expand Up @@ -43,26 +43,8 @@ where
return Err(CoreError::Input(anyhow!("no files provided")));
}

let mut num_chars = 0;
for f in self.files.iter() {
num_chars += f.len();
}

let branch = self.repo_status.read().branch.clone();

// windows command line length limit is 8191, so if we're close to that, checking using a file instead
if num_chars > 8000 {
match self.revert_with_listfile(&branch).await {
Ok(_) => return Ok(()),
Err(e) => {
error!(
"Revert files with listfile failed, falling back to chunked batches: {}",
e
)
}
}
}

let mut temp_file = NamedTempFile::new()?;
for file in &self.files {
writeln!(temp_file, "{}", file)?;
Expand All @@ -86,68 +68,6 @@ where
}
}

impl<T> RevertFilesOp<T>
where
T: EngineProvider,
{
async fn revert_with_listfile(&self, branch: &str) -> anyhow::Result<()> {
let mut listfile_path = std::env::temp_dir();
listfile_path.push("Friendshipper");

if !listfile_path.exists() {
if let Err(e) = fs::create_dir(&listfile_path) {
bail!(
"Failed to create directory for storing temp file: {:?}. Reason: {}",
listfile_path,
e
);
}
}

listfile_path.push("revert_files.txt");

match fs::File::create(&listfile_path) {
Err(e) => {
bail!(
"Failed to create listfile '{:?}' for RevertFilesOp: {}",
listfile_path,
e
);
}
Ok(file) => {
let mut writer = std::io::BufWriter::new(file);
for path in &self.files {
if let Err(e) = writeln!(writer, "{}", &path) {
bail!(
"Failed to write '{}' to file {:?}: {}",
path,
listfile_path,
e
);
}
}
match writer.flush() {
Ok(_) => {}
Err(e) => {
bail!(
"Failed to write listfile '{:?}' for RevertFilesOp: {}",
listfile_path,
e
);
}
}
}
}

let pathspec_arg = format!("--pathspec-from-file={}", listfile_path.to_string_lossy());
let args: Vec<&str> = vec!["checkout", &branch, &pathspec_arg];

self.git_client.run(&args, git::Opts::default()).await?;

Ok(())
}
}

#[instrument(skip(state))]
pub async fn revert_files_handler<T>(
State(state): State<AppState<T>>,
Expand Down

0 comments on commit 00f1db8

Please sign in to comment.