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
//! Configuration management for hyperforge
//!
//! This module handles `.hyperforge/config.toml` files which store
//! per-repository forge configuration.

use crate::types::{Forge, Visibility};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fs;
use std::path::{Path, PathBuf};
use thiserror::Error;

/// Configuration directory name
pub const CONFIG_DIR: &str = ".hyperforge";

/// Configuration file name
pub const CONFIG_FILE: &str = "config.toml";

/// Errors that can occur during config operations
#[derive(Debug, Error)]
pub enum ConfigError {
    #[error("Config not found at {path}")]
    NotFound { path: PathBuf },

    #[error("Failed to read config: {0}")]
    ReadError(#[from] std::io::Error),

    #[error("Failed to parse config: {0}")]
    ParseError(#[from] toml::de::Error),

    #[error("Failed to serialize config: {0}")]
    SerializeError(#[from] toml::ser::Error),

    #[error("Config already exists at {path}")]
    AlreadyExists { path: PathBuf },

    #[error("Invalid config: {message}")]
    Invalid { message: String },
}

pub type ConfigResult<T> = Result<T, ConfigError>;

/// Per-forge configuration overrides
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ForgeConfig {
    /// Override organization for this forge
    #[serde(skip_serializing_if = "Option::is_none")]
    pub org: Option<String>,

    /// Git remote name for this forge
    #[serde(skip_serializing_if = "Option::is_none")]
    pub remote: Option<String>,
}

/// Repository configuration (.hyperforge/config.toml)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HyperforgeConfig {
    /// Repository name (inferred from directory if not specified)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub repo_name: Option<String>,

    /// Default organization/user name
    #[serde(skip_serializing_if = "Option::is_none")]
    pub org: Option<String>,

    /// List of forges to sync to
    #[serde(default)]
    pub forges: Vec<String>,

    /// Repository visibility
    #[serde(default)]
    pub visibility: Visibility,

    /// Repository description
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,

    /// SSH key paths per forge
    #[serde(default, skip_serializing_if = "HashMap::is_empty")]
    pub ssh: HashMap<String, String>,

    /// Per-forge configuration overrides
    #[serde(default, rename = "forge", skip_serializing_if = "HashMap::is_empty")]
    pub forge_config: HashMap<String, ForgeConfig>,
}

impl Default for HyperforgeConfig {
    fn default() -> Self {
        Self {
            repo_name: None,
            org: None,
            forges: vec!["github".to_string()],
            visibility: Visibility::Public,
            description: None,
            ssh: HashMap::new(),
            forge_config: HashMap::new(),
        }
    }
}

impl HyperforgeConfig {
    /// Create a new config with specified forges
    pub fn new(forges: Vec<String>) -> Self {
        Self {
            forges,
            ..Default::default()
        }
    }

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

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

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

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

    /// Builder method: add SSH key for a forge
    pub fn with_ssh_key(mut self, forge: impl Into<String>, key_path: impl Into<String>) -> Self {
        self.ssh.insert(forge.into(), key_path.into());
        self
    }

    /// Get the config directory path for a repo
    pub fn config_dir(repo_path: &Path) -> PathBuf {
        repo_path.join(CONFIG_DIR)
    }

    /// Get the config file path for a repo
    pub fn config_path(repo_path: &Path) -> PathBuf {
        Self::config_dir(repo_path).join(CONFIG_FILE)
    }

    /// Check if a hyperforge config exists at the given path
    pub fn exists(repo_path: &Path) -> bool {
        Self::config_path(repo_path).exists()
    }

    /// Load config from .hyperforge/config.toml in the given repo
    pub fn load(repo_path: &Path) -> ConfigResult<Self> {
        let config_path = Self::config_path(repo_path);

        if !config_path.exists() {
            return Err(ConfigError::NotFound { path: config_path });
        }

        let content = fs::read_to_string(&config_path)?;
        let config: Self = toml::from_str(&content)?;

        Ok(config)
    }

    /// Save config to .hyperforge/config.toml in the given repo
    pub fn save(&self, repo_path: &Path) -> ConfigResult<()> {
        let config_dir = Self::config_dir(repo_path);
        let config_path = Self::config_path(repo_path);

        // Create .hyperforge directory if it doesn't exist
        fs::create_dir_all(&config_dir)?;

        let content = toml::to_string_pretty(self)?;
        fs::write(&config_path, content)?;

        Ok(())
    }

    /// Get the effective org for a forge (checks forge-specific override first)
    pub fn org_for_forge(&self, forge: &str) -> Option<&str> {
        // Check forge-specific override first
        if let Some(forge_config) = self.forge_config.get(forge) {
            if let Some(ref org) = forge_config.org {
                return Some(org);
            }
        }

        // Fall back to default org
        self.org.as_deref()
    }

    /// Get the remote name for a forge
    pub fn remote_for_forge(&self, forge: &str) -> String {
        // Check forge-specific override
        if let Some(forge_config) = self.forge_config.get(forge) {
            if let Some(ref remote) = forge_config.remote {
                return remote.clone();
            }
        }

        // Default: first forge is "origin", others use forge name
        if self.forges.first().map(|f| f.as_str()) == Some(forge) {
            "origin".to_string()
        } else {
            forge.to_string()
        }
    }

    /// Get SSH key path for a forge
    pub fn ssh_key_for_forge(&self, forge: &str) -> Option<&str> {
        self.ssh.get(forge).map(|s| s.as_str())
    }

    /// Get the repo name (explicit or from path)
    pub fn get_repo_name(&self, repo_path: &Path) -> String {
        self.repo_name
            .clone()
            .or_else(|| {
                repo_path
                    .file_name()
                    .and_then(|n| n.to_str())
                    .map(|s| s.to_string())
            })
            .unwrap_or_else(|| "unknown".to_string())
    }

    /// Parse forge string to Forge enum
    pub fn parse_forge(forge: &str) -> Option<Forge> {
        match forge.to_lowercase().as_str() {
            "github" => Some(Forge::GitHub),
            "codeberg" => Some(Forge::Codeberg),
            "gitlab" => Some(Forge::GitLab),
            _ => None,
        }
    }

    /// Validate the config
    pub fn validate(&self) -> ConfigResult<()> {
        if self.forges.is_empty() {
            return Err(ConfigError::Invalid {
                message: "At least one forge must be specified".to_string(),
            });
        }

        // Validate forge names
        for forge in &self.forges {
            if Self::parse_forge(forge).is_none() {
                return Err(ConfigError::Invalid {
                    message: format!(
                        "Unknown forge: {}. Valid forges: github, codeberg, gitlab",
                        forge
                    ),
                });
            }
        }

        Ok(())
    }
}

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

    #[test]
    fn test_config_default() {
        let config = HyperforgeConfig::default();
        assert_eq!(config.forges, vec!["github"]);
        assert_eq!(config.visibility, Visibility::Public);
    }

    #[test]
    fn test_config_builder() {
        let config = HyperforgeConfig::new(vec!["github".to_string(), "codeberg".to_string()])
            .with_org("alice")
            .with_repo_name("my-tool")
            .with_visibility(Visibility::Private)
            .with_ssh_key("github", "~/.ssh/github_key");

        assert_eq!(config.forges, vec!["github", "codeberg"]);
        assert_eq!(config.org, Some("alice".to_string()));
        assert_eq!(config.repo_name, Some("my-tool".to_string()));
        assert_eq!(config.visibility, Visibility::Private);
        assert_eq!(
            config.ssh.get("github"),
            Some(&"~/.ssh/github_key".to_string())
        );
    }

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

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

        config.save(temp.path()).unwrap();

        // Verify file exists
        assert!(HyperforgeConfig::exists(temp.path()));

        // Load it back
        let loaded = HyperforgeConfig::load(temp.path()).unwrap();
        assert_eq!(loaded.org, Some("alice".to_string()));
        assert_eq!(loaded.repo_name, Some("test-repo".to_string()));
        assert_eq!(loaded.forges, vec!["github"]);
    }

