Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

git fetch: clarify errors with bad characters in globs and branch names #4925

Merged
merged 2 commits into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion cli/src/commands/git/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ pub struct GitFetchArgs {
/// Fetch only some of the branches
///
/// By default, the specified name matches exactly. Use `glob:` prefix to
/// expand `*` as a glob. The other wildcard characters aren't supported.
/// expand `*` as a glob, e.g. `--branch 'glob:push-*'`. Other wildcard
/// characters such as `?` are *not* supported.
#[arg(
long, short,
alias = "bookmark",
Expand Down
2 changes: 1 addition & 1 deletion cli/src/git_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ pub fn git_fetch(
.any(|pattern| pattern.as_exact().is_some_and(|s| s.contains('*')))
{
user_error_with_hint(
err,
"Branch names may not include `*`.",
"Prefix the pattern with `glob:` to expand `*` as a glob",
)
} else {
Expand Down
3 changes: 2 additions & 1 deletion cli/tests/[email protected]
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
source: cli/tests/test_generate_md_cli_help.rs
description: "AUTO-GENERATED FILE, DO NOT EDIT. This cli reference is generated by a test as an `insta` snapshot. MkDocs includes this snapshot from docs/cli-reference.md."
snapshot_kind: text
---
<!-- BEGIN MARKDOWN-->

Expand Down Expand Up @@ -1095,7 +1096,7 @@ If a working-copy commit gets abandoned, it will be given a new, empty commit. T

* `-b`, `--branch <BRANCH>` — Fetch only some of the branches

By default, the specified name matches exactly. Use `glob:` prefix to expand `*` as a glob. The other wildcard characters aren't supported.
By default, the specified name matches exactly. Use `glob:` prefix to expand `*` as a glob, e.g. `--branch 'glob:push-*'`. Other wildcard characters such as `?` are *not* supported.

Default value: `glob:*`
* `--remote <REMOTE>` — The remote to fetch from (only named remotes are supported, can be repeated)
Expand Down
10 changes: 4 additions & 6 deletions cli/tests/test_git_fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -612,14 +612,12 @@ fn test_git_fetch_some_of_many_bookmarks() {
&target_jj_repo_path,
&["git", "fetch", "--branch", "glob:^:a*"],
);
insta::assert_snapshot!(stderr, @r###"
Error: Invalid branch pattern provided. Patterns may not contain the characters `:`, `^`, `?`, `[`, `]`
"###);
insta::assert_snapshot!(stderr, @"Error: Invalid branch pattern provided. When fetching, branch names and globs may not contain the characters `:`, `^`, `?`, `[`, `]`");
let stderr = test_env.jj_cmd_failure(&target_jj_repo_path, &["git", "fetch", "--branch", "a*"]);
insta::assert_snapshot!(stderr, @r###"
Error: Invalid branch pattern provided. Patterns may not contain the characters `:`, `^`, `?`, `[`, `]`
insta::assert_snapshot!(stderr, @r"
Error: Branch names may not include `*`.
Hint: Prefix the pattern with `glob:` to expand `*` as a glob
"###);
");

// Nothing in our repo before the fetch
insta::assert_snapshot!(get_log_output(&test_env, &target_jj_repo_path), @r###"
Expand Down
8 changes: 6 additions & 2 deletions lib/src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1218,7 +1218,7 @@ pub enum GitFetchError {
#[error("No git remote named '{0}'")]
NoSuchRemote(String),
#[error(
"Invalid branch pattern provided. Patterns may not contain the characters `{chars}`",
"Invalid branch pattern provided. When fetching, branch names and globs may not contain the characters `{chars}`",
chars = INVALID_REFSPEC_CHARS.iter().join("`, `")
)]
InvalidBranchPattern,
Expand Down Expand Up @@ -1273,7 +1273,11 @@ pub fn fetch(
.map(|pattern| {
pattern
.to_glob()
.filter(|glob| !glob.contains(INVALID_REFSPEC_CHARS))
.filter(
/* This triggered by non-glob `*`s in addition to INVALID_REFSPEC_CHARS
* because `to_glob()` escapes such `*`s as `[*]`. */
|glob| !glob.contains(INVALID_REFSPEC_CHARS),
)
.map(|glob| format!("+refs/heads/{glob}:refs/remotes/{remote_name}/{glob}"))
})
.collect::<Option<_>>()
Expand Down