Use git stash to reset third-party exercises

This commit is contained in:
mo8it 2024-04-18 11:20:51 +02:00
parent d64836f317
commit 9f5be60b40

View File

@ -1,10 +1,10 @@
use anyhow::{Context, Result}; use anyhow::{bail, Context, Result};
use crossterm::style::{style, StyledContent, Stylize}; use crossterm::style::{style, StyledContent, Stylize};
use std::{ use std::{
fmt::{self, Display, Formatter}, fmt::{self, Display, Formatter},
fs, fs,
path::Path, path::Path,
process::{Command, Output}, process::{Command, Output, Stdio},
}; };
use crate::{ use crate::{
@ -88,9 +88,31 @@ impl Exercise {
} }
pub fn reset(&self) -> Result<()> { pub fn reset(&self) -> Result<()> {
EMBEDDED_FILES if Path::new("info.toml").exists() {
.write_exercise_to_disk(self.path, WriteStrategy::Overwrite) let output = Command::new("git")
.with_context(|| format!("Failed to reset the exercise {self}")) .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<'_>> { pub fn terminal_link(&self) -> StyledContent<TerminalFileLink<'_>> {