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
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
//! # Magoo
//!
//! A wrapper for `git submodule` commands.
//!
//! ## CLI Usage
//! ```bash
//! cargo install magoo
//! magoo --help
//! ```
//! See [README](https://github.com/Pistonite/magoo) for more information.
//!
//! ## Library Usage
//! ```bash
//! cargo add magoo
//! ```
//! If you don't need `clap` for parsing arguments, you can add `--no-default-features` to
//! exclude the dependency.
//!
//! ### Examples
//! #### Run a command
//! ```rust
//! use magoo::{StatusCommand, PrintOptions};
//!
//! let command = magoo::StatusCommand {
//!     git: true,
//!     fix: false,
//!     long: false,
//!     options: PrintOptions {
//!         verbose: false,
//!         quiet: false,
//!         color: None,
//!     },
//! };
//!
//! // don't need this if you don't need output to stdout
//! command.set_print_options();
//! // runs `magoo status --git` in the current directory
//! command.run("."); //.unwrap();
//! ```
//! #### Use `clap` to parse arguments
//! ```rust
//! use magoo::Magoo;
//! use clap::Parser;
//!
//! // for assertion below only
//! use magoo::{Command, StatusCommand, PrintOptions};
//!
//! let magoo = Magoo::try_parse_from(["magoo", "--dir", "my/repo", "status", "--long", "--verbose"]).unwrap();
//!
//! assert_eq!(magoo, Magoo {
//!     subcmd: Command::Status(StatusCommand {
//!         git: false,
//!         fix: false,
//!         long: true,
//!         options: PrintOptions {
//!             verbose: true,
//!             quiet: false,
//!             color: None,
//!         },
//!     }),
//!     dir: "my/repo".to_string(),
//! });
//!
//! magoo.set_print_options();
//! magoo.run(); //.unwrap();
//! ```
//! You can also look at [main.rs](https://github.com/Pistonite/magoo/blob/master/src/main.rs) for
//! reference.
//!

pub mod git;
pub use git::SUPPORTED_GIT_VERSIONS;
use git::{GitContext, GitError};

pub mod print;
pub mod status;
pub mod submodule;
use status::Status;

use crate::print::{println_error, println_hint, println_info, println_verbose, println_warn};

/// The main entry point for the library
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "cli", derive(clap::Parser))]
#[cfg_attr(
    feature = "cli",
    command(author, about, version, arg_required_else_help(true))
)]
pub struct Magoo {
    /// Command to run
    #[clap(subcommand)]
    pub subcmd: Command,
    /// Set the working directory of commands. Useful if not running inside a git repository.
    #[cfg_attr(feature = "cli", clap(long, default_value(".")))]
    pub dir: String,
}

impl Magoo {
    /// Run the command
    pub fn run(&self) -> Result<(), GitError> {
        self.subcmd.run(&self.dir)
    }

    /// Apply the print options
    pub fn set_print_options(&self) {
        self.subcmd.set_print_options();
    }
}

/// Subcommands
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "cli", derive(clap::Parser))]
pub enum Command {
    /// Print the status of the submodules in the current git repository.
    Status(StatusCommand),
    /// Add or install dependencies
    ///
    /// Installs dependencies if no arguments are provided.
    /// Otherwise, adds the provided dependency as a git submodule.
    Install(InstallCommand),
    /// Updates all dependencies or the specified dependency.
    ///
    /// Dependencies will be updated to the branch (specified when adding the dependency) from the
    /// remote.
    Update(UpdateCommand),
    /// Remove a dependency
    Remove(RemoveCommand),
}

impl Command {
    /// Apply the print options
    pub fn set_print_options(&self) {
        match self {
            Command::Status(cmd) => cmd.set_print_options(),
            Command::Install(cmd) => cmd.set_print_options(),
            Command::Update(cmd) => cmd.set_print_options(),
            Command::Remove(cmd) => cmd.set_print_options(),
        }
    }

    /// Run the command in the given directory.
    pub fn run(&self, dir: &str) -> Result<(), GitError> {
        match self {
            Command::Status(cmd) => {
                cmd.run(dir)?;
            }
            Command::Install(cmd) => {
                cmd.run(dir)?;
            }
            Command::Update(cmd) => {
                cmd.run(dir)?;
            }
            Command::Remove(cmd) => {
                cmd.run(dir)?;
            }
        }

        Ok(())
    }
}

