alexiszamanidis / vscode-git-worktrees

A VS Code extension that wraps git worktree operations for simplicity and productivity

Home Page:https://marketplace.visualstudio.com/items?itemName=GitWorktrees.git-worktrees

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

FR: Open same folder/workspace when switching worktree

yrtimiD opened this issue · comments

Monorepo with many projects usually have many workspaces which isolate only relevant folders for specific project.
(can create a demo repo to visualize this)

Imaging next situation:
User is on master with workspace called "ProjectA".
User decides to start working on a new FeatureA for ProjectA, so he'll want to create a new worktree for it, open new vscode in this new worktree folder and open a "ProjectA" workspace in this new worktree.

This is something I'd love to see integrated into this extension.

moveIntoWorktree should check if current vscode instance has open folder or workspace, and reopen same folder/workspace in the new worktree (same or new vscode instance). (will definitely need some absolute-relative-newAbsolute path calculations). I don't know vscode api, so can't point you to specific function.

Can you please create the demo you mentioned?
I would also like some base cases to start thinking about what should happen in each scenario.
Would it be easy for you to create some?

Sure, here you go: https://github.com/yrtimiD/stunning-fiesta
If you'll just open a root folder you'll see all the folders in the repo, and in the big monorepo it might be messy:
image
So we create a separate workspaces which narrow explorer to only relevant items. For example, the workspace for "project-a" is located under project-a/.vscode/project-a.code-workspace and will look next:
image

Also I created few branches for example:
image

So, if I originally checkout a repo under stunning-fiesta/main folder, then all worktrees maybe created directly under stunning-fiesta root folder and exactly represent branches hierarchy:

stunning-fiesta
├── main
│   ├── .git
│   │   ├── index
│   │   ├── info
│   │   ├── logs
│   │   ├── objects
│   │   ├── ORIG_HEAD
│   │   ├── packed-refs
│   │   ├── refs
│   │   └── worktrees
│   ├── common
│   │   └── README.md
│   ├── project-a
│   │   ├── README.md
│   │   └── .vscode
│   │       └── project-a.code-workspace
│   ├── project-b
│   │   ├── README.md
│   │   └── .vscode
│   │       └── project-b.code-workspace
│   ├── README.md
│   └── .vscode
│       └── all.code-workspace
├── project-a
│   └── bugfix
│       └── 1
│           ├── common
│           │   └── README.md
│           ├── .git
│           ├── project-a
│           │   ├── README.md
│           │   └── .vscode
│           │       └── project-a.code-workspace
│           ├── project-b
│           │   ├── README.md
│           │   └── .vscode
│           │       └── project-b.code-workspace
│           └── README.md
└── project-b
    └── bugfix
        └── 2
            ├── common
            │   └── README.md
            ├── .git
            ├── project-a
            │   ├── README.md
            │   └── .vscode
            │       └── project-a.code-workspace
            ├── project-b
            │   ├── README.md
            │   └── .vscode
            │       └── project-b.code-workspace
            └── README.md

Notice that main .git folder is located at stunning-fiesta/main/.git (that's the folder we get with git rev-parse --git-common-dir) and can go two levels up to find the root repo folder "stunning-fiesta".

Hope this helps you understand the whole picture

now example:
assuming I have a project-a workspace opened from stunning-fiesta/main/project-a/.vscode/project-a.code-workspace
and add new or switch to worktree for the project-a/bugfix/1 branch, I'd like a new vscode to open as:

code stunning-fiesta/project-a/bugfix/1/project-a/.vscode/project-a.code-workspace
     \_repo_root__/ \worktree_(branch)/\_______path_to_the_workspace_file_______/

Thanks for the detailed explanation!

I will start investigating how we can add this feature to our extension.

Hi @yrtimiD,

The enhancement has been published(v1.0.20)!

I would really like to know if this enhancement was what you were looking for.

Please test it when you are free and let me know about your experience.

Thank you again! Just tested on 1.0.20 and the new worktree was opened with the correct workspace!

You are welcome!

I'm glad you liked it.

Thank you for your contribution!