Skip to content

Commit

Permalink
implement update
Browse files Browse the repository at this point in the history
  • Loading branch information
Pistonight committed Nov 25, 2023
1 parent b20bf29 commit 3492ff1
Show file tree
Hide file tree
Showing 7 changed files with 315 additions and 89 deletions.
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ It also deletes submodules that are deleted by others (by running `status --fix

### Show submodule status
```bash
magoo status [--all]
magoo status --fix [--all]
magoo status [--long] [--all] [--fix]
```
Shows everything ![magoo](https://raw.githubusercontent.com/Pistonite/magoo/main/magoo.webp) knows about submodules in the current repo.

Expand Down Expand Up @@ -108,10 +107,10 @@ the `BRANCH` specified when you add it.
```

### Remove submodules
![magoo](https://raw.githubusercontent.com/Pistonite/magoo/main/magoo.webp) can remove a submodule with ease:
```bash
magoo remove NAME [--force]
magoo remove NAME
```
`--force` will discard any changes made in the submodule (`git submodule deinit --force`)

Note: Newer versions of git lets you delete a submodule with `git rm`. However, it doesn't delete the content in
`.git/modules`. ![magoo](https://raw.githubusercontent.com/Pistonite/magoo/main/magoo.webp) deletes those.
9 changes: 5 additions & 4 deletions README.txtpp.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,7 @@ It also deletes submodules that are deleted by others (by running `status --fix

### Show submodule status
```bash
magoo status [--all]
magoo status --fix [--all]
magoo status [--long] [--all] [--fix]
```
TXTPP#tag MAGOO
TXTPP#include magoo.txt
Expand Down Expand Up @@ -142,10 +141,12 @@ the `BRANCH` specified when you add it.
```

### Remove submodules
TXTPP#tag MAGOO
TXTPP#include magoo.txt
MAGOO can remove a submodule with ease:
```bash
magoo remove NAME [--force]
magoo remove NAME
```
`--force` will discard any changes made in the submodule (`git submodule deinit --force`)

TXTPP#tag MAGOO
TXTPP#include magoo.txt
Expand Down
56 changes: 54 additions & 2 deletions src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,12 @@ impl GitContext {

/// Wrapper implementation for git commands
impl GitContext {
/// Run `git status` and print the status
pub fn status(&self) -> Result<(), GitError> {
self.run_git_command(&["status"], true)?;
Ok(())
}

/// Run `git -C top_level ls-files ...`
pub fn ls_files(&self, extra_args: &[&str]) -> Result<Vec<String>, GitError> {
let top_level_dir = self.top_level_dir()?.display().to_string();
Expand Down Expand Up @@ -387,15 +393,61 @@ impl GitContext {
Ok(())
}

/// Runs `git submodule set-branch`. Path should be from top level
pub fn submodule_set_branch(&self, path: &str, branch: Option<&str>) -> Result<(), GitError> {
let top_level_dir = self.top_level_dir()?.display().to_string();
let mut args = vec!["-C", &top_level_dir, "submodule", "set-branch"];
match branch {
Some(branch) => {
args.push("--branch");
args.push(branch);
}
None => {
args.push("--default");
}
}
args.push("--");
args.push(path);
self.run_git_command(&args, true)?;
Ok(())
}

/// Runs `git submodule set-url`. Path should be from top level
pub fn submodule_set_url(&self, path: &str, url: &str) -> Result<(), GitError> {
let top_level_dir = self.top_level_dir()?.display().to_string();
self.run_git_command(
&[
"-C",
&top_level_dir,
"submodule",
"set-url",
"--",
path,
url,
],
true,
)?;
Ok(())
}

/// Runs `git submodule update [-- <path>]`. Path should be from top level
pub fn submodule_update(&self, path: Option<&str>, force: bool) -> Result<(), GitError> {
pub fn submodule_update(
&self,
path: Option<&str>,
force: bool,
remote: bool,
) -> Result<(), GitError> {
let top_level_dir = self.top_level_dir()?.display().to_string();
let mut args = vec!["-C", &top_level_dir, "submodule", "update"];

if force {
args.push("--force");
}

if remote {
args.push("--remote");
}

if let Some(path) = path {
args.push("--");
args.push(path);
Expand Down Expand Up @@ -523,7 +575,7 @@ pub enum GitError {
LockFailed(String, std::io::Error),

#[error("fix the issues above and try again.")]
NeedFix,
NeedFix(bool /* should show fatal */),
}

/// Helper trait to canonicalize a path and return a [`GitError`] if failed
Expand Down
Loading

0 comments on commit 3492ff1

Please sign in to comment.