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
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
//! Status command - Show repository sync status
//!
//! `hyperforge status --path .`
//!
//! This command shows:
//! - Current branch and tracking info
//! - Sync status for each configured forge (ahead/behind)
//! - Working tree status (clean/dirty)

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

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

/// Errors that can occur during status
#[derive(Debug, Error)]
pub enum StatusError {
    #[error("Not a hyperforge repository. Run 'hyperforge init' first.")]
    NotInitialized,

    #[error("Not a git repository: {path}")]
    NotAGitRepo { path: String },

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

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

pub type StatusResult<T> = Result<T, StatusError>;

/// Status of a single forge remote
#[derive(Debug, Clone)]
pub struct ForgeStatus {
    /// Forge name (e.g., "github")
    pub forge: String,

    /// Git remote name (e.g., "origin")
    pub remote_name: String,

    /// Remote URL
    pub remote_url: Option<String>,

    /// Number of commits ahead of remote
    pub ahead: u32,

    /// Number of commits behind remote
    pub behind: u32,

    /// Whether the remote exists in git
    pub remote_exists: bool,

    /// Any error message
    pub error: Option<String>,
}

impl ForgeStatus {
    /// Check if this forge is up to date
    pub fn is_up_to_date(&self) -> bool {
        self.remote_exists && self.ahead == 0 && self.behind == 0 && self.error.is_none()
    }

    /// Check if needs push (ahead of remote)
    pub fn needs_push(&self) -> bool {
        self.ahead > 0
    }

    /// Check if needs pull (behind remote)
    pub fn needs_pull(&self) -> bool {
        self.behind > 0
    }

    /// Get a status symbol
    pub fn symbol(&self) -> &'static str {
        if self.error.is_some() {
            ""
        } else if !self.remote_exists {
            "?"
        } else if self.is_up_to_date() {
            ""
        } else if self.ahead > 0 && self.behind > 0 {
            ""
        } else if self.ahead > 0 {
            ""
        } else if self.behind > 0 {
            ""
        } else {
            ""
        }
    }

    /// Get a human-readable status message
    pub fn message(&self) -> String {
        if let Some(ref err) = self.error {
            return format!("error: {}", err);
        }

        if !self.remote_exists {
            return "remote not configured".to_string();
        }

        if self.is_up_to_date() {
            return "up to date".to_string();
        }

        let mut parts = Vec::new();
        if self.ahead > 0 {
            parts.push(format!("{} ahead", self.ahead));
        }
        if self.behind > 0 {
            parts.push(format!("{} behind", self.behind));
        }

        parts.join(", ")
    }
}

/// Overall repository status
#[derive(Debug)]
pub struct RepoStatusReport {
    /// Path to the repository
    pub repo_path: String,

    /// Current branch name
    pub branch: String,

    /// Status for each configured forge
    pub forges: Vec<ForgeStatus>,

    /// Whether working tree has uncommitted changes
    pub has_changes: bool,

    /// Whether there are staged changes
    pub has_staged: bool,

    /// Whether there are untracked files
    pub has_untracked: bool,
}

impl RepoStatusReport {
    /// Check if all forges are up to date
    pub fn all_up_to_date(&self) -> bool {
        self.forges.iter().all(|f| f.is_up_to_date())
    }

    /// Check if any forge needs push
    pub fn needs_push(&self) -> bool {
        self.forges.iter().any(|f| f.needs_push())
    }

    /// Check if any forge needs pull
    pub fn needs_pull(&self) -> bool {
        self.forges.iter().any(|f| f.needs_pull())
    }

    /// Check if working tree is clean
    pub fn is_clean(&self) -> bool {
        !self.has_changes && !self.has_staged && !self.has_untracked
    }

    /// Format as a human-readable string
    pub fn format(&self) -> String {
        let mut lines = Vec::new();

        // Header
        lines.push(format!("Repository: {}", self.repo_path));
        lines.push(format!("Branch: {}", self.branch));

        // Working tree status
        if self.is_clean() {
            lines.push("Working tree: clean".to_string());
        } else {
            let mut status_parts = Vec::new();
            if self.has_staged {
                status_parts.push("staged changes");
            }
            if self.has_changes {
                status_parts.push("unstaged changes");
            }
            if self.has_untracked {
                status_parts.push("untracked files");
            }
            lines.push(format!("Working tree: {}", status_parts.join(", ")));
        }

        lines.push(String::new());

        // Forge status
        lines.push("Forges:".to_string());
        for forge in &self.forges {
            lines.push(format!(
                "  {} {} ({}): {}",
                forge.symbol(),
                forge.forge,
                forge.remote_name,
                forge.message()
            ));
        }

        lines.join("\n")
    }
}

/// Get status for a hyperforge repository
///
/// # Arguments
/// * `path` - Path to the repository
///
/// # Returns
/// RepoStatusReport with status information
pub fn status(path: &Path) -> StatusResult<RepoStatusReport> {
    // Check if hyperforge config exists
    if !HyperforgeConfig::exists(path) {
        return Err(StatusError::NotInitialized);
    }

    // Check if it's a git repo
    if !Git::is_repo(path) {
        return Err(StatusError::NotAGitRepo {
            path: path.display().to_string(),
        });
    }

    // Load config
    let config = HyperforgeConfig::load(path)?;

    // Get git status
    let repo_status = Git::repo_status(path)?;

    // Fetch all remotes to get accurate ahead/behind counts
    // (ignore errors - remote might not be reachable)
    let _ = Git::fetch_all(path);

    // Check status for each forge
    let mut forge_statuses = Vec::new();

    for forge in &config.forges {
        let remote_name = config.remote_for_forge(forge);
        let mut forge_status = ForgeStatus {
            forge: forge.clone(),
            remote_name: remote_name.clone(),
            remote_url: None,
            ahead: 0,
            behind: 0,
            remote_exists: false,
            error: None,
        };

        // Check if remote exists
        match Git::get_remote(path, &remote_name) {
            Ok(remote_info) => {
                forge_status.remote_exists = true;
                forge_status.remote_url = Some(remote_info.fetch_url);

                // Get ahead/behind count
                match Git::ahead_behind(path, &remote_name, &repo_status.branch) {
                    Ok((ahead, behind)) => {
                        forge_status.ahead = ahead;
                        forge_status.behind = behind;
                    }
                    Err(e) => {
                        forge_status.error = Some(format!("Failed to get sync status: {}", e));
                    }
                }
            }
            Err(GitError::RemoteNotFound { .. }) => {
                forge_status.remote_exists = false;
            }
            Err(e) => {
                forge_status.error = Some(e.to_string());
            }
        }

        forge_statuses.push(forge_status);
    }

    Ok(RepoStatusReport {
        repo_path: path.display().to_string(),
        branch: repo_status.branch,
        forges: forge_statuses,
        has_changes: repo_status.has_changes,
        has_staged: repo_status.has_staged,
        has_untracked: repo_status.has_untracked,
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::commands::init::{init, InitOptions};
    use std::fs;
    use std::process::Command;
    use tempfile::TempDir;

    fn setup_repo_with_commit(path: &Path) {
        // Configure git user
        Git::config_set(path, "user.email", "test@test.com").unwrap();
        Git::config_set(path, "user.name", "Test").unwrap();

        // Create initial commit
        fs::write(path.join("README.md"), "# Test").unwrap();
        Command::new("git")
            .args(["add", "."])
            .current_dir(path)
            .output()
            .unwrap();
        Command::new("git")
            .args(["commit", "-m", "Initial commit"])
            .current_dir(path)
            .output()
            .unwrap();
    }

    #[test]
    fn test_status_not_initialized() {
        let temp = TempDir::new().unwrap();
        Git::init(temp.path()).unwrap();

        let result = status(temp.path());
        assert!(matches!(result, Err(StatusError::NotInitialized)));
    }

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

        // Initialize hyperforge
        let options = InitOptions::new(vec!["github".to_string()])
            .with_org("alice")
            .with_repo_name("test-repo");
        init(temp.path(), options).unwrap();

        // Create a commit
        setup_repo_with_commit(temp.path());

        // Get status
        let report = status(temp.path()).unwrap();

        assert!(!report.branch.is_empty());
        assert_eq!(report.forges.len(), 1);
        assert_eq!(report.forges[0].forge, "github");
        assert_eq!(report.forges[0].remote_name, "origin");
        assert!(report.forges[0].remote_exists);
    }

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

        let options = InitOptions::new(vec!["github".to_string(), "codeberg".to_string()])
            .with_org("alice");
        init(temp.path(), options).unwrap();
        setup_repo_with_commit(temp.path());

        let report = status(temp.path()).unwrap();

        assert_eq!(report.forges.len(), 2);
        let github = report.forges.iter().find(|f| f.forge == "github").unwrap();
        let codeberg = report.forges.iter().find(|f| f.forge == "codeberg").unwrap();

        assert_eq!(github.remote_name, "origin");
        assert_eq!(codeberg.remote_name, "codeberg");
    }

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

        let options = InitOptions::new(vec!["github".to_string()])
            .with_org("alice");
        init(temp.path(), options).unwrap();
        setup_repo_with_commit(temp.path());

        let report = status(temp.path()).unwrap();

        assert!(report.is_clean());
    }

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

        let options = InitOptions::new(vec!["github".to_string()])
            .with_org("alice");
        init(temp.path(), options).unwrap();
        setup_repo_with_commit(temp.path());

        // Make changes
        fs::write(temp.path().join("new-file.txt"), "content").unwrap();

        let report = status(temp.path()).unwrap();

        assert!(report.has_untracked);
        assert!(!report.is_clean());
    }

    #[test]
    fn test_forge_status_symbols() {
        let up_to_date = ForgeStatus {
            forge: "github".to_string(),
            remote_name: "origin".to_string(),
            remote_url: Some("git@github.com:test/repo.git".to_string()),
            ahead: 0,
            behind: 0,
            remote_exists: true,
            error: None,
        };
        assert_eq!(up_to_date.symbol(), "");

        let needs_push = ForgeStatus {
            ahead: 2,
            behind: 0,
            ..up_to_date.clone()
        };
        assert_eq!(needs_push.symbol(), "");

        let needs_pull = ForgeStatus {
            ahead: 0,
            behind: 3,
            ..up_to_date.clone()
        };
        assert_eq!(needs_pull.symbol(), "");

        let diverged = ForgeStatus {
            ahead: 1,
            behind: 1,
            ..up_to_date.clone()
        };
        assert_eq!(diverged.symbol(), "");

        let not_configured = ForgeStatus {
            remote_exists: false,
            ..up_to_date.clone()
        };
        assert_eq!(not_configured.symbol(), "?");

        let error = ForgeStatus {
            error: Some("network error".to_string()),
            ..up_to_date.clone()
        };
        assert_eq!(error.symbol(), "");
    }

    #[test]
    fn test_forge_status_messages() {
        let up_to_date = ForgeStatus {
            forge: "github".to_string(),
            remote_name: "origin".to_string(),
            remote_url: Some("url".to_string()),
            ahead: 0,
            behind: 0,
            remote_exists: true,
            error: None,
        };
        assert_eq!(up_to_date.message(), "up to date");

        let needs_push = ForgeStatus {
            ahead: 2,
            ..up_to_date.clone()
        };
        assert_eq!(needs_push.message(), "2 ahead");

        let diverged = ForgeStatus {
            ahead: 1,
            behind: 3,
            ..up_to_date.clone()
        };
        assert_eq!(diverged.message(), "1 ahead, 3 behind");

        let not_configured = ForgeStatus {
            remote_exists: false,
            ..up_to_date.clone()
        };
        assert_eq!(not_configured.message(), "remote not configured");
    }

    #[test]
    fn test_status_format() {
        let report = RepoStatusReport {
            repo_path: "/test/repo".to_string(),
            branch: "main".to_string(),
            forges: vec![
                ForgeStatus {
                    forge: "github".to_string(),
                    remote_name: "origin".to_string(),
                    remote_url: Some("git@github.com:test/repo.git".to_string()),
                    ahead: 0,
                    behind: 0,
                    remote_exists: true,
                    error: None,
                },
                ForgeStatus {
                    forge: "codeberg".to_string(),
                    remote_name: "codeberg".to_string(),
                    remote_url: Some("git@codeberg.org:test/repo.git".to_string()),
                    ahead: 2,
                    behind: 0,
                    remote_exists: true,
                    error: None,
                },
            ],
            has_changes: false,
            has_staged: false,
            has_untracked: false,
        };

        let formatted = report.format();
        assert!(formatted.contains("Repository: /test/repo"));
        assert!(formatted.contains("Branch: main"));
        assert!(formatted.contains("Working tree: clean"));
        assert!(formatted.contains("✓ github"));
        assert!(formatted.contains("↑ codeberg"));
    }
}