╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ net.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1231 var writer = file.writer(io, &.{}); 1232 try writer.interface.writeAll("EDIT"); 1233 }, 1234 else => {}, 1235 } 1236 } 1237 } 1238 1239 try client_repo.add(io, allocator, &.{dir_name}); 1240 } 1241 1242 const commit2 = try client_repo.commit(io, allocator, .{ .message = "more stuff" }); 1243 1244 // add remote 1245 { 1246 if (.windows == builtin.os.tag) { 1247 std.mem.replaceScalar(u8, server_path, '\\', '/'); 1248 } 1249 const separator = if (server_path[0] == '/') "" else "/"; 1250 1251 const remote_url = switch (transport_def) { 1252 //.file => try std.fmt.allocPrint(allocator, "file://{s}{s}", .{ separator, server_path }), 1253 .file => try std.fmt.allocPrint(allocator, "../server", .{}), // relative file paths work too 1254 .wire => |wire_kind| switch (wire_kind) { 1255 .http => try std.fmt.allocPrint(allocator, "http://localhost:{}/server", .{http_port}), 1256 .raw => try std.fmt.allocPrint(allocator, "git://localhost:{}/server", .{http_port}), 1257 .ssh => try std.fmt.allocPrint(allocator, "ssh://localhost:{}{s}{s}", .{ ssh_port, separator, server_path }), 1258 }, 1259 }; 1260 defer allocator.free(remote_url); 1261 1262 try client_repo.addRemote(io, allocator, .{ .name = "origin", .value = remote_url }); 1263 try client_repo.addConfig(io, allocator, .{ .name = "branch.master.remote", .value = "origin" }); 1264 } 1265 1266 const is_ssh = switch (transport_def) { 1267 .file => false, 1268 .wire => |wire_kind| .ssh == wire_kind, 1269 }; 1270 1271 if (shell_out_to_git) { 1272 const priv_key_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "key" }); 1273 defer allocator.free(priv_key_path); 1274 const ssh_config_arg = try std.fmt.allocPrint(allocator, "core.sshCommand=ssh -o BatchMode=yes -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o IdentitiesOnly=yes -o IdentityFile={s}", .{priv_key_path}); 1275 defer allocator.free(ssh_config_arg); 1276 1277 // shell out to git so it will send delta objects 1278 var process = try std.process.spawn(io, .{ 1279 .argv = if (is_ssh) 1280 &.{ "git", "-c", ssh_config_arg, "push", "origin", "master" } 1281 else 1282 &.{ "git", "push", "origin", "master" }, 1283 .cwd = .{ .path = client_path }, 1284 .stdin = .ignore, 1285 .stdout = .ignore, 1286 .stderr = .ignore, 1287 }); 1288 const term = try process.wait(io); 1289 if (term != .exited or term.exited != 0) { 1290 return error.GitCommandFailed; 1291 } 1292 } else { 1293 const ssh_cmd_maybe: ?[]const u8 = if (is_ssh) blk: { 1294 const known_hosts_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "known_hosts" }); 1295 defer allocator.free(known_hosts_path); 1296 1297 const priv_key_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "key" }); 1298 defer allocator.free(priv_key_path); 1299 1300 break :blk try std.fmt.allocPrint(allocator, "ssh -o UserKnownHostsFile=\"{s}\" -o IdentityFile=\"{s}\"", .{ known_hosts_path, priv_key_path }); 1301 } else null; 1302 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 1303 1304 try client_repo.push( 1305 io, 1306 allocator, 1307 "origin", 1308 "master", 1309 false, 1310 .{ .wire = .{ .ssh = .{ 1311 .command = ssh_cmd_maybe, 1312 } } }, 1313 ); 1314 1315 if (transport_def == .file) { 1316 // update the working dir 1317 try server_repo.restore(io, allocator, "."); 1318 } 1319 } 1320 1321 // make sure push was successful 1322 { 1323 const oid_master = (try server_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 1324 try std.testing.expectEqualStrings(&commit2, &oid_master); 1325 1326 const hello_txt = try temp_dir.openFile(io, "repos/server/hello.txt", .{}); 1327 defer hello_txt.close(io); 1328 } 1329 } 1330 1331 fn copyDir(io: std.Io, src_dir: std.Io.Dir, dest_dir: std.Io.Dir) !void { 1332 var iter = src_dir.iterate(); 1333 while (try iter.next(io)) |entry| { 1334 switch (entry.kind) { 1335 .file => try src_dir.copyFile(entry.name, dest_dir, entry.name, io, .{}), 1336 .directory => { 1337 try dest_dir.createDirPath(io, entry.name); 1338 var dest_entry_dir = try dest_dir.openDir(io, entry.name, .{ .access_sub_paths = true, .iterate = true, .follow_symlinks = false }); 1339 defer dest_entry_dir.close(io); 1340 var src_entry_dir = try src_dir.openDir(io, entry.name, .{ .access_sub_paths = true, .iterate = true, .follow_symlinks = false }); 1341 defer src_entry_dir.close(io); 1342 try copyDir(io, src_entry_dir, dest_entry_dir); 1343 }, 1344 else => {}, 1345 } 1346 } 1347 } 1348