Skip to content

Commit 6d0c05c

Browse files
committed
docs(topics): Add topics section with Traversing Git Repos guide
why: Provide high-level conceptual documentation with runnable examples what: - Create docs/topics/ directory with index.md - Add traversing_git.md explaining Manager/Cmd pattern for navigating git's object graph with typed Python objects - All doctests verified passing
1 parent d4e973e commit 6d0c05c

3 files changed

Lines changed: 166 additions & 0 deletions

File tree

docs/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
:hidden:
1010
1111
quickstart
12+
topics/index
1213
url/index
1314
cmd/index
1415
sync/index

docs/topics/index.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
orphan: true
3+
---
4+
5+
# Topics
6+
7+
Explore libvcs's core functionalities and design patterns at a high level,
8+
with detailed explanations and runnable examples.
9+
10+
```{toctree}
11+
12+
traversing_git
13+
filtering
14+
url_parsing
15+
```

docs/topics/traversing_git.md

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
(traversing-git-repos)=
2+
3+
# Traversing Git Repos
4+
5+
libvcs provides **Manager** and **Cmd** classes for navigating the "object graph"
6+
of your git repository. These aren't just convenient abstractions—they're also
7+
simulacra of the git commands themselves, giving you typed Python objects instead
8+
of raw strings.
9+
10+
## Overview
11+
12+
The pattern consists of two types of classes:
13+
14+
- **Manager** classes handle collection-level operations (`ls()`, `get()`,
15+
`filter()`, `add()`/`create()`)
16+
- **Cmd** classes handle per-entity operations (`show()`, `remove()`,
17+
`rename()`)
18+
19+
```
20+
Git instance
21+
├── branches: GitBranchManager
22+
│ ├── ls() -> QueryList[GitBranchCmd]
23+
│ ├── get() -> GitBranchCmd
24+
│ └── create()
25+
├── tags: GitTagManager
26+
├── remotes: GitRemoteManager
27+
├── stashes: GitStashManager
28+
├── worktrees: GitWorktreeManager
29+
├── notes: GitNotesManager
30+
├── submodules: GitSubmoduleManager
31+
└── reflog: GitReflogManager
32+
```
33+
34+
## Basic Usage
35+
36+
```python
37+
>>> from libvcs.cmd.git import Git
38+
>>> git = Git(path=example_git_repo.path)
39+
```
40+
41+
### Listing Items
42+
43+
All Manager classes have an `ls()` method that returns a
44+
{class}`~libvcs._internal.query_list.QueryList`:
45+
46+
```python
47+
>>> from libvcs.cmd.git import Git
48+
>>> git = Git(path=example_git_repo.path)
49+
>>> branches = git.branches.ls()
50+
>>> isinstance(branches, list)
51+
True
52+
>>> tags = git.tags.ls()
53+
>>> remotes = git.remotes.ls()
54+
```
55+
56+
### Getting a Single Item
57+
58+
Use `get()` with filter criteria to retrieve a single item:
59+
60+
```python
61+
>>> from libvcs.cmd.git import Git
62+
>>> git = Git(path=example_git_repo.path)
63+
>>> git.tags.create(name='v1.0.0', message='Release 1.0')
64+
''
65+
>>> tag = git.tags.get(tag_name='v1.0.0')
66+
>>> tag.tag_name
67+
'v1.0.0'
68+
```
69+
70+
### Creating Items
71+
72+
Manager classes provide `create()` or `add()` methods:
73+
74+
```python
75+
>>> from libvcs.cmd.git import Git
76+
>>> git = Git(path=example_git_repo.path)
77+
>>> git.tags.create(name='v2.0.0', message='Release 2.0')
78+
''
79+
>>> git.branches.create(branch='feature-branch')
80+
''
81+
```
82+
83+
### Per-Entity Operations
84+
85+
Cmd objects have methods for mutating or inspecting that entity:
86+
87+
```python
88+
>>> from libvcs.cmd.git import Git
89+
>>> git = Git(path=example_git_repo.path)
90+
>>> git.tags.create(name='v3.0.0', message='Release 3.0')
91+
''
92+
>>> tag = git.tags.get(tag_name='v3.0.0')
93+
>>> tag.delete() # doctest: +ELLIPSIS
94+
"Deleted tag 'v3.0.0' ..."
95+
>>> git.branches.create(branch='temp-branch')
96+
''
97+
>>> branch = git.branches.get(branch_name='temp-branch')
98+
>>> branch.delete() # doctest: +ELLIPSIS
99+
'Deleted branch temp-branch ...'
100+
```
101+
102+
## Comparison to Raw Commands
103+
104+
### Before: Parsing Strings
105+
106+
Without the Manager/Cmd pattern, you'd parse raw output:
107+
108+
```python
109+
>>> from libvcs.cmd.git import Git
110+
>>> git = Git(path=example_git_repo.path)
111+
>>> # Raw output requires parsing
112+
>>> raw_output = git.run(['tag', '-l'])
113+
>>> tag_names = [t for t in raw_output.strip().split('\\n') if t]
114+
```
115+
116+
### After: Typed Objects
117+
118+
With the Manager/Cmd pattern, you get typed objects:
119+
120+
```python
121+
>>> from libvcs.cmd.git import Git
122+
>>> git = Git(path=example_git_repo.path)
123+
>>> tags = git.tags.ls()
124+
>>> for tag in tags: # doctest: +SKIP
125+
... print(f"{tag.tag_name}")
126+
```
127+
128+
## When to Use
129+
130+
| Use Case | Approach |
131+
|----------|----------|
132+
| List/filter/get entities | Manager class (`git.branches.ls()`) |
133+
| Mutate a specific entity | Cmd class (`branch.delete()`) |
134+
| Run arbitrary git commands | Direct methods (`git.run([...])`) |
135+
| Complex pipelines | Mix of both |
136+
137+
## Available Managers
138+
139+
| Manager | Access | Operations |
140+
|---------|--------|------------|
141+
| {class}`~libvcs.cmd.git.GitBranchManager` | `git.branches` | List, create, checkout branches |
142+
| {class}`~libvcs.cmd.git.GitTagManager` | `git.tags` | List, create tags |
143+
| {class}`~libvcs.cmd.git.GitRemoteManager` | `git.remotes` | List, add, configure remotes |
144+
| {class}`~libvcs.cmd.git.GitStashManager` | `git.stashes` | List, push, clear stashes |
145+
| {class}`~libvcs.cmd.git.GitWorktreeManager` | `git.worktrees` | List, add, prune worktrees |
146+
| {class}`~libvcs.cmd.git.GitNotesManager` | `git.notes` | List, add, prune notes |
147+
| {class}`~libvcs.cmd.git.GitSubmoduleManager` | `git.submodules` | List, add, sync submodules |
148+
| {class}`~libvcs.cmd.git.GitReflogManager` | `git.reflog` | List, expire reflog entries |
149+
150+
See {doc}`/cmd/git/index` for the complete API reference.

0 commit comments

Comments
 (0)