    #[test]
    fn test_config_not_found() {
        let temp = TempDir::new().unwrap();
        let result = HyperforgeConfig::load(temp.path());
        assert!(matches!(result, Err(ConfigError::NotFound { .. })));
    }

    #[test]
    fn test_org_for_forge_default() {
        let config = HyperforgeConfig::new(vec!["github".to_string()]).with_org("default-org");

        assert_eq!(config.org_for_forge("github"), Some("default-org"));
        assert_eq!(config.org_for_forge("codeberg"), Some("default-org"));
    }

    #[test]
    fn test_org_for_forge_override() {
        let mut config =
            HyperforgeConfig::new(vec!["github".to_string(), "codeberg".to_string()])
                .with_org("default-org");

        config.forge_config.insert(
            "codeberg".to_string(),
            ForgeConfig {
                org: Some("codeberg-org".to_string()),
                remote: None,
            },
        );

        assert_eq!(config.org_for_forge("github"), Some("default-org"));
        assert_eq!(config.org_for_forge("codeberg"), Some("codeberg-org"));
    }

    #[test]
    fn test_remote_for_forge() {
        let config =
            HyperforgeConfig::new(vec!["github".to_string(), "codeberg".to_string()]);

        // First forge is "origin"
        assert_eq!(config.remote_for_forge("github"), "origin");
        // Others use forge name
        assert_eq!(config.remote_for_forge("codeberg"), "codeberg");
    }

