Unified · Split
| @@ -1,35 +1,33 @@ | |||
| 1 | // cargo run -p git-repository --example new | 1 | // cargo run -p git-repository --example new |
| 2 | 2 | ||
| 3 | use git_repository as git; | 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 | fn main() -> Result<(), Box<dyn std::error::Error>> { | 5 | fn main() -> Result<(), Box<dyn std::error::Error>> { |
| 10 | let path = Path::new(NEW_REPO_PATH); | 6 | let tmp = tempfile::TempDir::new()?; |
| 11 | if path.exists() { | 7 | let work_dir = tmp.path().join("repo-non-bare"); |
| 12 | fs::remove_dir_all(path)?; | 8 | let repo = git::init(work_dir)?; |
| 13 | } | 9 | |
| 14 | let repo = git::init(path)?.apply_environment(); | ||
| 15 | println!( | 10 | println!( |
| 16 | "Repo: {}", | 11 | "Repo: {:?}", |
| 17 | repo.work_dir().as_deref().unwrap_or(repo.git_dir()).display() | 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(); | 14 | |
| 20 | let author = git::actor::Signature { | 15 | let empty_tree_id = repo.write_object(git::objs::Tree::empty())?; |
| 16 | let author = git::actor::SignatureRef { | ||
| 21 | name: "Maria Sanchez".into(), | 17 | name: "Maria Sanchez".into(), |
| 22 | email: "maria@example.com".into(), | 18 | email: "maria@example.com".into(), |
| 23 | time: git_date::Time::now_local_or_utc(), | 19 | time: git_date::Time::now_local_or_utc(), |
| 24 | }; | 20 | }; |
| 25 | let id = repo.commit( | 21 | let id = repo.commit( |
| 26 | "HEAD", | 22 | "HEAD", |
| 27 | author.to_ref(), | 23 | author, |
| 28 | author.to_ref(), | 24 | author, |
| 29 | "initial commit", | 25 | "initial commit", |
| 30 | empty_tree_id, | 26 | empty_tree_id, |
| 31 | git::commit::NO_PARENT_IDS, | 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 | Ok(()) | 32 | Ok(()) |
| 35 | } | 33 | } |