╭ main.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1 const std = @import("std"); 2 const builtin = @import("builtin"); 3 const xit = @import("xit"); 4 const rp = xit.repo; 5 const cmd = @import("./command.zig"); 6 const serve = @import("./serve.zig"); 7 const ssh_helper = @import("./ssh_helper.zig"); 8 9 pub const RunOpts = struct { 10 out: *std.Io.Writer, 11 err: *std.Io.Writer, 12 environ_map: *std.process.Environ.Map, 13 }; 14 15 pub fn main(init: std.process.Init) !u8 { 16 var debug_allocator: std.heap.DebugAllocator(.{}) = .init; 17 const allocator = if (builtin.mode == .Debug) debug_allocator.allocator() else std.heap.smp_allocator; 18 defer if (builtin.mode == .Debug) { 19 _ = debug_allocator.deinit(); 20 }; 21 22 var threaded = std.Io.Threaded.init(allocator, .{}); 23 defer threaded.deinit(); 24 const io = threaded.io(); 25 26 var args: std.ArrayList([]const u8) = .empty; 27 defer args.deinit(allocator); 28 29 var arg_it = try init.minimal.args.iterateAllocator(allocator); 30 defer arg_it.deinit(); 31 _ = arg_it.skip(); 32 while (arg_it.next()) |arg| { 33 try args.append(allocator, arg); 34 } 35 36 var stdout_writer = std.Io.File.stdout().writer(io, &.{}); 37 var stderr_writer = std.Io.File.stderr().writer(io, &.{}); 38 const run_opts = RunOpts{ .out = &stdout_writer.interface, .err = &stderr_writer.interface, .environ_map = init.environ_map }; 39 40 const cwd_path = try std.process.currentPathAlloc(io, allocator); 41 defer allocator.free(cwd_path); 42 43 run(.xit, .{}, io, allocator, args.items, cwd_path, run_opts) catch |err| switch (err) { 44 error.HandledError => return 1, 45 else => |e| return e, 46 }; 47 48 return 0; 49 } 50 51 pub fn run( 52 comptime repo_kind: rp.RepoKind, 53 comptime any_repo_opts: rp.AnyRepoOpts(repo_kind), 54 io: std.Io, 55 allocator: std.mem.Allocator, 56 args: []const []const u8, 57 cwd_path: []const u8, 58 run_opts: RunOpts, 59 ) !void { 60 var cmd_args = try cmd.CommandArgs.init(allocator, args); 61 defer cmd_args.deinit(); 62 63 switch (try cmd.CommandDispatch(repo_kind, any_repo_opts.toRepoOpts().hash).init(&cmd_args)) { 64 .invalid => |invalid| switch (invalid) { 65 .command => |command| { 66 try run_opts.err.print("\"{s}\" is not a valid command\n\n", .{command}); 67 try cmd.printHelp(null, run_opts.err); 68 return error.HandledError; 69 }, 70 .argument => |argument| { 71 try run_opts.err.print("\"{s}\" is not a valid argument\n\n", .{argument.value}); 72 try cmd.printHelp(argument.command, run_opts.err); 73 return error.HandledError; 74 }, 75 }, 76 .help => |cmd_kind_maybe| try cmd.printHelp(cmd_kind_maybe, run_opts.out), 77 .cli => |cli_cmd| { 78 if (cli_cmd == .serve) { 79 try serve.run(repo_kind, any_repo_opts, io, allocator, cwd_path, .{ 80 .http_listen = cli_cmd.serve.http_listen, 81 .ssh_listen = cli_cmd.serve.ssh_listen, 82 .project_root = cli_cmd.serve.project_root, 83 }, run_opts.err); 84 return; 85 } 86 if (cli_cmd == .ssh_helper) { 87 try ssh_helper.run(io, allocator, .{ 88 .ssh_connect = cli_cmd.ssh_helper.ssh_connect, 89 .service = cli_cmd.ssh_helper.service, 90 .dir = cli_cmd.ssh_helper.dir, 91 }, run_opts.environ_map); 92 return; 93 } 94 95 // some commands allow the path to be specified. for all others, just use the cwd path. 96 const work_path = switch (cli_cmd) { 97 .upload_pack => |upload_pack| try std.fs.path.resolve(allocator, &.{ cwd_path, upload_pack.dir }), 98 .receive_pack => |receive_pack| try std.fs.path.resolve(allocator, &.{ cwd_path, receive_pack.dir }), 99 .http_backend => xit.net_server_http_backend.resolveDir(allocator, cwd_path, run_opts.environ_map) catch { 100 var http_stdout_buf: [any_repo_opts.buffer_size]u8 = undefined; 101 var http_stdout_writer = std.Io.File.stdout().writer(io, &http_stdout_buf); 102 try xit.net_server_http_backend.sendNotFound(&http_stdout_writer.interface); 103 return; 104 }, 105 .serve => unreachable, 106 .ssh_helper => unreachable, 107 }; 108 defer allocator.free(work_path); 109 110 if (any_repo_opts.hash) |hash_kind| { 111 var repo = try rp.Repo(repo_kind, any_repo_opts.toRepoOptsWithHash(hash_kind)).open(io, allocator, .{ .path = work_path }); 112 defer repo.deinit(io, allocator); 113 try runCommand(repo_kind, any_repo_opts.toRepoOptsWithHash(hash_kind), &repo, io, allocator, cli_cmd, run_opts); 114 } else { 115 // if no hash was specified, use AnyRepo to detect the hash being used 116 var any_repo = try rp.AnyRepo(repo_kind, any_repo_opts).open(io, allocator, .{ .path = work_path }); 117 defer any_repo.deinit(io, allocator); 118 switch (any_repo) { 119 inline else => |*repo| { 120 const cmd_maybe = try cmd.Command(repo.self_repo_kind, repo.self_repo_opts.hash).initMaybe(&cmd_args); 121 try runCommand(repo.self_repo_kind, repo.self_repo_opts, repo, io, allocator, cmd_maybe orelse return error.InvalidCommand, run_opts); 122 }, 123 } 124 } 125 }, 126 } 127 } 128 129 fn runCommand( 130 comptime repo_kind: rp.RepoKind, 131 comptime repo_opts: rp.RepoOpts(repo_kind), 132 repo: *rp.Repo(repo_kind, repo_opts), 133 io: std.Io, 134 allocator: std.mem.Allocator, 135 command: cmd.Command(repo_kind, repo_opts.hash), 136 run_opts: RunOpts, 137 ) !void { 138 switch (command) { 139 .upload_pack => |upload_pack_cmd| { 140 var options = upload_pack_cmd.options; 141 options.protocol_version = xit.net_server_common.detectProtocolVersion(run_opts.environ_map); 142 var stdin_buf: [repo_opts.net_buffer_size]u8 = undefined; 143 var stdin_reader = std.Io.File.stdin().reader(io, &stdin_buf); 144 var stdout_buf: [repo_opts.net_buffer_size]u8 = undefined; 145 var stdout_writer = std.Io.File.stdout().writer(io, &stdout_buf); 146 try repo.uploadPack(io, allocator, &stdin_reader.interface, &stdout_writer.interface, options); 147 }, 148 .receive_pack => |receive_pack_cmd| { 149 var options = receive_pack_cmd.options; 150 options.protocol_version = xit.net_server_common.detectProtocolVersion(run_opts.environ_map); 151 var stdin_buf: [repo_opts.net_buffer_size]u8 = undefined; 152 var stdin_reader = std.Io.File.stdin().reader(io, &stdin_buf); 153 var stdout_buf: [repo_opts.net_buffer_size]u8 = undefined; 154 var stdout_writer = std.Io.File.stdout().writer(io, &stdout_buf); 155 try repo.receivePack(io, allocator, &stdin_reader.interface, &stdout_writer.interface, options); 156 }, 157 .http_backend => { 158 const environ_map = run_opts.environ_map; 159 var path_buf: [std.Io.Dir.max_path_bytes]u8 = undefined; 160 const path = try xit.net_server_http_backend.resolveRepoPath(environ_map, &path_buf); 161 162 var stdout_buf: [repo_opts.net_buffer_size]u8 = undefined; 163 var stdout_writer = std.Io.File.stdout().writer(io, &stdout_buf); 164 165 const handler, const suffix = for (&xit.net_server_http_backend.routes) |*svc| { 166 if (std.mem.endsWith(u8, path, svc.suffix)) 167 break .{ svc.handler, svc.suffix }; 168 } else { 169 try xit.net_server_http_backend.sendNotFound(&stdout_writer.interface); 170 return; 171 }; 172 173 const request_method: std.http.Method = blk: { 174 const method_str = environ_map.get("REQUEST_METHOD") orelse break :blk .GET; 175 const method = std.meta.stringToEnum(std.http.Method, method_str) orelse break :blk .GET; 176 break :blk if (method == .HEAD) .GET else method; 177 }; 178 179 var stdin_buf: [repo_opts.net_buffer_size]u8 = undefined; 180 var stdin_reader = std.Io.File.stdin().reader(io, &stdin_buf); 181 try repo.httpBackend(io, allocator, &stdin_reader.interface, &stdout_writer.interface, .cgi, .{ 182 .request_method = request_method, 183 .handler = handler, 184 .suffix = suffix, 185 .query_string = environ_map.get("QUERY_STRING") orelse "", 186 .content_type = environ_map.get("CONTENT_TYPE") orelse "", 187 .has_remote_user = environ_map.get("REMOTE_USER") != null, 188 .protocol_version = xit.net_server_common.detectProtocolVersion(environ_map), 189 }); 190 }, 191 .serve => unreachable, 192 .ssh_helper => unreachable, 193 } 194 } 195