Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions native/gitinspection/inspection.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,8 @@ func Inspect(workspacePath, sha, operation, requestedPath, allowedHeads string)
return archive.inspectCommitChanges(sha, commit)
case "summary":
return archive.inspectCommitSummary(sha, commit)
case "last-commits":
return archive.inspectLastCommits(sha, commit)
default:
return archive.inspectCommit(sha, commit, false)
}
Expand Down
220 changes: 220 additions & 0 deletions native/gitinspection/last_commits.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
package gitinspection

import (
"bytes"
"encoding/hex"
"strings"
)

const maxLastCommitWalk = 2000

var maxLastCommitParseBytes = 64 << 20

type treeEntry struct {
OID string
Dir bool
}

func (a archive) readTreeEntries(oid string) (map[string]treeEntry, bool) {
kind, body, ok := a.readObject(oid)
if !ok || kind != "tree" {
return nil, false
}
entries := map[string]treeEntry{}
position := 0
for position < len(body) {
space := bytes.IndexByte(body[position:], ' ')
if space < 1 {
return nil, false
}
space += position
nul := bytes.IndexByte(body[space+1:], 0)
if nul < 1 {
return nil, false
}
nul += space + 1
if nul+21 > len(body) {
return nil, false
}
mode := string(body[position:space])
name := string(body[space+1 : nul])
if name == "" || name == "." || name == ".." || strings.ContainsAny(name, "/\\") {
return nil, false
}
entries[name] = treeEntry{OID: hex.EncodeToString(body[nul+1 : nul+21]), Dir: mode == "40000" || mode == "040000"}
position = nul + 21
}
return entries, true
}

func (a archive) headPaths(rootOID string) (map[string]bool, bool) {
paths := map[string]bool{}
var walk func(string, string, int) bool
walk = func(treeOID, prefix string, depth int) bool {
if depth > maxTreeDepth {
return false
}
entries, ok := a.readTreeEntries(treeOID)
if !ok {
return false
}
for name, entry := range entries {
path := joinTreePath(prefix, name)
paths[path] = true
if len(paths) > maxTreeFiles {
return false
}
if entry.Dir && !walk(entry.OID, path, depth+1) {
return false
}
}
return true
}
return paths, walk(rootOID, "", 0)
}

func joinTreePath(prefix, name string) string {
if prefix == "" {
return name
}
return prefix + "/" + name
}

type lastCommitWalk struct {
archive archive
wanted map[string]bool
budget int
exhausted bool
visit func(string)
}

func (w *lastCommitWalk) fractchChanged(beforeOID, afterOID string) bool {
_, before, beforeOK := w.archive.readObject(beforeOID)
_, after, afterOK := w.archive.readObject(afterOID)
if !beforeOK || !afterOK || len(before) > maxInlineDiffFileBytes || len(after) > maxInlineDiffFileBytes {
return true
}
if w.budget+len(before)+len(after) > maxLastCommitParseBytes {
w.exhausted = true
return false
}
w.budget += len(before) + len(after)
changes := compareFractch(string(before), string(after))
return len(changes.Scripts) > 0 || len(changes.Items) > 0
}

func (w *lastCommitWalk) changedPaths(currentOID, parentOID, prefix string, depth int) (bool, bool) {
if depth > maxTreeDepth {
return false, false
}
current, ok := w.archive.readTreeEntries(currentOID)
if !ok {
return false, false
}
parent := map[string]treeEntry{}
if parentOID != "" {
if parent, ok = w.archive.readTreeEntries(parentOID); !ok {
return false, false
}
}
changed := false
for name, entry := range current {
path := joinTreePath(prefix, name)
before, existed := parent[name]
if existed && before.OID == entry.OID {
continue
}
counted := true
if entry.Dir {
previous := ""
if existed && before.Dir {
previous = before.OID
}
nested, ok := w.changedPaths(entry.OID, previous, path, depth+1)
if !ok {
return false, false
}
counted = nested || previous == ""
} else if existed && !before.Dir && strings.HasSuffix(strings.ToLower(name), ".fractch") {
counted = w.fractchChanged(before.OID, entry.OID)
}
if !counted {
continue
}
changed = true
if w.wanted[path] {
w.visit(path)
}
}
for name := range parent {
if _, kept := current[name]; !kept {
changed = true
}
}
return changed, true
}

