Skip to content

Commit 64fc58b

Browse files
committed
feat: extend init example with --object-format option
1 parent 3f7517a commit 64fc58b

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

examples/init.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#![deny(warnings)]
1616

1717
use clap::Parser;
18+
use git2::ObjectFormat;
1819
use git2::{Error, Repository, RepositoryInitMode, RepositoryInitOptions};
1920
use std::path::{Path, PathBuf};
2021

@@ -40,6 +41,9 @@ struct Args {
4041
#[structopt(name = "perms", long = "shared")]
4142
/// permissions to create the repository with
4243
flag_shared: Option<String>,
44+
#[structopt(name = "object-format", long, value_parser = parse_object_format)]
45+
/// object format to use (sha1 or sha256, requires unstable-sha256 feature)
46+
flag_object_format: Option<ObjectFormat>,
4347
}
4448

4549
fn run(args: &Args) -> Result<(), Error> {
@@ -48,6 +52,7 @@ fn run(args: &Args) -> Result<(), Error> {
4852
&& args.flag_template.is_none()
4953
&& args.flag_shared.is_none()
5054
&& args.flag_separate_git_dir.is_none()
55+
&& args.flag_object_format.is_none()
5156
{
5257
Repository::init(&path)?
5358
} else {
@@ -68,6 +73,12 @@ fn run(args: &Args) -> Result<(), Error> {
6873
if let Some(ref s) = args.flag_shared {
6974
opts.mode(parse_shared(s)?);
7075
}
76+
77+
#[cfg(feature = "unstable-sha256")]
78+
if let Some(format) = args.flag_object_format {
79+
opts.object_format(format);
80+
}
81+
7182
Repository::init_opts(&path, &opts)?
7283
};
7384

@@ -136,6 +147,15 @@ fn parse_shared(shared: &str) -> Result<RepositoryInitMode, Error> {
136147
}
137148
}
138149

150+
fn parse_object_format(format: &str) -> Result<ObjectFormat, Error> {
151+
match format {
152+
"sha1" => Ok(ObjectFormat::Sha1),
153+
#[cfg(feature = "unstable-sha256")]
154+
"sha256" => Ok(ObjectFormat::Sha256),
155+
_ => Err(Error::from_str("object format must be 'sha1' or 'sha256'")),
156+
}
157+
}
158+
139159
fn main() {
140160
let args = Args::parse();
141161
match run(&args) {

0 commit comments

Comments
 (0)