/// The `status` command
#[derive(Debug, Default, Clone, PartialEq)]
#[cfg_attr(feature = "cli", derive(clap::Parser))]
pub struct StatusCommand {
    /// Show the current git version and if it is supported
    #[cfg_attr(feature = "cli", clap(long))]
    pub git: bool,

    /// Show more information in a longer format
    #[cfg_attr(feature = "cli", clap(long, short))]
    pub long: bool,

    /// Fix the submodules to be in a consistent state. (CAUTION - you should never have to do this if you let magoo manage the submodules, be sure to read the details in `magoo status --help` before using!)
    ///
    /// If any submodule appears to be broken (likely due to changing
    /// the git files manually), this will attempt to bring the submodule back
    /// to a consistent state by de-initializing it.
    ///
    /// USE WITH CAUTION - If the submodule state is so broken that there's not enough information
    /// to fix it, it will be removed from existence.
    /// This may delete local files and directories that look like submodules because they are referenced by git files.
    ///
    #[cfg_attr(feature = "cli", clap(long, short))]
    pub fix: bool,

    /// Print options
    #[cfg_attr(feature = "cli", clap(flatten))]
    pub options: PrintOptions,
}

impl StatusCommand {
    /// Apply the print options
    pub fn set_print_options(&self) {
        self.options.apply();
    }

    /// Run the command and return the status as a [`Status`] struct.
    pub fn run(&self, dir: &str) -> Result<Status, GitError> {
        let context = GitContext::try_from(dir)?;
        let _guard = context.lock()?;
        if self.git {
            context.print_version_info()?;
            return Ok(Status::default());
        }

        let mut status = Status::read_from(&context)?;
        let mut flat_status = status.flattened_mut();
        if flat_status.is_empty() {
            println!("No submodules found");
            return Ok(status);
        }
        if self.fix {
            for submodule in flat_status.iter_mut() {
                submodule.fix(&context)?;
            }
            return Ok(status);
        }

        let dir_switch = if dir == "." {
            "".to_string()
        } else {
            format!(" --dir {dir}")
        };

        for submodule in &flat_status {
            submodule.print(&context, &dir_switch, self.long)?;
        }
        Ok(status)
    }
}

/// The `install` command
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "cli", derive(clap::Parser))]
pub struct InstallCommand {
    /// URL of the git repository to add
    ///
    /// See the `add` command of <https://git-scm.com/docs/git-submodule> for what formats are
    /// supported.
    pub url: Option<String>,

    /// Local path to clone the git submodule to
    ///
    /// Unlike the path specified with `git submodule add`, this path should be relative from
    /// the top level (root) of the git repository.
    #[cfg_attr(feature = "cli", arg(requires("url")))]
    pub path: Option<String>,

    /// Branch to checkout and track
    ///
    /// This is the branch reference that will be used when updating the submodule.
    /// If not specified, the behavior is the same as `git submodule add` without `--branch`
    /// (`HEAD` is used)
    #[cfg_attr(feature = "cli", clap(long, short))]
    #[cfg_attr(feature = "cli", arg(requires("url")))]
    pub branch: Option<String>,

    /// Name of the submodule
    ///
    /// If not specified, the name of the submodule is the same as the path.
    #[cfg_attr(feature = "cli", clap(long))]
    #[cfg_attr(feature = "cli", arg(requires("url")))]
    pub name: Option<String>,

    /// Depth to clone the submodule
    #[cfg_attr(feature = "cli", clap(long))]
    #[cfg_attr(feature = "cli", arg(requires("url")))]
    pub depth: Option<usize>,

    /// Whether to force the submodule to be added
    ///
    /// This will pass the `--force` flag to `git submodule add` and `git submodule update`.
    #[cfg_attr(feature = "cli", clap(long, short))]
    pub force: bool,

    /// Print options
    #[cfg_attr(feature = "cli", clap(flatten))]
    pub options: PrintOptions,
}

impl InstallCommand {
    /// Apply the print options
    pub fn set_print_options(&self) {
        self.options.apply();
    }

