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
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
//! Init command - Initialize hyperforge for a repository
//!
//! `hyperforge init --path . --forges github,codeberg`
//!
//! This command:
//! 1. Creates a git repo if needed
//! 2. Creates .hyperforge/config.toml
//! 3. Configures git remotes for each forge
//! 4. Sets up SSH keys if specified

use std::path::Path;
use thiserror::Error;

use crate::config::HyperforgeConfig;
use crate::git::{self, Git, GitError};
use crate::types::Visibility;

/// Errors that can occur during init
#[derive(Debug, Error)]
pub enum InitError {
    #[error("Config already exists at {path}. Use --force to reinitialize.")]
    AlreadyExists { path: String },

    #[error("Git error: {0}")]
    GitError(#[from] GitError),

    #[error("Config error: {0}")]
    ConfigError(#[from] crate::config::ConfigError),

    #[error("Invalid forge: {forge}. Valid forges: github, codeberg, gitlab")]
    InvalidForge { forge: String },

    #[error("Organization required. Use --org to specify.")]
    OrgRequired,

    #[error("I/O error: {0}")]
    IoError(#[from] std::io::Error),
}

pub type InitResult<T> = Result<T, InitError>;

/// Options for the init command
#[derive(Debug, Clone)]
pub struct InitOptions {
    /// Forges to configure (e.g., ["github", "codeberg"])
    pub forges: Vec<String>,

    /// Organization/username on forges
    pub org: Option<String>,

    /// Repository name (defaults to directory name)
    pub repo_name: Option<String>,

    /// Repository visibility
    pub visibility: Visibility,

    /// Repository description
    pub description: Option<String>,

    /// SSH key paths per forge
    pub ssh_keys: Vec<(String, String)>,

    /// Force reinitialize even if config exists
    pub force: bool,

    /// Dry run - don't actually make changes
    pub dry_run: bool,
}

impl Default for InitOptions {
    fn default() -> Self {
        Self {
            forges: vec!["github".to_string()],
            org: None,
            repo_name: None,
            visibility: Visibility::Public,
            description: None,
            ssh_keys: Vec::new(),
            force: false,
            dry_run: false,
        }
    }
}

impl InitOptions {
    pub fn new(forges: Vec<String>) -> Self {
        Self {
            forges,
            ..Default::default()
        }
    }

    pub fn with_org(mut self, org: impl Into<String>) -> Self {
        self.org = Some(org.into());
        self
    }

    pub fn with_repo_name(mut self, name: impl Into<String>) -> Self {
        self.repo_name = Some(name.into());
        self
    }

    pub fn with_visibility(mut self, visibility: Visibility) -> Self {
        self.visibility = visibility;
        self
    }

    pub fn with_description(mut self, description: impl Into<String>) -> Self {
        self.description = Some(description.into());
        self
    }

    pub fn with_ssh_key(mut self, forge: impl Into<String>, key_path: impl Into<String>) -> Self {
        self.ssh_keys.push((forge.into(), key_path.into()));
        self
    }

    pub fn force(mut self) -> Self {
        self.force = true;
        self
    }

    pub fn dry_run(mut self) -> Self {
        self.dry_run = true;
        self
    }
}

/// Result of init operation
#[derive(Debug)]
pub struct InitReport {
    /// Path to the repository
    pub repo_path: String,

    /// Whether git was initialized
    pub git_initialized: bool,

    /// Config that was created
    pub config: HyperforgeConfig,

    /// Remotes that were added
    pub remotes_added: Vec<RemoteAdded>,

    /// Whether this was a dry run
    pub dry_run: bool,
}

#[derive(Debug, Clone)]
pub struct RemoteAdded {
    pub name: String,
    pub url: String,
    pub forge: String,
}

/// Initialize hyperforge for a repository
///
/// # Arguments
/// * `path` - Path to the repository
/// * `options` - Init options
///
/// # Returns
/// InitReport describing what was done
pub fn init(path: &Path, options: InitOptions) -> InitResult<InitReport> {
    // Validate forges
    for forge in &options.forges {
        if HyperforgeConfig::parse_forge(forge).is_none() {
            return Err(InitError::InvalidForge {
                forge: forge.clone(),
            });
        }
    }

    // Check if already initialized
    if HyperforgeConfig::exists(path) && !options.force {
        return Err(InitError::AlreadyExists {
            path: HyperforgeConfig::config_path(path).display().to_string(),
        });
    }

    let mut report = InitReport {
        repo_path: path.display().to_string(),
        git_initialized: false,
        config: HyperforgeConfig::default(),
        remotes_added: Vec::new(),
        dry_run: options.dry_run,
    };

    // Initialize git if needed
    if !Git::is_repo(path) {
        if !options.dry_run {
            Git::init(path)?;
        }
        report.git_initialized = true;
    }

    // Build config
    let mut config = HyperforgeConfig::new(options.forges.clone());

    if let Some(ref org) = options.org {
        config = config.with_org(org);
    }

    if let Some(ref name) = options.repo_name {
        config = config.with_repo_name(name);
    }

    config = config.with_visibility(options.visibility.clone());

    if let Some(ref desc) = options.description {
        config = config.with_description(desc);
    }

    for (forge, key_path) in &options.ssh_keys {
        config = config.with_ssh_key(forge, key_path);
    }

    // Validate config
    config.validate()?;

    // Get org (required for remote setup)
    let org = options.org.as_deref().or(config.org.as_deref());

    // Configure git remotes
    if let Some(org) = org {
        let repo_name = config.get_repo_name(path);

        for forge in &options.forges {
            let remote_name = config.remote_for_forge(forge);
            let remote_url = git::build_remote_url(forge, org, &repo_name);

            if !options.dry_run {
                // Check if remote already exists
                match Git::get_remote(path, &remote_name) {
                    Ok(existing) => {
                        // Remote exists - update URL if different
                        if existing.fetch_url != remote_url {
                            Git::set_remote_url(path, &remote_name, &remote_url)?;
                        }
                    }
                    Err(GitError::RemoteNotFound { .. }) => {
                        // Add new remote
                        Git::add_remote(path, &remote_name, &remote_url)?;
                    }
                    Err(e) => return Err(e.into()),
                }

                // Configure SSH key if specified
                if let Some(key_path) = config.ssh_key_for_forge(forge) {
                    Git::configure_ssh(path, key_path)?;
                }
            }

            report.remotes_added.push(RemoteAdded {
                name: remote_name,
                url: remote_url,
                forge: forge.clone(),
            });
        }
    }

    // Save config
    if !options.dry_run {
        config.save(path)?;
    }

    report.config = config;
    Ok(report)
}

#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::TempDir;

    #[test]
    fn test_init_new_repo() {
        let temp = TempDir::new().unwrap();

        let options = InitOptions::new(vec!["github".to_string()])
            .with_org("alice")
            .with_repo_name("test-repo");

        let report = init(temp.path(), options).unwrap();

        assert!(report.git_initialized);
        assert!(HyperforgeConfig::exists(temp.path()));
        assert_eq!(report.remotes_added.len(), 1);
        assert_eq!(report.remotes_added[0].name, "origin");
        assert_eq!(
            report.remotes_added[0].url,
            "git@github.com:alice/test-repo.git"
        );
    }

    #[test]
    fn test_init_existing_git_repo() {
        let temp = TempDir::new().unwrap();

        // Pre-initialize git
        Git::init(temp.path()).unwrap();

        let options = InitOptions::new(vec!["github".to_string()])
            .with_org("alice");

        let report = init(temp.path(), options).unwrap();

        assert!(!report.git_initialized); // Already existed
        assert!(HyperforgeConfig::exists(temp.path()));
    }

    #[test]
    fn test_init_multiple_forges() {
        let temp = TempDir::new().unwrap();

        let options = InitOptions::new(vec!["github".to_string(), "codeberg".to_string()])
            .with_org("alice")
            .with_repo_name("multi-forge");

        let report = init(temp.path(), options).unwrap();

        assert_eq!(report.remotes_added.len(), 2);

        let github = report.remotes_added.iter().find(|r| r.forge == "github").unwrap();
        let codeberg = report.remotes_added.iter().find(|r| r.forge == "codeberg").unwrap();

        assert_eq!(github.name, "origin"); // First forge is origin
        assert_eq!(codeberg.name, "codeberg");

        // Verify remotes in git
        let remotes = Git::list_remotes(temp.path()).unwrap();
        assert_eq!(remotes.len(), 2);
    }

    #[test]
    fn test_init_already_exists() {
        let temp = TempDir::new().unwrap();

        let options = InitOptions::new(vec!["github".to_string()])
            .with_org("alice");

        // First init
        init(temp.path(), options.clone()).unwrap();

        // Second init should fail
        let result = init(temp.path(), options);
        assert!(matches!(result, Err(InitError::AlreadyExists { .. })));
    }

    #[test]
    fn test_init_force_reinit() {
        let temp = TempDir::new().unwrap();

        let options1 = InitOptions::new(vec!["github".to_string()])
            .with_org("alice");

        init(temp.path(), options1).unwrap();

        // Force reinit with different forges
        let options2 = InitOptions::new(vec!["codeberg".to_string()])
            .with_org("alice")
            .force();

        let report = init(temp.path(), options2).unwrap();

        // Should have codeberg config now
        assert_eq!(report.config.forges, vec!["codeberg"]);
    }

    #[test]
    fn test_init_dry_run() {
        let temp = TempDir::new().unwrap();

        let options = InitOptions::new(vec!["github".to_string()])
            .with_org("alice")
            .dry_run();

        let report = init(temp.path(), options).unwrap();

        assert!(report.dry_run);
        // Git should NOT be initialized
        assert!(!Git::is_repo(temp.path()));
        // Config should NOT exist
        assert!(!HyperforgeConfig::exists(temp.path()));
        // But report should show what would be done
        assert!(report.git_initialized);
        assert_eq!(report.remotes_added.len(), 1);
    }

    #[test]
    fn test_init_invalid_forge() {
        let temp = TempDir::new().unwrap();

        let options = InitOptions::new(vec!["invalid-forge".to_string()])
            .with_org("alice");

        let result = init(temp.path(), options);
        assert!(matches!(result, Err(InitError::InvalidForge { .. })));
    }

    #[test]
    fn test_init_repo_name_from_path() {
        let temp = TempDir::new().unwrap();

        // Don't specify repo_name, should use directory name
        let options = InitOptions::new(vec!["github".to_string()])
            .with_org("alice");

        let report = init(temp.path(), options).unwrap();

        // Remote URL should use temp directory name
        let dir_name = temp.path().file_name().unwrap().to_str().unwrap();
        assert!(report.remotes_added[0].url.contains(dir_name));
    }

    #[test]
    fn test_init_with_ssh_key() {
        let temp = TempDir::new().unwrap();

        let options = InitOptions::new(vec!["github".to_string()])
            .with_org("alice")
            .with_ssh_key("github", "~/.ssh/github_key");

        init(temp.path(), options).unwrap();

        // Verify SSH command was set
        let ssh_cmd = Git::config_get(temp.path(), "core.sshCommand").unwrap();
        assert!(ssh_cmd.is_some());
        assert!(ssh_cmd.unwrap().contains("github_key"));
    }

    #[test]
    fn test_init_updates_existing_remote() {
        let temp = TempDir::new().unwrap();

        // Pre-setup: init git and add a remote with different URL
        Git::init(temp.path()).unwrap();
        Git::add_remote(temp.path(), "origin", "git@github.com:old/url.git").unwrap();

        // Init hyperforge - should update the remote URL
        let options = InitOptions::new(vec!["github".to_string()])
            .with_org("alice")
            .with_repo_name("new-repo");

        init(temp.path(), options).unwrap();

        // Verify remote was updated
        let remote = Git::get_remote(temp.path(), "origin").unwrap();
        assert_eq!(remote.fetch_url, "git@github.com:alice/new-repo.git");
    }
}