wfxr / forgit

:zzz: A utility tool powered by fzf for using git interactively.
MIT License
4.32k stars 136 forks source link

Feature Request: checkout_branch: Sort by committerdate #334

Closed guettli closed 5 months ago

guettli commented 5 months ago

Check list

Environment info

Problem / Steps to reproduce

I found forgit because I was looking for a convenient way to switch between branches.

Up to now I used:

alias gbs="git branch --sort=-committerdate | fzf --header Checkout | tr '*' ' ' | xargs git checkout"

But this does not work for remote branches.

I tried git forgit checkout_branch but the list of branches seem to be sorted by alphabet, not by committerdate.

It would be great if you could sort that by committerdate.

Related: https://stackoverflow.com/questions/5188320/how-can-i-get-a-list-of-git-branches-ordered-by-most-recent-commit

cjappl commented 5 months ago

Have you tried adding this to the environment vars:

$FORGIT_CHECKOUT_BRANCH_GIT_OPTS

$FORGIT_CHECKOUT_BRANCH_BRANCH_GIT_OPTS

?

I think one of them may do the trick! Just put it in your .rc file for your shell.

Looking at the code, I think it's the BRANCH_BRANCH one, as that is the one fed into fzf

    local git_checkout cmd preview opts branch
    cmd="git branch --color=always ${FORGIT_CHECKOUT_BRANCH_BRANCH_GIT_OPTS:---all} | LC_ALL=C sort -k1.1,1.1 -rs"
    preview="git log {1} $_forgit_log_preview_options"
    opts="
        $FORGIT_FZF_DEFAULT_OPTS
        +s +m --tiebreak=index --header-lines=1
        --preview=\"$preview\"
        $FORGIT_CHECKOUT_BRANCH_FZF_OPTS
        "
    branch="$(eval "$cmd" | FZF_DEFAULT_OPTS="$opts" fzf | awk '{print $1}')"
    [[ -z "$branch" ]] && return 1
guettli commented 5 months ago

Of course, I could configure it somehow, so that it works for me.

But why not provide it as default?

cjappl commented 5 months ago

I think we would have to have a very good reason to break our existing default, just in case others rely on it. I think allowing people to customize what they want it to be allows this functionality.

Anyone else have any thoughts? @carlfriedrich @sandr01d . I'm not totally opposed, my only opposition is breaking the existing default for another valid default without a really good reason to do so.

guettli commented 5 months ago

BTW, I use that now:

#!/bin/bash
# git switch sorted
# Switch to a branch, show branches sorted by last change.
# Related: https://stackoverflow.com/questions/5188320/

branch="$(git for-each-ref --sort=-committerdate refs \
--format='%(HEAD) %(color:yellow)%(refname:short)%(color:reset) - %(color:red)%(objectname:short)%(color:reset) - %(contents:subject) - %(authorname) (%(color:green)%(committerdate:relative)%(color:reset))' \
 | fzf | sed  's/^[ *]*//' | sed 's#origin/##'| cut -d' ' -f1)"
if [ -z "$branch" ]; then
    exit 1
fi
git switch "$branch"

Feel free to do what ever you like with my feedback. Feel free to close it.

sandr01d commented 5 months ago

I think we would have to have a very good reason to break our existing default, just in case others rely on it. I think allowing people to customize what they want it to be allows this functionality.

Anyone else have any thoughts? @carlfriedrich @sandr01d . I'm not totally opposed, my only opposition is breaking the existing default for another valid default without a really good reason to do so.

I think we should follow gits default behavior when we can. This way forgits behavior is predictable for users who are familiar with using git from the command line (probably most of our users). Also, if we do not provide any options to the git command, git follows what is defined in .gitconfig where you can set branch.sort. I think being able to control this behavior for both git and forgit at the same place is great.

guettli commented 5 months ago

@sandr01d thank you for this hint. I was not aware of that. Now I change my default setting like this:

git config --global branch.sort -committerdate

This is great, and the overall goal of what I was searching for. Thank you!

carlfriedrich commented 5 months ago

I also don't see any reason to change the default behavior. Requests like these are the reason why we have the config env variables in forgit, while @sandr01d 's proposal with the git config is even better in this specific case.

Maybe this is something we could add to the documentation? Otherwise I would say that this issue can be closed.

cjappl commented 5 months ago

I think I'll vote for closing for now! If we get a new issue in the future we can consider adding it to the documentation.