Fork of daniellemaywood.uk/gleam — Wasm codegen work
2

Configure Feed

Select the types of activity you want to include in your feed.

Extract git command helpers in dependencies

+43 -64
+43 -64
compiler-cli/src/dependencies.rs
··· 1086 1086 reason, 1087 1087 }) 1088 1088 } 1089 - Err(error) => Err(match error.kind() { 1090 - ErrorKind::NotFound => Error::ShellProgramNotFound { 1091 - program: "git".into(), 1092 - os: fs::get_os(), 1093 - }, 1089 + Err(error) => Err(git_io_error(error)), 1090 + } 1091 + } 1094 1092 1095 - other => Error::ShellCommand { 1096 - program: "git".into(), 1097 - reason: ShellCommandFailureReason::IoError(other), 1098 - }, 1099 - }), 1093 + /// A `git` command with its working directory set to `dir`. 1094 + fn git_command(dir: &Utf8Path) -> Command { 1095 + let mut command = Command::new("git"); 1096 + let _ = command.current_dir(dir); 1097 + command 1098 + } 1099 + 1100 + /// Map an IO error from spawning `git` onto the appropriate `Error`. 1101 + fn git_io_error(error: std::io::Error) -> Error { 1102 + match error.kind() { 1103 + ErrorKind::NotFound => Error::ShellProgramNotFound { 1104 + program: "git".into(), 1105 + os: fs::get_os(), 1106 + }, 1107 + other => Error::ShellCommand { 1108 + program: "git".into(), 1109 + reason: ShellCommandFailureReason::IoError(other), 1110 + }, 1100 1111 } 1112 + } 1113 + 1114 + /// Parse the trimmed UTF-8 stdout of a `git` command. 1115 + fn git_stdout(output: std::process::Output) -> EcoString { 1116 + String::from_utf8(output.stdout) 1117 + .expect("Output should be UTF-8") 1118 + .trim() 1119 + .into() 1101 1120 } 1102 1121 1103 1122 fn git_repo_dir_name(repo: &str) -> String { ··· 1214 1233 ) -> Result<GitCheckout, Error> { 1215 1234 let clone_path = project_paths.build_packages_package(package_name); 1216 1235 prepare_git_clone(&clone_path, repo)?; 1217 - let _ = execute_command( 1218 - Command::new("git") 1219 - .arg("checkout") 1220 - .arg(ref_) 1221 - .current_dir(&clone_path), 1222 - )?; 1223 - let output = execute_command( 1224 - Command::new("git") 1225 - .arg("rev-parse") 1226 - .arg("HEAD") 1227 - .current_dir(&clone_path), 1228 - )?; 1229 - let commit = String::from_utf8(output.stdout) 1230 - .expect("Output should be UTF-8") 1231 - .trim() 1232 - .into(); 1236 + let _ = execute_command(git_command(&clone_path).arg("checkout").arg(ref_))?; 1237 + let output = execute_command(git_command(&clone_path).arg("rev-parse").arg("HEAD"))?; 1238 + let commit = git_stdout(output); 1233 1239 1234 1240 Ok(GitCheckout::InPlace { commit }) 1235 1241 } ··· 1251 1257 // path. 1252 1258 let staging_path = git_staging_path(project_paths, repo, package_name); 1253 1259 fs::delete_directory(&staging_path)?; 1254 - let _ = Command::new("git") 1260 + let _ = git_command(&clone_path) 1255 1261 .arg("worktree") 1256 1262 .arg("prune") 1257 - .current_dir(&clone_path) 1258 1263 .output(); 1259 1264 1260 1265 let _ = execute_command( 1261 - Command::new("git") 1266 + git_command(&clone_path) 1262 1267 .arg("worktree") 1263 1268 .arg("add") 1264 1269 .arg("--force") 1265 1270 .arg("--detach") 1266 1271 .arg(&staging_path) 1267 - .arg(commit.as_str()) 1268 - .current_dir(&clone_path), 1272 + .arg(commit.as_str()), 1269 1273 )?; 1270 1274 1271 1275 let Some(subdir_source) = resolve_git_subdir(&staging_path, subdir) else { ··· 1300 1304 1301 1305 fs::mkdir(clone_path)?; 1302 1306 1303 - let _ = execute_command(Command::new("git").arg("init").current_dir(clone_path))?; 1307 + let _ = execute_command(git_command(clone_path).arg("init"))?; 1304 1308 1305 1309 // If this directory already exists, but the remote URL has been edited in 1306 1310 // `gleam.toml` without a `gleam clean`, `git remote add` will fail, causing ··· 1308 1312 // first, which ensures that `git remote add` properly add the remote each 1309 1313 // time. If this fails, that means we haven't set the remote in the first 1310 1314 // place, so we can safely ignore the error. 1311 - let _ = Command::new("git") 1315 + let _ = git_command(clone_path) 1312 1316 .arg("remote") 1313 1317 .arg("remove") 1314 1318 .arg("origin") 1315 - .current_dir(clone_path) 1316 1319 .output(); 1317 1320 1318 1321 let _ = execute_command( 1319 - Command::new("git") 1322 + git_command(clone_path) 1320 1323 .arg("remote") 1321 1324 .arg("add") 1322 1325 .arg("origin") 1323 - .arg(repo) 1324 - .current_dir(clone_path), 1326 + .arg(repo), 1325 1327 )?; 1326 1328 1327 - let _ = execute_command( 1328 - Command::new("git") 1329 - .arg("fetch") 1330 - .arg("origin") 1331 - .current_dir(clone_path), 1332 - )?; 1329 + let _ = execute_command(git_command(clone_path).arg("fetch").arg("origin"))?; 1333 1330 1334 1331 Ok(()) 1335 1332 } ··· 1346 1343 ]; 1347 1344 1348 1345 for revision in revisions { 1349 - let result = Command::new("git") 1346 + let result = git_command(clone_path) 1350 1347 .arg("rev-parse") 1351 1348 .arg("--verify") 1352 1349 .arg("--quiet") 1353 1350 .arg(&revision) 1354 - .current_dir(clone_path) 1355 1351 .output(); 1356 1352 1357 1353 match result { 1358 1354 // A failed rev-parse is expected when the ref form doesn't match 1359 1355 // this pattern, so try the next one. 1360 1356 Ok(output) if !output.status.success() => (), 1361 - Ok(output) => { 1362 - let commit = String::from_utf8(output.stdout) 1363 - .expect("Output should be UTF-8") 1364 - .trim() 1365 - .into(); 1366 - return Ok(commit); 1367 - } 1368 - Err(error) => { 1369 - return Err(match error.kind() { 1370 - ErrorKind::NotFound => Error::ShellProgramNotFound { 1371 - program: "git".into(), 1372 - os: fs::get_os(), 1373 - }, 1374 - other => Error::ShellCommand { 1375 - program: "git".into(), 1376 - reason: ShellCommandFailureReason::IoError(other), 1377 - }, 1378 - }); 1379 - } 1357 + Ok(output) => return Ok(git_stdout(output)), 1358 + Err(error) => return Err(git_io_error(error)), 1380 1359 } 1381 1360 } 1382 1361