Skip to content

fix: prefer exact match over suffix match when auto-selecting tracking branch for worktree#2569

Merged
love-linger merged 3 commits into
sourcegit-scm:developfrom
weiningwei:fix/worktree-upstream-match
Jul 26, 2026
Merged

fix: prefer exact match over suffix match when auto-selecting tracking branch for worktree#2569
love-linger merged 3 commits into
sourcegit-scm:developfrom
weiningwei:fix/worktree-upstream-match

Conversation

@weiningwei

Copy link
Copy Markdown
Contributor

When adding a worktree with an existing local branch and enabling "Set Upstream Tracking Branch", the AutoSelectTrackingBranch() method used EndsWith to match remote branches against the local branch name. This caused unintended matches — for example, local branch smart_build would match fix_smart_build instead of the exact match smart_build.

This commit fixes the logic by:

  1. First attempting an exact match (Equals) against remote branch names
  2. Falling back to the original suffix match (EndsWith) only if no exact match is found
  3. Keeping RemoteBranches[0] as the final fallback

@weiningwei weiningwei changed the title fix(AddWorktree): prioritize exact remote branch match when selecting… fix: prefer exact match over suffix match when auto-selecting tracking branch for worktree Jul 26, 2026
@love-linger love-linger self-assigned this Jul 26, 2026
@love-linger love-linger added the enhancement New feature or request label Jul 26, 2026
@love-linger

love-linger commented Jul 26, 2026

Copy link
Copy Markdown
Collaborator

EndsWith is used instead of Equals for the following reason: branch names may contain subpaths marked with /, such as feature/xxxx. When creating a worktree, users do not necessarily need to create the directory named feature and may directly use paths like ../xxxx.

I suggest refactor it to:

b.Name equals name or b.Name endWith `/{name}`

… tracking branch

Signed-off-by: weiningwei <weiningwei09@gmail.com>
Signed-off-by: weiningwei <weiningwei09@gmail.com>
@weiningwei
weiningwei force-pushed the fix/worktree-upstream-match branch from 0dc0de5 to 9de1c1b Compare July 26, 2026 13:38
@weiningwei

Copy link
Copy Markdown
Contributor Author

Thank you for the explanation. That makes sense — EndsWith("/" + name) prevents the fix ↔ hotfix issue while still handling path-component names like feature/fix. I've updated the PR to use your suggested condition:
b.Name.Equals(name, StringComparison.Ordinal) || b.Name.EndsWith("/" + name, StringComparison.Ordinal).
This replaces the old EndsWith(name) call in AutoSelectTrackingBranch().

Also, would you prefer the two conditions to be split into separate Find calls so that an exact match always takes priority over a suffix match regardless of list order? Currently as a single Find the first match in the list wins.

@love-linger

love-linger commented Jul 26, 2026 via email

Copy link
Copy Markdown
Collaborator

Signed-off-by: weiningwei <weiningwei09@gmail.com>
@weiningwei

Copy link
Copy Markdown
Contributor Author

Updated as suggested — split into two Finds so exact match takes priority:

@love-linger
love-linger merged commit 877c180 into sourcegit-scm:develop Jul 26, 2026
@weiningwei
weiningwei deleted the fix/worktree-upstream-match branch July 26, 2026 14:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants