╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ fork.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯
187 try rf.replaceHead(.xit, repo_opts, state, ctx.io, .{ .ref = ref }); 188 189 var config = try xit.config.Config(.xit, repo_opts).init(state.readOnly(), ctx.io, ctx.allocator); 190 defer config.deinit(); 191 try config.add(state, ctx.io, .{ .name = "receive.denydeletes", .value = "true" }); 192 193 // the copied db carries the target's forker entries 194 if (config.local_sections.get(forker_section)) |section| { 195 var names_arena = std.heap.ArenaAllocator.init(ctx.allocator); 196 defer names_arena.deinit(); 197 const na = names_arena.allocator(); 198 var names: std.ArrayList([]const u8) = .empty; 199 for (section.keys()) |key| { 200 if (std.mem.startsWith(u8, key, forker_prefix)) try names.append(na, try na.print(forker_section ++ ".{s}", .{key})); 201 } 202 for (names.items) |name| try config.remove(state, ctx.io, .{ .name = name }); 203 } 204 205 // the copied db carries the target's file index, so the patch 206 // branch's entry starts from one of those rather than walking 207 // the tree 208 _ = try find.refreshInTransaction(repo_opts, state, &moment, ctx.io, ctx.allocator, null); 209 210 try xit.undo.write(repo_opts, state, std.Io.Timestamp.now(ctx.io, .real).toSeconds(), .{ .custom = .{ .action_kind = undo_action } }); 211 } 212 }; 213 214 try fork_repo.core.db_file.lock(io, .exclusive); 215 defer fork_repo.core.db_file.unlock(io); 216 217 const history = try DB.ArrayList(.read_write).init(fork_repo.core.db.rootCursor()); 218 try history.appendContext( 219 .{ .slot = try history.getSlot(-1) }, 220 Ctx{ .core = &fork_repo.core, .io = io, .allocator = allocator }, 221 ); 222 } 223 224 // create the patch event 225 try evt.consume(.{ .server = .{ .users_dir = users_dir } }, .fork, .xit, repo_opts, io, allocator, &fork_repo, evt.events_ref, &.{.{ 226 .id = input.id, 227 .timestamp = input.timestamp, 228 .author = input.author, 229 .event = .{ .patch = .{ 230 .title = input.title, 231 .description = input.description, 232 .labels = input.labels, 233 .target_branch = input.target_branch, 234 } }, 235 }}); 236 237 // create the fork event 238 try evt.consume(.{ .server = .{ .users_dir = users_dir } }, .user, .xit, evt.user_repo_opts, io, allocator, user_repo, evt.events_ref, &.{.{ 239 .id = input.id, 240 .timestamp = input.timestamp, 241 .author = input.author, 242 .event = .{ .fork = .{ 243 .repo_id = std.fmt.bytesToHex(input.repo_id, .lower), 244 .repo_user_id = std.fmt.bytesToHex(input.repo_user_id, .lower), 245 .repo_name = input.repo_name, 246 } }, 247 }}); 248 } 249 250 // tombstone first so a missing fork never remains visible 251 pub fn remove( 252 io: std.Io, 253 allocator: std.mem.Allocator, 254 users_dir: []const u8, 255 forker_id: *const [evt.event_id_size]u8, 256 id: *const [evt.event_id_size * 2]u8, 257 author: evt.CommitAuthor, 258 ) !void { 259 const fork_id = try evt.parseEventId(id); 260 var user_repo = (try evt.openUserRepo(io, allocator, users_dir, forker_id)) orelse return error.InvalidPatchDraft; 261 defer user_repo.deinit(io, allocator); 262 var arena = std.heap.ArenaAllocator.init(allocator); 263 defer arena.deinit(); 264 const moment = (try evt.userMoment(&user_repo)) orelse return error.InvalidPatchDraft; 265 const record = (try evt.Fork.readById(evt.UserDB, evt.user_repo_opts.hash, moment, &arena, &fork_id)) orelse return error.InvalidPatchDraft; 266 if (!record.removed) try evt.remove(.{ .server = .{ .users_dir = users_dir } }, .user, .xit, evt.user_repo_opts, io, allocator, &user_repo, &fork_id, .fork, author); 267 268 const path = try forkPath(allocator, users_dir, forker_id, &fork_id); 269 defer allocator.free(path); 270 try std.Io.Dir.cwd().deleteTree(io, path); 271 } 272