forgit icon indicating copy to clipboard operation
forgit copied to clipboard

Feat: add interactive git worktree operations

Open suft opened this issue 1 year ago • 10 comments

Check list

  • [x] I have performed a self-review of my code
  • [x] I have commented my code in hard-to-understand areas
  • [x] I have made corresponding changes to the documentation

Description

I recently adopted using multiple fixed worktrees as part of my workflow to help with productivity. Each worktree is used for a different type of concurrent activity:

  • main for looking at the pristine code
  • work for looking at my code
  • review for looking at someone else’s code
  • background for my computer to look at my code
    • operates in the detached head state
  • scratch for everything else
. (bare repo)
├── background/ (* worktree)
├── config
├── description
├── HEAD
├── hooks/
├── info/
├── logs/
├── main/ (* worktree)
├── objects/
├── packed-refs
├── refs/
├── review/ (* worktree)
├── rr-cache/
├── scratch/ (*worktree)
├── work/(* worktree)
└── worktrees/

Example of worktrees in a bare clone Screenshot 2024-11-13 at 7 29 51 PM

Another approach is to use worktrees as a replacement of, or a supplement to git branches. Instead of switching branches, you just change directories. So that would involve creating a new worktree and branch, then delete the worktree upon merging.

Worktree Operations (should we stick with these abbreviations?)

  • locking a worktree - gwl
  • unlocking a worktree - gwu
  • removing a worktree - gwr
  • jumping to a worktree - gwj
    • similar to switching branches
    • involves a combination of selecting a result of git worktree list and cd into that result (not a native git operation)

Screenshots

bash (repo with short history) - gwj FORGIT_WORKTREE_PREVIEW_GIT_OPTS='--oneline --graph --decorate --color' Screenshot 2024-11-04 at 8 24 20 PM

fish (repo with mid-length history) - gwj FORGIT_WORKTREE_PREVIEW_GIT_OPTS='--oneline --graph --decorate --color' Screenshot 2024-11-10 at 11 41 59 AM

bash (repo with mid-length history) - gwj FORGIT_WORKTREE_PREVIEW_GIT_OPTS='--oneline --graph --decorate --color --max-count=100' Screenshot 2024-11-10 at 12 46 05 PM

Closes #399.

Type of change

  • [ ] Bug fix
  • [X] New feature
  • [ ] Refactor
  • [ ] Breaking change
  • [X] Documentation change

Test environment

  • Shell
    • [X] bash
    • [ ] zsh
    • [X] fish
  • OS
    • [ ] Linux
    • [X] Mac OS X
    • [ ] Windows
    • [ ] Others:

suft avatar Nov 03 '24 21:11 suft

@suft Thanks for your contribution! Is this ready for review or did you make it a draft on purpose?

carlfriedrich avatar Nov 04 '24 09:11 carlfriedrich

@carlfriedrich I made it draft on purpose. Still have a few things to adjust.

suft avatar Nov 04 '24 11:11 suft

@suft Great implementation so far, @sandr01d great review. This will be a good addition to forgit.

carlfriedrich avatar Nov 15 '24 12:11 carlfriedrich

(it looks like there is very thorough reviewing going on here, please ping me if you need another pair of eyes, otherwise I completely defer to @sandr01d and @carlfriedrich )

cjappl avatar Nov 15 '24 19:11 cjappl

Thanks for the changes @suft, looks good to me so far. Let me summarize the things that still need to be addressed:

The root worktree doesn't show up as the first selection, so --header-lines=1 will affect a different worktree (which I'm guessing is not the behaviour we want), unless we reverse the fzf item list.

Screenshot 2024-12-18 at 3 02 14 PM

suft avatar Dec 18 '24 20:12 suft

The root worktree doesn't show up as the first selection, so --header-lines=1 will affect a different worktree (which I'm guessing is not the behaviour we want), unless we reverse the fzf item list. Screenshot 2024-12-18 at 3 02 14 PM

I'm not exactly sure what you mean. git worktree list always seems to give me the root worktree as the first line and git worktree list | fzf --header-lines 1 does not allow me to select the root worktree as I'd expect. Could you please provide an example on how to reproduce this?

sandr01d avatar Dec 21 '24 17:12 sandr01d

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Jun 26 '25 23:06 stale[bot]

@suft @sandr01d Are you planning to finish this PR to get it merged anytime?

carlfriedrich avatar Jun 29 '25 18:06 carlfriedrich

@suft @sandr01d Are you planning to finish this PR to get it merged anytime?

Sorry for the delay — things have been a bit hectic on my end. I’m hoping to take another look at it sometime this week, but totally understand if it makes sense to close the PR in the meantime.

suft avatar Jun 29 '25 23:06 suft

Sorry for the late reply @suft. Just wanted to let you know that I saw your changes and will give you a review. Unfortunately I don't have much free time right now, so it might take a while until I get to it.

sandr01d avatar Jul 13 '25 13:07 sandr01d