diff options
| -rw-r--r-- | AGENTS.md | 320 |
1 files changed, 320 insertions, 0 deletions
diff --git a/AGENTS.md b/AGENTS.md new file mode 100644 index 0000000..561a44a --- /dev/null +++ b/AGENTS.md | |||
| @@ -0,0 +1,320 @@ | |||
| 1 | # AGENTS.md | ||
| 2 | |||
| 3 | Documentation for AI agents and automated tools working with the ngit codebase. | ||
| 4 | |||
| 5 | ## Project Overview | ||
| 6 | |||
| 7 | **ngit** is a nostr plugin for git that enables decentralized code collaboration using the nostr protocol. | ||
| 8 | |||
| 9 | - **Language**: Rust | ||
| 10 | - **Type**: Command-line tool with git integration | ||
| 11 | - **Architecture**: Two main binaries (`ngit` and `git-remote-nostr`) with shared library code | ||
| 12 | - **Key Dependencies**: nostr-sdk, git2, clap, tokio | ||
| 13 | |||
| 14 | ### Core Concepts | ||
| 15 | |||
| 16 | 1. **Nostr Integration**: Uses nostr for repository identification, discovery, state management, and collaboration (PRs, issues) | ||
| 17 | 2. **Git Server Agnostic**: Still requires git servers for data storage, but they're interchangeable | ||
| 18 | 3. **Decentralized**: Multiple relays and git servers can be used for redundancy | ||
| 19 | 4. **URL Format**: `nostr://<npub|nip05-address>/<identifier>` | ||
| 20 | |||
| 21 | ## Project Structure | ||
| 22 | |||
| 23 | ``` | ||
| 24 | ngit/ | ||
| 25 | ├── src/ | ||
| 26 | │ ├── bin/ | ||
| 27 | │ │ ├── git_remote_nostr/ # Git remote helper implementation | ||
| 28 | │ │ │ ├── capabilities.rs | ||
| 29 | │ │ │ ├── fetch.rs | ||
| 30 | │ │ │ ├── list.rs | ||
| 31 | │ │ │ ├── main.rs | ||
| 32 | │ │ │ └── push.rs | ||
| 33 | │ │ └── ngit/ # Main CLI tool | ||
| 34 | │ │ ├── main.rs | ||
| 35 | │ │ └── sub_commands/ | ||
| 36 | │ └── lib/ # Shared library code | ||
| 37 | │ ├── git/ # Git operations | ||
| 38 | │ ├── login/ # User authentication | ||
| 39 | │ ├── client.rs # Nostr client | ||
| 40 | │ ├── fetch.rs # Fetch operations | ||
| 41 | │ ├── git_events.rs # Git event handling | ||
| 42 | │ ├── list.rs # List operations | ||
| 43 | │ ├── push.rs # Push operations | ||
| 44 | │ ├── repo_ref.rs # Repository references | ||
| 45 | │ ├── repo_state.rs # Repository state | ||
| 46 | │ └── utils.rs # Utilities | ||
| 47 | ├── tests/ # Integration tests | ||
| 48 | ├── test_utils/ # Test utilities and helpers | ||
| 49 | └── git_hooks/ # Git hooks for development | ||
| 50 | ``` | ||
| 51 | |||
| 52 | ## Key Files and Their Purposes | ||
| 53 | |||
| 54 | ### Core Library (`src/lib/`) | ||
| 55 | |||
| 56 | - **`repo_ref.rs`**: Repository reference handling, URL parsing, grasp server detection | ||
| 57 | - Functions: `is_grasp_server_in_list()`, `is_grasp_server_clone_url()`, `normalize_grasp_server_url()` | ||
| 58 | - Critical for identifying and validating repository URLs | ||
| 59 | |||
| 60 | - **`repo_state.rs`**: Manages repository state (refs, commits, etc.) | ||
| 61 | |||
| 62 | - **`client.rs`**: Nostr client wrapper and relay communication | ||
| 63 | |||
| 64 | - **`git_events.rs`**: Handles git-related nostr events | ||
| 65 | |||
| 66 | - **`push.rs`**: Push operations to git servers and nostr relays | ||
| 67 | |||
| 68 | - **`fetch.rs`**: Fetch operations from git servers | ||
| 69 | |||
| 70 | ### Binaries | ||
| 71 | |||
| 72 | - **`git-remote-nostr`**: Git remote helper that enables git to work with nostr:// URLs | ||
| 73 | - **`ngit`**: Main CLI tool for managing nostr repositories | ||
| 74 | |||
| 75 | ## Development Guidelines | ||
| 76 | |||
| 77 | ### Code Style | ||
| 78 | |||
| 79 | - Follow Rust standard formatting (`rustfmt.toml` is configured) | ||
| 80 | - Run `cargo fmt` before committing | ||
| 81 | - Run `cargo clippy` to catch common issues | ||
| 82 | - Use `anyhow::Result` for error handling | ||
| 83 | - Prefer explicit error messages with context | ||
| 84 | |||
| 85 | ### Testing | ||
| 86 | |||
| 87 | ```bash | ||
| 88 | # Run all tests | ||
| 89 | cargo test | ||
| 90 | |||
| 91 | # Run specific test | ||
| 92 | cargo test test_name | ||
| 93 | |||
| 94 | # Run tests with output | ||
| 95 | cargo test -- --nocapture | ||
| 96 | |||
| 97 | # Run integration tests only | ||
| 98 | cargo test --test ngit_init | ||
| 99 | ``` | ||
| 100 | |||
| 101 | ### Important Testing Notes | ||
| 102 | |||
| 103 | 1. **URL Comparison**: When comparing git server URLs, be aware of normalization: | ||
| 104 | - `is_grasp_server_in_list()`: Compares full URLs (including repo path) | ||
| 105 | - `normalize_grasp_server_url()`: Extracts server part only (strips npub and path) | ||
| 106 | |||
| 107 | 2. **Test Structure**: Integration tests are in `tests/`, unit tests are in module files | ||
| 108 | |||
| 109 | ### Building | ||
| 110 | |||
| 111 | ```bash | ||
| 112 | # Development build | ||
| 113 | cargo build | ||
| 114 | |||
| 115 | # Release build | ||
| 116 | cargo build --release | ||
| 117 | |||
| 118 | # Binaries will be in target/debug/ or target/release/ | ||
| 119 | # - ngit | ||
| 120 | # - git-remote-nostr | ||
| 121 | ``` | ||
| 122 | |||
| 123 | ## Common Tasks for Agents | ||
| 124 | |||
| 125 | ### Adding New Features | ||
| 126 | |||
| 127 | 1. **Identify the correct module**: | ||
| 128 | - Git operations → `src/lib/git/` | ||
| 129 | - Nostr operations → `src/lib/client.rs`, `src/lib/git_events.rs` | ||
| 130 | - CLI commands → `src/bin/ngit/sub_commands/` | ||
| 131 | - Remote helper → `src/bin/git_remote_nostr/` | ||
| 132 | |||
| 133 | 2. **Add tests**: Always add tests for new functionality | ||
| 134 | |||
| 135 | 3. **Update error handling**: Use `anyhow::Context` for error messages | ||
| 136 | |||
| 137 | 4. **Check dependencies**: Avoid adding unnecessary dependencies | ||
| 138 | |||
| 139 | ### Debugging Issues | ||
| 140 | |||
| 141 | 1. **Check test failures**: Run `cargo test` to identify failing tests | ||
| 142 | |||
| 143 | 2. **Review error context**: Look at the full error chain with `.context()` | ||
| 144 | |||
| 145 | 3. **Examine URL handling**: Many issues relate to URL parsing/normalization | ||
| 146 | - Check `repo_ref.rs` for URL-related functions | ||
| 147 | - Verify grasp server detection logic | ||
| 148 | |||
| 149 | 4. **Trace nostr events**: Check event creation and publishing in `git_events.rs` | ||
| 150 | |||
| 151 | ### Refactoring | ||
| 152 | |||
| 153 | 1. **Maintain backward compatibility**: This is a CLI tool users depend on | ||
| 154 | |||
| 155 | 2. **Update all call sites**: Use `cargo check` to find all references | ||
| 156 | |||
| 157 | 3. **Run full test suite**: Ensure nothing breaks | ||
| 158 | |||
| 159 | 4. **Check integration tests**: These test real-world scenarios | ||
| 160 | |||
| 161 | ## Architecture Patterns | ||
| 162 | |||
| 163 | ### Error Handling | ||
| 164 | |||
| 165 | ```rust | ||
| 166 | use anyhow::{Context, Result}; | ||
| 167 | |||
| 168 | fn example() -> Result<()> { | ||
| 169 | something() | ||
| 170 | .context("descriptive error message")?; | ||
| 171 | Ok(()) | ||
| 172 | } | ||
| 173 | ``` | ||
| 174 | |||
| 175 | ### Async Operations | ||
| 176 | |||
| 177 | - Uses `tokio` runtime for async operations | ||
| 178 | - Nostr operations are async | ||
| 179 | - Git operations are mostly sync | ||
| 180 | |||
| 181 | ### Configuration | ||
| 182 | |||
| 183 | - User config stored in platform-specific directories (via `directories` crate) | ||
| 184 | - Repository config in `.git/config` and `ngit.yaml` | ||
| 185 | |||
| 186 | ## Important Invariants | ||
| 187 | |||
| 188 | ### URL Handling | ||
| 189 | |||
| 190 | 1. **Grasp Server URLs**: Must contain npub in path format: `/{npub}/` | ||
| 191 | 2. **Nostr URLs**: Format: `nostr://<npub|nip05>/<identifier>` | ||
| 192 | 3. **Normalization**: Different functions normalize differently: | ||
| 193 | - Full URL comparison: Use direct string comparison with `trim_end_matches('/')` | ||
| 194 | - Server identification: Use `normalize_grasp_server_url()` | ||
| 195 | |||
| 196 | ### Git Integration | ||
| 197 | |||
| 198 | 1. **Remote helper protocol**: Must follow git's remote helper protocol exactly | ||
| 199 | 2. **Ref naming**: PRs use `pr/` prefix | ||
| 200 | 3. **State sync**: Must keep git refs in sync with nostr events | ||
| 201 | |||
| 202 | ## Dependencies to Be Aware Of | ||
| 203 | |||
| 204 | ### Critical Dependencies | ||
| 205 | |||
| 206 | - **nostr**: Core nostr protocol implementation | ||
| 207 | - **git2**: Git operations via libgit2 | ||
| 208 | - **tokio**: Async runtime | ||
| 209 | - **anyhow**: Error handling | ||
| 210 | |||
| 211 | ### Optional Features | ||
| 212 | |||
| 213 | Check `Cargo.toml` for feature flags and optional dependencies. | ||
| 214 | |||
| 215 | ## Testing Strategy | ||
| 216 | |||
| 217 | ### Unit Tests | ||
| 218 | |||
| 219 | - Located in same file as code (`#[cfg(test)] mod tests`) | ||
| 220 | - Test individual functions in isolation | ||
| 221 | - Example: `is_grasp_server_in_list` tests in `repo_ref.rs` | ||
| 222 | |||
| 223 | ### Integration Tests | ||
| 224 | |||
| 225 | - Located in `tests/` directory | ||
| 226 | - Test full workflows (init, push, fetch, etc.) | ||
| 227 | - Require test utilities from `test_utils/` | ||
| 228 | |||
| 229 | ### Test Utilities | ||
| 230 | |||
| 231 | - Mock relay implementation | ||
| 232 | - Git repository setup helpers | ||
| 233 | - Located in `test_utils/` | ||
| 234 | |||
| 235 | ## Common Pitfalls | ||
| 236 | |||
| 237 | ### 1. URL Normalization | ||
| 238 | |||
| 239 | **Problem**: Using wrong normalization function leads to incorrect comparisons. | ||
| 240 | |||
| 241 | **Solution**: | ||
| 242 | - For full URL comparison: Direct string comparison | ||
| 243 | - For server identification: Use `normalize_grasp_server_url()` | ||
| 244 | |||
| 245 | ### 2. Async/Sync Boundaries | ||
| 246 | |||
| 247 | **Problem**: Mixing async and sync code incorrectly. | ||
| 248 | |||
| 249 | **Solution**: Use `tokio::runtime::Runtime` or proper async context. | ||
| 250 | |||
| 251 | ### 3. Git State | ||
| 252 | |||
| 253 | **Problem**: Git state getting out of sync with nostr events. | ||
| 254 | |||
| 255 | **Solution**: Always update both git refs and publish nostr events atomically. | ||
| 256 | |||
| 257 | ## Contribution Workflow | ||
| 258 | |||
| 259 | 1. **Clone**: Use ngit itself to clone: `nostr://dan@gitworkshop.dev/relay.damus.io/ngit` | ||
| 260 | 2. **Branch**: Create a branch with `pr/` prefix for pull requests | ||
| 261 | 3. **Test**: Run `cargo test` and `cargo clippy` | ||
| 262 | 4. **Commit**: Follow git commit message conventions (enforced by `git_hooks/commit-msg`) | ||
| 263 | 5. **Push**: Push branch to submit PR via ngit | ||
| 264 | |||
| 265 | ## Resources | ||
| 266 | |||
| 267 | - **Main Repository**: [gitworkshop.dev/dan@gitworkshop.dev/ngit](https://gitworkshop.dev/dan@gitworkshop.dev/ngit) | ||
| 268 | - **Homepage**: [gitworkshop.dev/ngit](https://gitworkshop.dev/ngit) | ||
| 269 | - **Source**: [codeberg.org/DanConwayDev/ngit-cli](https://codeberg.org/DanConwayDev/ngit-cli) | ||
| 270 | - **Nostr Protocol**: [github.com/nostr-protocol/nostr](https://github.com/nostr-protocol/nostr) | ||
| 271 | |||
| 272 | ## Quick Reference | ||
| 273 | |||
| 274 | ### Build Commands | ||
| 275 | |||
| 276 | ```bash | ||
| 277 | cargo build # Development build | ||
| 278 | cargo build --release # Release build | ||
| 279 | cargo test # Run tests | ||
| 280 | cargo clippy # Run linter | ||
| 281 | cargo fmt # Format code | ||
| 282 | ``` | ||
| 283 | |||
| 284 | ### File Locations | ||
| 285 | |||
| 286 | ```bash | ||
| 287 | src/lib/repo_ref.rs # URL handling, grasp server detection | ||
| 288 | src/lib/client.rs # Nostr client | ||
| 289 | src/lib/git_events.rs # Git event handling | ||
| 290 | src/bin/ngit/main.rs # Main CLI entry point | ||
| 291 | src/bin/git_remote_nostr/ # Git remote helper | ||
| 292 | tests/ # Integration tests | ||
| 293 | ``` | ||
| 294 | |||
| 295 | ### Key Functions | ||
| 296 | |||
| 297 | ```rust | ||
| 298 | // URL handling | ||
| 299 | is_grasp_server_in_list(url, list) // Check if URL in list (exact match) | ||
| 300 | is_grasp_server_clone_url(url) // Check if URL is grasp server format | ||
| 301 | normalize_grasp_server_url(url) // Extract server part from URL | ||
| 302 | |||
| 303 | // Repository operations | ||
| 304 | repo_ref.to_nostr_git_url() // Convert to nostr URL | ||
| 305 | push_to_remote() // Push to git server | ||
| 306 | fetch_from_remote() // Fetch from git server | ||
| 307 | ``` | ||
| 308 | |||
| 309 | ## Support and Questions | ||
| 310 | |||
| 311 | For questions about the codebase or contributions: | ||
| 312 | - Check existing issues on [gitworkshop.dev](https://gitworkshop.dev/dan@gitworkshop.dev/ngit) | ||
| 313 | - Review test files for usage examples | ||
| 314 | - Examine integration tests for workflow examples | ||
| 315 | |||
| 316 | --- | ||
| 317 | |||
| 318 | **Last Updated**: 2025-10-20 | ||
| 319 | **Version**: 1.7.4 | ||
| 320 | **Maintainer**: DanConwayDev | ||