func (a archive) inspectLastCommits(sha string, commit gitCommit) string {
wanted, ok := a.headPaths(commit.Tree)
if !ok {
return failure("commit tree is invalid")
}
paths := map[string]string{}
commits := map[string]any{}
remaining := len(wanted)
current, currentCommit := sha, commit
complete := true
walk := &lastCommitWalk{archive: a, wanted: wanted}
for walked := 0; remaining > 0; walked++ {
if walked >= maxLastCommitWalk {
complete = false
break
}
parentTree := ""
var parentCommit gitCommit
parentSHA := ""
if len(currentCommit.Parents) > 0 {
parentSHA = currentCommit.Parents[0]
parsed, found := a.parseCommit(parentSHA)
if !found {
complete = false
break
}
parentCommit = parsed
parentTree = parsed.Tree
}
used := false
walk.visit = func(path string) {
if _, done := paths[path]; done {
return
}
paths[path] = current
remaining--
used = true
}
if _, ok := walk.changedPaths(currentCommit.Tree, parentTree, "", 0); !ok {
return failure("commit tree is invalid")
}
if walk.exhausted {
complete = false
for path, owner := range paths {
if owner == current {
delete(paths, path)
}
}
break
}
if used {
commits[current] = map[string]any{
"author": currentCommit.Author, "date": currentCommit.Date, "message": currentCommit.Message,
}
}
if parentSHA == "" {
break
}
current, currentCommit = parentSHA, parentCommit
}
return encode(map[string]any{
"ok": true, "sha": sha, "paths": paths, "commits": commits, "complete": complete && remaining == 0,
}, "could not encode last commits")
}
119 changes: 119 additions & 0 deletions native/gitinspection/last_commits_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package gitinspection

import (
"strings"
"testing"
)

func TestInspectLastCommitsFindsNewestAuthoredChangePerPath(t *testing.T) {
repo := newTestRepository()
catCode := repo.object("blob", []byte(fractchBase))
catEdited := repo.object("blob", []byte(strings.Replace(fractchBase, "turn 15;", "turn 45;", 1)))
stageCode := repo.object("blob", []byte("var x = 1 id \"a\";\n"))
idle := repo.object("blob", []byte("<svg/>"))
firstTree := repo.nestedTree(map[string]string{
"Cat/main.fractch": catCode,
"Cat/assets/Idle.svg": idle,
"Stage/main.fractch": stageCode,
"fractch.json": repo.object("blob", []byte("{}")),
})
first := repo.commit(firstTree, "", "Initial version")
secondTree := repo.nestedTree(map[string]string{
"Cat/main.fractch": catEdited,
"Cat/assets/Idle.svg": idle,
"Stage/main.fractch": stageCode,
"fractch.json": repo.object("blob", []byte("{}")),
})
second := repo.commit(secondTree, first, "Change the cat")
third := repo.commit(secondTree, second, "Message only")
fourthTree := repo.nestedTree(map[string]string{
"Cat/main.fractch": catEdited,
"Cat/assets/Idle.svg": idle,
"Stage/main.fractch": repo.object("blob", []byte("var x = 2 id \"a\";\n")),
"Dog/main.fractch": repo.object("blob", []byte(fractchBase)),
"fractch.json": repo.object("blob", []byte("{}")),
})
fourth := repo.commit(fourthTree, third, "Add a dog and save a new stage value")

result := decodeResult(t, Inspect(repo.write(t), fourth, "last-commits", "", fourth))
if result["ok"] != true || result["complete"] != true {
t.Fatalf("unexpected result: %#v", result)
}
paths := result["paths"].(map[string]any)
expected := map[string]string{
"Cat": second,
"Cat/main.fractch": second,
"Cat/assets": first,
"Cat/assets/Idle.svg": first,
"Stage": first,
"Stage/main.fractch": first,
"Dog": fourth,
"Dog/main.fractch": fourth,
"fractch.json": first,
}
if len(paths) != len(expected) {
t.Fatalf("expected %d paths, got %#v", len(expected), paths)
}
for path, sha := range expected {
if paths[path] != sha {
t.Fatalf("%s: expected %s, got %v", path, sha, paths[path])
}
}
commits := result["commits"].(map[string]any)
if _, listed := commits[third]; listed {
t.Fatalf("a commit that changed nothing should not be listed: %#v", commits)
}
if commits[second].(map[string]any)["message"] != "Change the cat" {
t.Fatalf("unexpected commit details: %#v", commits[second])
}
}

func TestInspectLastCommitsStopsAtTheWalkLimit(t *testing.T) {
repo := newTestRepository()
stable := repo.object("blob", []byte("stable\n"))
parent := ""
head := ""
for index := 0; index <= maxLastCommitWalk; index++ {
tree := repo.nestedTree(map[string]string{
"stable.txt": stable,
"counter.txt": repo.object("blob", []byte{byte(index), byte(index >> 8)}),
})
head = repo.commit(tree, parent, "Step")
parent = head
}
result := decodeResult(t, Inspect(repo.write(t), head, "last-commits", "", head))
if result["ok"] != true || result["complete"] != false {
t.Fatalf("expected an incomplete result, got ok=%v complete=%v", result["ok"], result["complete"])
}
paths := result["paths"].(map[string]any)
if paths["counter.txt"] != head {
t.Fatalf("counter should point at the head, got %v", paths["counter.txt"])
}
if _, found := paths["stable.txt"]; found {
t.Fatalf("stable.txt should be unknown past the walk limit, got %v", paths["stable.txt"])
}
}

