srctree

Gregory Mullen parent 6006507c c4fa9715
add cwd.fd check to git.actions.cwd to protect git init

src/endpoints/admin.zig added: 30, removed: 15, total 15
@@ -33,7 +33,7 @@ fn createRepo(a: Allocator, reponame: []const u8) !void {
 
var actions = git.Actions{
.alloc = a,
.cwd_dir = std.fs.cwd(),
.cwd = null,
};
 
_ = try actions.gitInit(dir, .{});
@@ -103,7 +103,7 @@ fn postCloneUpstream(r: *Response, _: *UriIter) Error!void {
var dir = std.fs.cwd().openDir("repos", .{}) catch return error.Unknown;
var act = git.Actions{
.alloc = r.alloc,
.cwd_dir = dir,
.cwd = dir,
};
std.debug.print("fork bare {s}\n", .{
act.forkRemote(ruri.value, name) catch return error.Unknown,
@@ -152,7 +152,10 @@ fn postNewRepo(r: *Response, _: *UriIter) Error!void {
std.debug.print("creating {s}\n", .{rname.value});
var buf: [2048]u8 = undefined;
var dir_name = std.fmt.bufPrint(&buf, "repos/{s}", .{rname.value}) catch return error.Unknown;
var new_repo = git.Repo.createNew(r.alloc, dir_name) catch return error.Unknown;
 
if (std.fs.cwd().openDir(dir_name, .{})) |_| return error.Unknown else |_| {}
 
var new_repo = git.Repo.createNew(r.alloc, std.fs.cwd(), dir_name) catch return error.Unknown;
 
std.debug.print("creating {any}\n", .{new_repo});
 
 
src/git.zig added: 30, removed: 15, total 15
@@ -405,14 +405,14 @@ pub const Repo = struct {
}
 
/// Dir name must be relative (probably)
pub fn createNew(a: Allocator, dir_name: []const u8) !Repo {
pub fn createNew(a: Allocator, chdir: std.fs.Dir, dir_name: []const u8) !Repo {
var acts = Actions{
.alloc = a,
.cwd_dir = std.fs.cwd(),
.cwd = chdir,
};
 
_ = try acts.initRepo(dir_name, .{});
var dir = try std.fs.cwd().openDir(dir_name, .{});
a.free(try acts.initRepo(dir_name, .{}));
var dir = try chdir.openDir(dir_name, .{});
errdefer dir.close();
return init(dir);
}
@@ -583,8 +583,8 @@ pub const Repo = struct {
if (std.mem.eql(u8, head[0..5], "ref: ")) {
self.head = Ref{
.branch = Branch{
.name = try a.dupe(u8, head[5 .. head.len - 1]),
.sha = self.ref(head[16 .. head.len - 1]) catch return Ref{ .missing = {} },
.name = try a.dupe(u8, head[5 .. head.len - 1]),
.repo = self,
},
};
@@ -634,7 +634,7 @@ pub const Repo = struct {
return Actions{
.alloc = a,
.repo = self,
.cwd_dir = self.dir,
.cwd = self.dir,
};
}
 
@@ -1091,12 +1091,12 @@ pub const Tree = struct {
}
};
 
const DEBUG_GIT_ACTIONS = true;
const DEBUG_GIT_ACTIONS = false;
 
pub const Actions = struct {
alloc: Allocator,
repo: ?*const Repo = null,
cwd_dir: ?std.fs.Dir = null,
cwd: ?std.fs.Dir = null,
 
pub fn update(self: Actions) !void {
const data = try self.exec(&[_][]const u8{
@@ -1139,8 +1139,9 @@ pub const Actions = struct {
 
fn exec(self: Actions, argv: []const []const u8) ![]u8 {
std.debug.assert(std.mem.eql(u8, argv[0], "git"));
var cwd = if (self.cwd != null and self.cwd.?.fd != std.fs.cwd().fd) self.cwd else null;
var child = try std.ChildProcess.exec(.{
.cwd_dir = self.cwd_dir,
.cwd_dir = cwd,
.allocator = self.alloc,
.argv = argv,
});
@@ -1606,10 +1607,21 @@ test "forkRemote" {
 
var act = Actions{
.alloc = a,
.cwd_dir = tdir.dir,
.cwd = tdir.dir,
};
_ = act;
// TODO don't get banned from github
//var result = try act.forkRemote("https://github.com/grayhatter/srctree", "srctree_tmp");
//std.debug.print("{s}\n", .{result});
}
 
test "new repo" {
var a = std.testing.allocator;
var tdir = std.testing.tmpDir(.{});
defer tdir.cleanup();
 
var new_repo = try Repo.createNew(a, tdir.dir, "new_repo");
_ = try tdir.dir.openDir("new_repo", .{});
try new_repo.loadData(a);
defer new_repo.raze(a);
}