-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add git worktree visual separation (#42) #43
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
Open
shabaraba
wants to merge
1
commit into
main
Choose a base branch
from
claude/fix-pile-nvim-42-cvLl7
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| local log = require('pile.log') | ||
| local M = {} | ||
|
|
||
| -- Cache for worktree information | ||
| local worktree_cache = nil | ||
| local cache_timestamp = 0 | ||
| local cache_ttl = 5000 -- 5 seconds TTL | ||
|
|
||
| -- Parse git worktree list output | ||
| local function parse_worktree_list(output) | ||
| local worktrees = {} | ||
| local current_worktree = nil | ||
|
|
||
| for line in output:gmatch("[^\r\n]+") do | ||
| -- Worktree path line starts with "worktree " | ||
| if line:match("^worktree ") then | ||
| local path = line:match("^worktree (.+)$") | ||
| if path then | ||
| current_worktree = { | ||
| path = path, | ||
| head = nil, | ||
| branch = nil, | ||
| bare = false | ||
| } | ||
| table.insert(worktrees, current_worktree) | ||
| end | ||
| elseif current_worktree then | ||
| -- HEAD line | ||
| local head = line:match("^HEAD (.+)$") | ||
| if head then | ||
| current_worktree.head = head | ||
| end | ||
|
|
||
| -- Branch line | ||
| local branch = line:match("^branch refs/heads/(.+)$") | ||
| if branch then | ||
| current_worktree.branch = branch | ||
| end | ||
|
|
||
| -- Bare repo | ||
| if line:match("^bare$") then | ||
| current_worktree.bare = true | ||
| end | ||
| end | ||
| end | ||
|
|
||
| return worktrees | ||
| end | ||
|
|
||
| -- Get all git worktrees | ||
| function M.get_worktrees() | ||
| local current_time = vim.loop.now() | ||
|
|
||
| -- Return cached result if still valid | ||
| if worktree_cache and (current_time - cache_timestamp) < cache_ttl then | ||
| return worktree_cache | ||
| end | ||
|
|
||
| -- Execute git worktree list | ||
| local output = vim.fn.system("git worktree list --porcelain 2>/dev/null") | ||
|
|
||
| -- Check if git command succeeded | ||
| if vim.v.shell_error ~= 0 then | ||
| log.debug("git worktree list failed - not in a git repository or git not available") | ||
| worktree_cache = {} | ||
| cache_timestamp = current_time | ||
| return {} | ||
| end | ||
|
|
||
| local worktrees = parse_worktree_list(output) | ||
|
|
||
| -- Sort worktrees by path length (longer paths first) for proper matching | ||
| table.sort(worktrees, function(a, b) | ||
| return #a.path > #b.path | ||
| end) | ||
|
|
||
| log.debug(string.format("Found %d git worktrees", #worktrees)) | ||
| for i, wt in ipairs(worktrees) do | ||
| log.debug(string.format(" [%d] path=%s, branch=%s, bare=%s", | ||
| i, wt.path, wt.branch or "detached", wt.bare)) | ||
| end | ||
|
|
||
| worktree_cache = worktrees | ||
| cache_timestamp = current_time | ||
|
|
||
| return worktrees | ||
| end | ||
|
|
||
| -- Find which worktree a file belongs to | ||
| function M.get_worktree_for_file(filepath) | ||
| if not filepath or filepath == "" then | ||
| return nil | ||
| end | ||
|
|
||
| local worktrees = M.get_worktrees() | ||
|
|
||
| -- No worktrees found | ||
| if #worktrees == 0 then | ||
| return nil | ||
| end | ||
|
|
||
| -- Normalize the filepath | ||
| local normalized_path = vim.fn.fnamemodify(filepath, ":p") | ||
|
|
||
| -- Find the worktree that contains this file | ||
| -- We check longest paths first (already sorted) | ||
| for _, worktree in ipairs(worktrees) do | ||
| local wt_path = vim.fn.fnamemodify(worktree.path, ":p") | ||
|
|
||
| -- Check if the file is under this worktree's path | ||
| if normalized_path:sub(1, #wt_path) == wt_path then | ||
| return worktree | ||
| end | ||
| end | ||
|
|
||
| return nil | ||
| end | ||
|
|
||
| -- Get a display name for a worktree | ||
| function M.get_worktree_display_name(worktree) | ||
| if not worktree then | ||
| return "No Worktree" | ||
| end | ||
|
|
||
| if worktree.branch then | ||
| return worktree.branch | ||
| elseif worktree.head then | ||
| -- Show shortened commit hash for detached HEAD | ||
| return worktree.head:sub(1, 7) | ||
| else | ||
| -- Fallback to directory name | ||
| return vim.fn.fnamemodify(worktree.path, ":t") | ||
| end | ||
| end | ||
|
|
||
| -- Clear the worktree cache (useful for testing or manual refresh) | ||
| function M.clear_cache() | ||
| worktree_cache = nil | ||
| cache_timestamp = 0 | ||
| end | ||
|
|
||
| return M |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add language specifier to fenced code block.
The example sidebar visualization is helpful, but the fenced code block is missing a language specifier. Add "text" to clarify it's plain text output.
📝 Proposed fix
🧰 Tools
🪛 markdownlint-cli2 (0.18.1)
102-102: Fenced code blocks should have a language specified
(MD040, fenced-code-language)
🤖 Prompt for AI Agents