vcs2git 0.4.0

Import VCS .repos as Git submodules
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
mod cli;
mod git_ops;
mod progress;
mod state;
mod utils;
mod validation;
mod vcs;

use crate::{
    cli::Opts,
    git_ops::{checkout_to_version, fetch, remove_submodule, remove_submodule_rollback},
    progress::ProgressReporter,
    state::SubmoduleStateTracker,
    utils::{check_disjoint, check_subset},
    validation::{validate_main_repo_clean, validate_repositories, validate_submodule_states},
    vcs::{Repo, RepoType, ReposFile},
};
use clap::Parser;
use color_eyre::{
    eyre::{bail, ensure, Context},
    Result,
};
use git2::Repository;
use std::{
    collections::{HashMap, HashSet},
    fs::{self, File},
    io::BufReader,
    path::{Path, PathBuf},
};
use tracing::{error, info, warn};

fn main() -> Result<()> {
    // Install color-eyre panic and error report handlers
    color_eyre::install()?;

    // Initialize tracing
    tracing_subscriber::fmt::init();

    let opts = Opts::parse();

    // Open the repository
    let mut root_repo = Repository::open(".")
        .with_context(|| "Please run in the toplevel directory of the git repo")?;

    // List submodules
    let submod_names: HashMap<PathBuf, String> = root_repo
        .submodules()?
        .into_iter()
        .filter_map(|submod| {
            let path = submod.path().to_path_buf();
            let name = submod.name()?.to_string();
            Some((path, name))
        })
        .collect();

    // Parse the repo list
    let repos_list: ReposFile = {
        let reader = BufReader::new(File::open(&opts.repo_file)?);
        serde_yaml::from_reader(reader)?
    };

    ensure!(
        opts.prefix.is_relative(),
        "The prefix must be a relative path"
    );

    // Validate repository configuration
    validate_repositories(&repos_list.repositories, &opts.prefix)?;

    // Check for uncommitted changes in the main repository
    validate_main_repo_clean(&root_repo)?;

    // Validate existing submodule states
    info!("Checking existing submodule states...");
    validate_submodule_states(&root_repo)?;
    info!("All validation checks passed.");

    let selected_repos: HashMap<PathBuf, _> = {
        let all_suffixes: HashSet<&Path> = repos_list
            .repositories
            .keys()
            .map(|path| path.as_path())
            .collect();
        let skipped_suffixes = {
            let suffixes: HashSet<&Path> = opts
                .get_ignored()
                .iter()
                .flatten()
                .map(|path| path.as_path())
                .collect();
            check_subset(&all_suffixes, &suffixes)?;
            suffixes
        };
        let selected_suffixes: HashSet<&Path> = match opts.get_selected() {
            Some(names) => {
                let suffixes: HashSet<_> = names.iter().map(|path| path.as_path()).collect();
                check_subset(&all_suffixes, &suffixes)?;
                check_disjoint(&suffixes, &skipped_suffixes)?;
                suffixes
            }
            None => all_suffixes
                .difference(&skipped_suffixes)
                .copied()
                .collect(),
        };

        selected_suffixes
            .difference(&skipped_suffixes)
            .map(|&suffix| {
                let path = opts.prefix.join(suffix);
                let repo = &repos_list.repositories[suffix];
                (path, repo)
            })
            .collect()
    };

    // Check repo types
    for info in selected_repos.values() {
        match &info.r#type {
            RepoType::Git => (),
            RepoType::Unknown(ty) => {
                bail!("Repository type '{ty}' is not supported. Only 'git' repositories are supported.");
            }
        }
    }

    let (new_repos, updated_submods, removed_repos) =
        classify_submodules(&selected_repos, &submod_names, &opts.prefix);

    fs::create_dir_all(&opts.prefix)?;

    // Capture original state before any modifications
    let tracker = SubmoduleStateTracker::new(&root_repo)?;

    // Calculate total operations for progress reporting
    let total_operations = new_repos.len()
        + if opts.should_update() {
            updated_submods.len()
        } else {
            0
        }
        + if opts.sync_selection {
            removed_repos.len()
        } else {
            0
        };

    if total_operations == 0 {
        info!("No operations to perform - all repositories are up to date");
        return Ok(());
    }

    // Create progress reporter
    let progress = ProgressReporter::new(total_operations as u64);

    // Track which operations we've completed
    let mut completed_new = Vec::new();

    // Process all operations with rollback on failure
    let result = process_submodule_operations(
        &mut root_repo,
        &new_repos,
        &updated_submods,
        &removed_repos,
        &opts,
        &mut completed_new,
        &progress,
    );

    // Handle rollback if operation failed
    if let Err(e) = result {
        if !opts.dry_run {
            error!("Operation failed. Rolling back all changes...");

            // Remove any newly added submodules
            for path in completed_new {
                if let Err(remove_err) = remove_submodule_rollback(&root_repo, path) {
                    warn!("Failed to remove {}: {}", path.display(), remove_err);
                }
            }

            // Clean up .gitmodules if no submodules remain
            let gitmodules_path = PathBuf::from(".gitmodules");
            if gitmodules_path.exists() {
                // Check if any submodules remain
                let submodules = root_repo.submodules()?;
                if submodules.is_empty() {
                    // No submodules left, remove .gitmodules
                    fs::remove_file(&gitmodules_path)?;
                }
            }

            // Restore original states
            if let Err(rollback_err) = tracker.rollback(&root_repo) {
                error!("Error during rollback: {rollback_err}");
            }

            bail!("Operation failed and was rolled back: {}", e);
        } else {
            bail!("Operation failed: {}", e);
        }
    }

    // Only show found extras if not syncing
    if !opts.sync_selection {
        for (path, _submod_name) in removed_repos {
            info!("Found extra submodule {}", path.display());
        }
    }

    progress.finish_with_message("All operations completed successfully!");

    Ok(())
}

