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

git_branch checkout a non-local remote branch #1932

Open
mosheavni opened this issue May 10, 2022 · 13 comments
Open

git_branch checkout a non-local remote branch #1932

mosheavni opened this issue May 10, 2022 · 13 comments
Labels
bug Something isn't working

Comments

@mosheavni
Copy link

mosheavni commented May 10, 2022

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:

{ "git", "for-each-ref", "--perl", "--format", format, opts.pattern },

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

@mosheavni mosheavni added the bug Something isn't working label May 10, 2022
@Conni2461
Copy link
Member

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
Copy link
Author

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
Copy link
Member

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

@mosheavni
Copy link
Author

mosheavni commented May 22, 2022

<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
Copy link
Member

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
Copy link
Author

mosheavni commented May 22, 2022

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?

attach_mappings = function(_, map)

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:

sorter = conf.file_sorter(opts),

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

@ckoehler
Copy link

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
Copy link
Author

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
Copy link

jcorum11 commented Aug 25, 2022

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
Copy link

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

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

@searleser97
Copy link

+1 to this issue

@diegoulloao
Copy link

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

@mrwolfdevel
Copy link

+1 to this issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

7 participants