| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
use std::path::{Path, PathBuf}; |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
pub fn git_fetch_cmd(repo: &str) -> String { |
| 23 |
format!("git -C {repo} fetch --all --tags --prune") |
| 24 |
} |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
pub(super) fn tracked_lock_under_patch_cmd(repo: &str) -> String { |
| 45 |
format!( |
| 46 |
"git -C {repo} ls-files --error-unmatch Cargo.lock >/dev/null 2>&1 || exit 1; \ |
| 47 |
d=$(cd {repo} && pwd -P) || exit 1; \ |
| 48 |
while [ -n \"$d\" ] && [ \"$d\" != / ]; do \ |
| 49 |
for c in \"$d/.cargo/config.toml\" \"$d/.cargo/config\"; do \ |
| 50 |
[ -f \"$c\" ] && grep -q '^\\[patch' \"$c\" && exit 0; \ |
| 51 |
done; d=$(dirname \"$d\"); done; exit 1" |
| 52 |
) |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
pub(super) fn tracked_lock_under_patch_problem(repo: &str) -> String { |
| 59 |
format!( |
| 60 |
"`Cargo.lock` is tracked and {repo} sits under a `.cargo/config.toml` \ |
| 61 |
declaring `[patch]`. Cargo re-resolves there and rewrites the lock, so \ |
| 62 |
`cargo publish --dry-run` will refuse the tree as dirty and name only \ |
| 63 |
the lock. Untrack it: `git rm --cached Cargo.lock`. Every other library \ |
| 64 |
in this tree already does. Not `--allow-dirty`, which publishes a lock \ |
| 65 |
nobody reviewed, and not committing the rewritten lock, which resolves \ |
| 66 |
differently on the next machine and fails there instead. The build \ |
| 67 |
worktree is under `~/Code` on purpose so the patch block applies to it; \ |
| 68 |
that is not the half to change." |
| 69 |
) |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
pub fn git_tag_exists_cmd(repo: &str, tag: &str) -> String { |
| 78 |
format!("git -C {repo} rev-parse -q --verify \"refs/tags/{tag}^{{commit}}\"") |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
pub fn git_toplevel_and_prefix_cmd(repo: &str) -> String { |
| 88 |
format!("git -C {repo} rev-parse --show-toplevel --show-prefix") |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
pub fn parse_toplevel_and_prefix(out: &str) -> Option<(String, String)> { |
| 97 |
let mut lines = out.split('\n'); |
| 98 |
let toplevel = lines.next()?.trim().to_string(); |
| 99 |
let prefix = lines.next()?.trim().to_string(); |
| 100 |
(!toplevel.is_empty()).then_some((toplevel, prefix)) |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
pub fn repo_dir_name(toplevel: &str) -> &str { |
| 109 |
toplevel |
| 110 |
.trim_end_matches('/') |
| 111 |
.rsplit('/') |
| 112 |
.next() |
| 113 |
.unwrap_or(toplevel) |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
pub fn app_dir_in_worktree(worktree: &str, prefix: &str) -> String { |
| 119 |
let prefix = prefix.trim_matches('/'); |
| 120 |
if prefix.is_empty() { |
| 121 |
worktree.to_string() |
| 122 |
} else { |
| 123 |
format!("{}/{prefix}", worktree.trim_end_matches('/')) |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
pub fn git_worktree_probe_cmd(worktree: &str) -> String { |
| 133 |
format!("git -C \"{worktree}\" rev-parse --git-dir") |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
pub fn git_worktree_prune_cmd(toplevel: &str) -> String { |
| 140 |
format!("git -C \"{toplevel}\" worktree prune") |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
pub fn git_worktree_add_cmd(toplevel: &str, worktree: &str, tag: &str) -> String { |
| 146 |
format!("git -C \"{toplevel}\" worktree add --detach --force \"{worktree}\" \"{tag}\"") |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
pub fn git_worktree_pin_cmd(worktree: &str, tag: &str) -> String { |
| 156 |
format!("git -C \"{worktree}\" checkout --detach --force \"{tag}\"") |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
pub fn worktree_failure_reason(tag: &str, tag_exists: bool, stderr: &str) -> String { |
| 167 |
if !tag_exists { |
| 168 |
return format!("tag {tag} does not exist there (is it created and pushed?)"); |
| 169 |
} |
| 170 |
let stderr = stderr.trim(); |
| 171 |
if stderr.is_empty() { |
| 172 |
format!("tag {tag} exists, and git said nothing about why") |
| 173 |
} else { |
| 174 |
stderr.to_string() |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
pub fn git_rev_parse_cmd(repo: &str) -> String { |
| 181 |
format!("git -C {repo} rev-parse HEAD") |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
pub fn expand_tilde(p: &str) -> PathBuf { |
| 186 |
if let Some(rest) = p.strip_prefix("~/") |
| 187 |
&& let Ok(home) = std::env::var("HOME") |
| 188 |
{ |
| 189 |
return Path::new(&home).join(rest); |
| 190 |
} |
| 191 |
PathBuf::from(p) |
| 192 |
} |
| 193 |
|
| 194 |
#[cfg(test)] |
| 195 |
mod tests { |
| 196 |
use super::*; |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
#[test] |
| 204 |
fn expand_tilde_handles_home() { |
| 205 |
let home = PathBuf::from(std::env::var("HOME").expect("HOME is set")); |
| 206 |
assert_eq!(expand_tilde("~/Code/x"), home.join("Code/x")); |
| 207 |
assert_eq!(expand_tilde("/abs/path"), PathBuf::from("/abs/path")); |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
#[test] |
| 213 |
fn toplevel_and_prefix_read_both_shapes_of_repo() { |
| 214 |
let (top, prefix) = |
| 215 |
parse_toplevel_and_prefix("/home/max/Code/MNW\npom/\n").expect("two lines"); |
| 216 |
assert_eq!(top, "/home/max/Code/MNW"); |
| 217 |
assert_eq!(prefix, "pom/"); |
| 218 |
|
| 219 |
let (top, prefix) = |
| 220 |
parse_toplevel_and_prefix("/home/max/Code/Libraries/pter\n\n").expect("two lines"); |
| 221 |
assert_eq!(top, "/home/max/Code/Libraries/pter"); |
| 222 |
assert_eq!(prefix, ""); |
| 223 |
assert!( |
| 224 |
parse_toplevel_and_prefix("").is_none(), |
| 225 |
"no answer is not an answer" |
| 226 |
); |
| 227 |
} |
| 228 |
|
| 229 |
#[test] |
| 230 |
fn repo_dir_name_is_the_last_segment_on_every_platform() { |
| 231 |
assert_eq!(repo_dir_name("/home/max/Code/MNW"), "MNW"); |
| 232 |
assert_eq!(repo_dir_name("/home/max/Code/MNW/"), "MNW"); |
| 233 |
|
| 234 |
assert_eq!(repo_dir_name("C:/Users/me/Code/Apps/goingson"), "goingson"); |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
#[test] |
| 239 |
fn app_dir_in_worktree_follows_the_prefix() { |
| 240 |
assert_eq!( |
| 241 |
app_dir_in_worktree("/home/max/Code/.bento/MNW/pom", "pom/"), |
| 242 |
"/home/max/Code/.bento/MNW/pom/pom" |
| 243 |
); |
| 244 |
assert_eq!( |
| 245 |
app_dir_in_worktree("/home/max/Code/.bento/pter/pter", ""), |
| 246 |
"/home/max/Code/.bento/pter/pter" |
| 247 |
); |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
#[test] |
| 253 |
fn worktree_failure_reason_names_the_tag_or_repeats_git() { |
| 254 |
let missing = worktree_failure_reason("pom-v0.4.5", false, "irrelevant"); |
| 255 |
assert!(missing.contains("does not exist"), "{missing}"); |
| 256 |
let held = worktree_failure_reason( |
| 257 |
"pom-v0.4.5", |
| 258 |
true, |
| 259 |
"fatal: '/home/max/Code/.bento/MNW/pom' already exists", |
| 260 |
); |
| 261 |
assert!(held.contains("already exists"), "{held}"); |
| 262 |
let silent = worktree_failure_reason("pom-v0.4.5", true, " "); |
| 263 |
assert!(silent.contains("said nothing"), "{silent}"); |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
fn probe(repo: &std::path::Path) -> bool { |
| 270 |
std::process::Command::new("sh") |
| 271 |
.arg("-c") |
| 272 |
.arg(tracked_lock_under_patch_cmd(&repo.display().to_string())) |
| 273 |
.status() |
| 274 |
.unwrap() |
| 275 |
.success() |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
#[test] |
| 285 |
fn a_tracked_lock_is_only_a_problem_under_a_patch_block() { |
| 286 |
let root = tempfile::tempdir().unwrap(); |
| 287 |
let repo = root.path().join("crate"); |
| 288 |
std::fs::create_dir_all(&repo).unwrap(); |
| 289 |
let git = |args: &[&str]| { |
| 290 |
std::process::Command::new("git") |
| 291 |
.args(args) |
| 292 |
.current_dir(&repo) |
| 293 |
.env("GIT_AUTHOR_NAME", "t") |
| 294 |
.env("GIT_AUTHOR_EMAIL", "t@t") |
| 295 |
.env("GIT_COMMITTER_NAME", "t") |
| 296 |
.env("GIT_COMMITTER_EMAIL", "t@t") |
| 297 |
.output() |
| 298 |
.unwrap() |
| 299 |
}; |
| 300 |
git(&["init", "-q", "."]); |
| 301 |
std::fs::write(repo.join("Cargo.lock"), "# lock\n").unwrap(); |
| 302 |
|
| 303 |
|
| 304 |
assert!(!probe(&repo)); |
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
git(&["add", "Cargo.lock"]); |
| 309 |
git(&["commit", "-qm", "lock"]); |
| 310 |
assert!(!probe(&repo)); |
| 311 |
|
| 312 |
|
| 313 |
std::fs::create_dir_all(root.path().join(".cargo")).unwrap(); |
| 314 |
std::fs::write( |
| 315 |
root.path().join(".cargo/config.toml"), |
| 316 |
"[patch.\"https://makenot.work/git/max/docengine.git\"]\ndocengine = { path = \"x\" }\n", |
| 317 |
) |
| 318 |
.unwrap(); |
| 319 |
assert!(probe(&repo)); |
| 320 |
|
| 321 |
|
| 322 |
git(&["rm", "-q", "--cached", "Cargo.lock"]); |
| 323 |
assert!(!probe(&repo)); |
| 324 |
} |
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
#[test] |
| 329 |
fn the_tracked_lock_message_names_both_facts_and_refuses_both_wrong_fixes() { |
| 330 |
let msg = tracked_lock_under_patch_problem("~/Code/.bento/pter/pter"); |
| 331 |
assert!(msg.contains("Cargo.lock"), "{msg}"); |
| 332 |
assert!(msg.contains("[patch]"), "{msg}"); |
| 333 |
assert!(msg.contains("git rm --cached"), "{msg}"); |
| 334 |
assert!(msg.contains("--allow-dirty"), "{msg}"); |
| 335 |
assert!(msg.contains("~/Code/.bento/pter/pter"), "{msg}"); |
| 336 |
} |
| 337 |
} |
| 338 |
|