fn process_submodule_operations<'a>(
    root_repo: &mut Repository,
    new_repos: &[(&'a Path, &'a &'a Repo)],
    updated_submods: &[(&'a Path, (&'a String, &'a &'a Repo))],
    removed_repos: &[(&'a Path, &'a String)],
    opts: &Opts,
    completed_new: &mut Vec<&'a Path>,
    progress: &ProgressReporter,
) -> Result<()> {
    // Add new repos
    for (path, info) in new_repos {
        if opts.dry_run {
            progress.println(&format!("[DRY RUN] Would add {}", path.display()));
            progress.inc(1);
            continue;
        }

        progress.set_message(&format!("Adding {}", path.display()));
        let Repo { url, version, .. } = info;

        // Track the path before attempting to create submodule
        let result = (|| -> Result<()> {
            let mut submod = root_repo.submodule(url.as_str(), path, true)?;
            // At this point, .gitmodules has been modified

            let subrepo = match submod.open() {
                Ok(repo) => repo,
                Err(e) => {
                    // Submodule was created but clone failed - need cleanup
                    error!("Failed to clone submodule: {e}");
                    return Err(e.into());
                }
            };

            // Get remote branches and tags
            fetch(&subrepo, "origin", version)?;

            // Checkout
            checkout_to_version(&subrepo, version, !opts.no_checkout)?;

            submod.add_finalize()?;
            Ok(())
        })();

        match result {
            Ok(_) => {
                completed_new.push(path);
                progress.inc(1);
            }
            Err(e) => {
                error!("Failed to add {}: {}", path.display(), e);
                // The submodule entry was created but operation failed
                completed_new.push(path);
                return Err(e);
            }
        }
    }

    if opts.should_update() {
        for (path, (submod_name, info)) in updated_submods {
            if opts.dry_run {
                progress.println(&format!("[DRY RUN] Would update {}", path.display()));
                progress.inc(1);
                continue;
            }

            progress.set_message(&format!("Updating {}", path.display()));
            let Repo { url, version, .. } = info;
            let result = (|| -> Result<()> {
                root_repo.submodule_set_url(submod_name, url.as_str())?;
                let mut submod = root_repo.find_submodule(submod_name)?;
                let subrepo = submod.open()?;

                // Get remote branches and tags
                fetch(&subrepo, "origin", version)?;

                // Checkout
                checkout_to_version(&subrepo, version, !opts.no_checkout)?;

                submod.add_finalize()?;
                Ok(())
            })();

            match result {
                Ok(_) => {
                    progress.inc(1);
                }
                Err(e) => {
                    error!("Failed to update {}: {}", path.display(), e);
                    return Err(e);
                }
            }
        }
    } else {
        for (path, _) in updated_submods {
            progress.println(&format!("Skip existing {}", path.display()));
        }
    }

    // Handle --sync-selection: remove submodules not in current selection
    if opts.sync_selection {
        for (path, _submod_name) in removed_repos {
            if opts.dry_run {
                progress.println(&format!("[DRY RUN] Would remove {}", path.display()));
            } else {
                progress.set_message(&format!("Removing {}", path.display()));

                if let Err(e) = remove_submodule(root_repo, path) {
                    error!("Failed to remove {}: {}", path.display(), e);
                    return Err(e);
                }
            }
            progress.inc(1);
        }
    }

    Ok(())
}

