Skip to content

gtr cd fails with locale error for branch names containing hyphen ranges (e.g. fix-cron) #134

@karan925

Description

@karan925

Bug Description

gtr cd fails with a zsh locale/collation error when the worktree branch name contains a substring that can be interpreted as a character range in reverse order (e.g., x-c in fix-cron).

$ gtr cd fix-cron
gtr: range-endpoints of 'x-c' are in reverse collating sequence order

Root Cause

Zsh interprets character sequences like x-c as glob bracket expression ranges (similar to [x-c]). Under en_US.UTF-8 locale, x has a higher collation value than c, making the range x-c invalid (reverse order). This happens somewhere during the processing of the branch name in the shell function generated by git gtr init zsh.

The error is locale-dependent — under the C locale, character ranges follow strict ASCII byte order and the error does not occur.

Reproduction

Environment:

  • macOS (Darwin), zsh with oh-my-zsh + powerlevel10k
  • Locale: LANG=en_US.UTF-8
  • gtr version: 2.3.1
  • Shell integration: eval "$(git gtr init zsh)" in .zshrc

Steps:

  1. Create a worktree with a hyphen in the name where the characters around the hyphen form a reverse range:
    git gtr new fix-cron
  2. Try to cd into it:
    gtr cd fix-cron
  3. Observe the error: gtr: range-endpoints of 'x-c' are in reverse collating sequence order

Branch names that would trigger this: any name where a hyphen creates a reverse character range, e.g.:

  • fix-cron (x-c → x > c)
  • fix-auth (x-a → x > a)
  • test-app (t-a → t > a)
  • update-branch (e-b → e > b)

Workaround: Setting LC_ALL=C before running the command:

LC_ALL=C gtr cd fix-cron  # works

Or permanently in ~/.zshrc:

export LC_COLLATE=C

Suggested Fix

The shell function generated by git gtr init zsh should be resilient to locale settings. A few options:

Option 1: Use emulate -L zsh at the top of the function (Recommended)

This resets all zsh options to defaults within the function scope, preventing locale-dependent glob interpretation:

gtr() {
  emulate -L zsh
  # ... rest of function
}

emulate -L zsh is the standard idiom for zsh functions that need predictable behavior regardless of user configuration. The -L flag makes it local to the function scope.

Option 2: Set LC_COLLATE=C locally within the function

gtr() {
  local LC_COLLATE=C
  # ... rest of function
}

Option 3: Disable globbing for the function

gtr() {
  setopt local_options no_glob_subst no_bad_pattern
  # ... rest of function
}

Option 1 (emulate -L zsh) is the most robust as it protects against all zsh option-related edge cases, not just this one.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions