hyperforge 3.3.0

Multi-forge repository management
Documentation
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
# LFORGE2-20: Remote Import Commands

**Status**: Planning
**Blocked by**: LFORGE2-2, LFORGE2-3, LFORGE2-16 (forge API)
**Unlocks**: Migration from existing forges

---

## Goal

Import existing repositories from forges (GitHub, Codeberg, GitLab) into hyperforge management.

---

## Commands

### `hyperforge remote <forge> list`

List all repositories for an org on a forge.

```bash
hyperforge remote github list --org alice

# Output:
# alice/repo1 (public)
# alice/repo2 (private)
# alice/archive-old (public, archived)
```

**Implementation**:
- Request token: scope `git/github/alice/*/token` (or more specific)
- Call forge API: `GET /orgs/alice/repos` or `GET /users/alice/repos`
- Display repo names, visibility, archived status

### `hyperforge remote <forge> pull`

Import repos from forge into hyperforge management.

```bash
# Import specific repos
hyperforge remote github pull \
  --org alice \
  --repo "^alice-tool-.*" \
  --repo "alice-lib" \
  --exclude ".*-archive$" \
  --dry-run

# Import all repos
hyperforge remote github pull \
  --org alice \
  --all

# Import to specific directory
hyperforge remote github pull \
  --org alice \
  --repo "alice-tool" \
  --target ~/projects/tools
```

**What it does**:
1. List repos on forge matching criteria
2. For each repo:
   - Clone to local directory (or use existing if present)
   - Create `.hyperforge/config.toml` in repo
   - Configure git remotes based on workspace defaults
   - Set git config for SSH keys
3. Show summary of imported repos

---

## Directory-Level Defaults

When importing, check for workspace-level defaults:

```
~/projects/alice/
  ├── .hyperforge/
  │   └── defaults.toml    # Default config for all repos in this workspace
  ├── repo1/
  │   └── .hyperforge/
  │       └── config.toml  # Inherits from ../defaults.toml
  └── repo2/
      └── .hyperforge/
          └── config.toml
```

**defaults.toml**:
```toml
# Defaults for all repos in this workspace
forges = ["github", "codeberg"]
org = "alice"

[ssh]
github = "~/.ssh/alice_github"
codeberg = "~/.ssh/alice_codeberg"

[defaults]
visibility = "public"
```

**Inheritance**:
- Repo `config.toml` inherits from parent `defaults.toml`
- Repo can override any field
- Multiple levels: `/projects/.hyperforge/defaults.toml``/projects/alice/.hyperforge/defaults.toml``repo/.hyperforge/config.toml`

---

## Implementation Details

### 1. List Command

```rust
// src/remote/list.rs

pub async fn list_repos(
    forge: &Forge,
    org: &str,
    auth: &dyn AuthProvider,
) -> Result<Vec<RemoteRepo>> {
    // 1. Request token
    let scope = format!("git/{}/{}/*/token", forge, org);
    let token = auth.get_secret_lazy(&scope).await?;

    // 2. Call forge API
    let repos = match forge {
        Forge::GitHub => {
            github_api::list_repos(org, &token).await?
        }
        Forge::Codeberg => {
            codeberg_api::list_repos(org, &token).await?
        }
        _ => todo!()
    };

    Ok(repos)
}

pub struct RemoteRepo {
    pub name: String,
    pub full_name: String,  // "alice/repo1"
    pub visibility: Visibility,
    pub archived: bool,
    pub clone_url: String,
}
```

### 2. Pull Command

```rust
// src/remote/pull.rs

pub struct PullOptions {
    pub org: String,
    pub forge: Forge,
    pub include_patterns: Vec<Regex>,
    pub exclude_patterns: Vec<Regex>,
    pub target_dir: Option<PathBuf>,
    pub dry_run: bool,
}

pub async fn pull_repos(
    options: PullOptions,
    auth: &dyn AuthProvider,
) -> Result<PullResult> {
    // 1. List all repos on forge
    let all_repos = list_repos(&options.forge, &options.org, auth).await?;

    // 2. Filter by patterns
    let filtered = filter_repos(all_repos, &options)?;

    if options.dry_run {
        return Ok(PullResult::DryRun(filtered));
    }

    // 3. Load workspace defaults (if they exist)
    let defaults = load_workspace_defaults(&options.target_dir)?;

    // 4. For each repo
    let mut results = Vec::new();
    for repo in filtered {
        let result = import_repo(&repo, &defaults, &options, auth).await;
        results.push(result);
    }

    Ok(PullResult::Imported(results))
}

async fn import_repo(
    repo: &RemoteRepo,
    defaults: &Option<WorkspaceDefaults>,
    options: &PullOptions,
    auth: &dyn AuthProvider,
) -> Result<ImportedRepo> {
    let target = options.target_dir
        .clone()
        .unwrap_or_else(|| PathBuf::from("."));

    let repo_path = target.join(&repo.name);

    // 1. Clone repo if doesn't exist
    if !repo_path.exists() {
        git::clone(&repo.clone_url, &repo_path).await?;
    }

    // 2. Create .hyperforge/config.toml
    let config = build_config_from_defaults(repo, defaults)?;
    config.save(&repo_path.join(".hyperforge/config.toml"))?;

    // 3. Sync git remotes to match config
    git::sync_remotes(&repo_path, &config).await?;

    // 4. Set git config for SSH
    git::configure_ssh(&repo_path, &config).await?;

    Ok(ImportedRepo {
        name: repo.name.clone(),
        path: repo_path,
    })
}
```

