From a857075ad001dcd6710cb4644723f6b9e432b4c6 Mon Sep 17 00:00:00 2001 From: Ilya Grigoriev Date: Tue, 19 Nov 2024 21:19:43 -0800 Subject: [PATCH 1/2] docs: add example glob to `jj help git fetch` I tried to clarify the text a bit as well. --- cli/src/commands/git/fetch.rs | 3 ++- cli/tests/cli-reference@.md.snap | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/cli/src/commands/git/fetch.rs b/cli/src/commands/git/fetch.rs index 183d3823bc..98ecf2f6a1 100644 --- a/cli/src/commands/git/fetch.rs +++ b/cli/src/commands/git/fetch.rs @@ -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", diff --git a/cli/tests/cli-reference@.md.snap b/cli/tests/cli-reference@.md.snap index e3da2f4863..0515bae980 100644 --- a/cli/tests/cli-reference@.md.snap +++ b/cli/tests/cli-reference@.md.snap @@ -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 --- @@ -1095,7 +1096,7 @@ If a working-copy commit gets abandoned, it will be given a new, empty commit. T * `-b`, `--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 ` — The remote to fetch from (only named remotes are supported, can be repeated) From 893a1624f140f0a3e137221f73ea3561f4f967c2 Mon Sep 17 00:00:00 2001 From: Ilya Grigoriev Date: Tue, 19 Nov 2024 21:19:43 -0800 Subject: [PATCH 2/2] cli git fetch: clarify the error for invalid branch names/globs Create a clearer error when a branch name contains `*`. Slightly clarify the error message for `?` in branch name/glob. --- cli/src/git_util.rs | 2 +- cli/tests/test_git_fetch.rs | 10 ++++------ lib/src/git.rs | 8 ++++++-- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/cli/src/git_util.rs b/cli/src/git_util.rs index 74ebe2d2fa..1edb7a6216 100644 --- a/cli/src/git_util.rs +++ b/cli/src/git_util.rs @@ -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 { diff --git a/cli/tests/test_git_fetch.rs b/cli/tests/test_git_fetch.rs index e6c2f96acc..232ddae660 100644 --- a/cli/tests/test_git_fetch.rs +++ b/cli/tests/test_git_fetch.rs @@ -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###" diff --git a/lib/src/git.rs b/lib/src/git.rs index 3bc7ef2970..6bff6faac8 100644 --- a/lib/src/git.rs +++ b/lib/src/git.rs @@ -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, @@ -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::>()