Added dirty checking to `mlc clean` and `--force` arg to counteract it

main
Michal 2 years ago
parent 3ddf25bb99
commit ad9c242fa0

@ -10,14 +10,15 @@ Work it harder, make it better!
### Basic Commands
| Action | Command | Extra Flags |
|--------------------------------------------------------|-------------------------------------------|------------------------------------------------------------------------------------------------------------------|
| Build a package/packages. | `mlc build <package>` [all if left empty] | `--no-regen`: Doesn't regenerate repository after build |
| Generate pacman repository | `mlc repo-gen` | |
| Update local repos/PKGBUILDs | `mlc pull/update` [all if left empty] | `--no-regen`: If `mode.repository.build_on_update` is `true`, Do not regenerate repository after package rebuild |
| Create and/or open config file | `mlc conf` | |
| Initialises repo/workspace based on config in mlc.toml | `mlc clone/init` | |
| Displays an info panel/overview of the current repo | `mlc info/status` | |
| Action | Command | Extra Flags |
|-----------------------------------------------------------------------------------------|-------------------------------------------|------------------------------------------------------------------------------------------------------------------|
| Build a package/packages. | `mlc build <package>` [all if left empty] | `--no-regen`: Doesn't regenerate repository after build |
| Generate pacman repository | `mlc repo-gen` | |
| Update local repos/PKGBUILDs | `mlc pull/update` [all if left empty] | `--no-regen`: If `mode.repository.build_on_update` is `true`, Do not regenerate repository after package rebuild |
| Create and/or open config file | `mlc conf` | |
| Initialises repo/workspace based on config in mlc.toml | `mlc clone/init` | |
| Displays an info panel/overview of the current repo | `mlc info/status` | |
| Resets Malachite repository by deleting all directories, omitting `mlc.toml` and `.git` | `mlc clean/reset` | `--force`: Remove dirty directories (unstaged, untracked, etc) |
### Exit Codes

@ -4,7 +4,7 @@ smart_pull = true
[mode.workspace]
git_info = true
colorblind = true
colorblind = false
[repositories]
repos = [

@ -39,7 +39,11 @@ pub enum Operation {
/// Removes everything in directory except for mlc.toml
#[clap(name = "clean", aliases = & ["clean", "cl", "reset"])]
Clean,
Clean {
/// Force removes everything, even if git directory is dirty or has unpushed changes or changes at remote
#[clap(short = 'f', long = "force", action = ArgAction::SetTrue)]
force: bool,
},
/// Removes all but the latest 3 versions of each package in a repository
#[clap(name = "prune", aliases = & ["prune", "p"])]

@ -9,4 +9,5 @@ pub enum AppExitCode {
ConfigParseError = 8,
InvalidRepo = 9,
NotInit = 10,
NotClean = 11,
}

@ -72,7 +72,7 @@ fn main() {
}
operations::prune(verbose);
}
Operation::Clean => operations::clean(verbose),
Operation::Clean { force, .. } => operations::clean(verbose, force),
Operation::Info => operations::info(verbose),
}
}

@ -1,6 +1,6 @@
use crate::{info, log};
use crate::{info, log, crash, internal::AppExitCode};
pub fn clean(verbose: bool) {
pub fn clean(verbose: bool, force: bool) {
info!("Resetting mlc repo, deleting all directories");
// Get a vec of all files/dirs in the current directory
let dir_paths = std::fs::read_dir("./").unwrap();
@ -9,9 +9,42 @@ pub fn clean(verbose: bool) {
.map(|x| x.unwrap().path().display().to_string())
.collect::<Vec<String>>();
// Remove all files/dirs in the current directory, excluding ./mlc.toml and .git
// Remove mlc.toml and .git from output
dirs.retain(|x| *x != "./mlc.toml");
dirs.retain(|x| *x != "./.git");
// Enter each directory and check git status
let mut unclean_dirs = vec![];
for dir in &dirs {
let root_dir = std::env::current_dir().unwrap();
log!(verbose, "Entering directory: {}", dir);
std::env::set_current_dir(dir).unwrap();
let status = std::process::Command::new("git")
.arg("status")
.output()
.unwrap();
let output = std::string::String::from_utf8(status.stdout).unwrap();
log!(verbose, "Git status: {}", output);
if output.contains("Your branch is up to date with") && !output.contains("Untracked files") && !output.contains("Changes not staged for commit") {
log!(verbose, "Directory {} is clean", dir);
} else {
unclean_dirs.push(dir);
}
std::env::set_current_dir(&root_dir).unwrap();
log!(verbose, "Current directory: {}", root_dir.display());
}
if unclean_dirs.len() > 0 && !force{
crash!(
AppExitCode::NotClean,
"The following directories are not clean: \n {}\n\
If you are sure no important changes are staged, run `mlc clean` with the `--force` flag to delete them.",
unclean_dirs.iter().map(|x| x.to_string().replace("./", "")).collect::<Vec<String>>().join(", ")
);
}
log!(verbose, "Paths with mlc.toml excluded: {:?}", dirs);
for dir in &dirs {
std::fs::remove_dir_all(dir).unwrap();

Loading…
Cancel
Save