Skip to content

Commit

Permalink
cli: make large file snapshot error more readable
Browse files Browse the repository at this point in the history
When the difference is within epsilon, output the raw bytes so that the user
can easily see it; otherwise use human readable bytes, as the exact size isn't
too important.

Signed-off-by: Austin Seipp <[email protected]>
Change-Id: Id67394bf9eacfd05f934a2bd420f2265ad77a298
  • Loading branch information
thoughtpolice committed Apr 18, 2024
1 parent 7bcfab6 commit db42b54
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 19 deletions.
45 changes: 33 additions & 12 deletions cli/src/command_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,23 +300,44 @@ impl From<SnapshotError> for CommandError {
path,
size,
max_size,
} => user_error(format!(
"Failed to snapshot the working copy\nThe file '{}' is too large to be \
snapshotted ({} bytes).\nThe maximum size allowed is {} bytes.",
path.display(),
size.0,
max_size.0
))
.hinted(format!(
"This is to prevent large files from being added on accident. You can fix this \
error by:
} => {
// 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
// like '1.0MiB, maximum size allowed is ~1.0MiB'
let size_diff = size.0 - max_size.0;
let err_str = if size_diff <= 1024 {
format!(
"it is {} bytes too large; the maximum size allowed is {} bytes ({}).",
size_diff,
max_size.0,
max_size.to_string()
)
} else {
format!(
"it is {}; the maximum size allowed is ~{}.",
size.to_string(),
max_size.to_string()
)
};

user_error(format!(
"Failed to snapshot the working copy\nThe file '{}' is too large to be \
snapshotted: {}",
path.display(),
err_str,
))
.hinted(format!(
"This is to prevent large files from being added on accident. You can fix \
this error by:
- Adding the file to `.gitignore`
- Run `jj config set --repo snapshot.max-new-file-size \"{}B\"`
This will increase the maximum file size allowed for new files, in this repository only.
- Run `jj --config-toml 'snapshot.max-new-file-size=\"{}B\"' st`
This will increase the maximum file size allowed for new files, for this command only.",
size.0, size.0
)),
size.0, size.0
))
}
err => internal_error_with_message("Failed to snapshot the working copy", err),
}
}
Expand Down
12 changes: 5 additions & 7 deletions cli/tests/test_working_copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ fn test_snapshot_large_file() {
let stderr = test_env.jj_cmd_failure(&repo_path, &["files"]);
insta::assert_snapshot!(stderr, @r###"
Error: Failed to snapshot the working copy
The file '$TEST_ENV/repo/large' is too large to be snapshotted (13 bytes).
The maximum size allowed is 10 bytes.
The file '$TEST_ENV/repo/large' is too large to be snapshotted: it is 3 bytes too large; the maximum size allowed is 10 bytes (10.0B).
Hint: This is to prevent large files from being added on accident. You can fix this error by:
- Adding the file to `.gitignore`
- Run `jj config set --repo snapshot.max-new-file-size "13B"`
Expand All @@ -39,18 +38,17 @@ fn test_snapshot_large_file() {

// test with a larger file using 'KB' human-readable syntax
test_env.add_config(r#"snapshot.max-new-file-size = "10KB""#);
let big_string = vec![0; 1024 * 11];
let big_string = vec![0; 1024 * 12];
std::fs::write(repo_path.join("large"), big_string).unwrap();
let stderr = test_env.jj_cmd_failure(&repo_path, &["files"]);
insta::assert_snapshot!(stderr, @r###"
Error: Failed to snapshot the working copy
The file '$TEST_ENV/repo/large' is too large to be snapshotted (11264 bytes).
The maximum size allowed is 10240 bytes.
The file '$TEST_ENV/repo/large' is too large to be snapshotted: it is 12.0KiB; the maximum size allowed is ~10.0KiB.
Hint: This is to prevent large files from being added on accident. You can fix this error by:
- Adding the file to `.gitignore`
- Run `jj config set --repo snapshot.max-new-file-size "11264B"`
- Run `jj config set --repo snapshot.max-new-file-size "12288B"`
This will increase the maximum file size allowed for new files, in this repository only.
- Run `jj --config-toml 'snapshot.max-new-file-size="11264B"' st`
- Run `jj --config-toml 'snapshot.max-new-file-size="12288B"' st`
This will increase the maximum file size allowed for new files, for this command only.
"###);
}

0 comments on commit db42b54

Please sign in to comment.