MCPcopy Create free account
hub / github.com/atomicdotdev/atomic / install_single_hook

Function install_single_hook

atomic-cli/src/commands/git/hooks.rs:125–158  ·  view source on GitHub ↗

Install a single hook. Returns Ok(true) if newly installed, Ok(false) if already present.

(path: &Path, _name: &str)

Source from the content-addressed store, hash-verified

123
124/// Install a single hook. Returns Ok(true) if newly installed, Ok(false) if already present.
125fn install_single_hook(path: &Path, _name: &str) -> Result<bool, std::io::Error> {
126 let existing = if path.exists() {
127 fs::read_to_string(path)?
128 } else {
129 String::new()
130 };
131
132 // Already installed?
133 if existing.contains(MARKER_BEGIN) {
134 return Ok(false);
135 }
136
137 // Build the section to append
138 let section = format!("\n{}\n{}\n{}\n", MARKER_BEGIN, HOOK_BODY, MARKER_END);
139
140 let new_content = if existing.is_empty() {
141 format!("#!/bin/sh\n{}", section)
142 } else {
143 format!("{}{}", existing.trim_end(), section)
144 };
145
146 fs::write(path, &new_content)?;
147
148 // Make executable (unix only)
149 #[cfg(unix)]
150 {
151 use std::os::unix::fs::PermissionsExt;
152 let mut perms = fs::metadata(path)?.permissions();
153 perms.set_mode(0o755);
154 fs::set_permissions(path, perms)?;
155 }
156
157 Ok(true)
158}
159
160/// Remove Atomic sections from all hooks.
161fn uninstall_hooks() -> CliResult<()> {

Callers 3

install_hooksFunction · 0.85

Calls 6

writeFunction · 0.85
set_permissionsFunction · 0.85
existsMethod · 0.45
containsMethod · 0.45
is_emptyMethod · 0.45
permissionsMethod · 0.45