### 3. Workspace Defaults Discovery

```rust
// src/config/defaults.rs

pub fn load_workspace_defaults(path: &Path) -> Result<Option<WorkspaceDefaults>> {
    // Walk up directory tree looking for .hyperforge/defaults.toml
    let mut current = path.to_path_buf();

    loop {
        let defaults_path = current.join(".hyperforge/defaults.toml");

        if defaults_path.exists() {
            let content = fs::read_to_string(&defaults_path)?;
            let defaults: WorkspaceDefaults = toml::from_str(&content)?;
            return Ok(Some(defaults));
        }

        if !current.pop() {
            break;  // Reached root
        }
    }

    Ok(None)  // No defaults found
}

pub fn build_config_from_defaults(
    repo: &RemoteRepo,
    defaults: &Option<WorkspaceDefaults>,
) -> Result<HyperforgeConfig> {
    let mut config = HyperforgeConfig::default();

    // Apply defaults if present
    if let Some(d) = defaults {
        config.forges = d.forges.clone();
        config.org = Some(d.org.clone());
        config.ssh = d.ssh.clone();
        config.visibility = d.defaults.visibility.clone();
    }

    // Set repo-specific fields
    config.repo_name = repo.name.clone();
    config.visibility = repo.visibility.clone();

    Ok(config)
}
```

### 4. Pattern Matching

```rust
fn filter_repos(
    repos: Vec<RemoteRepo>,
    options: &PullOptions,
) -> Result<Vec<RemoteRepo>> {
    repos.into_iter()
        .filter(|repo| {
            // Must match at least one include pattern
            let included = options.include_patterns.is_empty()
                || options.include_patterns.iter().any(|p| p.is_match(&repo.name));

            // Must not match any exclude pattern
            let excluded = options.exclude_patterns.iter().any(|p| p.is_match(&repo.name));

            included && !excluded
        })
        .collect()
}
```

---

## Usage Examples

### Example 1: Import all repos for an org

```bash
cd ~/projects

# Create workspace defaults
mkdir -p .hyperforge
cat > .hyperforge/defaults.toml <<EOF
forges = ["github", "codeberg"]
org = "alice"

[ssh]
github = "~/.ssh/alice_github"
codeberg = "~/.ssh/alice_codeberg"
EOF

# Import all repos from GitHub
hyperforge remote github pull --org alice --all

# Result:
# ~/projects/
#   ├── .hyperforge/defaults.toml
#   ├── alice-tool-1/.hyperforge/config.toml
#   ├── alice-tool-2/.hyperforge/config.toml
#   └── alice-lib/.hyperforge/config.toml
```

### Example 2: Import specific repos with patterns

```bash
# Import only tools, exclude archives
hyperforge remote github pull \
  --org alice \
  --repo "^tool-.*" \
  --exclude ".*-archive$" \
  --dry-run

# Dry run output:
# Would import:
#   - tool-cli
#   - tool-server
# Would skip:
#   - lib-core (doesn't match pattern)
#   - tool-old-archive (excluded)

# Actually import
hyperforge remote github pull \
  --org alice \
  --repo "^tool-.*" \
  --exclude ".*-archive$"
```

### Example 3: Import from multiple forges

```bash
# Import from GitHub
hyperforge remote github pull --org alice --all

# Import from Codeberg (merges with existing)
hyperforge remote codeberg pull --org alice --all

# Result: repos have both github and codeberg remotes
```

### Example 4: Import without defaults (prompted to create)

```bash
cd ~/new-workspace
hyperforge remote github pull --org bob --all

# Output:
# ✗ No workspace defaults found
#
# Create .hyperforge/defaults.toml? [Y/n]: y
#
# Enter default forges (comma-separated): github,codeberg
# Enter SSH key for github: ~/.ssh/bob_github
# Enter SSH key for codeberg: ~/.ssh/bob_codeberg
#
# Created .hyperforge/defaults.toml
#
# Importing repos...
```

---

## Acceptance Criteria

- `hyperforge remote github list --org alice` shows all repos
-`hyperforge remote github pull --org alice --all` imports all repos
- ✅ Pattern matching works (--repo, --exclude)
- ✅ Dry run shows what would be imported
- ✅ Workspace defaults are discovered and applied
- ✅ Prompts to create defaults if not found
- ✅ Config inheritance: workspace defaults → repo config
- ✅ Git remotes synced to match config
- ✅ SSH keys configured via git config

---

## Testing

### Unit Tests
- Pattern matching logic
- Config inheritance
- Defaults discovery (walk up tree)

### Integration Tests
- Mock forge API
- Import repos
- Verify .hyperforge/config.toml created
- Verify git remotes configured

### Manual Tests
- Real GitHub/Codeberg import
- Verify workspace defaults applied
- Verify SSH keys work

---

## Next Steps

This enables migration from existing forges:
1. Create workspace with defaults
2. Import repos from GitHub
3. Repos now managed by hyperforge
4. Can push to multiple forges via `workspace push`