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

fix: dont set origin prefix 2 times #104

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

zkygr
Copy link

@zkygr zkygr commented Nov 3, 2022

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:

@brandoncc
Copy link
Contributor

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.

Copy link

@pjedynak pjedynak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see it only fixes git worktree add command, but still problem is with setting corectly upstream. I've got following command from step 5 in creation of worktree:
5/8: git branch --set-upstream-to=origin/origin/XXXX

@Clumsy-Coder
Copy link

@pjedynak you can string replace origin/ before setting upstream

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
})

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 added a commit to Clumsy-Coder/git-worktree.nvim that referenced this pull request Nov 7, 2023
  ## what
  - apply PR ThePrimeagen#104

  ## how

 ## why
 - main repo owner hasn't applied PR
 - fixes issue when creating worktree from a origin branch

  ## where
  - ./lua/git-worktree/init.lua

  ## usage
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants