╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ try.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
847 if (cli) { 848 var stdout_writer = std.Io.File.stdout().writer(io, &.{}); 849 var stderr_writer = std.Io.File.stderr().writer(io, &.{}); 850 const run_opts = hx.main.RunOpts{ .out = &stdout_writer.interface, .err = &stderr_writer.interface }; 851 852 const key_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "key" }); 853 defer allocator.free(key_path); 854 855 const Runnable = struct { 856 io: std.Io, 857 key_path: []const u8, 858 859 pub fn run(self: @This(), web_port: u16, http_port: ?u16, ssh_port_maybe: ?u16) !void { 860 _ = web_port; 861 _ = http_port; 862 const ssh_port = ssh_port_maybe orelse return error.SshDisabled; 863 std.debug.print( 864 \\ 865 \\connect to the TUI with: 866 \\ ssh -p {d} -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null localhost 867 \\ 868 \\create a git repo and push over SSH: 869 \\ mkdir -p temp-try/client/test 870 \\ cd temp-try/client/test 871 \\ git init 872 \\ echo "hello" > hello.txt 873 \\ git add hello.txt 874 \\ git commit -m "let there be light" 875 \\ GIT_SSH_COMMAND='ssh -p {d} -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o IdentitiesOnly=yes -i {s}' git push localhost:admin/test HEAD:master 876 \\ 877 \\to quit, press enter. 878 \\ 879 , .{ ssh_port, ssh_port, self.key_path }); 880 // portable stdin read via std.Io — equivalent to the 881 // std.posix.read(STDIN_FILENO, ...) that doesn't compile 882 // on windows. 883 var buf: [1]u8 = undefined; 884 var stdin_reader = std.Io.File.stdin().reader(self.io, &buf); 885 _ = stdin_reader.interface.takeByte() catch {}; 886 } 887 }; 888 889 try srv.run(.xit, .{}, io, allocator, cwd_path, serve_options, run_opts.err, Runnable{ .io = io, .key_path = key_path }); 890 } else { 891 var null_writer = std.Io.Writer.Discarding.init(&.{}); 892 const run_opts = hx.main.RunOpts{ .out = &null_writer.writer, .err = &null_writer.writer }; 893 894 const Runnable = struct { 895 io: std.Io, 896 allocator: std.mem.Allocator, 897 session: *ui.Session, 898 repo: *Repo, 899 900 pub fn run(self: @This(), web_port: u16, http_port: ?u16, ssh_port: ?u16) !void { 901 // launch the TUI; the footer points at whatever port was bound 902 self.session.web_port = web_port; 903 self.session.data.clone_http_port = http_port; 904 self.session.data.clone_ssh_port = ssh_port; 905 try hx.ui.run(self.io, self.allocator, self.session, self.repo); 906 } 907 }; 908 909 try srv.run(.xit, .{}, io, allocator, cwd_path, serve_options, run_opts.err, Runnable{ .io = io, .allocator = allocator, .session = &session, .repo = &repo }); 910 } 911 } 912 913 // words cycled through to pad generated lines into prose-like content 914 const scatter_words = [_][]const u8{ 915 "lorem", "ipsum", "dolor", "sit", "amet", "consectetur", 916 "quantum", "vector", "matrix", "buffer", "kernel", "socket", 917 "falcon", "otter", "badger", "walrus", "ferret", "marmot", 918 "scatter", "gather", "encode", "decode", "render", "commit", 919 }; 920 921 // write `path` as 40 lines of mostly-stable filler. revision `c` and file 922 // index `fi` shift which lines change and the words used, so each (c, fi) 923 // yields a distinct multi-hunk diff against the previous revision. untouched 924 // lines stay byte-identical across revisions, so a bump in `c` is a scatter of 925 // small hunks rather than a full rewrite. 926 fn writeScatterFile(io: std.Io, allocator: std.mem.Allocator, repo_dir: std.Io.Dir, path: []const u8, fi: usize, c: usize) !void { 927 const file = try repo_dir.createFile(io, path, .{}); 928 defer file.close(io); 929 var writer = std.Io.Writer.Allocating.init(allocator); 930 defer writer.deinit(); 931 for (0..40) |line| { 932 const start = writer.written().len; 933 if ((line + fi + c) % 8 == 0) { 934 const tag = scatter_words[(line + fi + c * 3) % scatter_words.len]; 935 try writer.writer.print("rev {d} {s}", .{ c, tag }); 936 } 937 const target = 40 + (line * 17 + fi * 23) % 70; 938 var w = line + fi; 939 while (true) : (w += 1) { 940 const word = scatter_words[w % scatter_words.len]; 941 const len = writer.written().len - start; 942 if (len >= target or len + 1 + word.len > 120) break; 943 if (len == 0) 944 try writer.writer.print("{s}", .{word}) 945 else 946 try writer.writer.print(" {s}", .{word}); 947 } 948 try writer.writer.writeByte('\n'); 949 } 950 try file.writeStreamingAll(io, writer.written()); 951 } 952 953 // tag the current HEAD as the next sequential version: v1, v2, and so on 954 fn addNextTag(repo: *rp.Repo(.xit, .{}), io: std.Io, allocator: std.mem.Allocator, n: *usize) !void { 955 var buf: [16]u8 = undefined; 956 const name = try std.fmt.bufPrint(&buf, "v{d}", .{n.*}); 957 _ = try repo.addTag(io, allocator, .{ .name = name }); 958 n.* += 1; 959 } 960 961 // recursively copy the contents of src_dir into dest_dir 962 fn copyDir(io: std.Io, src_dir: std.Io.Dir, dest_dir: std.Io.Dir) !void { 963 var iter = src_dir.iterate(); 964 while (try iter.next(io)) |entry| { 965 switch (entry.kind) { 966 .file => try src_dir.copyFile(entry.name, dest_dir, entry.name, io, .{}), 967 .directory => { 968 try dest_dir.createDirPath(io, entry.name); 969 var dest_entry_dir = try dest_dir.openDir(io, entry.name, .{ .access_sub_paths = true, .iterate = true, .follow_symlinks = false }); 970 defer dest_entry_dir.close(io); 971 var src_entry_dir = try src_dir.openDir(io, entry.name, .{ .access_sub_paths = true, .iterate = true, .follow_symlinks = false }); 972 defer src_entry_dir.close(io); 973 try copyDir(io, src_entry_dir, dest_entry_dir); 974 }, 975 else => {}, 976 } 977 } 978 } 979