╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ serve_ssh.zig ────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯
264 // event loop. nextEvent blocks until something interesting arrives; 265 // for each event, rebuild the widget tree and re-render so the user 266 // sees the effect of their input on the same iteration. 267 event_loop: while (true) { 268 const event = try nextTuiEvent(sess, if (terminal_maybe) |*terminal| terminal else null); 269 if (event) |ev| switch (ev) { 270 .data => |payload| { 271 defer allocator.free(payload); 272 if (terminal_maybe) |*terminal| { 273 try terminal.writeBytes(payload); 274 } else { 275 // the copyable text stays up until enter 276 if (std.mem.indexOfAny(u8, payload, "\r\n") == null) continue; 277 var terminal = try StreamTerminal.init(allocator, &session_writer.interface, terminal_size); 278 terminal.render_state.no_color = !color; 279 terminal_maybe = terminal; 280 } 281 }, 282 .resize => |sz| { 283 terminal_size = .{ .width = sz.width_cells, .height = sz.height_cells }; 284 if (terminal_maybe) |*terminal| terminal.pushResize(terminal_size) else continue; 285 }, 286 .eof, .close => break :event_loop, 287 }; 288 if (terminal_maybe) |*terminal| { 289 while (terminal.popKey()) |key| { 290 try ui.inputKey(allocator, &nav.root, key, &ui_session); 291 } 292 } 293 294 if (ui_session.host_request) |request| { 295 ui_session.host_request = null; 296 switch (request) { 297 .show_copyable_text => |copyable_text| { 298 if (terminal_maybe) |*terminal| terminal.deinit(); 299 terminal_maybe = null; 300 try session_writer.interface.print("\x1b[2J\x1b[Hcopy the following text and then press enter to go back:\r\n\r\n{s}\r\n", .{copyable_text}); 301 try session_writer.interface.flush(); 302 continue; 303 }, 304 .sync_events => {}, 305 .undo => |target| ui.Repo.Undo.handleRequest(allocator, &ui_session, target), 306 } 307 } 308 309 // pick up data written by other handles so the next navigation 310 // builds its page from a current moment 311 try ui_session.reloadMoment(allocator, &repo); 312 313 // reconcile navigation: forward to a new page, or back on escape. 314 // the terminal background can change at any time, so recheck it on each page 315 if (try nav.sync(allocator, &ui_session)) { 316 if (terminal_maybe) |*terminal| try terminal.queryBackground(); 317 } 318 319 // the quit button (on the quit tab) asks the host to tear down 320 if (ui_session.quit_requested) break :event_loop; 321 322 try nav.root.build(allocator, .{ 323 .min_size = .{ .width = null, .height = null }, 324 .max_size = .{ .width = terminal_size.width, .height = terminal_size.height }, 325 }, nav.root.getFocus()); 326 if (terminal_maybe) |*terminal| _ = try terminal.render(&nav.root); 327 } 328 } 329 330 // only ambiguous input (a lone ESC or ESC ]) needs a deadline; longer 331 // fragments must remain buffered until complete. a timeout leaves partial SSH 332 // packets in the reader. 333 pub fn nextTuiEvent(sess: *ssh.SessionCtx, terminal: ?*StreamTerminal) !?ssh.Event { 334 if (terminal) |t| { 335 if (t.parser.isAmbiguous()) { 336 sess.conn.read_timeout = escape_timeout.toDeadline(sess.conn.io); 337 defer sess.conn.read_timeout = .none; 338 return sess.nextEvent() catch |err| switch (err) { 339 error.Timeout => { 340 try t.flushEscape(); 341 return null; 342 }, 343 else => return err, 344 }; 345 } 346 } 347 return try sess.nextEvent(); 348 } 349 350 // --------------------------------------------------------------------------- 351 // git path (exec) 352 // --------------------------------------------------------------------------- 353 354 const GitService = enum { upload_pack, receive_pack }; 355 356 const ParsedGitCommand = struct { 357 service: GitService, 358 dir: []u8, 359 360 fn deinit(self: ParsedGitCommand, allocator: std.mem.Allocator) void { 361 allocator.free(self.dir); 362 } 363 }; 364 365 fn runGitSession(handler: *const SessionHandler, sess: *ssh.SessionCtx, exec: ssh.Request.Exec) !void { 366 const allocator = sess.conn.allocator; 367 const io = sess.conn.io; 368 const protocol_version = xit.net_server_common.parseProtocolVersion(exec.git_protocol); 369 370 // the repo's pack functions consume the channel like any other stream. 371 // whatever they leave buffered is flushed before the exit status goes out. 372 var reader_buf: [4096]u8 = undefined; 373 var writer_buf: [4096]u8 = undefined; 374 var reader = ssh.SessionReader.init(sess, &reader_buf); 375 var writer = ssh.SessionWriter.init(sess, &writer_buf); 376 defer writer.interface.flush() catch {}; 377 378 const parsed = parseGitCommand(allocator, exec.command) catch return writeError(sess, "unsupported command (expected git-upload-pack or git-receive-pack)"); 379 defer parsed.deinit(allocator); 380 const repo_identity = std.mem.trimStart(u8, parsed.dir, "/"); 381 382 // a "+" names a fork's patch draft 383 if (std.mem.indexOfScalar(u8, repo_identity, '+') != null) { 384 return runForkSession(handler, sess, repo_identity, parsed.service, protocol_version, &reader.interface, &writer.interface); 385 } 386 387 const owner_repo = evt.parseOwnerRepoPath(repo_identity) orelse return writeError(sess, "repo path must be <owner>:<repo>"); 388 var allowed_arena = std.heap.ArenaAllocator.init(allocator); 389 defer allowed_arena.deinit(); 390 const allowed = switch (try authorizeRepoKey(io, &allowed_arena, handler.admin_repo_path, handler.server.users_dir, owner_repo.owner, owner_repo.name, parsed.service, &sess.fingerprint)) { 391 .allowed => |value| value, 392 .create => |creator| return runCreatingPush(handler, sess, &creator, owner_repo.name, protocol_version, &reader.interface, &writer.interface), 393 .denied => return switch (parsed.service) { 394 .upload_pack => writeError(sess, "unauthorized: this SSH key cannot read this repo"), 395 .receive_pack => writeError(sess, "unauthorized: this SSH key cannot push to this repo"), 396 }, 397 .not_found => return writeError(sess, "repo not found"), 398 }; 399 400 const repo_path = switch (try serve_common.resolveRepoPath(io, allocator, handler.server.users_dir, handler.admin_repo_path, repo_identity)) { 401 .ok => |p| p, 402 .invalid => unreachable, // parsed above 403 .not_found => return writeError(sess, "repo not found"), 404 }; 405 defer allocator.free(repo_path); 406 407 var any_repo = try rp.AnyRepo(.xit, any_repo_opts).open(io, allocator, .{ .path = repo_path }); 408 defer any_repo.deinit(io, allocator); 409 410 switch (any_repo) { 411 inline else => |*repo| switch (allowed) { 412 .fetch => { 413 var sideband = progress.Sideband{ .writer = &writer.interface, .sess = sess }; 414 try repo.uploadPack(io, allocator, &reader.interface, &writer.interface, .{ .protocol_version = protocol_version }, &sideband); 415 }, 416 .push => |pusher| try push.receivePackAndConsume(repo.self_repo_opts, io, allocator, repo, &reader.interface, &writer.interface, .{ .protocol_version = protocol_version, .protected_branches = pusher.protected_branches, .locked_branches = pusher.locked_branches }, pusher.author, &pusher.user_id, handler.server, handler.err, sess), 417 }, 418 } 419 } 420 421 // push to a repo that doesn't exist yet, creating it in the hash format the 422 // client's first command names 423 fn runCreatingPush( 424 handler: *const SessionHandler, 425 sess: *ssh.SessionCtx, 426 creator: *const RepoCreator, 427 name: []const u8, 428 protocol_version: xit.net_server_common.ProtocolVersion, 429 reader: *std.Io.Reader, 430 writer: *std.Io.Writer, 431 ) !void { 432 const allocator = sess.conn.allocator; 433 const io = sess.conn.io; 434 435 evt.Repo.validateName(name) catch return writeError(sess, "invalid repo name"); 436 437 try xit.net_server_receive_pack.advertiseUncreated(.xit, writer, .{ .protocol_version = protocol_version }); 438 try writer.flush(); 439 // a client with nothing to push creates nothing 440 const hash_kind = (try xit.net_server_receive_pack.peekObjectFormat(reader)) orelse return; 441 442 const location = evt.createRepo(io, allocator, handler.server.users_dir, &creator.owner_id, creator.owner, name, "", .private, hash_kind) catch |err| switch (err) { 443 error.NameTaken => return writeError(sess, "repo was just created by another push, try again"), 444 else => |e| return e, 445 }; 446 const repo_path = try evt.repoPath(allocator, handler.server.users_dir, &location.owner_id, &location.repo_id); 447 defer allocator.free(repo_path); 448 449 switch (hash_kind) { 450 inline else => |kind| { 451 var repo = try rp.Repo(.xit, any_repo_opts.toRepoOptsWithHash(kind)).open(io, allocator, .{ .path = repo_path }); 452 defer repo.deinit(io, allocator); 453 // stateless since the advertisement already went out 454 try push.receivePackAndConsume(repo.self_repo_opts, io, allocator, &repo, reader, writer, .{ .protocol_version = protocol_version, .is_stateless = true }, creator.owner, &creator.owner_id, handler.server, handler.err, sess); 455 }, 456 } 457 } 458 459 // serve a patch draft: anyone may fetch it, only its author may push to it 460 fn runForkSession( 461 handler: *const SessionHandler, 462 sess: *ssh.SessionCtx, 463 fork_path: []const u8, 464 service: GitService, 465 protocol_version: xit.net_server_common.ProtocolVersion, 466 reader: *std.Io.Reader, 467 writer: *std.Io.Writer, 468 ) !void { 469 const allocator = sess.conn.allocator; 470 const io = sess.conn.io; 471 472 const route = fork.parseRoute(fork_path) orelse return writeError(sess, "invalid fork path"); 473 // the route names the forker and the target repo 474 const forker_repo = evt.parseOwnerRepoPath(route.identity) orelse return writeError(sess, "repo path must be <forker>:<repo>"); 475 476 var admin = try rp.Repo(.xit, evt.admin_repo_opts).open(io, allocator, .{ .path = handler.admin_repo_path }); 477 defer admin.deinit(io, allocator); 478 var admin_arena = std.heap.ArenaAllocator.init(allocator); 479 defer admin_arena.deinit(); 480 const admin_moment = try evt.currentMoment(evt.admin_repo_opts, &admin); 481 const fork_id = try evt.parseEventId(&route.id); 482 const forker_id = (try evt.User.readIdByName(evt.AdminDB, evt.admin_repo_opts.hash, admin_moment, forker_repo.owner)) orelse 483 return writeError(sess, "patch draft not found"); 484 const fork_record = (try evt.readForkById(io, allocator, &admin_arena, handler.server.users_dir, &forker_id, &fork_id)) orelse 485 return writeError(sess, "patch draft not found"); 486 if (fork_record.removed) return writeError(sess, "patch draft not found"); 487 const user = (try evt.User.readById(evt.AdminDB, evt.admin_repo_opts.hash, admin_moment, &admin_arena, &forker_id)) orelse 488 return writeError(sess, "invalid patch draft"); 489 if (user.removed) return writeError(sess, "invalid patch draft"); 490 if (service == .receive_pack and !try evt.User.holdsKey(evt.AdminDB, evt.admin_repo_opts.hash, admin_moment, &admin_arena, &forker_id, &sess.fingerprint)) 491 return writeError(sess, "unauthorized: this SSH key is not registered to the patch author"); 492 493 const target_owner_id = try evt.parseEventId(&fork_record.event.repo_user_id); 494 const target_id = try evt.parseEventId(&fork_record.event.repo_id); 495 const target = (try evt.readRepoById(io, allocator, &admin_arena, handler.server.users_dir, &target_owner_id, &target_id, forker_id)) orelse 496 return writeError(sess, "repo not found"); 497 // patches that are off take their drafts with them 498 const patch_role = target.repo.event.patch_role orelse return writeError(sess, "patch draft not found"); 499 if (service == .receive_pack) { 500 const role = target.role orelse return writeError(sess, "repo not found"); 501 if (!role.atLeast(patch_role)) return writeError(sess, "unauthorized: your role in this repo may not write patches"); 502 } 503 504 const draft_path = try fork.forkPath(allocator, handler.server.users_dir, &forker_id, &fork_id); 505 defer allocator.free(draft_path); 506 var draft = rp.AnyRepo(.xit, any_repo_opts).open(io, allocator, .{ .path = draft_path }) catch 507 return writeError(sess, "patch draft not found"); 508 defer draft.deinit(io, allocator); 509 if (service == .upload_pack) { 510 var sideband = progress.Sideband{ .writer = writer, .sess = sess }; 511 switch (draft) { 512 inline else => |*repo| try repo.uploadPack(io, allocator, reader, writer, .{ .protocol_version = protocol_version }, &sideband), 513 } 514 } else { 515 if (!std.mem.eql(u8, target.repo.event.name, forker_repo.name)) return writeError(sess, "patch draft belongs to another repo"); 516 const target_path = try evt.repoPath(allocator, handler.server.users_dir, &target_owner_id, &target_id); 517 defer allocator.free(target_path); 518 const author: evt.CommitAuthor = .{ .name = user.event.name, .email = user.event.email }; 519 const timestamp: u64 = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()); 520 switch (draft) { 521 inline else => |*repo| { 522 var target_repo = rp.Repo(.xit, repo.self_repo_opts).open(io, allocator, .{ .path = target_path }) catch 523 return writeError(sess, "repo not found or has the wrong hash"); 524 defer target_repo.deinit(io, allocator); 525 try push.receiveFork(repo.self_repo_opts, io, allocator, repo, &target_repo, handler.server, &route.id, author, timestamp, reader, writer, handler.err, sess); 526 }, 527 } 528 } 529 } 530 531 fn parseGitCommand(allocator: std.mem.Allocator, command: []const u8) !ParsedGitCommand { 532 var tokens = try std.process.Args.IteratorGeneral(.{ .single_quotes = true }).init(allocator, command); 533 defer tokens.deinit(); 534 535 const service_token = tokens.next() orelse return error.InvalidCommand; 536 const dir_token = tokens.next() orelse return error.InvalidCommand; 537 538 const service: GitService = 539 if (std.mem.eql(u8, service_token, "git-upload-pack") or std.mem.eql(u8, service_token, "upload-pack")) 540 .upload_pack 541 else if (std.mem.eql(u8, service_token, "git-receive-pack") or std.mem.eql(u8, service_token, "receive-pack")) 542 .receive_pack 543 else 544 return error.UnsupportedService; 545 546 return .{ .service = service, .dir = try allocator.dupe(u8, dir_token) }; 547 } 548 549 const RepoAuthorization = union(enum) { allowed: Allowed, create: RepoCreator, denied, not_found }; 550 551 // what a key may do in an existing repo 552 const Allowed = union(enum) { 553 fetch, 554 push: Pusher, 555 }; 556 557 // the key holder a push acts as 558 const Pusher = struct { 559 user_id: [evt.event_id_size]u8, 560 author: evt.CommitAuthor, 561 protected_branches: []const []const u8, 562 locked_branches: []const []const u8, 563 }; 564 565 // the owner of a repo a push may create 566 const RepoCreator = struct { 567 owner_id: [evt.event_id_size]u8, 568 owner: evt.CommitAuthor, 569 }; 570 571 fn authorizeRepoKey( 572 io: std.Io, 573 arena: *std.heap.ArenaAllocator, 574 admin_repo_path: []const u8, 575 users_dir: []const u8, 576 owner_name: []const u8, 577 repo_name: []const u8, 578 service: GitService, 579 fingerprint: *const [ssh.fingerprint_len]u8, 580 ) !RepoAuthorization { 581 const allocator = arena.child_allocator; 582 var admin = rp.Repo(.xit, evt.admin_repo_opts).open(io, allocator, .{ .path = admin_repo_path }) catch |err| switch (err) { 583 error.RepoNotFound => return .not_found, 584 else => |e| return e, 585 }; 586 defer admin.deinit(io, allocator); 587 588 const moment = try evt.currentMoment(evt.admin_repo_opts, &admin); 589 // no viewer yet, so this carries the base role 590 const repo = (try evt.readRepoByOwnerAndName(io, allocator, arena, moment, users_dir, owner_name, repo_name, null)) orelse { 591 if (service == .upload_pack) return .not_found; 592 const owner_id = (try evt.User.readIdByName(evt.AdminDB, evt.admin_repo_opts.hash, moment, owner_name)) orelse return .not_found; 593 if (!try evt.User.holdsKey(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, &owner_id, fingerprint)) return .denied; 594 return .{ .create = .{ .owner_id = owner_id, .owner = try commitAuthor(moment, arena, &owner_id) } }; 595 }; 596 597 const min_role: evt.Repo.Role = switch (service) { 598 .upload_pack => .read, 599 .receive_pack => .write, 600 }; 601 // any key may fetch a public repo; a push always needs a key holder 602 if (service == .upload_pack and repo.role != null) return .{ .allowed = .fetch }; 603 604 // a key can be shared, so the push acts as the holder with the highest role 605 const owner_id = try evt.parseEventId(&repo.repo.event.user_id); 606 var best: ?struct { id: [evt.event_id_size]u8, role: evt.Repo.Role } = null; 607 for (try evt.User.readIdsByFingerprint(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, fingerprint)) |holder_id| { 608 const seen = (try evt.readRepoById(io, allocator, arena, users_dir, &owner_id, &repo.event_id, holder_id)) orelse return .not_found; 609 const role = seen.role orelse continue; 610 if (best) |held| if (held.role.atLeast(role)) continue; 611 best = .{ .id = holder_id, .role = role }; 612 } 613 const holder = best orelse return .denied; 614 if (!holder.role.atLeast(min_role)) return .denied; 615 return .{ 616 .allowed = switch (service) { 617 .upload_pack => .fetch, 618 .receive_pack => blk: { 619 const names = try repo.repo.event.protectedBranchNames(arena.allocator()); 620 // members change protected branches only by merging a patch 621 const owner = holder.role == .owner; 622 break :blk .{ .push = .{ 623 .user_id = holder.id, 624 .author = try commitAuthor(moment, arena, &holder.id), 625 .protected_branches = if (owner) names else &.{}, 626 .locked_branches = if (owner) &.{} else names, 627 } }; 628 }, 629 }, 630 }; 631 } 632 633 // an active user as a commit author 634 fn commitAuthor( 635 moment: evt.AdminDB.HashMap(.read_only), 636 arena: *std.heap.ArenaAllocator, 637 user_id: *const [evt.event_id_size]u8, 638 ) !evt.CommitAuthor { 639 const user = (try evt.User.readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, user_id)) orelse unreachable; 640 return .{ .name = user.event.name, .email = user.event.email }; 641 } 642 643 // the authorized_keys line of a pubkey blob: its type, then the blob in base64 644 fn authorizedKeyLine(allocator: std.mem.Allocator, blob: []const u8) ![]const u8 { 645 var reader = std.Io.Reader.fixed(blob); 646 const key_type = try ssh.takeString(&reader, 64); 647 const encoder = std.base64.standard.Encoder; 648 const line = try allocator.alloc(u8, key_type.len + 1 + encoder.calcSize(blob.len)); 649 @memcpy(line[0..key_type.len], key_type); 650 line[key_type.len] = ' '; 651 _ = encoder.encode(line[key_type.len + 1 ..], blob); 652 return line; 653 } 654 655 fn writeError(sess: *ssh.SessionCtx, comptime msg: []const u8) !void { 656 const body = "ERR haxy ssh: " ++ msg; 657 var packet: [body.len + 4]u8 = undefined; 658 const line = try std.fmt.bufPrint(&packet, "{x:0>4}{s}", .{ packet.len, body }); 659 try sess.writeBytes(line); 660 try sess.exit(1); 661 } 662 663 test "parseGitCommand rejects malformed input" { 664 const allocator = std.testing.allocator; 665 try std.testing.expectError(error.InvalidCommand, parseGitCommand(allocator, "git-upload-pack")); 666 try std.testing.expectError(error.UnsupportedService, parseGitCommand(allocator, "ls -la")); 667 try std.testing.expectError(error.UnsupportedService, parseGitCommand(allocator, "git-fake-pack 'repo'")); 668 } 669 670 test "parseGitCommand happy paths" { 671 const allocator = std.testing.allocator; 672 673 { 674 const parsed = try parseGitCommand(allocator, "git-upload-pack 'some-repo'"); 675 defer parsed.deinit(allocator); 676 try std.testing.expectEqual(GitService.upload_pack, parsed.service); 677 try std.testing.expectEqualStrings("some-repo", parsed.dir); 678 } 679 { 680 const parsed = try parseGitCommand(allocator, "git-receive-pack 'user/proj'"); 681 defer parsed.deinit(allocator); 682 try std.testing.expectEqual(GitService.receive_pack, parsed.service); 683 try std.testing.expectEqualStrings("user/proj", parsed.dir); 684 } 685 { 686 const parsed = try parseGitCommand(allocator, "git-upload-pack repo"); 687 defer parsed.deinit(allocator); 688 try std.testing.expectEqual(GitService.upload_pack, parsed.service); 689 try std.testing.expectEqualStrings("repo", parsed.dir); 690 } 691 } 692