Unified · Split
| @@ -1,35 +1,33 @@ | ||
| 1 | 1 | // cargo run -p git-repository --example new |
| 2 | 2 | |
| 3 | 3 | use git_repository as git; |
| 4 | −use std::fs; | |
| 5 | −use std::path::Path; | |
| 6 | − | |
| 7 | −const NEW_REPO_PATH: &str = "example-new-repo"; | |
| 8 | 4 | |
| 9 | 5 | 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 | + | |
| 15 | 10 | 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") | |
| 18 | 13 | ); |
| 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 { | |
| 21 | 17 | name: "Maria Sanchez".into(), |
| 22 | 18 | email: "maria@example.com".into(), |
| 23 | 19 | time: git_date::Time::now_local_or_utc(), |
| 24 | 20 | }; |
| 25 | 21 | let id = repo.commit( |
| 26 | 22 | "HEAD", |
| 27 | − author.to_ref(), | |
| 28 | − author.to_ref(), | |
| 23 | + author, | |
| 24 | + author, | |
| 29 | 25 | "initial commit", |
| 30 | 26 | empty_tree_id, |
| 31 | 27 | git::commit::NO_PARENT_IDS, |
| 32 | 28 | )?; |
| 33 | − println!("new object id: {:?}", id); | |
| 29 | + println!("new commit id with empty tree: {:?}", id); | |
| 30 | + | |
| 31 | + tmp.into_path(); | |
| 34 | 32 | Ok(()) |
| 35 | 33 | } |