Skip to content

Commit

Permalink
cli: simplify formatting of sizes in "file too large" warning message
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
yuja committed Dec 11, 2024
1 parent 3de3e44 commit f80eb55
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 20 deletions.
24 changes: 7 additions & 17 deletions cli/src/cli_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}")?;
Expand Down
6 changes: 3 additions & 3 deletions cli/tests/test_working_copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand All @@ -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`
Expand Down Expand Up @@ -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`
Expand Down

0 comments on commit f80eb55

Please sign in to comment.