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
118 changes: 112 additions & 6 deletions native/gitinspection/changes.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,14 +216,32 @@ func treeFiles(source archive, tree string) (map[string]gitFile, bool) {
return files, true
}

func changesBetween(baseArchive archive, baseSHA string, baseCommit gitCommit, headArchive archive, headSHA string, headCommit gitCommit) string {
type commitChanges struct {
legacy bool
targets []fractchTargetChanges
others []map[string]any
totals changeTotals
spriteDirs map[string]bool
}

func spriteDirsOf(trees ...map[string]gitFile) map[string]bool {
dirs := map[string]bool{}
for _, files := range trees {
for dir := range fractchFilesByDir(files) {
dirs[dir] = true
}
}
return dirs
}

func computeChanges(baseArchive archive, baseCommit gitCommit, headArchive archive, headCommit gitCommit, inline bool) (commitChanges, string) {
parentFiles, ok := treeFiles(baseArchive, baseCommit.Tree)
if !ok {
return failure("parent commit tree is invalid")
return commitChanges{}, "parent commit tree is invalid"
}
currentFiles, ok := treeFiles(headArchive, headCommit.Tree)
if !ok || headCommit.Tree == "" {
return failure("commit tree is invalid")
return commitChanges{}, "commit tree is invalid"
}
changes := []map[string]any{}
legacy := false
Expand All @@ -247,13 +265,101 @@ func changesBetween(baseArchive archive, baseSHA string, baseCommit gitCommit, h
}
sortChanges(changes)
targets, others, totals := structuredChanges(changes, baseArchive, parentFiles, headArchive, currentFiles)
inlineDiffContent(others, baseArchive, parentFiles, headArchive, currentFiles)
if inline {
inlineDiffContent(others, baseArchive, parentFiles, headArchive, currentFiles)
}
return commitChanges{legacy: legacy, targets: targets, others: others, totals: totals, spriteDirs: spriteDirsOf(parentFiles, currentFiles)}, ""
}

func changesBetween(baseArchive archive, baseSHA string, baseCommit gitCommit, headArchive archive, headSHA string, headCommit gitCommit) string {
computed, problem := computeChanges(baseArchive, baseCommit, headArchive, headCommit, true)
if problem != "" {
return failure(problem)
}
return encode(map[string]any{
"ok": true, "sha": headSHA, "parent": baseSHA, "commit": headCommit, "legacy": legacy,
"targets": targets, "files": others, "totals": totals,
"ok": true, "sha": headSHA, "parent": baseSHA, "commit": headCommit, "legacy": computed.legacy,
"targets": computed.targets, "files": computed.others, "totals": computed.totals,
}, "could not encode commit changes")
}

const maxSummarySprites = 5

func summarizeComputed(computed commitChanges) map[string]any {
assetDirs := map[string]bool{}
files := 0
for _, file := range computed.others {
path, _ := file["path"].(string)
if isAssetPath(path) {
if dir := spriteDir(path); dir != "" && computed.spriteDirs[dir] {
assetDirs[dir] = true
}
} else if path != "fractch.json" {
files++
}
}
sprites := []string{}
count := 0
tooLarge := false
for _, target := range computed.targets {
tooLarge = tooLarge || target.TooLarge
if len(target.Scripts) == 0 && len(target.Items) == 0 && !target.TooLarge && !assetDirs[target.Path] {
continue
}
delete(assetDirs, target.Path)
count++
if len(sprites) < maxSummarySprites {
sprites = append(sprites, summaryName(target.Path, target.Name))
}
}
remaining := make([]string, 0, len(assetDirs))
for dir := range assetDirs {
remaining = append(remaining, dir)
}
sort.Strings(remaining)
for _, dir := range remaining {
count++
if len(sprites) < maxSummarySprites {
sprites = append(sprites, summaryName(dir, ""))
}
}
return map[string]any{
"legacy": computed.legacy, "totals": computed.totals, "sprites": sprites, "spriteCount": count,
"authoredFiles": files, "tooLarge": tooLarge,
}
}

func summaryName(dir, name string) string {
if dir == "Stage" {
return "Stage"
}
if name != "" {
return name
}
return dir
}

func (a archive) inspectCommitSummary(sha string, commit gitCommit) string {
base := archive{objects: map[string]*zip.File{}}
parentCommit := gitCommit{}
initial := len(commit.Parents) == 0
if !initial {
parsed, ok := a.parseCommit(commit.Parents[0])
if !ok {
return failure("parent commit is unavailable")
}
base, parentCommit = a, parsed
}
computed, problem := computeChanges(base, parentCommit, a, commit, false)
if problem != "" {
return failure(problem)
}
summary := summarizeComputed(computed)
summary["ok"] = true
summary["sha"] = sha
summary["initial"] = initial
return encode(summary, "could not encode commit summary")
}

func (a archive) inspectCommitChanges(sha string, commit gitCommit) string {
if len(commit.Parents) == 0 {
return changesBetween(archive{objects: map[string]*zip.File{}}, "", gitCommit{}, a, sha, commit)
Expand Down
76 changes: 76 additions & 0 deletions native/gitinspection/fractch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gitinspection

import (
"encoding/hex"
"encoding/json"
"sort"
"strings"
"testing"
Expand Down Expand Up @@ -298,6 +299,27 @@ func TestInspectChangesSummarisesCommit(t *testing.T) {
}
}

summary := decodeResult(t, Inspect(path, second, "summary", "", second))
if summary["ok"] != true || summary["initial"] != false || summary["spriteCount"] != float64(2) {
t.Fatalf("unexpected summary: %v", summary)
}
if names := summary["sprites"].([]any); len(names) != 2 || names[0] != "Cat" || names[1] != "Stage" {
t.Fatalf("unexpected summary sprites: %v", names)
}
if summary["totals"].(map[string]any)["scripts"].(map[string]any)["changed"] != float64(1) {
t.Fatalf("summary totals differ from the changes view: %v", summary["totals"])
}
if summary["authoredFiles"] != float64(1) || summary["tooLarge"] != false {
t.Fatalf("summary should count the readme and nothing else: %v", summary)
}
if _, inlined := summary["files"]; inlined {
t.Fatalf("a summary should not carry file contents: %v", summary)
}
firstSummary := decodeResult(t, Inspect(path, first, "summary", "", second))
if firstSummary["initial"] != true {
t.Fatalf("the first commit should be marked initial: %v", firstSummary)
}

initial := decodeResult(t, Inspect(path, first, "changes", "", second))
initialTotals := initial["totals"].(map[string]any)
if initialTotals["scripts"].(map[string]any)["added"] != float64(3) || initialTotals["stateOnly"] != false {
Expand All @@ -319,6 +341,60 @@ func TestInspectChangesStateOnlyCommitIsNotCode(t *testing.T) {
}
}

func TestInspectSummaryNamesOnlySpriteFolders(t *testing.T) {
repo := newTestRepository()
code := repo.object("blob", []byte(fractchBase))
firstTree := repo.nestedTree(map[string]string{
"Cat/main.fractch": code,
"Cat/assets/a.svg": repo.object("blob", []byte("<svg/>")),
"fonts/Brand.ttf": repo.object("blob", []byte("font one")),
"assets/loose.png": repo.object("blob", []byte("png one")),
"Stage/stage.fractch": repo.object("blob", []byte("stage \"Stage\";\n")),
})
first := repo.commit(firstTree, "", "Initial version")
secondTree := repo.nestedTree(map[string]string{
"Cat/main.fractch": code,
"Cat/assets/a.svg": repo.object("blob", []byte("<svg width=\"2\"/>")),
"fonts/Brand.ttf": repo.object("blob", []byte("font two")),
"assets/loose.png": repo.object("blob", []byte("png two")),
"Stage/stage.fractch": repo.object("blob", []byte("stage \"Stage\";\n")),
})
second := repo.commit(secondTree, first, "Updated project")
path := repo.write(t)
summary := decodeResult(t, Inspect(path, second, "summary", "", second))
if names := summary["sprites"].([]any); len(names) != 1 || names[0] != "Cat" || summary["spriteCount"] != float64(1) {
t.Fatalf("only the Cat folder should be named: %v", summary)
}
assets := summary["totals"].(map[string]any)["assets"].(map[string]any)
if assets["modified"] != float64(3) {
t.Fatalf("every changed asset should still be counted: %v", assets)
}
changes := decodeResult(t, Inspect(path, second, "changes", "", second))
summaryTotals, _ := json.Marshal(summary["totals"])
changeTotals, _ := json.Marshal(changes["totals"])
if string(summaryTotals) != string(changeTotals) {
t.Fatalf("summary totals %s differ from changes totals %s", summaryTotals, changeTotals)
}
}

func TestInspectSummaryStateOnlyAndMissingParent(t *testing.T) {
repo := newTestRepository()
firstTree := repo.nestedTree(map[string]string{"Cat/main.fractch": repo.object("blob", []byte(fractchBase))})
first := repo.commit(firstTree, "", "Initial version")
moved := strings.Replace(fractchBase, `sprite "Cat" at 10,20`, `sprite "Cat" at 99,20`, 1)
second := repo.commit(repo.nestedTree(map[string]string{"Cat/main.fractch": repo.object("blob", []byte(moved))}), first, "Updated project")
orphan := repo.commit(firstTree, strings.Repeat("e", 40), "Updated project")
path := repo.write(t)
summary := decodeResult(t, Inspect(path, second, "summary", "", second+","+orphan))
totals := summary["totals"].(map[string]any)
if totals["stateOnly"] != true || len(summary["sprites"].([]any)) != 0 || summary["spriteCount"] != float64(0) {
t.Fatalf("a state-only commit names no sprites: %v", summary)
}
if failed := decodeResult(t, Inspect(path, orphan, "summary", "", second+","+orphan)); failed["ok"] == true {
t.Fatalf("a commit whose parent is missing should fail: %v", failed)
}
}

func TestCompareArchivesChangesAcrossForks(t *testing.T) {
base := newTestRepository()
baseCommit := base.commit(base.nestedTree(map[string]string{"Cat/main.fractch": base.object("blob", []byte(fractchBase))}), "", "Initial version")
Expand Down
2 changes: 2 additions & 0 deletions native/gitinspection/inspection.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,8 @@ func Inspect(workspacePath, sha, operation, requestedPath, allowedHeads string)
return archive.inspectCommit(sha, commit, true)
case "changes":
return archive.inspectCommitChanges(sha, commit)
case "summary":
return archive.inspectCommitSummary(sha, commit)
default:
return archive.inspectCommit(sha, commit, false)
}
Expand Down
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.2.0",
"version": "1.3.0",
"main": "main.osl",
"scripts": {
"dev": ["osl", "run", "main.osl"],
Expand Down
8 changes: 8 additions & 0 deletions scripts/e2e_smoke.py
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,10 @@ def head_and_edited():
head, edited = head_and_edited()
upload("upload live editor", {"expectedEdited": edited}, who="alice")

private_head, _ = head_and_edited()
step("commit summaries of a private project signed out", "GET", path + "/commit-summaries?shas=" + private_head)
step("commit summaries of a private project as a stranger", "GET", path + "/commit-summaries?shas=" + private_head, "bob")
step("commit summaries of a private project as an editor", "GET", path + "/commit-summaries?shas=" + private_head, "alice")
step("share history", "POST", path + "/visibility", "sam", {"visibility": "public"})
status, remixed = step("remix history", "POST", path + "/remix", "alice", {})
remix_id = str(remixed.get("id", "") or (remixed.get("project") or {}).get("id", "")) if isinstance(remixed, dict) else ""
Expand Down Expand Up @@ -644,6 +648,10 @@ def head_and_edited():
step("commit changes", "GET", path + "/commits/" + head + "/changes", "sam")
step("commit changes signed out", "GET", path + "/commits/" + head + "/changes")
step("changes of unknown commit", "GET", path + "/commits/" + "1" * 40 + "/changes", "sam")
step("commit summaries", "GET", path + "/commit-summaries?shas=" + head + ",nothex," + "1" * 40 + "," + head, "sam")
step("commit summaries signed out", "GET", path + "/commit-summaries?shas=" + head)
step("commit summaries from cache", "GET", path + "/commit-summaries?shas=" + head, "sam")
step("commit summaries without shas", "GET", path + "/commit-summaries", "sam")
step("rename branch", "PATCH", path + "/branches", "sam", {"branch": "feature", "name": "feature-two"})
step("rename branch as stranger", "PATCH", path + "/branches", "bob", {"branch": "feature-two", "name": "x"})
step("delete branch", "DELETE", path + "/branches", "sam", {"branch": "feature-two"})
Expand Down
57 changes: 57 additions & 0 deletions src/git/commit-summary-cache.osl
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
def commitSummaryLimit() number (
return 50
)

def requestedCommitShas(string raw) array (
array shas = []
object seen = {}
for entry in raw.split(",") (
string sha = entry.toStr().trim()
if regex.match("^[a-f0-9]{40}$", sha) and !seen.contains(sha) and shas.len < commitSummaryLimit() (
seen[sha] = true
shas.append(sha)
)
)
return shas
)

def commitSummaryCacheLimit() number (
return 2000
)

def commitSummaryRetryMs() number (
return 600000
)

def cachedCommitSummary(object cached, string sha, number now) object (
if !cached.contains(sha) or cached[sha] == null (
return {}
)
object entry = cached[sha].assertElse(object, {})
if entry.contains("ok") and entry.ok == true (
return entry
)
if entry.contains("failedAt") and now - entry.failedAt.toNum() < commitSummaryRetryMs() (
return {ok: false}
)
return {}
)

def mergedCommitSummaryCache(object cached, object computed) object (
object merged = cached
array keys = computed.getKeys()
number added = 0
for i keys.len (
if !cached.contains(keys[i].toStr()) (
added += 1
)
)
if cached.len + added > commitSummaryCacheLimit() (
merged = {}
)
for i keys.len (
string sha = keys[i].toStr()
merged[sha] = computed[sha]
)
return merged
)
37 changes: 37 additions & 0 deletions src/git/commit-summary-cache.test.osl
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import "std:testing"
import "std:regex"
import "./commit-summary-cache.osl"

string first = "84223eef0258932ae0bf0bcb885ecb252431f6ec"
string second = "ef2bedd84c4a6403078ab088c795fae166ea9baf"
array parsed = requestedCommitShas(first ++ ", nothex," ++ first.toUpper() ++ "," ++ second ++ "," ++ first)
testing.assert(parsed.len == 2, "invalid, uppercase and repeated shas are dropped")
testing.assert(parsed[1] == first and parsed[2] == second, "shas keep their requested order")

string many = ""
for i 60 (
string hex = i.toStr().hashSHA256().trim(1, 40)
many ++= hex ++ ","
)
testing.assert(requestedCommitShas(many).len == commitSummaryLimit(), "at most fifty shas are summarised per request")

object cached = {}
cached[first] = {ok: true, sha: first}
cached[second] = {ok: false, failedAt: 1000}
testing.assert(cachedCommitSummary(cached, first, 5000).ok == true, "stored summaries are reused")
testing.assert(cachedCommitSummary(cached, second, 2000).ok == false, "a recent failure is not retried")
testing.assert(!cachedCommitSummary(cached, second, 1000 + commitSummaryRetryMs()).contains("ok"), "an old failure is retried")
testing.assert(!cachedCommitSummary(cached, "1" ++ first.trim(2, 40), 5000).contains("ok"), "unknown shas need computing")

object computed = {}
computed["new"] = {ok: true}
object merged = mergedCommitSummaryCache(cached, computed)
testing.assert(merged.len == 3 and merged["new"].ok == true, "new summaries are added to the cache")
object full = {}
for i commitSummaryCacheLimit() (
full["k" ++ i.toStr()] = {ok: true}
)
testing.assert(mergedCommitSummaryCache(full, computed).len == 1, "a full cache starts again from this request")
object retried = {}
retried["k1"] = {ok: true}
testing.assert(mergedCommitSummaryCache(full, retried).len == commitSummaryCacheLimit(), "replacing stored entries does not count toward the limit")
Loading
Loading