Skip to main content

max / makenotwork

2.0 KB · 69 lines History Blame Raw
1 //! Building bare git repositories for the git browser, issue and SSH tests.
2 //!
3 //! Written with gitoxide, the same library the server reads through.
4
5 use gix::objs::tree::EntryKind;
6
7 /// Initialize a bare repo at `<dir>/<owner>/<name>.git`.
8 pub(crate) fn init_bare(dir: &std::path::Path, owner: &str, name: &str) -> gix::Repository {
9 let bare_path = dir.join(owner).join(format!("{name}.git"));
10 std::fs::create_dir_all(&bare_path).unwrap();
11 gix::init_bare(&bare_path).unwrap()
12 }
13
14 /// Write a blob and return its id.
15 pub(crate) fn blob(repo: &gix::Repository, content: &[u8]) -> gix::ObjectId {
16 repo.write_blob(content).unwrap().detach()
17 }
18
19 /// Write a tree from `(name, id, kind)` entries.
20 pub(crate) fn tree(
21 repo: &gix::Repository,
22 entries: &[(&str, gix::ObjectId, EntryKind)],
23 ) -> gix::ObjectId {
24 let mut tree = gix::objs::Tree::empty();
25 for (name, oid, kind) in entries {
26 tree.entries.push(gix::objs::tree::Entry {
27 mode: (*kind).into(),
28 filename: (*name).into(),
29 oid: *oid,
30 });
31 }
32 tree.entries.sort();
33 repo.write_object(&tree).unwrap().detach()
34 }
35
36 /// Commit a tree onto `refs/heads/main` with a fixed test identity.
37 pub(crate) fn commit(
38 repo: &gix::Repository,
39 message: &str,
40 tree: gix::ObjectId,
41 parents: Vec<gix::ObjectId>,
42 ) -> gix::ObjectId {
43 let signature = gix::actor::SignatureRef {
44 name: "Test".into(),
45 email: "test@example.com".into(),
46 time: "1700000000 +0000",
47 };
48 repo.commit_as(
49 signature,
50 signature,
51 "refs/heads/main",
52 message,
53 tree,
54 parents,
55 )
56 .unwrap()
57 .detach()
58 }
59
60 /// Tip commit sha of `refs/heads/main` in an on-disk bare repo.
61 pub(crate) fn main_tip_sha(dir: &std::path::Path, owner: &str, name: &str) -> String {
62 let repo = gix::open(dir.join(owner).join(format!("{name}.git"))).unwrap();
63 repo.find_reference("refs/heads/main")
64 .unwrap()
65 .peel_to_id()
66 .unwrap()
67 .to_string()
68 }
69