Repository navigation
How the Shelf Works
The shelf is Shelve Changes: chosen files are saved as a named patch and taken out of the work tree, then applied again later, in full or file by file. For the user side, see Shelf.
Git's stash (see How Stashes Work) is all or nothing in our UI, has no names beyond a message, and hides its files. People expect to shelve a few files, keep a copy without reverting, see each shelved file's diff and unshelve only part of it. A patch on disk gives all of that with plain git commands, and it survives git stash clear.
shelf/store.rs keeps every shelved change list in <common git dir>/nibtree-shelf/:
-
<id>.patch: onegit diff --binarypatch, one section per file; -
<id>.json:{ version, id, name, createdAt, branch, headCommit, files }, each file withpath,oldPath,change,binaryandoldId(the blob it started from), in patch order.
The id is the time in milliseconds plus a counter (1790765466000-0), so ids sort by age. check_id refuses anything else from the UI, so a path can never leave the folder. Both files are written through a temporary file and a rename. list skips unreadable or orphaned entries but never deletes them. The folder is in the common git dir (repo.commondir()), so linked worktrees share one shelf.
sequenceDiagram
participant UI as ShelveDialog
participant CMD as commands/shelf.rs
participant Git as git CLI
participant Disk as nibtree-shelf
UI->>CMD: shelve_changes(repo, name, files, keep)
CMD->>CMD: select: split tracked and untracked, refuse conflicts
CMD->>Git: add --intent-to-add (scratch index, new files only)
CMD->>Git: diff --raw -z HEAD -- files (scratch index)
CMD->>Git: diff --binary --full-index ... HEAD -- files
CMD->>Disk: save patch, then json
CMD->>Git: apply --check -R (patch on stdin)
alt the check fails
CMD->>Disk: delete the entry
CMD-->>UI: error, nothing touched
else keep in working tree
CMD-->>UI: ShelfEntry
else
CMD->>Git: apply -R, then reset -q (or rm --cached when unborn) the paths
CMD-->>UI: ShelfEntry
end
Details that matter:
-
New files join a scratch copy of the index (
<id>.index, set throughGIT_INDEX_FILE) as intent-to-add. That way onegit diff HEADincludes them and pairs a delete plus an add into a rename, and the real index is never touched. The scratch file is removed when its guard drops. -
Fixed patch flags (
PATCH_FLAGS):--binary --full-index --no-color --no-ext-diff --no-textconv --src-prefix=a/ --dst-prefix=b/ --no-relative --find-renames --ignore-submodules=all. The user's diff config can never produce a patchgit applycannot read back. The--raw -zlisting uses the same rename detection, so its entries line up one to one with the patch sections; if they do not, shelving stops with "Could not read the changes to shelve". -
An empty repository diffs against the empty tree (
4b825dc...), and reverting usesgit rm --cachedinstead ofgit reset. -
Saved, then checked, then reverted. The work tree is touched only once the patch is on disk and
git apply --check -Rproved it takes the changes out exactly. Applies always pass--whitespace=nowarn, soapply.whitespace=fixcannot change content.
unshelve(repo, shelfId, filePaths, removeFromShelf) loads the patch, splits it into sections (shelf/patch.rs), keeps the chosen ones and applies them:
flowchart TD
Start["chosen sections"] --> Check{"git apply --check"}
Check -->|"fits"| Apply["git apply"]
Check -->|"does not fit"| Three["git apply --3way"]
Three -->|"conflicts"| Keep["OpOutcome conflicts: shelf unchanged"]
Three -->|"failed, no conflicts"| Err["error: the shelved changes do not apply"]
Apply --> Remove{"remove from shelf?"}
Three -->|"clean"| Remove
Remove -->|"all files"| Delete["delete entry"]
Remove -->|"some files"| Rewrite["save the other sections as the new patch"]
Remove -->|"keep"| Done["done"]
Conflicts come back as OpOutcome { conflicts: true }, so repoStore.runOp opens the Conflicts dialog like any merge.
shelf_file_diff rebuilds both sides in Rust. The original is the blob named in the section's index line (or oldId, or empty for an added file); apply_hunks applies the hunks to it to get the shelved side. When that blob is gone from the repository, hunk_sides builds the two sides from the hunks alone and the result says partial. Binary files come back as a binary diff. The tab is a pseudo tab (gitTabPath({ kind: "shelf", ... })) titled "Shelved: name" and shown by ShelfDiffTab.svelte with DiffView in read-only mode, labels Base and Shelved.
| File | What it does |
|---|---|
src-tauri/src/commands/shelf.rs |
shelve_changes, list_shelf, unshelve, shelf_file_diff, rename_shelf, delete_shelf; selection, scratch index, revert, apply |
src-tauri/src/shelf/store.rs |
Folder layout, ShelfEntry, ids, names, atomic writes |
src-tauri/src/shelf/patch.rs |
Splitting patches, reading ids and hunks, rebuilding sides |
src-tauri/src/git/cli.rs |
run_with_env, run_bytes_with_env, run_with_stdin, run_raw
|
src/lib/shelf/ShelveDialog.svelte |
The Shelve Changes dialog |
src/lib/shelf/ShelfPanel.svelte |
The Shelf tab of the bottom panel: a library List (role tree) of Rows |
src/lib/shelf/ShelfDiffTab.svelte |
The read-only "Shelved:" tab |
src/lib/shelf/shelfActions.svelte.ts |
Open dialog, Show Shelf, unshelve, rename, delete, shelfState.version
|
src/lib/shelf/shelfModel.ts |
Default name, candidates, the tree's rows, selected files |
src/lib/views/changes/repoMenu.ts, RepoSection.svelte
|
The Stash submenu items and the file menu item |
src/lib/menu/menuSpec.ts, menuActions.ts
|
Git > Uncommitted Changes items |
A patch, not a stash commit. A patch can hold any subset of files, be applied in part, and be rewritten when part of it is unshelved. It also stays out of refs/stash, so git's own stash commands never touch it.
Inside .git, not the work tree. The shelf is never committed or pushed and needs no .gitignore entry. Being in the common dir, every worktree of a repository sees the same shelf.
Save before revert. If anything fails after the patch is written, the user's work is still on disk in one of two places. The reverse check before reverting means a patch that would not revert cleanly is deleted and the files stay untouched.
Rebuild diffs in Rust. Show Diff needs the original blob and the hunks, both of which the backend already has. The frontend gets an ordinary FileDiff.
Unshelving does not stage. The changes come back to the work tree only (a clean git apply without --index), so new files are untracked again. Shelving resets the index of its paths to HEAD, so the staged and unstaged split is not kept.
Conflicts keep the shelf. Removing a shelved change only after a clean apply means a conflicted unshelve can always be retried or undone.
Submodules and nested repositories are excluded (--ignore-submodules=all, shelveCandidates): they are other repositories, not changes of this one.
-
src-tauri/src/commands/shelf.rs: a round trip of every kind of change (modified, added, deleted, renamed, binary, staged and unstaged); keep in working tree; a partial unshelve keeps the other files; an unshelve onto changed lines stops with conflicts and keeps the shelf; refusing nothing, conflicts and escaping paths; shelving before the first commit; Show Diff rebuilding both sides; rename and delete; the raw listing parser. -
src-tauri/src/shelf/store.rs: ids are checked before touching files, names are trimmed and required, older JSON without optional fields still reads. -
src-tauri/src/shelf/patch.rs: splitting, ids, binary and hunks, missing final newlines, new files, CRLF line endings. -
src/lib/shelf/shelfModel.test.ts: default names, labels, candidates, the rows and the selected files.
- Update this page when
commands/shelf.rs,shelf/orsrc/lib/shelf/change, or if the storage format changes (bumpFORMAT_VERSIONand keep reading old files). - Update Shelf for any change to the dialog, the tab or the menus, and retake
shelf-shelve-dialog.pngandshelf-panel.png. - The commands are listed in Commands and Events.
None yet.
These pages are generated from docs/wiki. To correct something, open a pull request against the source file; edits made here are overwritten.
Using Nibtree
- Getting Started
- Welcome Screen
- Workspaces
- Files Panel
- File Operations
- File Icons
- Rounded Panels
- Editor and Tabs
- Pin Reorder and Wrap Tabs
- Single Tab Title
- Split Editor
- New File and Unsaved Changes
- Unload Hidden Tabs
- Editing Code
- Code Appearance
- Markdown Editor
- Image and PDF Preview
- Search Everywhere
- Recent Files
- Navigation Bar
- Find and Replace
- Changes and Commits
- Sign In Prompts
- Commit Box Layout
- Repository Actions
- Commit Options
- Ignoring Files
- Diffs
- Inline Diffs
- Staging View
- Revert a Change
- Resolving Conflicts
- Merge Tool
- Blame
- History and Log
- Merge Commits
- Branches and Tags
- Branches Popup
- Remotes
- Stashes
- Shelf
- Git Menu
- Git Dialogs
- Interactive Rebase
- Worktrees
- AI Agents
- AI Tasks
- AI Usage
- Keep Computer Awake
- Submodules
- Git LFS
- GitHub
- Git Console
- Terminal
- Terminal Features
- Scripts
- Navigation
- Menus
- Settings
- Settings Search
- Color Themes
- Settings Terminal and Automation
- Settings Files
- Updates
- Status Bar and Help
- Notifications
- Memory Use
- Clear Cache
- MCP and CLI
- Git Mergetool
- Keyboard Shortcuts
- Keyboard Shortcuts Editor
- Troubleshooting
- Troubleshooting Git
Developers
- Developer Guide
- Architecture
- Project Layout
- Backend
- How Folder Watching Works
- Folder Watching Bugs We Fixed
- How Workspace Files Work
- How the Preview Scheme Works
- Backend Services
- How Terminal Features Work
- Terminal Bugs We Fixed
- Frontend
- How the Rich Markdown Editor Works
- Rich Markdown Bugs We Fixed
- Git Menu Links
- Menu Keys and Routing
- Frontend Modules
- Blame Bugs We Fixed
- Project Color And Title Bar
- One Diff View
- Diff Bugs We Fixed
- Merge Tool Layouts
- Merge Tool Bugs We Fixed
- Color Theme Bugs We Fixed
- How the Connected Diff Works
- Rounded Panels Bugs We Fixed
- Update Bugs We Fixed
- Status Bar Bugs We Fixed
- MCP and CLI Bugs We Fixed
- Repository Actions Bugs We Fixed
- How the Changes Tab Works
- CodeMirror Patterns
- How the Cursor Works
- How Editor Features Work
- How the Path Bar Works
- Commands and Events
- Settings Reference
- Commands Workspace and Changes
- Commands Branches and Remotes
- Commands History and Extras
- Commands App and Tools
- Testing
- Test Suites
- Debugging
- Debugging with MCP
- Measuring Setting Memory
- How Terminal Memory Is Measured
- How Markdown Diagrams Work
- Releases and CI
- Publishing the Wiki
- Versioning and Changelog
- Browser Version Packages
- Browser Release Checks
- Docker Image
- Platforms and Signing
- Windows Support
- Windows Bugs We Fixed
- Windows MCP and CLI
- Docs and Screenshots
- UI Library
- UI Library MCP
- UI Library Bugs
- UI Library Migration
- Contributing
How features work
- Welcome screen
- Workspaces and folders
- Files panel
- File operations
- File icons
- Editor and tabs
- Pin, reorder and wrap tabs
- Single tab title
- Split editor
- New File and unsaved changes
- Unload hidden tabs
- Editing code
- Code appearance
- Markdown editor
- Image and PDF preview
- Search Everywhere
- Recent Files
- Navigation Bar
- Find and replace
- Changes and commits
- Sign-in prompts
- Commit box layout
- Repository actions
- Commit options
- Ignoring files
- Diffs
- Inline diffs
- Staging view
- Revert a change
- Resolving conflicts
- Merge tool
- Blame
- History and log
- Merge commits
- Commit tabs
- Branches and tags
- Branches popup
- Remotes
- Stashes
- Shelf
- Git menu
- Git dialogs
- Interactive rebase
- Worktrees
- AI agents
- AI usage
- Keep computer awake
- AI tasks
- Submodules
- Git LFS
- GitHub account
- Git Console
- Back and Forward
- Integrated terminal
- Scripts tool window
- Menu bar
- Settings
- Search in Settings
- Color themes
- Rounded panels
- Updates and release channels
- Status bar and help links
- Notifications
- Memory readout and memory log
- Clear Cache
- MCP server and command line tool
- git mergetool mode