-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclone.rs
More file actions
70 lines (54 loc) · 1.52 KB
/
Copy pathclone.rs
File metadata and controls
70 lines (54 loc) · 1.52 KB
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
use anyhow::Result;
use clap::Parser;
use std::process::Command;
#[derive(Parser)]
pub struct CloneArgs {
/// Repository URL or path to clone
repository: String,
/// Directory to clone into (defaults to repository name)
directory: Option<String>,
/// Clone specific branch
#[arg(short, long)]
branch: Option<String>,
}
pub fn run(args: CloneArgs) -> Result<()> {
let repository = &args.repository;
let directory = &args.directory;
let branch = &args.branch;
println!();
println!(" ◇ cloning repository...");
let mut cmd = Command::new("git");
cmd.arg("clone");
if let Some(b) = branch {
cmd.arg("--branch").arg(b);
}
cmd.arg(repository);
if let Some(d) = directory {
cmd.arg(d);
}
let status = cmd
.output()
.map_err(|e| anyhow::anyhow!("Failed to run 'git clone': {}", e))?
.status;
if !status.success() {
anyhow::bail!("git clone failed");
}
println!(" ◇ clone completed ✓");
let target_dir = if let Some(d) = directory {
d.to_string()
} else {
repository
.split('/')
.next_back()
.unwrap_or(repository)
.trim_end_matches(".git")
.to_string()
};
std::env::set_current_dir(&target_dir)
.map_err(|e| anyhow::anyhow!("Failed to enter directory '{}': {}", target_dir, e))?;
println!();
println!(" ◇ running gitkit init...");
println!();
crate::init::run()?;
Ok(())
}