git-worktree.nvim icon indicating copy to clipboard operation
git-worktree.nvim copied to clipboard

fix: dont set origin prefix 2 times

Open zkygr opened this issue 2 years ago • 2 comments

Hello!

When creating a new worktree from a remote ref the origin/ prefix is added 2 times to the tracking branch. git worktree can handle the case that one tracking branch exists when a new worktree should be created. The PR fixes the Issue #77

If <commit-ish> is a branch name (call it <branch>) and is not found, and neither -b nor -B nor --detach are used, but there does exist a tracking branch in exactly one remote (call it <remote>) with a matching name, treat as equivalent to:

$ git worktree add --track -b <branch> <path> <remote>/<branch>

Links:

  • https://git-scm.com/docs/git-worktree

zkygr avatar Nov 03 '22 19:11 zkygr

Thank you @zkygr, I created a zsh function to do this for now. Hopefully these fix PRs get merged one day so I don't need the function anymore.

brandoncc avatar Mar 31 '23 00:03 brandoncc

@pjedynak you can string replace origin/ before setting upstream

https://github.com/ThePrimeagen/git-worktree.nvim/blob/c8ff32c63674f7f7c9af6264c5043338c8430210/lua/git-worktree/init.lua#L319-L328

add the code right before Line 319

    branch = branch:gsub("origin/", "")

it'd look like this

    branch = branch:gsub("origin/", "")
    local set_branch_cmd = 'git'
    local set_branch_args= {'branch', string.format('--set-upstream-to=%s/%s', upstream, branch)}
    local set_branch = Job:new({
        command = set_branch_cmd,
        args = set_branch_args,
        cwd = worktree_path,
        on_start = function()
            status:next_status(set_branch_cmd .. " " .. table.concat(set_branch_args, " "))
        end
    })

PS: I don't know how to suggest a change outside of file changed area

Clumsy-Coder avatar Nov 07 '23 15:11 Clumsy-Coder