-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #283 from siketyan/feat/dump-restore
feat: Dump and Restore repositories through a TOML file
- Loading branch information
Showing
13 changed files
with
445 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
use anyhow::Result; | ||
use clap::Parser; | ||
use itertools::Itertools; | ||
|
||
use crate::repository::Repositories; | ||
use crate::root::Root; | ||
use crate::sync::File; | ||
|
||
#[derive(Debug, Parser)] | ||
pub struct Cmd {} | ||
|
||
impl Cmd { | ||
pub fn run(self) -> Result<()> { | ||
let root = Root::find()?; | ||
let file = Repositories::try_collect(&root)? | ||
.into_iter() | ||
.map(|(p, _)| p) | ||
.sorted_by_key(|p| p.to_string()) | ||
.collect::<File>(); | ||
|
||
println!("{}", toml::to_string(&file)?); | ||
|
||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
mod dump; | ||
mod restore; | ||
|
||
use anyhow::Result; | ||
use clap::{Parser, Subcommand}; | ||
|
||
#[derive(Debug, Subcommand)] | ||
pub enum Action { | ||
/// Dump remotes and the current ref of all repositories. | ||
Dump(dump::Cmd), | ||
/// Restore repositories from the dumped file. | ||
Restore(restore::Cmd), | ||
} | ||
|
||
#[derive(Debug, Parser)] | ||
pub struct Cmd { | ||
#[clap(subcommand)] | ||
action: Action, | ||
} | ||
|
||
impl Cmd { | ||
pub async fn run(self) -> Result<()> { | ||
use Action::*; | ||
match self.action { | ||
Dump(cmd) => cmd.run(), | ||
Restore(cmd) => cmd.run().await, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
use std::io::{read_to_string, stdin}; | ||
use std::path::PathBuf; | ||
|
||
use anyhow::Result; | ||
use clap::Parser; | ||
use git2::{ErrorCode, Repository as GitRepository}; | ||
use tracing::info; | ||
|
||
use crate::cmd::clone; | ||
use crate::config::Config; | ||
use crate::console::Spinner; | ||
use crate::git::{CheckoutBranch, Fetch}; | ||
use crate::path::Path; | ||
use crate::root::Root; | ||
use crate::sync::{File, Ref, Repository}; | ||
use crate::url::Url; | ||
|
||
#[derive(Debug, Parser)] | ||
pub struct Cmd {} | ||
|
||
impl Cmd { | ||
pub async fn run(self) -> Result<()> { | ||
let root = Root::find()?; | ||
let config = Config::load_from(&root)?; | ||
let file = toml::from_str::<File>(read_to_string(stdin())?.as_str())?; | ||
|
||
for Repository { | ||
host, | ||
owner, | ||
repo, | ||
r#ref, | ||
remotes, | ||
} in file.repositories | ||
{ | ||
let origin = remotes.iter().find(|r| { | ||
Url::from_str(&r.url, &config.patterns, config.defaults.owner.as_deref()) | ||
.ok() | ||
.map(|u| u.host.to_string() == host) | ||
.unwrap_or_default() | ||
}); | ||
|
||
clone::Cmd { | ||
repo: vec![origin | ||
.map(|r| r.url.to_string()) | ||
.unwrap_or_else(|| format!("{}:{}/{}", host, owner, repo))], | ||
origin: origin.map(|r| r.name.to_string()), | ||
..Default::default() | ||
} | ||
.run() | ||
.await?; | ||
|
||
let path = PathBuf::from(Path::new(&root, host, owner, repo)); | ||
let repo = GitRepository::open(&path)?; | ||
|
||
for remote in remotes { | ||
if let Err(e) = repo | ||
.remote(&remote.name, &remote.url) | ||
.and_then(|_| repo.remote_set_pushurl(&remote.name, remote.push_url.as_deref())) | ||
{ | ||
match e.code() { | ||
ErrorCode::Exists => (), | ||
_ => return Err(e.into()), | ||
} | ||
} | ||
|
||
Spinner::new("Fetching objects from remotes...") | ||
.spin_while(|| async { | ||
config.git.strategy.clone.fetch(&path, &remote.name)?; | ||
Ok::<(), anyhow::Error>(()) | ||
}) | ||
.await?; | ||
|
||
info!("Fetched from remote: {}", &remote.name); | ||
} | ||
|
||
match r#ref { | ||
Some(Ref::Remote(r)) => { | ||
repo.checkout_tree(&repo.revparse_single(&r)?, None)?; | ||
|
||
info!("Successfully checked out a remote ref: {}", &r); | ||
} | ||
Some(Ref::Branch(b)) => { | ||
config.git.strategy.checkout.checkout_branch( | ||
&path, | ||
&b.name, | ||
Some(b.upstream.to_string()), | ||
)?; | ||
|
||
info!("Successfully checked out a branch: {}", &b.name); | ||
} | ||
_ => (), | ||
} | ||
} | ||
|
||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.