diff options
| author | mo8it <mo8it@proton.me> | 2024-04-18 11:20:51 +0200 |
|---|---|---|
| committer | mo8it <mo8it@proton.me> | 2024-04-18 11:20:51 +0200 |
| commit | 9f5be60b400f7af505770d9001731f592cb552bb (patch) | |
| tree | 63eda3e594338e5cc7933b064a15ca6b69834f3c /src | |
| parent | d64836f3170c443c6fb5f131930223831c6d724c (diff) | |
Use git stash to reset third-party exercises
Diffstat (limited to 'src')
| -rw-r--r-- | src/exercise.rs | 32 |
1 files changed, 27 insertions, 5 deletions
diff --git a/src/exercise.rs b/src/exercise.rs index bed247e..7f924f9 100644 --- a/src/exercise.rs +++ b/src/exercise.rs @@ -1,10 +1,10 @@ -use anyhow::{Context, Result}; +use anyhow::{bail, Context, Result}; use crossterm::style::{style, StyledContent, Stylize}; use std::{ fmt::{self, Display, Formatter}, fs, path::Path, - process::{Command, Output}, + process::{Command, Output, Stdio}, }; use crate::{ @@ -88,9 +88,31 @@ impl Exercise { } pub fn reset(&self) -> Result<()> { - EMBEDDED_FILES - .write_exercise_to_disk(self.path, WriteStrategy::Overwrite) - .with_context(|| format!("Failed to reset the exercise {self}")) + if Path::new("info.toml").exists() { + let output = Command::new("git") + .arg("stash") + .arg("push") + .arg("--") + .arg(self.path) + .stdin(Stdio::null()) + .stdout(Stdio::null()) + .output() + .with_context(|| format!("Failed to run `git stash push -- {}`", self.path))?; + + if !output.status.success() { + bail!( + "`git stash push -- {}` didn't run successfully: {}", + self.path, + String::from_utf8_lossy(&output.stderr), + ); + } + } else { + EMBEDDED_FILES + .write_exercise_to_disk(self.path, WriteStrategy::Overwrite) + .with_context(|| format!("Failed to reset the exercise {self}"))?; + } + + Ok(()) } pub fn terminal_link(&self) -> StyledContent<TerminalFileLink<'_>> { |
