Install a single hook. Returns Ok(true) if newly installed, Ok(false) if already present.
(path: &Path, _name: &str)
| 123 | |
| 124 | /// Install a single hook. Returns Ok(true) if newly installed, Ok(false) if already present. |
| 125 | fn 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. |
| 161 | fn uninstall_hooks() -> CliResult<()> { |