nvim-telescope / telescope.nvim

Find, Filter, Preview, Pick. All lua, all the time.
MIT License
15.43k stars 824 forks source link

git_branch checkout a non-local remote branch #1932

Open mosheavni opened 2 years ago

mosheavni commented 2 years ago

Description

On :Telescope git_branch I see all branches including non-local origin/ branches (branches I still not checked out to since git clone), and when I try to checkout to this branch by pressing Enter, I get HEAD detached: 2022-05-10_19-21-26 (1)

Also, I want to sort based on the branches with latest push, I came up with this git command:

git for-each-ref --sort=-committerdate --format="%(refname:short)" | grep -n . | sed "s?origin/??g" | sort -t: -k2 -u | sort -n | cut -d: -f2

Which I wish I could use instead of the plugin's default git branch command, here: https://github.com/nvim-telescope/telescope.nvim/blob/d743d70292956f55f4a71f291281287d206f29f2/lua/telescope/builtin/git.lua#L199

Neovim version

NVIM v0.7.0
Build type: Release
LuaJIT 2.1.0-beta3
Compiled by brew@Monterey

Features: +acl +iconv +tui
See ":help feature-compile"

   system vimrc file: "$VIM/sysinit.vim"
  fall-back for $VIM: "/usr/local/Cellar/neovim/0.7.0/share/nvim"

Operating system and version

macOS 12.2.1

checkhealth telescope

telescope: require("telescope.health").check()
========================================================================
## Checking for required plugins
  - OK: plenary installed.
  - OK: nvim-treesitter installed.

## Checking external dependencies
  - OK: rg: found ripgrep 13.0.0
  - OK: fd: found fd 8.3.2

## ===== Installed extensions =====

## Telescope Extension: `fzf`
  - INFO: No healthcheck provided

Steps to reproduce

  1. Create a minimal-init.lua file:
mkdir /tmp/git-branch-ts-test/
cd /tmp/git-branch-ts-test/
curl -sLO https://gist.githubusercontent.com/mosheavni/2de985493b6746b49fdf1e9ef93902b9/raw/9f7c66de2c6ce5f30c50e42ab1f03029dcdcf8ae/minimal-init.lua
  1. Clone a repository, whatever one, and cd into it:
cd /tmp/git-branch-ts-test/
git clone https://github.com/junegunn/fzf.git
cd fzf
  1. Open git with minimal configuration
nvim -nu /tmp/git-branch-ts-test/minimal-init.lua
  1. Run :Telescope git_branches and choose any branch
  2. Run :!git status to see what happened at checkout
:!git status
HEAD detached at origin/dependabot/github_actions/github/codeql-action-2.1.10
nothing to commit, working tree clean

Press ENTER or type command to continue

Now you can see that the HEAD is detached which is a not wanted git behaviour. When I'm checking out to a branch I want it to be a local one tracked remotely, so basically I either wouldn't want to see branches with origin/, or having something that removes the remote prefix to check out to a local branch and not get HEAD detached at...

Expected behavior