// Type aliases for clarity
type NewRepos<'a> = Vec<(&'a Path, &'a &'a Repo)>;
type UpdatedRepos<'a> = Vec<(&'a Path, (&'a String, &'a &'a Repo))>;
type RemovedRepos<'a> = Vec<(&'a Path, &'a String)>;

fn classify_submodules<'a>(
    selected_repos: &'a HashMap<PathBuf, &'a Repo>,
    submod_names: &'a HashMap<PathBuf, String>,
    prefix: &Path,
) -> (NewRepos<'a>, UpdatedRepos<'a>, RemovedRepos<'a>) {
    let selected_paths: HashSet<&Path> = selected_repos.keys().map(|p| p.as_path()).collect();
    let submod_paths: HashSet<&Path> = submod_names.keys().map(|p| p.as_path()).collect();

    let new_paths = selected_paths.difference(&submod_paths);
    let updated_paths = selected_paths.intersection(&submod_paths);
    let removed_paths = submod_paths
        .difference(&selected_paths)
        .filter(|path| path.starts_with(prefix));

    let mut new_repos: Vec<(&Path, _)> = new_paths
        .map(|&path| (path, &selected_repos[path]))
        .collect();
    new_repos.sort_unstable_by(|(lp, _), (rp, _)| lp.cmp(rp));

    let mut updated_repos: Vec<(&Path, _)> = {
        updated_paths
            .map(|&path| {
                let repo = &selected_repos[path];
                let submod_name = &submod_names[path];
                (path, (submod_name, repo))
            })
            .collect()
    };
    updated_repos.sort_unstable_by(|(lp, _), (rp, _)| lp.cmp(rp));

    let mut removed_submods: Vec<(&Path, _)> = removed_paths
        .map(|&path| (path, &submod_names[path]))
        .collect();
    removed_submods.sort_unstable_by(|(lp, _), (rp, _)| lp.cmp(rp));

    (new_repos, updated_repos, removed_submods)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::vcs::{Repo, RepoType};
    use std::collections::HashMap;
    use std::path::PathBuf;

    #[test]
    fn test_repository_type_error_message() {
        let unknown_type = RepoType::Unknown("mercurial".to_string());

        match &unknown_type {
            RepoType::Git => panic!("Should be unknown type"),
            RepoType::Unknown(ty) => {
                let error_msg = format!(
                    "Repository type '{ty}' is not supported. Only 'git' repositories are supported."
                );
                assert_eq!(error_msg, "Repository type 'mercurial' is not supported. Only 'git' repositories are supported.");
            }
        }
    }

    #[test]
    fn test_classify_submodules_all_new() {
        let mut selected_repos = HashMap::new();
        let repo1 = Repo {
            r#type: RepoType::Git,
            url: "https://github.com/test/repo1".parse().unwrap(),
            version: "main".to_string(),
        };
        let repo2 = Repo {
            r#type: RepoType::Git,
            url: "https://github.com/test/repo2".parse().unwrap(),
            version: "main".to_string(),
        };
        selected_repos.insert(PathBuf::from("prefix/repo1"), &repo1);
        selected_repos.insert(PathBuf::from("prefix/repo2"), &repo2);

        let submod_names = HashMap::new();
        let prefix = PathBuf::from("prefix");

        let (new, updated, removed) = classify_submodules(&selected_repos, &submod_names, &prefix);

        assert_eq!(new.len(), 2);
        assert_eq!(updated.len(), 0);
        assert_eq!(removed.len(), 0);
    }

    #[test]
    fn test_classify_submodules_all_existing() {
        let mut selected_repos = HashMap::new();
        let repo1 = Repo {
            r#type: RepoType::Git,
            url: "https://github.com/test/repo1".parse().unwrap(),
            version: "main".to_string(),
        };
        selected_repos.insert(PathBuf::from("prefix/repo1"), &repo1);

        let mut submod_names = HashMap::new();
        submod_names.insert(PathBuf::from("prefix/repo1"), "prefix/repo1".to_string());

        let prefix = PathBuf::from("prefix");

        let (new, updated, removed) = classify_submodules(&selected_repos, &submod_names, &prefix);

        assert_eq!(new.len(), 0);
        assert_eq!(updated.len(), 1);
        assert_eq!(removed.len(), 0);
    }

    #[test]
    fn test_classify_submodules_with_removed() {
        let selected_repos = HashMap::new();

        let mut submod_names = HashMap::new();
        submod_names.insert(PathBuf::from("prefix/repo1"), "prefix/repo1".to_string());
        submod_names.insert(PathBuf::from("prefix/repo2"), "prefix/repo2".to_string());
        submod_names.insert(PathBuf::from("other/repo3"), "other/repo3".to_string());

        let prefix = PathBuf::from("prefix");

        let (new, updated, removed) = classify_submodules(&selected_repos, &submod_names, &prefix);

        assert_eq!(new.len(), 0);
        assert_eq!(updated.len(), 0);
        assert_eq!(removed.len(), 2); // Only repos under prefix
    }

    #[test]
    fn test_classify_submodules_mixed() {
        let mut selected_repos = HashMap::new();
        let repo1 = Repo {
            r#type: RepoType::Git,
            url: "https://github.com/test/repo1".parse().unwrap(),
            version: "main".to_string(),
        };
        let repo2 = Repo {
            r#type: RepoType::Git,
            url: "https://github.com/test/repo2".parse().unwrap(),
            version: "main".to_string(),
        };
        selected_repos.insert(PathBuf::from("prefix/repo1"), &repo1);
        selected_repos.insert(PathBuf::from("prefix/repo2"), &repo2);

        let mut submod_names = HashMap::new();
        submod_names.insert(PathBuf::from("prefix/repo1"), "prefix/repo1".to_string());
        submod_names.insert(PathBuf::from("prefix/repo3"), "prefix/repo3".to_string());

        let prefix = PathBuf::from("prefix");

        let (new, updated, removed) = classify_submodules(&selected_repos, &submod_names, &prefix);

        assert_eq!(new.len(), 1); // repo2 is new
        assert_eq!(updated.len(), 1); // repo1 exists
        assert_eq!(removed.len(), 1); // repo3 should be removed
    }
}