2022-11-26 23:57:50 +00:00
|
|
|
// Copyright 2022 The Jujutsu Authors
|
2022-04-01 06:44:28 +00:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// https://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2022-04-03 05:22:33 +00:00
|
|
|
use std::collections::HashSet;
|
2022-05-03 04:34:26 +00:00
|
|
|
use std::path::{Path, PathBuf};
|
2022-04-01 06:44:28 +00:00
|
|
|
use std::process::exit;
|
|
|
|
|
|
|
|
use clap::Parser;
|
|
|
|
use itertools::Itertools;
|
|
|
|
|
|
|
|
/// A fake diff-editor, useful for testing
|
|
|
|
#[derive(Parser, Debug)]
|
|
|
|
#[clap()]
|
|
|
|
struct Args {
|
|
|
|
/// Path to the "before" directory
|
|
|
|
before: PathBuf,
|
|
|
|
|
|
|
|
/// Path to the "after" directory
|
|
|
|
after: PathBuf,
|
|
|
|
}
|
|
|
|
|
2022-05-03 04:34:26 +00:00
|
|
|
fn files_recursively(dir: &Path) -> HashSet<String> {
|
2022-04-03 05:22:33 +00:00
|
|
|
let mut files = HashSet::new();
|
|
|
|
for dir_entry in std::fs::read_dir(dir).unwrap() {
|
|
|
|
let dir_entry = dir_entry.unwrap();
|
|
|
|
let base_name = dir_entry.file_name().to_str().unwrap().to_string();
|
|
|
|
if dir_entry.path().is_dir() {
|
|
|
|
for sub_path in files_recursively(&dir_entry.path()) {
|
|
|
|
files.insert(format!("{base_name}/{sub_path}"));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
files.insert(base_name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
files
|
|
|
|
}
|
|
|
|
|
2022-04-01 06:44:28 +00:00
|
|
|
fn main() {
|
|
|
|
let args: Args = Args::parse();
|
2022-04-09 22:53:32 +00:00
|
|
|
let edit_script_path = PathBuf::from(std::env::var_os("DIFF_EDIT_SCRIPT").unwrap());
|
2022-11-04 04:14:41 +00:00
|
|
|
let edit_script = String::from_utf8(std::fs::read(edit_script_path).unwrap()).unwrap();
|
2022-04-01 06:44:28 +00:00
|
|
|
for instruction in edit_script.split('\0') {
|
|
|
|
let (command, payload) = instruction.split_once('\n').unwrap_or((instruction, ""));
|
|
|
|
let parts = command.split(' ').collect_vec();
|
|
|
|
match parts.as_slice() {
|
|
|
|
[""] => {}
|
|
|
|
["fail"] => exit(1),
|
2022-04-03 05:22:33 +00:00
|
|
|
["files-before", ..] => {
|
|
|
|
let expected = parts[1..].iter().copied().map(str::to_string).collect();
|
|
|
|
let actual = files_recursively(&args.before);
|
|
|
|
if actual != expected {
|
|
|
|
eprintln!(
|
2022-11-23 05:15:20 +00:00
|
|
|
"fake-diff-editor: unexpected files before. EXPECTED: {:?} ACTUAL: {:?}",
|
|
|
|
expected.iter().sorted().collect_vec(),
|
|
|
|
actual.iter().sorted().collect_vec(),
|
2022-04-03 05:22:33 +00:00
|
|
|
);
|
|
|
|
exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
["files-after", ..] => {
|
|
|
|
let expected = parts[1..].iter().copied().map(str::to_string).collect();
|
|
|
|
let actual = files_recursively(&args.after);
|
|
|
|
if actual != expected {
|
|
|
|
eprintln!(
|
2022-11-23 05:15:20 +00:00
|
|
|
"fake-diff-editor: unexpected files after. EXPECTED: {:?} ACTUAL: {:?}",
|
|
|
|
expected.iter().sorted().collect_vec(),
|
|
|
|
actual.iter().sorted().collect_vec(),
|
2022-04-03 05:22:33 +00:00
|
|
|
);
|
|
|
|
exit(1)
|
|
|
|
}
|
|
|
|
}
|
2022-04-01 06:44:28 +00:00
|
|
|
["rm", file] => {
|
|
|
|
std::fs::remove_file(args.after.join(file)).unwrap();
|
|
|
|
}
|
|
|
|
["reset", file] => {
|
|
|
|
if args.before.join(file).exists() {
|
2022-11-04 04:14:41 +00:00
|
|
|
std::fs::copy(args.before.join(file), args.after.join(file)).unwrap();
|
2022-04-01 06:44:28 +00:00
|
|
|
} else {
|
|
|
|
std::fs::remove_file(args.after.join(file)).unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
["write", file] => {
|
|
|
|
std::fs::write(args.after.join(file), payload).unwrap();
|
|
|
|
}
|
|
|
|
_ => {
|
2022-12-15 02:30:06 +00:00
|
|
|
eprintln!("fake-diff-editor: unexpected command: {command}");
|
2022-04-01 06:44:28 +00:00
|
|
|
exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|