If I select a branch with name `REMOTE_NAME/XXX', I want the checkout actions to be:

git branch XXX
git branch --set-upstream-to=REMOTE_NAME/XXX XXX
git checkout XXX

Actual behavior

branches with origin/ prefix are checked out to and HEAD detaches, behind the scenes it's probably:

git checkout ${GIVEN_BRANCH}

Minimal config

https://gist.githubusercontent.com/mosheavni/2de985493b6746b49fdf1e9ef93902b9/raw/9f7c66de2c6ce5f30c50e42ab1f03029dcdcf8ae/minimal-init.lua

Conni2461 commented 2 years ago

Honestly i dont get what you want. Stuff like this:

Expected behavior No response

Makes helping you impossible. Do you not want to see remote branches? Do you want to track remote branches because you can do that with <c-t>. Is this a bug report a feature request?

git for-each-ref --sort=-committerdate --format="%(refname:short)" | grep -n . | sed "s?origin/??g" | sort -t: -k2 -u | sort -n | cut -d: -f2

We currently dont support piping. And changing the command is currently pretty much impossible because that would also result in changing some of the lua code. So you basically can just write your own picker.

I am tempted to close this issue as invalid based the fact that you filled out the template that bad and i will do that in the following days unless you provide more information.

mosheavni commented 2 years ago

I'm sorry I missed that field, I had hoped that I described the issue in a clear way. Providing a clear and tested reproduction steps:

Reproduce

  1. Create a minimal-init.lua file:
mkdir /tmp/git-branch-ts-test/
cd /tmp/git-branch-ts-test/
curl -sLO https://gist.githubusercontent.com/mosheavni/2de985493b6746b49fdf1e9ef93902b9/raw/9f7c66de2c6ce5f30c50e42ab1f03029dcdcf8ae/minimal-init.lua
  1. Clone a repository, whatever one, and cd into it:
cd /tmp/git-branch-ts-test/
git clone https://github.com/junegunn/fzf.git
cd fzf
  1. Open git with minimal configuration
nvim -nu /tmp/git-branch-ts-test/minimal-init.lua
  1. Run :Telescope git_branches and choose any branch
  2. Run :!git status to see what happened at checkout
:!git status
HEAD detached at origin/dependabot/github_actions/github/codeql-action-2.1.10
nothing to commit, working tree clean

Press ENTER or type command to continue

Now you can see that the HEAD is detached which is a not wanted git behaviour. When I'm checking out to a branch I want it to be a local one tracked remotely, so basically I either wouldn't want to see branches with origin/, or having something that removes the remote prefix to check out to a local branch and not get HEAD detached at...

Conni2461 commented 2 years ago

<C-t> to track the branch. Does that work for you?

mosheavni commented 2 years ago

<C-t> to track the branch. Does that work for you?

it does. However, I think it should be the default behaviour, no point in detaching head.

Conni2461 commented 2 years ago

our default action does git checkout which also doesn't do that on default so i think its sensible to copy this behavior.

We also have git switch on <c-s>

mosheavni commented 2 years ago

our default action does git checkout which also doesn't do that on default so i think its sensible to copy this behavior.

We also have git switch on <c-s>

Ok, is there a way to override the mappings? https://github.com/nvim-telescope/telescope.nvim/blob/39b12d84e86f5054e2ed98829b367598ae53ab41/lua/telescope/builtin/git.lua#L287 not sure by this code I can.

Also regarding the second sort question, this flag added to git for-each-rev: --sort committerdate better sorts the branches, but I believe that overrides it: https://github.com/nvim-telescope/telescope.nvim/blob/39b12d84e86f5054e2ed98829b367598ae53ab41/lua/telescope/builtin/git.lua#L286

Is there a way to not sort the results? Thanks.

ckoehler commented 2 years ago

I'd also love to change default behavior. Rarely do I want to just checkout the commit, I most often want to switch/track.

mosheavni commented 2 years ago

I'd also love to change default behavior. Rarely do I want to just checkout the commit, I most often want to switch/track.

so strange that this is the default behavior they chose, so un-intuitive!

jcorum11 commented 2 years ago

our default action does git checkout which also doesn't do that on default so i think its sensible to copy this behavior.

We also have git switch on <c-s>

I know this is a rather trivial subject, the functionality is there and I'm happy with that, but since we're here and talking about which would be more sensible...

Since git switch is its own command in git which was made to separate out the functionality of switching branches from git checkout which was meant for overwriting files, wouldn't it be more sensible to follow the lead of git and separate out the behavior here as well?

Adding a Telescope switch would be a better experience for users and follow the paradigm of git by making switch its own function rather than making switch seem more like a subset of checkout (which is the opposite of what git switch was meant to be) by having its functionality executed by Telescope checkout then <c-s>

ckoehler commented 1 year ago

I was able to override the keybinding with telescope.setup() :

telescope.setup({
  pickers = {
    git_branches = {
      mappings = {
        i = { ["<cr>"] = actions.git_switch_branch },
      },
    },
  },
})
searleser97 commented 1 year ago

+1 to this issue

diegoulloao commented 11 months ago

+1 I agree with this guy. The default behavior should be checkout to the HEAD of the branch, not detached.

mrwolfdevel commented 6 months ago

+1 to this issue