Skip to content

Commit 946dd3a

Browse files
committed
Merge branch 'example-new-repo'
2 parents d5396f0 + 3630684 commit 946dd3a

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// creates a repo with user-specified path (which must not exist)
2+
// adds initial commit with empty tree
3+
4+
use anyhow::Context;
5+
use git_repository as git;
6+
7+
fn main() -> anyhow::Result<()> {
8+
// Note use of args_os:
9+
// paths may not be UTF-8 encoded and thus can't be forced into a String.
10+
// gitoxide does not assume encodings that aren't there
11+
// to match the way git does it as a bare minimum and be just as flexible.
12+
let work_dir = std::env::args_os()
13+
.nth(1)
14+
.context("First argument needs to be the directory to initialize the repository in")?;
15+
let repo = git::init(work_dir)?;
16+
17+
println!(
18+
"Repo: {:?}",
19+
repo.work_dir().expect("non-bare repositories have a work-dir")
20+
);
21+
22+
let empty_tree_id = repo.write_object(git::objs::Tree::empty())?;
23+
let author = git::actor::SignatureRef {
24+
name: "Maria Sanchez".into(),
25+
email: "[email protected]".into(),
26+
time: git_date::Time::now_local_or_utc(),
27+
};
28+
let id = repo.commit(
29+
"HEAD",
30+
author,
31+
author,
32+
"initial commit",
33+
empty_tree_id,
34+
git::commit::NO_PARENT_IDS,
35+
)?;
36+
println!("new commit id with empty tree: {:?}", id);
37+
Ok(())
38+
}

0 commit comments

Comments
 (0)