1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
|
**ARCHIVED: 2025-11-04**
**Reason:** Decided to validate grasp-audit against ngit-relay first
**See:** docs/archive/2025-11-04-test-strategy-decision.md for rationale
---
# TDD Plan for GRASP-01 Git Backend
**Date:** 2025-11-04
**Status:** ARCHIVED - Superseded by test-first approach
**Goal:** Implement Git Smart HTTP backend with inline authorization using TDD
---
## Current State
### What We Have
- ✅ NIP-01 compliant Nostr relay (working, tested)
- ✅ NIP-34 event handling (announcements accepted)
- ✅ Storage layer (in-memory + disk paths configured)
- ✅ Test infrastructure (integration tests with auto relay management)
- ✅ grasp-audit compliance testing library
### What We Need
- ❌ Git Smart HTTP protocol handler
- ❌ Git repository management (init, receive-pack, upload-pack)
- ❌ Push authorization (validate against Nostr state events)
- ❌ Integration with existing Nostr relay
---
## Tool Selection Analysis
### Option 1: git2-rs (libgit2 bindings)
**Pros:**
- ✅ Pure Rust bindings to battle-tested libgit2
- ✅ Full Git functionality (init, push, pull, refs, objects)
- ✅ Thread-safe, well-maintained
- ✅ Used by cargo, widely deployed
- ✅ Can intercept and validate operations programmatically
**Cons:**
- ❌ Requires libgit2 system dependency
- ❌ Higher-level API - may be overkill for our needs
- ❌ Harder to intercept low-level protocol for inline auth
**Use Cases:**
- Repository initialization
- Reading/writing refs
- Object storage queries
- Validation of commits/trees
### Option 2: Standard git in subprocess
**Pros:**
- ✅ Uses system git (already available)
- ✅ No additional dependencies
- ✅ Battle-tested Git implementation
- ✅ Easy to spawn for upload-pack/receive-pack
**Cons:**
- ❌ Harder to intercept for inline authorization
- ❌ Must parse git protocol to validate pushes
- ❌ Subprocess overhead
- ❌ Complex error handling
**Use Cases:**
- git-upload-pack (clone, fetch)
- git-receive-pack (push) - but need to intercept
### Option 3: git-http-backend crate
**Pros:**
- ✅ Purpose-built for Git Smart HTTP
- ✅ Handles protocol parsing
- ✅ Works with system git
- ✅ Can intercept receive-pack before spawning git
**Cons:**
- ❌ Less mature (but we're already planning to use it per README)
- ❌ Still need to parse pack protocol for validation
**Use Cases:**
- HTTP endpoint handling
- Protocol negotiation
- Spawning git processes
### Option 4: Hybrid Approach (RECOMMENDED)
**Combination:**
1. **git-http-backend** - HTTP protocol handling
2. **git2-rs** - Repository management, ref validation
3. **System git** - Actual pack operations (upload-pack/receive-pack)
**Why Hybrid:**
- git-http-backend handles HTTP → Git protocol translation
- git2 for safe repository operations (init, refs, validation)
- System git for pack operations (proven, fast)
- We intercept at the HTTP layer before spawning git
**Architecture:**
```
HTTP Request → git-http-backend → Our Auth Layer → git2/system git
↓
Nostr Relay
(state validation)
```
---
## Recommended Approach: Hybrid
### Dependencies to Add
```toml
[dependencies]
# Git operations
git2 = "0.20" # Repository management, refs
# git-http-backend - TBD (research if available, or implement minimal)
[dev-dependencies]
tempfile = "3.8" # Temporary repos for testing
```
### Why This Works
1. **git2 for Repository Management:**
- Initialize bare repos when announcements arrive
- Read/write refs safely
- Query repository state
- Validate commits exist
2. **System git for Pack Operations:**
- Spawn `git-upload-pack` for clones/fetches (read-only, safe)
- Spawn `git-receive-pack` ONLY after auth passes
- Leverage proven pack protocol implementation
3. **Inline Authorization:**
- Parse HTTP request to extract ref updates
- Query Nostr relay for latest state event
- Validate push matches state
- Only spawn git-receive-pack if authorized
---
## TDD Implementation Plan
### Phase 1: Repository Management (git2)
**Goal:** Create and manage bare Git repositories
**Tests:**
1. ✅ Create bare repository when announcement received
2. ✅ Initialize with proper config (bare, shared)
3. ✅ Set HEAD from state event
4. ✅ Read refs from repository
5. ✅ Write refs to repository
6. ✅ Query if commit exists in repository
**Implementation:**
```rust
// src/git/repository.rs
pub struct GitRepository {
path: PathBuf,
repo: git2::Repository,
}
impl GitRepository {
pub fn init_bare(path: PathBuf) -> Result<Self>;
pub fn set_head(ref_name: &str) -> Result<()>;
pub fn get_ref(&self, name: &str) -> Result<Option<String>>;
pub fn set_ref(&self, name: &str, oid: &str) -> Result<()>;
pub fn has_commit(&self, oid: &str) -> Result<bool>;
}
```
**Test Example:**
```rust
#[test]
fn test_init_bare_repository() {
let temp = TempDir::new().unwrap();
let repo = GitRepository::init_bare(temp.path().to_path_buf()).unwrap();
assert!(temp.path().join("HEAD").exists());
assert!(temp.path().join("config").exists());
assert!(temp.path().join("objects").exists());
assert!(temp.path().join("refs").exists());
}
```
### Phase 2: Git Protocol Parsing
**Goal:** Parse Git Smart HTTP protocol for authorization
**Tests:**
1. ✅ Parse info/refs request
2. ✅ Parse upload-pack request (clone/fetch)
3. ✅ Parse receive-pack request (push)
4. ✅ Extract ref updates from receive-pack
5. ✅ Extract capabilities from request
**Implementation:**
```rust
// src/git/protocol.rs
pub struct RefUpdate {
pub old_oid: String,
pub new_oid: String,
pub ref_name: String,
}
pub fn parse_receive_pack_request(body: &[u8]) -> Result<Vec<RefUpdate>>;
pub fn parse_capabilities(body: &[u8]) -> Result<Vec<String>>;
```
**Test Example:**
```rust
#[test]
fn test_parse_receive_pack_single_ref() {
let body = b"0000000000000000000000000000000000000000 \
a1b2c3d4e5f6789012345678901234567890abcd \
refs/heads/main\0 report-status\n";
let updates = parse_receive_pack_request(body).unwrap();
assert_eq!(updates.len(), 1);
assert_eq!(updates[0].ref_name, "refs/heads/main");
assert_eq!(updates[0].new_oid, "a1b2c3d4e5f6789012345678901234567890abcd");
}
```
### Phase 3: Authorization Logic
**Goal:** Validate pushes against Nostr state events
**Tests:**
1. ✅ Get maintainers from announcement
2. ✅ Get maintainers recursively
3. ✅ Handle circular maintainer references
4. ✅ Validate ref update matches state
5. ✅ Validate branch push matches state
6. ✅ Validate tag push matches state
7. ✅ Accept push to refs/nostr/*
8. ✅ Reject push not matching state
9. ✅ Reject push from non-maintainer
**Implementation:**
```rust
// src/git/authorization.rs
pub struct PushValidator {
storage: Storage,
}
impl PushValidator {
pub async fn validate_push(
&self,
npub: &str,
identifier: &str,
updates: &[RefUpdate],
) -> Result<()>;
async fn get_maintainers(&self, npub: &str, identifier: &str) -> Vec<String>;
async fn get_latest_state(&self, npub: &str, identifier: &str) -> Option<StateEvent>;
fn validate_ref_update(&self, state: &StateEvent, update: &RefUpdate) -> Result<()>;
}
```
**Test Example:**
```rust
#[tokio::test]
async fn test_validate_matching_push() {
let storage = test_storage().await;
// Create announcement and state
let announcement = create_announcement("alice", "repo1");
let state = create_state("alice", "repo1")
.branch("main", "a1b2c3d4...");
storage.store_event(announcement).await.unwrap();
storage.store_event(state).await.unwrap();
// Validate matching push
let validator = PushValidator::new(storage);
let update = RefUpdate {
old_oid: "0000...".into(),
new_oid: "a1b2c3d4...".into(),
ref_name: "refs/heads/main".into(),
};
let result = validator.validate_push("alice", "repo1", &[update]).await;
assert!(result.is_ok());
}
#[tokio::test]
async fn test_reject_mismatched_push() {
let storage = test_storage().await;
// State points to commit A
let state = create_state("alice", "repo1")
.branch("main", "aaaa1111...");
storage.store_event(state).await.unwrap();
// Try to push commit B
let validator = PushValidator::new(storage);
let update = RefUpdate {
old_oid: "0000...".into(),
new_oid: "bbbb2222...".into(), // Different!
ref_name: "refs/heads/main".into(),
};
let result = validator.validate_push("alice", "repo1", &[update]).await;
assert!(result.is_err());
assert!(result.unwrap_err().to_string().contains("state"));
}
```
### Phase 4: HTTP Handlers
**Goal:** Serve Git Smart HTTP protocol
**Tests:**
1. ✅ GET /npub/repo.git/info/refs?service=git-upload-pack
2. ✅ POST /npub/repo.git/git-upload-pack (clone/fetch)
3. ✅ POST /npub/repo.git/git-receive-pack (push with auth)
4. ✅ Return 403 for unauthorized push
5. ✅ Return 404 for non-existent repository
6. ✅ Set correct content-type headers
7. ✅ Include CORS headers
**Implementation:**
```rust
// src/git/handler.rs
pub async fn handle_info_refs(
npub: String,
identifier: String,
service: String,
) -> Result<Response>;
pub async fn handle_upload_pack(
npub: String,
identifier: String,
body: Bytes,
) -> Result<Response>;
pub async fn handle_receive_pack(
npub: String,
identifier: String,
body: Bytes,
validator: PushValidator,
) -> Result<Response>;
```
**Test Example:**
```rust
#[tokio::test]
async fn test_info_refs_returns_correct_headers() {
let app = test_app().await;
// Create repository
app.create_repo("alice", "test-repo").await;
// Request info/refs
let response = app.get("/alice-npub/test-repo.git/info/refs?service=git-upload-pack").await;
assert_eq!(response.status(), 200);
assert_eq!(
response.headers().get("content-type").unwrap(),
"application/x-git-upload-pack-advertisement"
);
assert_eq!(
response.headers().get("access-control-allow-origin").unwrap(),
"*"
);
}
#[tokio::test]
async fn test_receive_pack_rejects_unauthorized() {
let app = test_app().await;
// Create repo with state
let (announcement, state) = app.create_repo_with_state()
.branch("main", "aaaa1111...")
.build()
.await;
// Try to push different commit
let body = create_receive_pack_request()
.ref_update("refs/heads/main", "0000...", "bbbb2222...")
.build();
let response = app.post(
&format!("/{}/repo.git/git-receive-pack", announcement.author_npub()),
body
).await;
assert_eq!(response.status(), 403);
}
```
### Phase 5: Integration with Nostr Events
**Goal:** Automatic repository creation and state updates
**Tests:**
1. ✅ Create repository when announcement received
2. ✅ Update HEAD when state event received
3. ✅ Handle announcement updates (maintainers change)
4. ✅ Clean up orphaned refs/nostr/* refs
**Implementation:**
```rust
// src/nostr/events.rs (extend existing)
async fn handle_repository_announcement(event: &Event, storage: &Storage) -> Result<()> {
// Extract npub and identifier
// Create bare repository
// Store event
}
async fn handle_repository_state(event: &Event, storage: &Storage) -> Result<()> {
// Find repository
// Update refs to match state
// Update HEAD
}
```
**Test Example:**
```rust
#[tokio::test]
async fn test_repository_created_on_announcement() {
let app = test_app().await;
let announcement = create_announcement("alice", "test-repo")
.with_clone_tag(app.domain())
.build();
app.send_event(announcement).await.unwrap();
// Wait for async processing
tokio::time::sleep(Duration::from_millis(100)).await;
// Verify repository exists
let repo_path = app.git_data_path()
.join("alice-npub")
.join("test-repo.git");
assert!(repo_path.exists());
assert!(repo_path.join("HEAD").exists());
}
```
### Phase 6: End-to-End Testing
**Goal:** Test with real Git client
**Tests:**
1. ✅ Clone repository with git client
2. ✅ Fetch from repository
3. ✅ Push to repository (authorized)
4. ✅ Push rejected (unauthorized)
5. ✅ Multiple concurrent operations
**Implementation:**
```rust
// tests/e2e/git_client.rs
#[tokio::test]
async fn test_real_git_clone() {
let app = test_app().await;
// Setup repository with content
let (announcement, _) = app.create_repo_with_commits()
.commit("Initial", "README.md", "# Test")
.build()
.await;
// Clone with real git
let temp = TempDir::new().unwrap();
let url = format!(
"http://{}/{}/{}.git",
app.domain(),
announcement.author_npub(),
announcement.identifier()
);
let output = Command::new("git")
.args(&["clone", &url])
.current_dir(&temp)
.output()
.await
.unwrap();
assert!(output.status.success());
let cloned_path = temp.path().join(announcement.identifier());
assert!(cloned_path.exists());
assert!(cloned_path.join("README.md").exists());
}
```
---
## Testing Strategy
### Unit Tests (40%)
- Git repository operations (git2)
- Protocol parsing
- Authorization logic
- Pure functions, no I/O
### Integration Tests (30%)
- HTTP handlers with test server
- Repository + Nostr event interaction
- Multi-maintainer flows
- State validation
### Compliance Tests (20%)
- GRASP-01 Git requirements
- Use grasp-audit library
- Spec-driven assertions
### E2E Tests (10%)
- Real git client operations
- End-to-end workflows
- Performance testing
---
## Implementation Order
### Week 1: Foundation
1. Add git2 dependency
2. Implement GitRepository (Phase 1)
3. Write unit tests for repository operations
4. Test repository creation from announcements
### Week 2: Protocol & Authorization
1. Implement protocol parsing (Phase 2)
2. Implement authorization logic (Phase 3)
3. Write unit tests for both
4. Integration tests for validation
### Week 3: HTTP & Integration
1. Implement HTTP handlers (Phase 4)
2. Integrate with Nostr events (Phase 5)
3. Integration tests for full flow
4. CORS and error handling
### Week 4: E2E & Polish
1. E2E tests with real git (Phase 6)
2. Performance testing
3. GRASP-01 compliance testing
4. Documentation and examples
---
## Success Criteria
### Functional
- ✅ Clone repository via HTTP
- ✅ Push authorized commits
- ✅ Reject unauthorized pushes
- ✅ Support multi-maintainer
- ✅ Support refs/nostr/* for PRs
### Quality
- ✅ >80% unit test coverage
- ✅ All integration tests pass
- ✅ GRASP-01 compliance 100%
- ✅ E2E tests with real git
### Performance
- ✅ Clone 1MB repo < 1s
- ✅ Push validation < 100ms
- ✅ 100 concurrent ops without errors
---
## Next Steps
1. **Review this plan** - Does the hybrid approach make sense?
2. **Start Phase 1** - Add git2, implement GitRepository
3. **Write first test** - test_init_bare_repository
4. **Iterate with TDD** - Red → Green → Refactor
---
## Questions for Review
1. **Hybrid approach?** git2 + system git + HTTP layer - good balance?
2. **git-http-backend crate?** Worth using or implement minimal HTTP layer?
3. **Authorization granularity?** Validate per-ref or entire push?
4. **Error messages?** How detailed for push rejections?
5. **Testing scope?** Is 6 phases reasonable for first iteration?
---
**Ready to proceed?** Let me know if this plan looks good, or if you'd like to adjust the approach!
|