1use grit_lib::objects::{serialize_commit, CommitData, ObjectKind};
6use grit_lib::refs;
7use grit_lib::repo::init_repository;
8use grit_lib::repo::Repository;
9use grit_lib::rev_list::{rev_list, OutputMode, RevListOptions};
10use grit_lib::write_tree::write_tree_from_index;
11
12fn make_initial_commit(repo: &Repository) -> grit_lib::error::Result<grit_lib::objects::ObjectId> {
13 use grit_lib::index::{Index, IndexEntry, MODE_REGULAR};
14
15 let blob_oid = repo.odb.write(ObjectKind::Blob, b"log line\n")?;
16 let path = b"file.txt".to_vec();
17 let entry = IndexEntry {
18 ctime_sec: 0,
19 ctime_nsec: 0,
20 mtime_sec: 0,
21 mtime_nsec: 0,
22 dev: 0,
23 ino: 0,
24 mode: MODE_REGULAR,
25 uid: 0,
26 gid: 0,
27 size: 0,
28 oid: blob_oid,
29 flags: (path.len().min(0xfff)) as u16,
30 flags_extended: None,
31 path,
32 base_index_pos: 0,
33 };
34 let mut index = Index::new();
35 index.add_or_replace(entry);
36 repo.write_index(&mut index)?;
37 let index = repo.load_index()?;
38 let tree_oid = write_tree_from_index(&repo.odb, &index, "")?;
39
40 let commit = CommitData {
41 tree: tree_oid,
42 parents: Vec::new(),
43 author: "Example <example@example.com> 1700000000 +0000".to_owned(),
44 committer: "Example <example@example.com> 1700000000 +0000".to_owned(),
45 author_raw: Vec::new(),
46 committer_raw: Vec::new(),
47 encoding: None,
48 message: "root\n".to_owned(),
49 raw_message: None,
50 };
51 let oid = repo
52 .odb
53 .write(ObjectKind::Commit, &serialize_commit(&commit))?;
54 refs::write_ref(&repo.git_dir, "refs/heads/main", &oid)?;
55 Ok(oid)
56}
57
58fn main() -> grit_lib::error::Result<()> {
59 let root = tempfile::tempdir()?;
60 let repo = init_repository(root.path(), false, "main", None, "files")?;
61 let _root_commit = make_initial_commit(&repo)?;
62
63 let mut opts = RevListOptions::default();
64 opts.output_mode = OutputMode::OidOnly;
65 let result = rev_list(&repo, &[String::from("HEAD")], &[], &opts)?;
66
67 println!("commits from HEAD (oldest-first internal order may vary):");
68 for oid in &result.commits {
69 println!(" {oid}");
70 }
71
72 Ok(())
73}