goobits-repos 2.1.0

Fast Git repository management and synchronization tool
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
//! Subrepo status analysis and drift detection

use super::SubrepoInstance;
use anyhow::Result;
use std::collections::{HashMap, HashSet};

/// Uncommitted changes state across instances
#[derive(Debug, PartialEq)]
enum UncommittedState {
    /// All instances are clean (no uncommitted changes)
    AllClean,
    /// All instances have uncommitted changes
    AllDirty,
    /// Some instances are clean, some have uncommitted changes
    Mixed,
}

/// Status of a single subrepo across multiple parent repositories
#[derive(Debug)]
pub struct SubrepoStatus {
    pub name: String,
    pub remote_url: String,
    pub instances: Vec<SubrepoInstance>,
    pub sync_score: f32,
    pub unique_commits: usize,
    pub has_drift: bool,
}

impl SubrepoStatus {
    /// Calculate sync score: (total_instances - unique_commits) / (total_instances - 1) × 100
    ///
    /// Examples:
    /// - 2 instances, same commit   → (2-1)/(2-1) = 100%
    /// - 2 instances, diff commits  → (2-2)/(2-1) = 0%
    /// - 3 instances, 2 commits     → (3-2)/(3-1) = 50%
    fn calculate_sync_score(instances: &[SubrepoInstance]) -> (f32, usize) {
        let unique_commits = instances
            .iter()
            .map(|i| &i.commit_hash)
            .collect::<HashSet<_>>()
            .len();

        if instances.len() <= 1 {
            return (100.0, unique_commits);
        }

        let score = ((instances.len() - unique_commits) as f32)
            / ((instances.len() - 1) as f32)
            * 100.0;
        (score, unique_commits)
    }

    /// Create a new SubrepoStatus from instances
    pub fn new(name: String, remote_url: String, instances: Vec<SubrepoInstance>) -> Self {
        let (sync_score, unique_commits) = Self::calculate_sync_score(&instances);
        let has_drift = sync_score < 100.0;

        SubrepoStatus {
            name,
            remote_url,
            instances,
            sync_score,
            unique_commits,
            has_drift,
        }
    }
}

/// Analyze all subrepos and return status for shared ones
pub fn analyze_subrepos() -> Result<Vec<SubrepoStatus>> {
    let report = super::validation::validate_subrepos()?;

    let mut statuses = Vec::new();
    for (remote_url, instances) in report.by_remote {
        // Skip non-shared subrepos
        if instances.len() <= 1 {
            continue;
        }

        let name = instances[0].subrepo_name.clone();
        statuses.push(SubrepoStatus::new(name, remote_url, instances));
    }

    // Sort by sync score (worst first)
    statuses.sort_by(|a, b| a.sync_score.partial_cmp(&b.sync_score).unwrap());

    Ok(statuses)
}