    /// Run the command in the given directory
    pub fn run(&self, dir: &str) -> Result<(), GitError> {
        let context = GitContext::try_from(dir)?;
        let _guard = context.lock()?;

        let mut status = Status::read_from(&context)?;
        for submodule in status.flattened_mut() {
            submodule.fix(&context)?;
        }

        match &self.url {
            Some(url) => {
                println_verbose!("Adding submodule from url: {url}");
                context.submodule_add(
                    url,
                    self.path.as_deref(),
                    self.branch.as_deref(),
                    self.name.as_deref(),
                    self.depth.as_ref().copied(),
                    self.force,
                )?;
            }
            None => {
                println_verbose!("Installing submodules");
                context.submodule_init(None)?;
                context.submodule_sync(None)?;
                context.submodule_update(None, self.force, false)?;
            }
        }

        Ok(())
    }
}

/// The `update` command
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "cli", derive(clap::Parser))]
pub struct UpdateCommand {
    /// Name of the submodule to update
    ///
    /// If not specified, all submodules will be updated.
    pub name: Option<String>,

    /// Change the branch of the submodule
    #[cfg_attr(feature = "cli", clap(long, short))]
    #[cfg_attr(feature = "cli", arg(requires("name")))]
    pub branch: Option<String>,

    /// Unset the update branch of the submodule
    #[cfg_attr(feature = "cli", clap(long))]
    #[cfg_attr(feature = "cli", arg(requires("name"), conflicts_with("branch")))]
    pub unset_branch: bool,

    /// Change the url of the submodule
    #[cfg_attr(feature = "cli", clap(long, short))]
    #[cfg_attr(feature = "cli", arg(requires("name")))]
    pub url: Option<String>,

    /// Whether to force the submodule to be updated
    ///
    /// This will pass the `--force` flag to `git submodule update`.
    #[cfg_attr(feature = "cli", clap(long, short))]
    pub force: bool,

    /// Bypass warnings in the submodule state
    #[cfg_attr(feature = "cli", clap(long))]
    pub bypass: bool,

    /// Print options
    #[cfg_attr(feature = "cli", clap(flatten))]
    pub options: PrintOptions,
}

impl UpdateCommand {
    /// Apply the print options
    pub fn set_print_options(&self) {
        self.options.apply();
    }

    /// Run the command in the given directory
    pub fn run(&self, dir: &str) -> Result<(), GitError> {
        let context = GitContext::try_from(dir)?;
        let _guard = context.lock()?;

        match &self.name {
            Some(name) => {
                println_verbose!("Updating submodule: {name}");
                let status = Status::read_from(&context)?;
                let submodule = match status.modules.get(name) {
                    Some(submodule) => submodule,
                    None => {
                        println_error!("Submodule `{name}` not found!");
                        // maybe user passed in path instead of name?
                        println_verbose!("Trying to search for a path matching `{name}`");
                        for submodule in status.flattened() {
                            if let Some(other_name) = submodule.name() {
                                if let Some(path) = submodule.path() {
                                    if path == name {
                                        println_hint!("  however, there is a submodule \"{other_name}\" with path \"{path}\"");
                                        println_hint!("  if you meant to update this submodule, use `magoo update {other_name}`");
                                        break;
                                    }
                                }
                            }
                        }

                        return Err(GitError::NeedFix(false));
                    }
                };
                if !submodule.is_healthy(&context)? {
                    if !self.bypass {
                        println_error!("Submodule `{name}` is not healthy!");
                        println_hint!("  run `magoo status` to investigate. Some issues might be fixable with `magoo status --fix`.");
                        println_hint!("  alternatively, use the `--bypass` flag to ignore and continue anyway.");
                        return Err(GitError::NeedFix(false));
                    } else {
                        println_warn!("Bypassing warnings from unhealthy submodule `{name}`");
                    }
                }

                let path = match submodule.path() {
                    Some(x) => x,
                    None => {
                        println_error!("Submodule `{name}` does not have a path!");
                        println_hint!("  run `magoo status` to investigate.");
                        println_hint!("  if you are unsure of the problem, try hard removing the submodule with `magoo remove {name} --force` and then re-adding it");
                        return Err(GitError::NeedFix(false));
                    }
                };
                context.submodule_init(Some(path))?;
                if self.unset_branch {
                    context.submodule_set_branch(path, None)?;
                } else if let Some(branch) = &self.branch {
                    context.submodule_set_branch(path, Some(branch))?;
                }

                if let Some(url) = &self.url {
                    context.submodule_set_url(path, url)?;
                }

                context.submodule_sync(Some(path))?;
                context.submodule_update(Some(path), self.force, true)?;
            }
            None => {
                println_verbose!("Updating submodules");
                context.submodule_init(None)?;
                context.submodule_sync(None)?;
                context.submodule_update(None, self.force, true)?;
            }
        }

        println_info!();
        println_info!("Submodules updated successfully.");
        println_hint!(
            "  run `git status` to check the changes and run `git add ...` to stage them"
        );
        println_hint!("  run `magoo status` to check the status of the submodules");
        Ok(())
    }
}