func TestInspectLastCommitsReportsAnExhaustedParseBudget(t *testing.T) {
repo := newTestRepository()
first := repo.commit(repo.nestedTree(map[string]string{
"Cat/main.fractch": repo.object("blob", []byte(fractchBase)),
"notes.txt": repo.object("blob", []byte("one\n")),
}), "", "Initial version")
head := repo.commit(repo.nestedTree(map[string]string{
"Cat/main.fractch": repo.object("blob", []byte(strings.Replace(fractchBase, "turn 15;", "turn 45;", 1))),
"notes.txt": repo.object("blob", []byte("two\n")),
}), first, "Change the cat")
previous := maxLastCommitParseBytes
maxLastCommitParseBytes = 1
defer func() { maxLastCommitParseBytes = previous }()

result := decodeResult(t, Inspect(repo.write(t), head, "last-commits", "", head))
if result["ok"] != true || result["complete"] != false {
t.Fatalf("expected an incomplete result, got %#v", result)
}
paths := result["paths"].(map[string]any)
if len(paths) != 0 {
t.Fatalf("nothing should be attributed once the budget runs out, got %#v", paths)
}
}
2 changes: 1 addition & 1 deletion opal.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mistwarp-api",
"version": "1.3.0",
"version": "1.4.0",
"main": "main.osl",
"scripts": {
"dev": ["osl", "run", "main.osl"],
Expand Down
2 changes: 1 addition & 1 deletion src/classroom/classroom-policy.osl
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ array studentReadRoutes = [
"^/projects/[^/]+/(project\\.json|trusted-extensions|editor|workspace\\.mwp|card\\.svg|commits|branches|game-inventory-config|me/save|me/inventory)$",
"^/projects/[^/]+/releases/[^/]+/project\\.json$",
"^/projects/[^/]+/extensions/[^/]+/source$",
"^/projects/[^/]+/commits/[^/]+(/(tree|file|co-authors|collaborators))?$"
"^/projects/[^/]+/commits/[^/]+(/(tree|file|last-commits|co-authors|collaborators))?$"
]

def studentRouteAllowed(string method, string rawPath) boolean (
Expand Down
1 change: 1 addition & 0 deletions src/classroom/classroom-policy.test.osl
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ testing.assert(studentRouteAllowed("GET", "/v1/classroom/join/ABC"), "the class
testing.assert(studentRouteAllowed("GET", "/v1/projects/p1"), "students can open a project they have a role on")
testing.assert(studentRouteAllowed("GET", "/v1/projects/p1/project.json"), "students can load their project")
testing.assert(studentRouteAllowed("GET", "/v1/projects/p1/commits/abc/tree"), "students can read their history")
testing.assert(studentRouteAllowed("GET", "/v1/projects/p1/commits/abc/last-commits"), "students can see when their files last changed")
testing.assert(studentRouteAllowed("OPTIONS", "/v1/anything"), "preflight requests pass")
testing.assert(studentRouteAllowed("GET", "/v1/me/backpack"), "students can load their synced backpack")
testing.assert(studentRouteAllowed("GET", "/v1/projects/p1/trusted-extensions"), "the editor can read a class project's trusted extensions")
Expand Down
9 changes: 9 additions & 0 deletions src/git/git-inspection.osl
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,15 @@ def handleGetCommitTree(*serve.Context c) (
gitInspectionResponse(c, res.project.assert(object), c.param("sha"), "tree", "")
)

def handleGetCommitLastCommits(*serve.Context c) (
auto res = loadProject(c.param("id"))
if !res.ok (
c.notFound("project not found")
return
)
gitInspectionResponse(c, res.project.assert(object), c.param("sha"), "last-commits", "")
)

def handleGetCommitFile(*serve.Context c) (
auto res = loadProject(c.param("id"))
if !res.ok (
Expand Down
1 change: 1 addition & 0 deletions src/git/routes.osl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def registerGitRoutes(*serve.Router api) (

api.GET("/projects/:id/commits/:sha/tree", handleGetCommitTree)
api.GET("/projects/:id/commits/:sha/file", handleGetCommitFile)
api.GET("/projects/:id/commits/:sha/last-commits", handleGetCommitLastCommits)
api.GET("/projects/:id/pulls", handleListPulls)
api.POST("/projects/:id/pulls", requireAuth, handleCreatePull)
api.GET("/projects/:id/pulls/:index", handleGetPull)
Expand Down
Loading