History

refactor

Sebastian Thiel committed 4 years ago

Commit bfc34321 parent 367b8091 file · +14 −16Browse files

Unified · Split

git-repository/examples/new.rs+14−16
@@ -1,35 +1,33 @@
11 // cargo run -p git-repository --example new
22
33 use git_repository as git;
4−use std::fs;
5−use std::path::Path;
6−
7−const NEW_REPO_PATH: &str = "example-new-repo";
84
95 fn main() -> Result<(), Box<dyn std::error::Error>> {
10− let path = Path::new(NEW_REPO_PATH);
11− if path.exists() {
12− fs::remove_dir_all(path)?;
13− }
14− let repo = git::init(path)?.apply_environment();
6+ let tmp = tempfile::TempDir::new()?;
7+ let work_dir = tmp.path().join("repo-non-bare");
8+ let repo = git::init(work_dir)?;
9+
1510 println!(
16− "Repo: {}",
17− repo.work_dir().as_deref().unwrap_or(repo.git_dir()).display()
11+ "Repo: {:?}",
12+ repo.work_dir().expect("non-bare repositories have a work-dir")
1813 );
19− let empty_tree_id = repo.write_object(&git::objs::Tree::empty()).unwrap().detach();
20− let author = git::actor::Signature {
14+
15+ let empty_tree_id = repo.write_object(git::objs::Tree::empty())?;
16+ let author = git::actor::SignatureRef {
2117 name: "Maria Sanchez".into(),
2218 email: "maria@example.com".into(),
2319 time: git_date::Time::now_local_or_utc(),
2420 };
2521 let id = repo.commit(
2622 "HEAD",
27− author.to_ref(),
28− author.to_ref(),
23+ author,
24+ author,
2925 "initial commit",
3026 empty_tree_id,
3127 git::commit::NO_PARENT_IDS,
3228 )?;
33− println!("new object id: {:?}", id);
29+ println!("new commit id with empty tree: {:?}", id);
30+
31+ tmp.into_path();
3432 Ok(())
3533 }