mirror of
https://github.com/martinvonz/jj.git
synced 2025-01-19 19:08:08 +00:00
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:
parent
168c7979fe
commit
71440fce11
2 changed files with 10 additions and 20 deletions
|
@ -2575,24 +2575,14 @@ pub fn print_snapshot_stats(
|
||||||
let ui_path = path_converter.format_file_path(path);
|
let ui_path = path_converter.format_file_path(path);
|
||||||
let message = match reason {
|
let message = match reason {
|
||||||
UntrackedReason::FileTooLarge { size, max_size } => {
|
UntrackedReason::FileTooLarge { size, max_size } => {
|
||||||
// if the size difference is < 1KiB, then show exact bytes.
|
// Show both exact and human bytes sizes to avoid something
|
||||||
// otherwise, show in human-readable form; this avoids weird cases
|
|
||||||
// where a file is 400 bytes too large but the error says something
|
|
||||||
// like '1.0MiB, maximum size allowed is ~1.0MiB'
|
// like '1.0MiB, maximum size allowed is ~1.0MiB'
|
||||||
let size_diff = size - max_size;
|
let size_approx = HumanByteSize(*size);
|
||||||
if size_diff <= 1024 {
|
let max_size_approx = HumanByteSize(*max_size);
|
||||||
format!(
|
format!(
|
||||||
"{size_diff} bytes too large; the maximum size allowed is {max_size} \
|
"{size_approx} ({size} bytes); the maximum size allowed is \
|
||||||
bytes ({max_size_approx})",
|
{max_size_approx} ({max_size} bytes)",
|
||||||
max_size_approx = HumanByteSize(*max_size)
|
)
|
||||||
)
|
|
||||||
} else {
|
|
||||||
format!(
|
|
||||||
"{size}; the maximum size allowed is ~{max_size}",
|
|
||||||
size = HumanByteSize(*size),
|
|
||||||
max_size = HumanByteSize(*max_size)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
writeln!(formatter, " {ui_path}: {message}")?;
|
writeln!(formatter, " {ui_path}: {message}")?;
|
||||||
|
|
|
@ -31,7 +31,7 @@ fn test_snapshot_large_file() {
|
||||||
insta::assert_snapshot!(stdout, @"empty");
|
insta::assert_snapshot!(stdout, @"empty");
|
||||||
insta::assert_snapshot!(stderr, @r"
|
insta::assert_snapshot!(stderr, @r"
|
||||||
Warning: Refused to snapshot some files:
|
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:
|
Hint: This is to prevent large files from being added by accident. You can fix this by:
|
||||||
- Adding the file to `.gitignore`
|
- Adding the file to `.gitignore`
|
||||||
- Run `jj config set --repo snapshot.max-new-file-size 13`
|
- 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!(stdout, @"empty");
|
||||||
insta::assert_snapshot!(stderr, @r"
|
insta::assert_snapshot!(stderr, @r"
|
||||||
Warning: Refused to snapshot some files:
|
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:
|
Hint: This is to prevent large files from being added by accident. You can fix this by:
|
||||||
- Adding the file to `.gitignore`
|
- Adding the file to `.gitignore`
|
||||||
- Run `jj config set --repo snapshot.max-new-file-size 11264`
|
- 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)"]);
|
test_env.jj_cmd_ok(&repo_path, &["restore", "--from=description(committed)"]);
|
||||||
insta::assert_snapshot!(stderr, @r"
|
insta::assert_snapshot!(stderr, @r"
|
||||||
Warning: Refused to snapshot some files:
|
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:
|
Hint: This is to prevent large files from being added by accident. You can fix this by:
|
||||||
- Adding the file to `.gitignore`
|
- Adding the file to `.gitignore`
|
||||||
- Run `jj config set --repo snapshot.max-new-file-size 13`
|
- Run `jj config set --repo snapshot.max-new-file-size 13`
|
||||||
|
|
Loading…
Reference in a new issue