/// The `remove` command
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "cli", derive(clap::Parser))]
pub struct RemoveCommand {
    /// The name of the submodule to remove
    pub name: String,

    /// Force remove the submodule. Will delete any local changes to the submodule
    #[cfg_attr(feature = "cli", clap(long, short))]
    pub force: bool,

    /// Pass the `--force` flag to `git submobule deinit`
    ///
    /// Cannot be used together with `--force`, since `--force` skips de-initializing.
    #[cfg_attr(feature = "cli", clap(long))]
    #[cfg_attr(feature = "cli", arg(conflicts_with("force")))]
    pub force_deinit: bool,

    /// Print options
    #[cfg_attr(feature = "cli", clap(flatten))]
    pub options: PrintOptions,
}

impl RemoveCommand {
    /// Apply the print options
    pub fn set_print_options(&self) {
        self.options.apply();
    }

    /// Run the command in the given directory
    pub fn run(&self, dir: &str) -> Result<(), GitError> {
        let context = GitContext::try_from(dir)?;
        let _guard = context.lock()?;

        let name = &self.name;

        println_verbose!("Removing submodule: {name}");
        let mut status = Status::read_from(&context)?;
        let submodule = match status.modules.get_mut(name) {
            Some(submodule) => submodule,
            None => {
                println_error!("Submodule `{name}` not found!");
                // maybe user passed in path instead of name?
                println_verbose!("Trying to search for a path matching `{name}`");
                for submodule in status.flattened() {
                    if let Some(other_name) = submodule.name() {
                        if let Some(path) = submodule.path() {
                            if path == name {
                                println_hint!("  however, there is a submodule \"{other_name}\" with path \"{path}\"");
                                println_hint!("  if you meant to remove this submodule, use `magoo remove {other_name}`");
                                break;
                            }
                        }
                    }
                }

                return Err(GitError::NeedFix(false));
            }
        };

        if self.force {
            println_verbose!("Removing (force): {name}");
            submodule.force_delete(&context)?;
        } else {
            let path = match submodule.path() {
                Some(x) => x,
                None => {
                    println_error!("Submodule `{name}` does not have a path!");
                    println_hint!("  run `magoo status` to investigate.");
                    println_hint!("  if you are unsure of the problem, try hard removing the submodule with `magoo remove {name} --force`");
                    return Err(GitError::NeedFix(false));
                }
            };
            if let Err(e) = context.submodule_deinit(Some(path), self.force_deinit) {
                println_error!("Failed to deinitialize submodule `{name}`: {e}");
                println_hint!(
                    "  try running with `--force-deinit` to force deinitialize the module"
                );
                println_hint!(
                    "  alternatively, running with `--force` will remove the module anyway."
                );
                return Err(GitError::NeedFix(false));
            }

            submodule.force_remove_module_dir(&context)?;
            submodule.force_remove_config(&context)?;
            submodule.force_remove_from_dot_gitmodules(&context)?;
            submodule.force_remove_from_index(&context)?;
        }

        println_info!();
        println_info!("Submodules removed successfully.");
        println_hint!("  run `git status` to check the changes");
        Ok(())
    }
}

/// Printing options for all commands
#[derive(Debug, Default, Clone, PartialEq)]
#[cfg_attr(feature = "cli", derive(clap::Parser))]
pub struct PrintOptions {
    /// Enable verbose output
    ///
    /// Display more information about what is happening, for example which git commands are
    /// executed and their output
    #[cfg_attr(feature = "cli", clap(long))]
    pub verbose: bool,

    /// Disable output to stdout and stderr
    #[cfg_attr(feature = "cli", clap(long, short))]
    pub quiet: bool,

    /// Color options
    ///
    /// `Some(true)` and `Some(false)` to always/never use color in output.
    /// `None` to read the color setting from git config.
    #[cfg_attr(feature = "cli", clap(skip))]
    pub color: Option<bool>,
}

impl PrintOptions {
    /// Apply the options
    pub fn apply(&self) {
        print::set_options(self.verbose, self.quiet, self.color);
    }
}