forge
Explore
Log in

Don't you dare go Hollow

forge

Clone
★ 0

0 watching0 forksRustAGPL-3.09.3 MiBcd4373fTahinli4 hours ago347 commits

setup.rs117 lines, 3.6 KiBHistoryRawDownload
1use std::fs::{self, File};
2use std::io::Write;
3use std::os::unix::fs::PermissionsExt;
4use std::path::Path;
5
6use crate::error::{Error, Result};
7
8fn quote_config_value(v: &str) -> Result<String> {
9 if v.chars().any(char::is_control) {
10 return Err(Error::InvalidInput(format!(
11 "config value contains control characters: {v:?}"
12 )));
13 }
14 Ok(format!(
15 "\"{}\"",
16 v.replace('\\', "\\\\").replace('"', "\\\"")
17 ))
18}
19
20fn write_atomic(dir: &Path, name: &str, contents: &[u8], mode: u32) -> Result<()> {
21 let tmp = dir.join(format!(".{name}.tmp"));
22 {
23 let mut f = fs::OpenOptions::new()
24 .write(true)
25 .create(true)
26 .truncate(true)
27 .open(&tmp)?;
28 f.write_all(contents)?;
29 f.set_permissions(fs::Permissions::from_mode(mode))?;
30 f.sync_all()?;
31 }
32 fs::rename(&tmp, dir.join(name))?;
33 File::open(dir)?.sync_all()?;
34 Ok(())
35}
36
37pub fn write_managed_gitconfig(data_dir: &Path) -> Result<()> {
38 if !data_dir.is_absolute() {
39 return Err(Error::InvalidInput(format!(
40 "data dir must be absolute: {}",
41 data_dir.display()
42 )));
43 }
44 let hooks = data_dir.join("hooks");
45 let hooks = hooks
46 .to_str()
47 .ok_or_else(|| Error::InvalidInput("data dir is not valid UTF-8".into()))?;
48 let hooks = quote_config_value(hooks)?;
49
50 let body = format!(
51 "[core]\n\
52 \thooksPath = {hooks}\n\
53 \tfsync = committed,reference\n\
54 \tfsyncMethod = fsync\n\
55 \tcommitGraph = true\n\
56 [receive]\n\
57 \tfsckObjects = true\n\
58 \tadvertisePushOptions = true\n\
59 \tdenyNonFastForwards = false\n\
60 [transfer]\n\
61 \tfsckObjects = true\n\
62 \thideRefs = refs/forge-internal/\n\
63 [fetch]\n\
64 \tfsckObjects = true\n\
65 \twriteCommitGraph = true\n\
66 [uploadpack]\n\
67 \tallowFilter = true\n\
68 \thideRefs = refs/forge-internal/\n\
69 [protocol \"file\"]\n\
70 \tallow = never\n\
71 [protocol \"ext\"]\n\
72 \tallow = never\n"
73 );
74 fs::create_dir_all(data_dir)?;
75 write_atomic(data_dir, "gitconfig", body.as_bytes(), 0o644)
76}
77
78pub fn write_hook_shims(data_dir: &Path, forge_bin: &Path) -> Result<()> {
79 let bin = forge_bin
80 .to_str()
81 .ok_or_else(|| Error::InvalidInput("forge binary path is not valid UTF-8".into()))?;
82 if !forge_bin.is_absolute() {
83 return Err(Error::InvalidInput(format!(
84 "forge binary path must be absolute: {bin}"
85 )));
86 }
87 if bin.chars().any(|c| c == '\'' || c.is_control()) {
88 return Err(Error::InvalidInput(format!(
89 "forge binary path contains a quote or control character: {bin:?}"
90 )));
91 }
92 let hooks = data_dir.join("hooks");
93 fs::create_dir_all(&hooks)?;
94 for name in ["pre-receive", "post-receive"] {
95 let body = format!("#!/bin/sh\nexec '{bin}' hook {name}\n");
96 write_atomic(&hooks, name, body.as_bytes(), 0o755)?;
97 }
98 Ok(())
99}
100
101#[cfg(test)]
102mod tests {
103 use super::*;
104
105 #[test]
106 fn managed_gitconfig_enables_commit_graph() {
107 let tmp = tempfile::tempdir().expect("tempdir");
108 write_managed_gitconfig(tmp.path()).expect("write gitconfig");
109 let body = fs::read_to_string(tmp.path().join("gitconfig")).expect("read gitconfig");
110 assert!(body.contains("\tcommitGraph = true\n"));
111 assert!(body.contains("\twriteCommitGraph = true\n"));
112 assert!(
113 !body.contains("gc.auto"),
114 "gc.auto must stay at its default"
115 );
116 }
117}