/// Display concise drift summary for use in repos push
pub fn display_drift_summary(statuses: &[SubrepoStatus]) {
    let drifted: Vec<_> = statuses.iter().filter(|s| s.has_drift).collect();

    if drifted.is_empty() {
        return; // Don't show anything if no drift
    }

    let synced: Vec<_> = statuses.iter().filter(|s| !s.has_drift).collect();

    println!("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
    println!("🔴 SUBREPO DRIFT ({})", drifted.len());
    println!("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n");

    for status in &drifted {
        display_drift_summary_item(status);
    }

    if !synced.is_empty() {
        println!("💡 {} subrepos are fully synced.", synced.len());
    }
    println!("💡 Run 'repos subrepo status' for a detailed analysis.");
    println!("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n");
}

/// Display a single drifted subrepo in concise format
fn display_drift_summary_item(status: &SubrepoStatus) {
    println!("{}: {} instances at different commits", status.name, status.instances.len());

    // Find the latest clean commit (sync target)
    let latest_clean = status.instances.iter()
        .filter(|i| !i.has_uncommitted)
        .max_by_key(|i| i.commit_timestamp);

    // Find absolute latest
    let latest = status.instances.iter()
        .max_by_key(|i| i.commit_timestamp)
        .unwrap();

    // Group by commit for display
    let mut by_commit: std::collections::HashMap<String, Vec<&SubrepoInstance>> = std::collections::HashMap::new();
    for instance in &status.instances {
        by_commit
            .entry(instance.commit_hash.clone())
            .or_default()
            .push(instance);
    }

    // Sort commits by timestamp (newest first)
    let mut commits: Vec<_> = by_commit.into_iter().collect();
    commits.sort_by(|a, b| {
        let a_timestamp = a.1.iter().map(|i| i.commit_timestamp).max().unwrap_or(0);
        let b_timestamp = b.1.iter().map(|i| i.commit_timestamp).max().unwrap_or(0);
        b_timestamp.cmp(&a_timestamp)
    });

    // Display commits with arrow notation
    for (_commit, instances) in &commits {
        for instance in instances {
            let is_sync_target = latest_clean.is_some_and(|t| t.commit_hash == instance.commit_hash);
            let is_latest = latest.commit_hash == instance.commit_hash;

            let prefix = if is_sync_target { "" } else { " " };
            let status_indicator = if instance.has_uncommitted {
                "⚠️ uncommitted"
            } else {
                "✅ clean"
            };

            let mut suffix = String::new();
            if is_latest && !instance.has_uncommitted {
                suffix.push_str("  ⬆️ LATEST");
            } else if !is_latest {
                suffix.push_str("  (outdated)");
            }

            println!("  {} {}  {:30}  {}{}",
                prefix,
                instance.short_hash,
                instance.parent_repo,
                status_indicator,
                suffix
            );
        }
    }

    // Show sync command
    let target_commit = latest_clean.map(|t| &t.short_hash).unwrap_or(&latest.short_hash);
    println!("    Sync: repos subrepo sync {} --to {}", status.name, target_commit);
    println!();
}

/// Display subrepo status (problem-first by default)
pub fn display_status(statuses: &[SubrepoStatus], show_all: bool) {
    if statuses.is_empty() {
        println!("\n✅ No shared subrepos found.");
        println!("   Run 'repos validate' to see all nested repositories.\n");
        return;
    }

    let drifted: Vec<_> = statuses.iter().filter(|s| s.has_drift).collect();
    let synced: Vec<_> = statuses.iter().filter(|s| !s.has_drift).collect();

    println!("\n🔍 Analyzing {} shared subrepos...\n", statuses.len());

    // Show drifted subrepos
    if !drifted.is_empty() {
        println!("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
        println!("🔴 SUBREPO DRIFT ({})", drifted.len());
        println!("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n");

        for status in &drifted {
            display_drift_status(status);
        }
    }

    // Show synced subrepos if requested
    if show_all && !synced.is_empty() {
        println!("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
        println!("🟢 SYNCED SUBREPOS ({})", synced.len());
        println!("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n");

        for status in synced {
            display_synced_status(status);
        }
    } else if !synced.is_empty() {
        println!("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
        println!("💡 {} subrepos fully synced (100%)", synced.len());
        println!("   Use --all to see them");
        println!("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
    }

    // Add footer with global update suggestion if there's drift
    if !drifted.is_empty() {
        println!();
        println!("🔧 To update a drifted repo to its 'origin/main' branch instead, run:");
        println!("   repos subrepo update <name>  (e.g., 'repos subrepo update docs-engine')");
    }

    println!();
}

/// Analyze the uncommitted state across instances
fn analyze_uncommitted_state(instances: &[SubrepoInstance]) -> UncommittedState {
    let uncommitted_count = instances.iter()
        .filter(|i| i.has_uncommitted)
        .count();

    if uncommitted_count == 0 {
        UncommittedState::AllClean
    } else if uncommitted_count == instances.len() {
        UncommittedState::AllDirty
    } else {
        UncommittedState::Mixed
    }
}

/// Display a drifted subrepo with safe, actionable commands
fn display_drift_status(status: &SubrepoStatus) {
    println!("{}", status.name);
    println!("  Remote: {}", status.remote_url);
    println!(
        "  Sync Score: {}% ({} commits across {} repos)",
        status.sync_score as u32,
        status.unique_commits,
        status.instances.len()
    );
    println!();

    // Find the latest timestamp (absolute latest)
    let latest_timestamp = status.instances.iter()
        .map(|i| i.commit_timestamp)
        .max()
        .unwrap_or(0);

    // Find the latest CLEAN commit timestamp (sync target)
    let latest_clean_timestamp = status.instances.iter()
        .filter(|i| !i.has_uncommitted)
        .map(|i| i.commit_timestamp)
        .max();

    // Group instances by commit
    let mut by_commit: HashMap<String, Vec<&SubrepoInstance>> = HashMap::new();
    for instance in &status.instances {
        by_commit
            .entry(instance.commit_hash.clone())
            .or_default()
            .push(instance);
    }

    // Sort commits by number of instances (most common first), then by timestamp (newest first)
    let mut commits: Vec<_> = by_commit.into_iter().collect();
    commits.sort_by(|a, b| {
        // First sort by count (descending)
        let count_cmp = b.1.len().cmp(&a.1.len());
        if count_cmp != std::cmp::Ordering::Equal {
            return count_cmp;
        }
        // Then by timestamp (descending - newest first)
        let a_timestamp = a.1.iter().map(|i| i.commit_timestamp).max().unwrap_or(0);
        let b_timestamp = b.1.iter().map(|i| i.commit_timestamp).max().unwrap_or(0);
        b_timestamp.cmp(&a_timestamp)
    });

    // Display commits and their instances with arrow notation
    for (_commit, instances) in &commits {
        for instance in instances {
            let is_sync_target = latest_clean_timestamp.is_some_and(|t|
                instance.commit_timestamp == t && !instance.has_uncommitted
            );
            let is_latest = instance.commit_timestamp == latest_timestamp;

            let prefix = if is_sync_target { "" } else { " " };
            let status_indicator = if instance.has_uncommitted {
                "⚠️ uncommitted"
            } else {
                "✅ clean"
            };

            let mut suffix = String::new();
            if is_latest && !instance.has_uncommitted {
                suffix.push_str("  ⬆️ LATEST");
            } else if !is_latest {
                suffix.push_str("  (outdated)");
            }

            // Align the repo name and status
            println!("  {} {}  {:width$}  {}{}",
                prefix,
                instance.short_hash,
                instance.parent_repo,
                status_indicator,
                suffix,
                width = 30
            );
        }
    }
    println!();

    // Analyze uncommitted state and provide safe, actionable suggestions
    let uncommitted_state = analyze_uncommitted_state(&status.instances);

    match uncommitted_state {
        UncommittedState::AllDirty => {
            println!("  ⚠️ All instances have uncommitted changes.");
            println!();

            // No clean target - use the latest commit
            let target_instance = status.instances.iter()
                .max_by_key(|i| i.commit_timestamp)
                .unwrap();
            let target_commit = &target_instance.short_hash;

            let dirty_repos: Vec<&str> = status.instances.iter()
                .map(|i| i.parent_repo.as_str())
                .collect();
            let repos_desc = if dirty_repos.len() == 1 {
                format!("'{}'", dirty_repos[0])
            } else {
                format!("{} repos", dirty_repos.len())
            };

            println!("  💡 EASY FIX (Recommended):");
            println!("     repos subrepo sync {} --to {} --stash", status.name, target_commit);
            println!("     (Stashes uncommitted changes in {})", repos_desc);
            println!();
            println!("  🔥 FORCE FIX (Discards all local changes):");
            println!("     repos subrepo sync {} --to {} --force", status.name, target_commit);
        }

        UncommittedState::Mixed => {
            // Find the latest CLEAN commit (the sync target)
            let target_instance = status.instances.iter()
                .filter(|i| !i.has_uncommitted)
                .max_by_key(|i| i.commit_timestamp)
                .unwrap();
            let target_commit = &target_instance.short_hash;
            let target_repo = &target_instance.parent_repo;

            // Collect dirty repo names
            let dirty_repos: Vec<&str> = status.instances.iter()
                .filter(|i| i.has_uncommitted)
                .map(|i| i.parent_repo.as_str())
                .collect();

            let dirty_list = if dirty_repos.len() == 1 {
                format!("'{}'", dirty_repos[0])
            } else {
                dirty_repos.iter()
                    .map(|r| format!("'{}'", r))
                    .collect::<Vec<_>>()
                    .join(", ")
            };

            println!("  💡 EASY FIX (Recommended):");
            println!("     repos subrepo sync {} --to {} --stash", status.name, target_commit);
            println!("     (Syncs {} to the clean commit from '{}')", dirty_list, target_repo);
            println!();

            println!("  🔥 FORCE FIX (Discards changes in {}):", dirty_list);
            println!("     repos subrepo sync {} --to {} --force", status.name, target_commit);
        }

        UncommittedState::AllClean => {
            // All clean - normal sync suggestions work
            // Find the latest commit
            let latest_commit = status.instances.iter()
                .max_by_key(|i| i.commit_timestamp)
                .unwrap();

            println!("  🔧 SYNC to latest commit:");
            println!("     repos subrepo sync {} --to {}", status.name, latest_commit.short_hash);
        }
    }

    println!();
}

/// Display a synced subrepo
fn display_synced_status(status: &SubrepoStatus) {
    println!("{}", status.name);
    println!("  Remote: {}", status.remote_url);
    println!("  Sync Score: 100% (all at same commit)");
    println!();

    // Show the commit they're all at
    println!("  {}  (all instances)", status.instances[0].short_hash);
    println!();

    // Check if any have uncommitted changes
    let has_any_uncommitted = status.instances.iter().any(|i| i.has_uncommitted);

    if has_any_uncommitted {
        for instance in &status.instances {
            let status_indicator = if instance.has_uncommitted {
                "⚠️  uncommitted"
            } else {
                "✅ clean"
            };
            println!("    {:width$}  {}",
                instance.parent_repo,
                status_indicator,
                width = 30
            );
        }
        println!();
        println!("  ✅ Already synchronized");
        println!("  ⚠️  But some have uncommitted changes");
    } else {
        for instance in &status.instances {
            println!("{}", instance.parent_repo);
        }
        println!();
        println!("  ✅ Already synchronized and clean");
    }

    println!();
}