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
|
use std::path::{Path, PathBuf};
#[derive(clap::Parser)]
struct Options {
/// Show more verbose statement.
#[clap(long, short)]
#[clap(global = true)]
#[clap(action = clap::ArgAction::Count)]
verbose: u8,
/// The subcommand.
#[clap(subcommand)]
command: Command,
}
#[derive(clap::Subcommand)]
enum Command {
Clone(CloneCommand),
Fetch(FetchCommand),
Push(PushCommand),
}
/// Clone a repository.
#[derive(clap::Parser)]
struct CloneCommand {
/// The URL of the repository to clone.
#[clap(value_name = "URL")]
repo: String,
/// The path where to clone the repository.
#[clap(value_name = "PATH")]
local_path: Option<PathBuf>,
}
/// Fetch from a remote.
#[derive(clap::Parser)]
struct FetchCommand {
/// The repository to operate on.
#[clap(value_name = "PATH")]
#[clap(short = 'C', long)]
repo: PathBuf,
/// The repository to operate on.
#[clap(value_name = "REMOTE")]
remote: String,
/// The refs to fetch.
#[clap(trailing_var_arg = true)]
#[clap(required = true)]
refspec: Vec<String>,
}
/// Push to a remote.
#[derive(clap::Parser)]
struct PushCommand {
/// The repository to operate on.
#[clap(value_name = "PATH")]
#[clap(short = 'C', long)]
#[clap(default_value = ".")]
repo: PathBuf,
/// The repository to operate on.
#[clap(value_name = "REMOTE")]
remote: String,
/// The refs to fetch.
#[clap(trailing_var_arg = true)]
#[clap(required = true)]
refspec: Vec<String>,
}
fn main() {
if let Err(()) = do_main(clap::Parser::parse()) {
std::process::exit(1);
}
}
fn log_level(verbose: u8) -> log::LevelFilter {
match verbose {
0 => log::LevelFilter::Info,
1 => log::LevelFilter::Debug,
2.. => log::LevelFilter::Trace,
}
}
fn do_main(options: Options) -> Result<(), ()> {
let log_level = log_level(options.verbose);
env_logger::builder()
.parse_default_env()
.filter_module(module_path!(), log_level)
.filter_module("auth_git2", log_level)
.init();
match options.command {
Command::Clone(command) => clone(command),
Command::Fetch(command) => fetch(command),
Command::Push(command) => push(command),
}
}
fn clone(command: CloneCommand) -> Result<(), ()> {
let local_path = command.local_path.as_deref()
.unwrap_or_else(|| Path::new(repo_name_from_url(&command.repo)));
log::info!("Cloning {} into {}", command.repo, local_path.display());
let auth = auth_git2::GitAuthenticator::default();
auth.clone_repo(&command.repo, local_path)
.map_err(|e| log::error!("Failed to clone {}: {}", command.repo, e))?;
Ok(())
}
fn fetch(command: FetchCommand) -> Result<(), ()> {
let repo = git2::Repository::open(&command.repo)
.map_err(|e| log::error!("Failed to open git repo at {}: {e}", command.repo.display()))?;
let refspecs: Vec<_> = command.refspec.iter().map(|x| x.as_str()).collect();
let auth = auth_git2::GitAuthenticator::default();
let mut remote = repo.find_remote(&command.remote)
.map_err(|e| log::error!("Failed to find remote {:?}: {e}", command.remote))?;
auth.fetch(&repo, &mut remote, &refspecs, None)
.map_err(|e| log::error!("Failed to fetch from remote {:?}: {e}", command.remote))?;
Ok(())
}
fn push(command: PushCommand) -> Result<(), ()> {
let repo = git2::Repository::open(&command.repo)
.map_err(|e| log::error!("Failed to open git repo at {}: {e}", command.repo.display()))?;
log::info!("Fetching {:?} from remote {:?}", command.refspec, command.remote);
let refspecs: Vec<_> = command.refspec.iter().map(|x| x.as_str()).collect();
let auth = auth_git2::GitAuthenticator::default();
let mut remote = repo.find_remote(&command.remote)
.map_err(|e| log::error!("Failed to find remote {:?}: {e}", command.remote))?;
auth.push(&repo, &mut remote, &refspecs,)
.map_err(|e| log::error!("Failed to push to remote {:?}: {e}", command.remote))?;
Ok(())
}
fn repo_name_from_url(url: &str) -> &str {
url.rsplit_once('/')
.map(|(_head, tail)| tail)
.unwrap_or(url)
}
|