    #[test]
    fn test_get_repo_name_explicit() {
        let config = HyperforgeConfig::default().with_repo_name("explicit-name");
        let temp = TempDir::new().unwrap();

        assert_eq!(config.get_repo_name(temp.path()), "explicit-name");
    }

    #[test]
    fn test_get_repo_name_from_path() {
        let config = HyperforgeConfig::default();
        let path = Path::new("/home/user/projects/my-project");

        assert_eq!(config.get_repo_name(path), "my-project");
    }

    #[test]
    fn test_validate_empty_forges() {
        let config = HyperforgeConfig {
            forges: vec![],
            ..Default::default()
        };

        let result = config.validate();
        assert!(matches!(result, Err(ConfigError::Invalid { .. })));
    }

    #[test]
    fn test_validate_unknown_forge() {
        let config = HyperforgeConfig::new(vec!["unknown-forge".to_string()]);

        let result = config.validate();
        assert!(matches!(result, Err(ConfigError::Invalid { .. })));
    }

    #[test]
    fn test_validate_valid() {
        let config =
            HyperforgeConfig::new(vec!["github".to_string(), "codeberg".to_string()]);

        config.validate().unwrap();
    }

    #[test]
    fn test_toml_roundtrip() {
        let mut config =
            HyperforgeConfig::new(vec!["github".to_string(), "codeberg".to_string()])
                .with_org("alice")
                .with_repo_name("my-tool")
                .with_visibility(Visibility::Private)
                .with_description("A cool tool")
                .with_ssh_key("github", "~/.ssh/github_key");

        config.forge_config.insert(
            "codeberg".to_string(),
            ForgeConfig {
                org: Some("different-org".to_string()),
                remote: Some("cb".to_string()),
            },
        );

        let toml_str = toml::to_string_pretty(&config).unwrap();
        let parsed: HyperforgeConfig = toml::from_str(&toml_str).unwrap();

        assert_eq!(parsed.org, config.org);
        assert_eq!(parsed.repo_name, config.repo_name);
        assert_eq!(parsed.forges, config.forges);
        assert_eq!(parsed.visibility, config.visibility);
    }
}