cli: simplify formatting of sizes in "file too large" warning message

As Martin spotted, the original code can't prevent "1.0GiB, maximum size allowed
is ~1.0GiB." I personally don't mind if the error message contained the exact
size, so I simply let it print both exact and human byte sizes unconditionally.
This commit is contained in:
Yuya Nishihara 2024-12-11 16:09:55 +09:00
parent 168c7979fe
commit 71440fce11
2 changed files with 10 additions and 20 deletions

View file

@ -2575,24 +2575,14 @@ pub fn print_snapshot_stats(
let ui_path = path_converter.format_file_path(path);
let message = match reason {
UntrackedReason::FileTooLarge { size, max_size } => {
// if the size difference is < 1KiB, then show exact bytes.
// otherwise, show in human-readable form; this avoids weird cases
// where a file is 400 bytes too large but the error says something
// Show both exact and human bytes sizes to avoid something
// like '1.0MiB, maximum size allowed is ~1.0MiB'
let size_diff = size - max_size;
if size_diff <= 1024 {
format!(
"{size_diff} bytes too large; the maximum size allowed is {max_size} \
bytes ({max_size_approx})",
max_size_approx = HumanByteSize(*max_size)
)
} else {
format!(
"{size}; the maximum size allowed is ~{max_size}",
size = HumanByteSize(*size),
max_size = HumanByteSize(*max_size)
)
}
let size_approx = HumanByteSize(*size);
let max_size_approx = HumanByteSize(*max_size);
format!(
"{size_approx} ({size} bytes); the maximum size allowed is \
{max_size_approx} ({max_size} bytes)",
)
}
};
writeln!(formatter, " {ui_path}: {message}")?;

View file

@ -31,7 +31,7 @@ fn test_snapshot_large_file() {
insta::assert_snapshot!(stdout, @"empty");
insta::assert_snapshot!(stderr, @r"
Warning: Refused to snapshot some files:
large: 3 bytes too large; the maximum size allowed is 10 bytes (10.0B)
large: 13.0B (13 bytes); the maximum size allowed is 10.0B (10 bytes)
Hint: This is to prevent large files from being added by accident. You can fix this by:
- Adding the file to `.gitignore`
- Run `jj config set --repo snapshot.max-new-file-size 13`
@ -48,7 +48,7 @@ fn test_snapshot_large_file() {
insta::assert_snapshot!(stdout, @"empty");
insta::assert_snapshot!(stderr, @r"
Warning: Refused to snapshot some files:
large: 1024 bytes too large; the maximum size allowed is 10240 bytes (10.0KiB)
large: 11.0KiB (11264 bytes); the maximum size allowed is 10.0KiB (10240 bytes)
Hint: This is to prevent large files from being added by accident. You can fix this by:
- Adding the file to `.gitignore`
- Run `jj config set --repo snapshot.max-new-file-size 11264`
@ -97,7 +97,7 @@ fn test_snapshot_large_file_restore() {
test_env.jj_cmd_ok(&repo_path, &["restore", "--from=description(committed)"]);
insta::assert_snapshot!(stderr, @r"
Warning: Refused to snapshot some files:
file: 3 bytes too large; the maximum size allowed is 10 bytes (10.0B)
file: 13.0B (13 bytes); the maximum size allowed is 10.0B (10 bytes)
Hint: This is to prevent large files from being added by accident. You can fix this by:
- Adding the file to `.gitignore`
- Run `jj config set --repo snapshot.max-new-file-size 13`