From a5dae68cb22557b29099e90e31bfe5b31919d8fa Mon Sep 17 00:00:00 2001 From: Mistium <92952823+Mistium@users.noreply.github.com> Date: Fri, 20 Nov 2026 16:39:17 +0000 Subject: [PATCH 1/5] Describe commit changes by script, with saved state kept apart from edits GET /v1/projects/:id/commits/:sha/changes and pulls/:index/diff?view=changes split each sprite's fractch file into scripts, declarations and saved state. Scripts are matched by content and hat, so moving or reordering them is not an edit, and hidden values under reporters and older snake_case spellings no longer count as changes. Variable values, sprite position, size, layer and visibility, the current costume, monitors and script positions are reported as state. Costumes are matched by their asset file, so renames and redraws are named. --- native/gitinspection/changes.go | 236 +++++++ native/gitinspection/fractch.go | 987 +++++++++++++++++++++++++++ native/gitinspection/fractch_test.go | 418 ++++++++++++ native/gitinspection/inspection.go | 2 + opal.json | 2 +- scripts/e2e_smoke.py | 5 + src/git/git-inspection.osl | 9 + src/git/pulls.osl | 12 +- src/git/routes.osl | 1 + 9 files changed, 1670 insertions(+), 2 deletions(-) create mode 100644 native/gitinspection/changes.go create mode 100644 native/gitinspection/fractch.go create mode 100644 native/gitinspection/fractch_test.go diff --git a/native/gitinspection/changes.go b/native/gitinspection/changes.go new file mode 100644 index 0000000..1f4d65b --- /dev/null +++ b/native/gitinspection/changes.go @@ -0,0 +1,236 @@ +package gitinspection + +import ( + "archive/zip" + "sort" + "strings" +) + +type fractchTargetChanges struct { + Path string `json:"path"` + Status string `json:"status"` + TooLarge bool `json:"tooLarge,omitempty"` + Scripts []fractchScriptChange `json:"scripts"` + Items []fractchItemChange `json:"items"` + State []fractchStateChange `json:"state"` +} + +type scriptCounts struct { + Added int `json:"added"` + Removed int `json:"removed"` + Changed int `json:"changed"` + Moved int `json:"moved"` +} + +type assetCounts struct { + Added int `json:"added"` + Removed int `json:"removed"` + Modified int `json:"modified"` +} + +type changeTotals struct { + Scripts scriptCounts `json:"scripts"` + Items int `json:"items"` + State int `json:"state"` + Assets assetCounts `json:"assets"` + Files int `json:"files"` + CodeChanged bool `json:"codeChanged"` +} + +func targetAssets(files map[string]gitFile, targetPath string) func(string) string { + dir := "" + if slash := strings.LastIndexByte(targetPath, '/'); slash >= 0 { + dir = targetPath[:slash+1] + } + return func(relative string) string { + return files[dir+relative].OID + } +} + +func isFractchPath(path string) bool { + return strings.HasSuffix(strings.ToLower(path), ".fractch") +} + +func isAssetPath(path string) bool { + return strings.HasPrefix(path, "assets/") || strings.Contains(path, "/assets/") || strings.HasPrefix(path, "fonts/") +} + +func blobText(source archive, files map[string]gitFile, path string) (string, bool, bool) { + file, ok := files[path] + if !ok { + return "", true, false + } + if file.Size > maxInlineDiffFileBytes { + return "", false, true + } + kind, content, found := source.readObject(file.OID) + if !found || kind != "blob" { + return "", false, false + } + return string(content), true, false +} + +func structuredChanges(changes []map[string]any, baseArchive archive, parentFiles map[string]gitFile, headArchive archive, currentFiles map[string]gitFile) ([]fractchTargetChanges, []map[string]any, changeTotals) { + targets := []fractchTargetChanges{} + others := []map[string]any{} + totals := changeTotals{} + budget := 0 + for _, change := range changes { + path, _ := change["path"].(string) + status, _ := change["status"].(string) + if !isFractchPath(path) { + if isAssetPath(path) { + switch status { + case "added": + totals.Assets.Added++ + case "removed": + totals.Assets.Removed++ + default: + totals.Assets.Modified++ + } + } else { + totals.Files++ + totals.CodeChanged = totals.CodeChanged || path != "fractch.json" + } + others = append(others, change) + continue + } + target := fractchTargetChanges{Path: path, Status: status, Scripts: []fractchScriptChange{}, Items: []fractchItemChange{}, State: []fractchStateChange{}} + before, beforeOK, beforeLarge := blobText(baseArchive, parentFiles, path) + after, afterOK, afterLarge := blobText(headArchive, currentFiles, path) + size := len(before) + len(after) + if beforeLarge || afterLarge || budget+size > maxInlineDiffTotalBytes { + target.TooLarge = true + totals.CodeChanged = true + targets = append(targets, target) + continue + } + if !beforeOK || !afterOK { + target.TooLarge = true + totals.CodeChanged = true + targets = append(targets, target) + continue + } + budget += size + compared := compareFractchAssets(before, after, assetLookup{before: targetAssets(parentFiles, path), after: targetAssets(currentFiles, path)}) + target.Scripts, target.Items, target.State = compared.Scripts, compared.Items, compared.State + for _, script := range target.Scripts { + switch script.Change { + case "added": + totals.Scripts.Added++ + case "removed": + totals.Scripts.Removed++ + default: + totals.Scripts.Changed++ + } + } + for _, state := range target.State { + if state.Kind == "script" && state.Field == "position" { + totals.Scripts.Moved++ + } + } + totals.Items += len(target.Items) + totals.State += len(target.State) + if len(target.Scripts) > 0 || len(target.Items) > 0 { + totals.CodeChanged = true + } + if len(target.Scripts) == 0 && len(target.Items) == 0 && len(target.State) == 0 { + continue + } + targets = append(targets, target) + } + return targets, others, totals +} + +func treeFiles(source archive, tree string) (map[string]gitFile, bool) { + files := map[string]gitFile{} + if tree == "" { + return files, true + } + listed, ok := source.walkTree(tree) + if !ok { + return nil, false + } + for _, file := range listed { + files[file.Path] = file + } + return files, true +} + +func changesBetween(baseArchive archive, baseSHA string, baseCommit gitCommit, headArchive archive, headSHA string, headCommit gitCommit) string { + parentFiles, ok := treeFiles(baseArchive, baseCommit.Tree) + if !ok { + return failure("parent commit tree is invalid") + } + currentFiles, ok := treeFiles(headArchive, headCommit.Tree) + if !ok || headCommit.Tree == "" { + return failure("commit tree is invalid") + } + changes := []map[string]any{} + legacy := false + for path, before := range parentFiles { + after, exists := currentFiles[path] + if !exists { + changes = append(changes, map[string]any{"path": path, "status": "removed", "oldOid": before.OID, "newOid": nil, "oldSize": before.Size, "newSize": 0}) + } else if before.OID != after.OID { + changes = append(changes, map[string]any{"path": path, "status": "modified", "oldOid": before.OID, "newOid": after.OID, "oldSize": before.Size, "newSize": after.Size}) + } else { + continue + } + legacy = legacy || path == "project.sb3" + } + for path, after := range currentFiles { + if _, exists := parentFiles[path]; exists { + continue + } + changes = append(changes, map[string]any{"path": path, "status": "added", "oldOid": nil, "newOid": after.OID, "oldSize": 0, "newSize": after.Size}) + legacy = legacy || path == "project.sb3" + } + sortChanges(changes) + targets, others, totals := structuredChanges(changes, baseArchive, parentFiles, headArchive, currentFiles) + inlineDiffContent(others, baseArchive, parentFiles, headArchive, currentFiles) + return encode(map[string]any{ + "ok": true, "sha": headSHA, "parent": baseSHA, "commit": headCommit, "legacy": legacy, + "targets": targets, "files": others, "totals": totals, + }, "could not encode commit changes") +} + +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) + } + parent := commit.Parents[0] + parentCommit, ok := a.parseCommit(parent) + if !ok { + return failure("parent commit is unavailable") + } + return changesBetween(a, parent, parentCommit, a, sha, commit) +} + +func CompareArchivesChangesJSON(basePath, baseSHA, headPath, headSHA string) string { + baseReader, baseArchive, err := openArchive(basePath) + if err != nil { + return failure("base workspace archive is unavailable") + } + defer baseReader.Close() + headReader, headArchive, err := openArchive(headPath) + if err != nil { + return failure("head workspace archive is unavailable") + } + defer headReader.Close() + baseCommit, ok := baseArchive.parseCommit(baseSHA) + if !ok { + return failure("base commit is unavailable") + } + headCommit, ok := headArchive.parseCommit(headSHA) + if !ok { + return failure("head commit is unavailable") + } + return changesBetween(baseArchive, baseSHA, baseCommit, headArchive, headSHA, headCommit) +} + +func sortChanges(changes []map[string]any) { + sort.Slice(changes, func(i, j int) bool { + return changes[i]["path"].(string) < changes[j]["path"].(string) + }) +} diff --git a/native/gitinspection/fractch.go b/native/gitinspection/fractch.go new file mode 100644 index 0000000..272bbc5 --- /dev/null +++ b/native/gitinspection/fractch.go @@ -0,0 +1,987 @@ +package gitinspection + +import ( + "math" + "regexp" + "sort" + "strconv" + "strings" +) + +const ( + maxStateValueChars = 160 + maxCrossPairProducts = 40_000 + crossPairSimilarity = 0.6 +) + +type fractchStatement struct { + Kind string + Key string + Name string + Text string + Head string + At string + Body string + Norm string +} + +type fractchScriptChange struct { + Change string `json:"change"` + Kind string `json:"kind"` + Head string `json:"head"` + Before string `json:"before,omitempty"` + After string `json:"after,omitempty"` + BeforeAt string `json:"beforeAt,omitempty"` + AfterAt string `json:"afterAt,omitempty"` +} + +type fractchItemChange struct { + Change string `json:"change"` + Kind string `json:"kind"` + Name string `json:"name"` + Before string `json:"before,omitempty"` + After string `json:"after,omitempty"` +} + +type fractchStateChange struct { + Kind string `json:"kind"` + Name string `json:"name"` + Field string `json:"field"` + Before string `json:"before"` + After string `json:"after"` + Truncated bool `json:"truncated,omitempty"` +} + +type fractchFileChanges struct { + Scripts []fractchScriptChange `json:"scripts"` + Items []fractchItemChange `json:"items"` + State []fractchStateChange `json:"state"` + assets assetLookup +} + +type assetLookup struct { + before func(string) string + after func(string) string +} + +func noAssets(string) string { + return "" +} + +func isIdentifierByte(c byte) bool { + return c == '_' || c == ')' || c == ']' || (c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') +} + +func stripHiddenValues(src string) string { + out, _, _ := stripHiddenFrom(src, 0, false) + return out +} + +func stripHiddenFrom(src string, i int, inParen bool) (string, int, bool) { + var builder strings.Builder + hidden := -1 + commaAfterHidden := false + for i < len(src) { + c := src[i] + if isFractchQuote(c) { + end := skipFractchString(src, i) + builder.WriteString(src[i:end]) + i = end + continue + } + if strings.HasPrefix(src[i:], "//") || strings.HasPrefix(src[i:], "/*") { + end := skipFractchComment(src, i) + builder.WriteString(src[i:end]) + i = end + continue + } + if c == '(' { + inner, next, stripped := stripHiddenFrom(src, i+1, true) + before := byte(' ') + if i > 0 { + before = src[i-1] + } + after := nextNonSpace(src, next) + standalone := !isIdentifierByte(before) && (after == 0 || strings.IndexByte(";,){]\n", after) >= 0) + if stripped && standalone { + builder.WriteString(inner) + } else { + builder.WriteString("(" + inner + ")") + } + i = next + continue + } + if c == ')' && inParen { + content := builder.String() + if hidden >= 0 && !commaAfterHidden { + return strings.TrimSpace(content[:hidden]), i + 1, true + } + return content, i + 1, false + } + if c == ',' && hidden >= 0 { + commaAfterHidden = true + } + if inParen && hidden < 0 && strings.HasPrefix(src[i:], "??") { + hidden = builder.Len() + } + builder.WriteByte(c) + i++ + } + return builder.String(), i, false +} + +func camelIdentifiers(src string) string { + var builder strings.Builder + for i := 0; i < len(src); { + c := src[i] + if isFractchQuote(c) { + end := skipFractchString(src, i) + builder.WriteString(src[i:end]) + i = end + continue + } + if strings.HasPrefix(src[i:], "//") || strings.HasPrefix(src[i:], "/*") { + end := skipFractchComment(src, i) + builder.WriteString(src[i:end]) + i = end + continue + } + if !(c == '_' || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) || (i > 0 && isIdentifierByte(src[i-1])) { + builder.WriteByte(c) + i++ + continue + } + end := i + for end < len(src) && (isIdentifierByte(src[end]) && src[end] != ')' && src[end] != ']') { + end++ + } + word := src[i:end] + if c >= 'a' && c <= 'z' && strings.Contains(word, "_") { + upper := false + for index := 0; index < len(word); index++ { + letter := word[index] + if letter == '_' { + upper = true + continue + } + if upper && letter >= 'a' && letter <= 'z' { + letter -= 'a' - 'A' + } + upper = false + builder.WriteByte(letter) + } + } else { + builder.WriteString(word) + } + i = end + } + return builder.String() +} + +func nextNonSpace(src string, i int) byte { + for i < len(src) { + if src[i] != ' ' && src[i] != '\t' && src[i] != '\r' { + return src[i] + } + i++ + } + return 0 +} + +var ( + positionSuffix = regexp.MustCompile(`\s+at\s+(-?[0-9.eE+-]+)\s*,\s*(-?[0-9.eE+-]+)\s*$`) + definitionName = regexp.MustCompile(`^def\s+(@?[^\s(]+)`) + variableIDSuffix = regexp.MustCompile(`\s+id\s+("(?:[^"\\]|\\.)*")\s*;?\s*$`) + firstWord = regexp.MustCompile(`^[A-Za-z_]+`) +) + +func skipFractchString(src string, i int) int { + if strings.HasPrefix(src[i:], `"""`) { + end := strings.Index(src[i+3:], `"""`) + if end < 0 { + return len(src) + } + return i + 3 + end + 3 + } + quote := src[i] + j := i + 1 + for j < len(src) && src[j] != quote && src[j] != '\n' { + if src[j] == '\\' { + j++ + } + j++ + } + if j < len(src) && src[j] == quote { + j++ + } + if j > len(src) { + return len(src) + } + return j +} + +func skipFractchComment(src string, i int) int { + if strings.HasPrefix(src[i:], "//") { + end := strings.IndexByte(src[i:], '\n') + if end < 0 { + return len(src) + } + return i + end + } + end := strings.Index(src[i+2:], "*/") + if end < 0 { + return len(src) + } + return i + 2 + end + 2 +} + +func isFractchQuote(c byte) bool { + return c == '"' || c == '`' +} + +func splitFractchStatements(src string) []string { + statements := []string{} + pending := []string{} + i := 0 + for i < len(src) { + c := src[i] + if c == ' ' || c == '\t' || c == '\r' || c == '\n' { + i++ + continue + } + if strings.HasPrefix(src[i:], "//") || strings.HasPrefix(src[i:], "/*") { + end := skipFractchComment(src, i) + text := src[i:end] + if !(strings.HasPrefix(text, "/**") && strings.Contains(text, "* target:")) { + pending = append(pending, strings.TrimSpace(text)) + } + i = end + continue + } + start := i + depth := 0 + for i < len(src) { + c = src[i] + if isFractchQuote(c) { + i = skipFractchString(src, i) + continue + } + if strings.HasPrefix(src[i:], "//") || strings.HasPrefix(src[i:], "/*") { + i = skipFractchComment(src, i) + continue + } + i++ + if c == '{' { + depth++ + } else if c == '}' { + depth-- + if depth <= 0 { + break + } + } else if c == ';' && depth == 0 { + break + } + } + text := strings.TrimSpace(src[start:i]) + if len(pending) > 0 { + text = strings.Join(pending, "\n") + "\n" + text + pending = pending[:0] + } + statements = append(statements, text) + } + if len(pending) > 0 { + statements = append(statements, strings.Join(pending, "\n")) + } + return statements +} + +func leadingFractchComments(text string) (string, string) { + lines := strings.Split(text, "\n") + inBlock := false + for index, line := range lines { + trimmed := strings.TrimSpace(line) + if inBlock { + if strings.Contains(trimmed, "*/") { + inBlock = false + } + continue + } + if strings.HasPrefix(trimmed, "//") { + continue + } + if strings.HasPrefix(trimmed, "/*") { + inBlock = !strings.Contains(trimmed[2:], "*/") + continue + } + return strings.Join(lines[:index], "\n"), strings.Join(lines[index:], "\n") + } + return text, "" +} + +func scriptHeadEnd(text string) int { + for i := 0; i < len(text); { + c := text[i] + if isFractchQuote(c) { + i = skipFractchString(text, i) + continue + } + if c == '{' { + return i + } + i++ + } + return len(text) +} + +func fractchTokens(text string) []string { + tokens := []string{} + i := 0 + for i < len(text) { + c := text[i] + if c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == ';' { + i++ + continue + } + start := i + if isFractchQuote(c) { + i = skipFractchString(text, i) + } else { + for i < len(text) && !strings.ContainsRune(" \t\r\n;", rune(text[i])) { + if isFractchQuote(text[i]) { + i = skipFractchString(text, i) + continue + } + i++ + } + } + tokens = append(tokens, text[start:i]) + } + return tokens +} + +func unquoteFractch(token string) string { + if len(token) >= 2 && isFractchQuote(token[0]) && token[len(token)-1] == token[0] { + inner := token[1 : len(token)-1] + if token[0] == '"' { + inner = strings.ReplaceAll(inner, `\"`, `"`) + inner = strings.ReplaceAll(inner, `\\`, `\`) + } + return inner + } + return token +} + +var flagAttributes = map[string]bool{"hidden": true, "draggable": true, "current": true, "minimized": true, "continuous": true, "large": true, "slider": true} + +func fractchAttributes(tokens []string) map[string]string { + attributes := map[string]string{} + for i := 0; i < len(tokens); i++ { + word := tokens[i] + if flagAttributes[word] { + attributes[word] = "true" + continue + } + if i+1 < len(tokens) { + attributes[word] = tokens[i+1] + i++ + } + } + return attributes +} + +func parseFractchStatement(text string) fractchStatement { + comments, code := leadingFractchComments(text) + statement := fractchStatement{Text: text} + if code == "" { + statement.Kind = "note" + statement.Key = text + return statement + } + statement.Kind = firstWord.FindString(code) + switch statement.Kind { + case "when", "def", "script": + headEnd := scriptHeadEnd(code) + head := strings.TrimSpace(code[:headEnd]) + if match := positionSuffix.FindStringSubmatch(head); match != nil { + statement.At = match[1] + "," + match[2] + head = strings.TrimSpace(head[:len(head)-len(match[0])]) + } + statement.Head = head + statement.Key = head + if match := definitionName.FindStringSubmatch(head); match != nil { + statement.Key = "def " + match[1] + } + body := head + " " + strings.TrimSpace(code[headEnd:]) + if comments != "" { + body = comments + "\n" + body + } + statement.Body = body + statement.Norm = camelIdentifiers(stripHiddenValues(body)) + statement.Name = head + statement.Kind = "script" + case "var", "cloud": + tokens := fractchTokens(code) + if len(tokens) > 1 { + statement.Name = unquoteFractch(tokens[1]) + } + statement.Key = statement.Kind + " " + statement.Name + if match := variableIDSuffix.FindStringSubmatch(code); match != nil { + statement.Key = "id " + unquoteFractch(match[1]) + } + statement.Body = code + case "watch": + tokens := fractchTokens(code) + name := "" + if len(tokens) > 2 && (tokens[1] == "var" || tokens[1] == "list") { + name = tokens[1] + " " + unquoteFractch(tokens[2]) + } else if len(tokens) > 1 { + name = tokens[1] + } + statement.Name = name + statement.Key = "watch " + name + attributes := fractchAttributes(tokens) + if id, ok := attributes["id"]; ok { + statement.Key = "watch id " + unquoteFractch(id) + } + statement.Body = code + case "sprite", "stage", "costume", "backdrop", "sound", "use", "platform", "comment": + tokens := fractchTokens(code) + if len(tokens) > 1 && isFractchQuote(tokens[1][0]) { + statement.Name = unquoteFractch(tokens[1]) + } + statement.Key = statement.Kind + " " + statement.Name + if statement.Kind == "stage" { + statement.Key = "stage" + statement.Name = "Stage" + } + if statement.Kind == "backdrop" { + statement.Key = "costume " + statement.Name + } + statement.Body = code + default: + statement.Kind = "other" + statement.Key = code + statement.Name = firstLine(code) + statement.Body = code + } + return statement +} + +func firstLine(text string) string { + if index := strings.IndexByte(text, '\n'); index >= 0 { + return text[:index] + } + return text +} + +func parseFractchFile(src string) []fractchStatement { + raw := splitFractchStatements(src) + statements := make([]fractchStatement, 0, len(raw)) + for _, text := range raw { + statements = append(statements, parseFractchStatement(text)) + } + return statements +} + +func clipStateValue(value string) (string, bool) { + runes := []rune(value) + if len(runes) <= maxStateValueChars { + return value, false + } + return string(runes[:maxStateValueChars]), true +} + +func (changes *fractchFileChanges) addState(kind, name, field, before, after string) { + clippedBefore, beforeCut := clipStateValue(before) + clippedAfter, afterCut := clipStateValue(after) + changes.State = append(changes.State, fractchStateChange{ + Kind: kind, Name: name, Field: field, Before: clippedBefore, After: clippedAfter, Truncated: beforeCut || afterCut, + }) +} + +func variableValue(code string) string { + equals := -1 + for i := 0; i < len(code); { + if isFractchQuote(code[i]) { + i = skipFractchString(code, i) + continue + } + if code[i] == '=' { + equals = i + break + } + i++ + } + if equals < 0 { + return "" + } + value := code[equals+1:] + if match := variableIDSuffix.FindStringIndex(value); match != nil { + value = value[:match[0]] + } + return strings.TrimSuffix(strings.TrimSpace(value), ";") +} + +func variableKind(statement fractchStatement) string { + if strings.HasPrefix(variableValue(statement.Body), "[") { + return "list" + } + if statement.Kind == "cloud" { + return "cloud" + } + return "variable" +} + +var targetStateFields = map[string]bool{"at": true, "size": true, "direction": true, "hidden": true, "layer": true, "volume": true, "tempo": true, "video": true, "transparency": true, "tts": true} + +func headerAttributes(statement fractchStatement) map[string]string { + tokens := fractchTokens(statement.Body) + if len(tokens) == 0 { + return map[string]string{} + } + tokens = tokens[1:] + if statement.Kind == "sprite" && len(tokens) > 0 && isFractchQuote(tokens[0][0]) { + tokens = tokens[1:] + } + return fractchAttributes(tokens) +} + +func sortedUnion(a, b map[string]string) []string { + keys := []string{} + for key := range a { + keys = append(keys, key) + } + for key := range b { + if _, ok := a[key]; !ok { + keys = append(keys, key) + } + } + sort.Strings(keys) + return keys +} + +var targetDefaults = map[string]string{"at": "0,0", "size": "100", "direction": "90", "volume": "100", "tempo": "60", "video": "on", "transparency": "50"} + +func attributeValue(attributes map[string]string, key string) string { + if value, ok := attributes[key]; ok { + return unquoteFractch(value) + } + return targetDefaults[key] +} + +func (changes *fractchFileChanges) compareHeader(before, after fractchStatement) { + if before.Name != after.Name { + changes.Items = append(changes.Items, fractchItemChange{Change: "renamed", Kind: after.Kind, Name: after.Name, Before: before.Name, After: after.Name}) + } + beforeAttributes := headerAttributes(before) + afterAttributes := headerAttributes(after) + authored := false + for _, key := range sortedUnion(beforeAttributes, afterAttributes) { + if beforeAttributes[key] == afterAttributes[key] { + continue + } + if targetStateFields[key] { + field := key + if key == "at" { + field = "position" + } else if key == "hidden" { + field = "visible" + } + beforeValue, afterValue := attributeValue(beforeAttributes, key), attributeValue(afterAttributes, key) + if key == "hidden" { + beforeValue, afterValue = visibleText(beforeAttributes[key]), visibleText(afterAttributes[key]) + } + changes.addState(after.Kind, after.Name, field, beforeValue, afterValue) + continue + } + authored = true + } + if authored { + changes.Items = append(changes.Items, fractchItemChange{Change: "changed", Kind: after.Kind, Name: after.Name, Before: before.Body, After: after.Body}) + } +} + +func visibleText(hidden string) string { + if hidden == "true" { + return "false" + } + return "true" +} + +func withoutAttributes(statement fractchStatement, names ...string) string { + tokens := fractchTokens(statement.Body) + kept := []string{} + for i := 0; i < len(tokens); i++ { + dropped := false + for _, name := range names { + if tokens[i] != name { + continue + } + dropped = true + if !flagAttributes[name] && i+1 < len(tokens) { + i++ + } + } + if !dropped { + kept = append(kept, tokens[i]) + } + } + return strings.Join(kept, " ") +} + +func (changes *fractchFileChanges) compareKeyed(before, after fractchStatement) { + if before.Body == after.Body { + if (after.Kind == "costume" || after.Kind == "backdrop" || after.Kind == "sound") && changes.assetEdited(before, after) { + changes.Items = append(changes.Items, fractchItemChange{Change: "edited", Kind: after.Kind, Name: after.Name, Before: before.Body, After: after.Body}) + } + return + } + switch after.Kind { + case "sprite", "stage": + changes.compareHeader(before, after) + case "var", "cloud": + if before.Name != after.Name { + changes.Items = append(changes.Items, fractchItemChange{Change: "renamed", Kind: variableKind(after), Name: after.Name, Before: before.Name, After: after.Name}) + } + if beforeValue, afterValue := variableValue(before.Body), variableValue(after.Body); beforeValue != afterValue { + changes.addState(variableKind(after), after.Name, "value", beforeValue, afterValue) + } + case "watch": + changes.addState("monitor", after.Name, "display", before.Body, after.Body) + case "costume", "backdrop", "sound": + beforeCurrent := headerFlag(before, "current") + afterCurrent := headerFlag(after, "current") + if beforeCurrent != afterCurrent { + changes.addState(after.Kind, after.Name, "current", beforeCurrent, afterCurrent) + } + if assetSignature(before) != assetSignature(after) { + changes.Items = append(changes.Items, fractchItemChange{Change: "changed", Kind: after.Kind, Name: after.Name, Before: before.Body, After: after.Body}) + } else if changes.assetEdited(before, after) { + changes.Items = append(changes.Items, fractchItemChange{Change: "edited", Kind: after.Kind, Name: after.Name, Before: before.Body, After: after.Body}) + } + case "comment": + if withoutAttributes(before, "at", "size", "minimized", "for") == withoutAttributes(after, "at", "size", "minimized", "for") { + changes.addState("comment", after.Name, "layout", before.Body, after.Body) + return + } + changes.Items = append(changes.Items, fractchItemChange{Change: "changed", Kind: "comment", Name: after.Name, Before: before.Body, After: after.Body}) + default: + changes.Items = append(changes.Items, fractchItemChange{Change: "changed", Kind: after.Kind, Name: after.Name, Before: before.Body, After: after.Body}) + } +} + +func assetFile(statement fractchStatement) string { + return unquoteFractch(fractchAttributes(fractchTokens(statement.Body))["file"]) +} + +func (changes *fractchFileChanges) assetEdited(before, after fractchStatement) bool { + beforeFile, afterFile := assetFile(before), assetFile(after) + if beforeFile == "" || afterFile == "" { + return false + } + beforeOID, afterOID := changes.assets.before(beforeFile), changes.assets.after(afterFile) + return beforeOID != "" && afterOID != "" && beforeOID != afterOID +} + +func (changes *fractchFileChanges) sameAsset(before, after fractchStatement) bool { + beforeFile, afterFile := assetFile(before), assetFile(after) + if beforeFile == "" || afterFile == "" { + return false + } + beforeOID, afterOID := changes.assets.before(beforeFile), changes.assets.after(afterFile) + return beforeOID != "" && beforeOID == afterOID +} + +func roundedCenter(value string) string { + parts := strings.Split(value, ",") + for index, part := range parts { + number, err := strconv.ParseFloat(strings.TrimSpace(part), 64) + if err != nil { + return value + } + parts[index] = strconv.FormatFloat(math.Round(number*100)/100, 'f', -1, 64) + } + return strings.Join(parts, ",") +} + +func assetSignature(statement fractchStatement) string { + tokens := fractchTokens(statement.Body) + kept := []string{} + for i := 0; i < len(tokens); i++ { + switch tokens[i] { + case "current": + continue + case "file": + i++ + continue + case "center": + if i+1 < len(tokens) { + kept = append(kept, "center", roundedCenter(tokens[i+1])) + i++ + } + continue + } + kept = append(kept, tokens[i]) + } + return strings.Join(kept, " ") +} + +func headerFlag(statement fractchStatement, flag string) string { + for _, token := range fractchTokens(statement.Body) { + if token == flag { + return "true" + } + } + return "false" +} + +func itemKind(statement fractchStatement) string { + if statement.Kind == "var" || statement.Kind == "cloud" { + return variableKind(statement) + } + return statement.Kind +} + +func (changes *fractchFileChanges) addItem(change string, statement fractchStatement) { + item := fractchItemChange{Change: change, Kind: itemKind(statement), Name: statement.Name} + if change == "removed" { + item.Before = statement.Body + } else { + item.After = statement.Body + } + if statement.Kind == "watch" { + field := "shown" + if change == "removed" { + field = "removed" + } + changes.addState("monitor", statement.Name, field, item.Before, item.After) + return + } + changes.Items = append(changes.Items, item) +} + +func lineSimilarity(a, b string) float64 { + aLines := strings.Split(a, "\n") + bLines := strings.Split(b, "\n") + counts := make(map[string]int, len(aLines)) + for _, line := range aLines { + counts[strings.TrimSpace(line)]++ + } + common := 0 + for _, line := range bLines { + key := strings.TrimSpace(line) + if counts[key] > 0 { + counts[key]-- + common++ + } + } + longest := len(aLines) + if len(bLines) > longest { + longest = len(bLines) + } + if longest == 0 { + return 1 + } + return float64(common) / float64(longest) +} + +type scriptPair struct { + before int + after int + score float64 +} + +func pairBySimilarity(before, after []fractchStatement, beforeUsed, afterUsed []bool, candidates func(i, j int) bool, minimum float64) []scriptPair { + pairs := []scriptPair{} + for i := range before { + if beforeUsed[i] { + continue + } + for j := range after { + if afterUsed[j] || !candidates(i, j) { + continue + } + score := lineSimilarity(before[i].Norm, after[j].Norm) + if score >= minimum { + pairs = append(pairs, scriptPair{before: i, after: j, score: score}) + } + } + } + sort.SliceStable(pairs, func(a, b int) bool { return pairs[a].score > pairs[b].score }) + chosen := []scriptPair{} + for _, pair := range pairs { + if beforeUsed[pair.before] || afterUsed[pair.after] { + continue + } + beforeUsed[pair.before] = true + afterUsed[pair.after] = true + chosen = append(chosen, pair) + } + return chosen +} + +func (changes *fractchFileChanges) compareScripts(before, after []fractchStatement) { + beforeUsed := make([]bool, len(before)) + afterUsed := make([]bool, len(after)) + partner := make([]int, len(after)) + for j := range partner { + partner[j] = -1 + } + byBody := map[string][]int{} + for i, statement := range before { + byBody[statement.Norm] = append(byBody[statement.Norm], i) + } + for j, statement := range after { + queue := byBody[statement.Norm] + if len(queue) == 0 { + continue + } + i := queue[0] + byBody[statement.Norm] = queue[1:] + beforeUsed[i] = true + afterUsed[j] = true + if before[i].At != statement.At { + changes.addState("script", statement.Head, "position", before[i].At, statement.At) + } + } + sameKey := func(i, j int) bool { return before[i].Key == after[j].Key } + for _, pair := range pairBySimilarity(before, after, beforeUsed, afterUsed, sameKey, 0) { + partner[pair.after] = pair.before + } + remainingBefore, remainingAfter := 0, 0 + for i := range before { + if !beforeUsed[i] { + remainingBefore++ + } + } + for j := range after { + if !afterUsed[j] { + remainingAfter++ + } + } + if remainingBefore*remainingAfter <= maxCrossPairProducts { + anyKey := func(i, j int) bool { return true } + for _, pair := range pairBySimilarity(before, after, beforeUsed, afterUsed, anyKey, crossPairSimilarity) { + partner[pair.after] = pair.before + } + } + for j, statement := range after { + if i := partner[j]; i >= 0 { + changes.Scripts = append(changes.Scripts, fractchScriptChange{ + Change: "changed", Kind: scriptKind(statement), Head: statement.Head, + Before: before[i].Body, After: statement.Body, BeforeAt: before[i].At, AfterAt: statement.At, + }) + continue + } + if !afterUsed[j] { + changes.Scripts = append(changes.Scripts, fractchScriptChange{ + Change: "added", Kind: scriptKind(statement), Head: statement.Head, After: statement.Body, AfterAt: statement.At, + }) + } + } + for i, statement := range before { + if !beforeUsed[i] { + changes.Scripts = append(changes.Scripts, fractchScriptChange{ + Change: "removed", Kind: scriptKind(statement), Head: statement.Head, Before: statement.Body, BeforeAt: statement.At, + }) + } + } +} + +func scriptKind(statement fractchStatement) string { + return firstWord.FindString(statement.Head) +} + +func (changes *fractchFileChanges) compareItems(before, after []fractchStatement) { + queues := map[string][]int{} + for i, statement := range before { + queues[statement.Key] = append(queues[statement.Key], i) + } + beforeUsed := make([]bool, len(before)) + unmatched := []fractchStatement{} + for _, statement := range after { + queue := queues[statement.Key] + if len(queue) == 0 { + unmatched = append(unmatched, statement) + continue + } + i := queue[0] + queues[statement.Key] = queue[1:] + beforeUsed[i] = true + changes.compareKeyed(before[i], statement) + } + removed := []fractchStatement{} + for i, statement := range before { + if !beforeUsed[i] { + removed = append(removed, statement) + } + } + emptyStage := fractchStatement{Kind: "stage", Key: "stage", Name: "Stage", Body: "stage"} + for _, statement := range unmatched { + if statement.Kind == "stage" { + changes.compareHeader(emptyStage, statement) + continue + } + renamedFrom := -1 + if statement.Kind == "costume" || statement.Kind == "backdrop" || statement.Kind == "sound" || statement.Kind == "sprite" { + for index, old := range removed { + if old.Kind == statement.Kind && (changes.sameAsset(old, statement) || (assetFile(old) != "" && assetFile(old) == assetFile(statement)) || (statement.Kind == "sprite" && withoutName(old) == withoutName(statement))) { + renamedFrom = index + break + } + } + } + if renamedFrom >= 0 { + old := removed[renamedFrom] + removed = append(removed[:renamedFrom], removed[renamedFrom+1:]...) + if statement.Kind == "sprite" { + changes.compareHeader(old, statement) + } else { + changes.Items = append(changes.Items, fractchItemChange{Change: "renamed", Kind: statement.Kind, Name: statement.Name, Before: old.Name, After: statement.Name}) + } + continue + } + changes.addItem("added", statement) + } + for _, statement := range removed { + if statement.Kind == "stage" { + changes.compareHeader(statement, emptyStage) + continue + } + changes.addItem("removed", statement) + } +} + +func withoutName(statement fractchStatement) string { + tokens := fractchTokens(statement.Body) + if len(tokens) > 1 && isFractchQuote(tokens[1][0]) { + tokens = append([]string{tokens[0]}, tokens[2:]...) + } + return strings.Join(tokens, " ") +} + +func compareFractch(before, after string) fractchFileChanges { + return compareFractchAssets(before, after, assetLookup{before: noAssets, after: noAssets}) +} + +func compareFractchAssets(before, after string, assets assetLookup) fractchFileChanges { + changes := fractchFileChanges{Scripts: []fractchScriptChange{}, Items: []fractchItemChange{}, State: []fractchStateChange{}, assets: assets} + beforeScripts, beforeItems := partitionStatements(parseFractchFile(before)) + afterScripts, afterItems := partitionStatements(parseFractchFile(after)) + changes.compareItems(beforeItems, afterItems) + changes.compareScripts(beforeScripts, afterScripts) + return changes +} + +func partitionStatements(statements []fractchStatement) ([]fractchStatement, []fractchStatement) { + scripts := []fractchStatement{} + items := []fractchStatement{} + for _, statement := range statements { + if statement.Kind == "script" { + scripts = append(scripts, statement) + } else { + items = append(items, statement) + } + } + return scripts, items +} diff --git a/native/gitinspection/fractch_test.go b/native/gitinspection/fractch_test.go new file mode 100644 index 0000000..b24eda7 --- /dev/null +++ b/native/gitinspection/fractch_test.go @@ -0,0 +1,418 @@ +package gitinspection + +import ( + "encoding/hex" + "sort" + "strings" + "testing" +) + +const fractchBase = `/** + * target: Cat + * targetId: abc + */ + +sprite "Cat" at 10,20 layer 2; +var score = 0 id "v1"; +var items = [1,2] id "l1"; +watch var "score" at 5,5; +costume "Idle" file "assets/Idle.svg" center 48,50 current; +costume "Run" file "assets/Run.svg" center 48,50; +sound "Meow" file "assets/Meow.wav"; + +when flag at 0,0 { + score = 0; + say "hi {there}; // not a comment"; +} + +when flag at 300,0 { + forever { + turn 15; + } +} + +// helper +def @jump(height) warp at 0,400 { + changeY height; +} +` + +func scriptChanges(t *testing.T, changes fractchFileChanges, change string) []fractchScriptChange { + t.Helper() + found := []fractchScriptChange{} + for _, script := range changes.Scripts { + if script.Change == change { + found = append(found, script) + } + } + return found +} + +func stateFields(changes fractchFileChanges) []string { + fields := []string{} + for _, state := range changes.State { + fields = append(fields, state.Kind+" "+state.Name+" "+state.Field+" "+state.Before+" -> "+state.After) + } + sort.Strings(fields) + return fields +} + +func TestCompareFractchUnchangedFileHasNoChanges(t *testing.T) { + changes := compareFractch(fractchBase, fractchBase) + if len(changes.Scripts) != 0 || len(changes.Items) != 0 || len(changes.State) != 0 { + t.Fatalf("expected no changes, got %+v", changes) + } +} + +func TestCompareFractchSeparatesSavedStateFromCode(t *testing.T) { + after := strings.NewReplacer( + `sprite "Cat" at 10,20 layer 2;`, `sprite "Cat" at -4,7 hidden layer 5;`, + `var score = 0 id "v1";`, `var score = 12 id "v1";`, + `watch var "score" at 5,5;`, `watch var "score" at 5,40 hidden;`, + `center 48,50 current;`, `center 48,50;`, + `file "assets/Run.svg" center 48,50;`, `file "assets/Run.svg" center 48,50 current;`, + `when flag at 300,0 {`, `when flag at 512,96 {`, + ).Replace(fractchBase) + changes := compareFractch(fractchBase, after) + if len(changes.Scripts) != 0 || len(changes.Items) != 0 { + t.Fatalf("saved state was reported as code: scripts %+v items %+v", changes.Scripts, changes.Items) + } + want := []string{ + "costume Idle current true -> false", + "costume Run current false -> true", + `monitor var score display watch var "score" at 5,5; -> watch var "score" at 5,40 hidden;`, + "script when flag position 300,0 -> 512,96", + "sprite Cat layer 2 -> 5", + "sprite Cat position 10,20 -> -4,7", + "sprite Cat visible true -> false", + "variable score value 0 -> 12", + } + got := stateFields(changes) + if strings.Join(got, "\n") != strings.Join(want, "\n") { + t.Fatalf("state changes:\n%s\nwant:\n%s", strings.Join(got, "\n"), strings.Join(want, "\n")) + } +} + +func TestCompareFractchPairsEditedScriptAmongSameHats(t *testing.T) { + after := strings.Replace(fractchBase, "turn 15;", "turn 30;", 1) + changes := compareFractch(fractchBase, after) + changed := scriptChanges(t, changes, "changed") + if len(changes.Scripts) != 1 || len(changed) != 1 { + t.Fatalf("expected one changed script, got %+v", changes.Scripts) + } + if !strings.Contains(changed[0].Before, "turn 15") || !strings.Contains(changed[0].After, "turn 30") || changed[0].Head != "when flag" { + t.Fatalf("wrong script pairing: %+v", changed[0]) + } + if strings.Contains(changed[0].After, "at 300,0") { + t.Fatalf("script text keeps its canvas position: %q", changed[0].After) + } +} + +func TestCompareFractchIgnoresScriptOrder(t *testing.T) { + first := "when flag at 0,0 {\n score = 0;\n say \"hi {there}; // not a comment\";\n}" + second := "when flag at 300,0 {\n forever {\n turn 15;\n }\n}" + reordered := strings.Replace(fractchBase, first+"\n\n"+second, second+"\n\n"+first, 1) + if reordered == fractchBase { + t.Fatal("test setup did not reorder scripts") + } + changes := compareFractch(fractchBase, reordered) + if len(changes.Scripts) != 0 || len(changes.Items) != 0 || len(changes.State) != 0 { + t.Fatalf("reordering scripts reported changes: %+v", changes) + } +} + +func TestCompareFractchReportsAddedAndRemovedScripts(t *testing.T) { + after := strings.Replace(fractchBase, "when flag at 300,0 {\n forever {\n turn 15;\n }\n}", "when key \"space\" pressed at 0,700 {\n jump(10);\n}", 1) + changes := compareFractch(fractchBase, after) + added := scriptChanges(t, changes, "added") + removed := scriptChanges(t, changes, "removed") + if len(added) != 1 || len(removed) != 1 || len(changes.Scripts) != 2 { + t.Fatalf("expected one added and one removed script, got %+v", changes.Scripts) + } + if added[0].Head != `when key "space" pressed` || added[0].AfterAt != "0,700" || removed[0].BeforeAt != "300,0" { + t.Fatalf("wrong scripts: %+v %+v", added[0], removed[0]) + } +} + +func TestCompareFractchMatchesDefinitionsByName(t *testing.T) { + after := strings.Replace(fractchBase, "def @jump(height) warp at 0,400 {\n changeY height;", "def @jump(height, speed) warp at 0,400 {\n changeY height * speed;", 1) + changes := compareFractch(fractchBase, after) + changed := scriptChanges(t, changes, "changed") + if len(changes.Scripts) != 1 || len(changed) != 1 || changed[0].Kind != "def" { + t.Fatalf("expected the definition to be changed, got %+v", changes.Scripts) + } + if !strings.HasPrefix(changed[0].Before, "// helper\n") { + t.Fatalf("leading comment is not part of the script: %q", changed[0].Before) + } +} + +func TestCompareFractchReportsAssetAndVariableEdits(t *testing.T) { + after := strings.NewReplacer( + `costume "Run" file "assets/Run.svg"`, `costume "Sprint" file "assets/Run.svg"`, + `sound "Meow" file "assets/Meow.wav";`, "sound \"Meow\" file \"assets/Meow.wav\";\nsound \"Purr\" file \"assets/Purr.wav\";", + `var items = [1,2] id "l1";`, `var stock = [1,2] id "l1";`, + `costume "Idle" file "assets/Idle.svg" center 48,50 current;`, `costume "Idle" file "assets/Idle.svg" center 10,10 current;`, + ).Replace(fractchBase) + changes := compareFractch(fractchBase, after) + got := []string{} + for _, item := range changes.Items { + got = append(got, item.Change+" "+item.Kind+" "+item.Name) + } + sort.Strings(got) + want := []string{"added sound Purr", "changed costume Idle", "renamed costume Sprint", "renamed list stock"} + if strings.Join(got, ", ") != strings.Join(want, ", ") { + t.Fatalf("items %v, want %v", got, want) + } + if len(changes.State) != 0 || len(changes.Scripts) != 0 { + t.Fatalf("unexpected changes: %+v", changes) + } +} + +func TestCompareFractchStageSettingsAreState(t *testing.T) { + before := "/**\n * target: Stage\n * targetId: \n */\n\nuse \"pen\";\nvar x = 1 id \"a\";\n" + after := "/**\n * target: Stage\n * targetId: \n */\n\nstage volume 40 tempo 90;\nuse \"pen\";\nvar x = 1 id \"a\";\n" + changes := compareFractch(before, after) + got := stateFields(changes) + want := []string{"stage Stage tempo 60 -> 90", "stage Stage volume 100 -> 40"} + if strings.Join(got, "\n") != strings.Join(want, "\n") || len(changes.Items) != 0 { + t.Fatalf("stage changes %v items %+v", got, changes.Items) + } +} + +func TestSplitFractchStatementsSkipsStringsAndComments(t *testing.T) { + src := "var a = \"x;y{\" id \"1\";\nwhen flag at 1,2 {\n say \"\"\"raw } text\n{\"\"\";\n // brace } in comment\n say `tick } ;`;\n}\n/* stray */\n" + statements := splitFractchStatements(src) + if len(statements) != 3 { + t.Fatalf("expected three statements, got %d: %q", len(statements), statements) + } + if !strings.HasSuffix(statements[1], "say `tick } ;`;\n}") { + t.Fatalf("script cut short: %q", statements[1]) + } + if statements[2] != "/* stray */" { + t.Fatalf("trailing comment lost: %q", statements[2]) + } +} + +func TestCompareFractchClipsLongStateValues(t *testing.T) { + long := "[" + strings.Repeat("1.2345,", 200) + "0]" + before := "sprite \"A\";\nvar y = [] id \"l\";\n" + after := "sprite \"A\";\nvar y = " + long + " id \"l\";\n" + changes := compareFractch(before, after) + if len(changes.State) != 1 || !changes.State[0].Truncated || len([]rune(changes.State[0].After)) != maxStateValueChars || changes.State[0].Kind != "list" { + t.Fatalf("long list value not clipped: %+v", changes.State) + } +} + +func (r *testRepository) nestedTree(files map[string]string) string { + type entry struct { + name string + mode string + oid string + } + children := map[string]map[string]string{} + entries := []entry{} + for path, oid := range files { + if slash := strings.IndexByte(path, '/'); slash >= 0 { + dir := path[:slash] + if children[dir] == nil { + children[dir] = map[string]string{} + } + children[dir][path[slash+1:]] = oid + continue + } + entries = append(entries, entry{name: path, mode: "100644", oid: oid}) + } + for dir, nested := range children { + entries = append(entries, entry{name: dir, mode: "40000", oid: r.nestedTree(nested)}) + } + sort.Slice(entries, func(i, j int) bool { return entries[i].name < entries[j].name }) + body := []byte{} + for _, item := range entries { + decoded, _ := hex.DecodeString(item.oid) + body = append(body, []byte(item.mode+" "+item.name+"\x00")...) + body = append(body, decoded...) + } + return r.object("tree", body) +} + +func TestInspectChangesSummarisesCommit(t *testing.T) { + repo := newTestRepository() + asset := repo.object("blob", []byte("")) + firstTree := repo.nestedTree(map[string]string{ + "Cat/main.fractch": repo.object("blob", []byte(fractchBase)), + "Cat/assets/Idle.svg": asset, + "Stage/main.fractch": repo.object("blob", []byte("var x = 1 id \"a\";\n")), + "fractch.json": repo.object("blob", []byte(`{"order":["Cat"]}`)), + "Cat/assets/Run.svg": asset, + "Cat/assets/Meow.wav": repo.object("blob", []byte("RIFF")), + "Stage/assets/bg.svg": asset, + "README.md": repo.object("blob", []byte("hello\n")), + "Stage/assets/old.svg": asset, + }) + first := repo.commit(firstTree, "", "Initial version") + edited := strings.Replace(fractchBase, "turn 15;", "turn 45;", 1) + secondTree := repo.nestedTree(map[string]string{ + "Cat/main.fractch": repo.object("blob", []byte(edited)), + "Cat/assets/Idle.svg": asset, + "Stage/main.fractch": repo.object("blob", []byte("var x = 2 id \"a\";\n")), + "fractch.json": repo.object("blob", []byte(`{"order":["Cat","Dog"]}`)), + "Cat/assets/Run.svg": asset, + "Cat/assets/Meow.wav": repo.object("blob", []byte("RIFF2")), + "Stage/assets/bg.svg": asset, + "README.md": repo.object("blob", []byte("hello\nworld\n")), + "Stage/assets/new.svg": repo.object("blob", []byte("")), + }) + second := repo.commit(secondTree, first, "Updated project") + path := repo.write(t) + + result := decodeResult(t, Inspect(path, second, "changes", "", second)) + if result["ok"] != true { + t.Fatalf("inspection failed: %v", result) + } + totals := result["totals"].(map[string]any) + scripts := totals["scripts"].(map[string]any) + assets := totals["assets"].(map[string]any) + if scripts["changed"] != float64(1) || scripts["added"] != float64(0) || totals["state"] != float64(1) || totals["codeChanged"] != true { + t.Fatalf("unexpected totals: %v", totals) + } + if assets["added"] != float64(1) || assets["removed"] != float64(1) || assets["modified"] != float64(1) || totals["files"] != float64(2) { + t.Fatalf("unexpected asset totals: %v", totals) + } + targets := result["targets"].([]any) + if len(targets) != 2 || targets[0].(map[string]any)["path"] != "Cat/main.fractch" || targets[1].(map[string]any)["path"] != "Stage/main.fractch" { + t.Fatalf("unexpected targets: %v", targets) + } + for _, raw := range result["files"].([]any) { + file := raw.(map[string]any) + if file["path"] == "README.md" && file["newData"] == nil { + t.Fatalf("readme text was not inlined: %v", file) + } + if isFractchPath(file["path"].(string)) { + t.Fatalf("fractch file listed with plain files: %v", file) + } + } + + initial := decodeResult(t, Inspect(path, first, "changes", "", second)) + initialTotals := initial["totals"].(map[string]any) + if initialTotals["scripts"].(map[string]any)["added"] != float64(3) || initialTotals["codeChanged"] != true { + t.Fatalf("initial commit totals: %v", initialTotals) + } +} + +func TestInspectChangesStateOnlyCommitIsNotCode(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) + secondTree := repo.nestedTree(map[string]string{"Cat/main.fractch": repo.object("blob", []byte(moved))}) + second := repo.commit(secondTree, first, "Updated project") + result := decodeResult(t, Inspect(repo.write(t), second, "changes", "", second)) + totals := result["totals"].(map[string]any) + if totals["codeChanged"] != false || totals["state"] != float64(1) { + t.Fatalf("state-only commit totals: %v", totals) + } +} + +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") + head := newTestRepository() + headCommit := head.commit(head.nestedTree(map[string]string{"Cat/main.fractch": head.object("blob", []byte(strings.Replace(fractchBase, "score = 0;", "score = 5;", 1)))}), "", "Remix") + result := decodeResult(t, CompareArchivesChangesJSON(base.write(t), baseCommit, head.write(t), headCommit)) + if result["ok"] != true { + t.Fatalf("compare failed: %v", result) + } + scripts := result["totals"].(map[string]any)["scripts"].(map[string]any) + if scripts["changed"] != float64(1) { + t.Fatalf("unexpected totals: %v", result["totals"]) + } +} + +func TestCompareFractchIgnoresHiddenValuesUnderReporters(t *testing.T) { + before := "sprite \"A\";\nwhen flag at 0,0 {\n setX clamp(xPosition(), -100, 100);\n say answer();\n point (x * 2) + 1;\n}\n" + after := "sprite \"A\";\nwhen flag at 0,0 {\n setX (clamp(xPosition(), -100, 100) ?? -100);\n say (answer() ?? \"Hello (there)\");\n point (x * 2) + 1;\n}\n" + changes := compareFractch(before, after) + if len(changes.Scripts) != 0 { + t.Fatalf("hidden values were reported as edits: %+v", changes.Scripts) + } + edited := strings.Replace(after, "say (answer()", "think (answer()", 1) + if changed := compareFractch(before, edited); len(changed.Scripts) != 1 || changed.Scripts[0].Change != "changed" { + t.Fatalf("a real edit next to a hidden value was lost: %+v", changed.Scripts) + } +} + +func TestStripHiddenValuesKeepsCallsAndExpressions(t *testing.T) { + cases := map[string]string{ + "setX (x ?? -30);": "setX x;", + "say (join(a, b) ?? \"hi\");": "say join(a, b);", + "go((x ?? 1));": "go(x);", + "f(x ?? 1);": "f(x);", + "move (a + b ?? 3) * 2;": "move (a + b) * 2;", + "say \"(x ?? 1)\";": "say \"(x ?? 1)\";", + "say (answer() ?? (\"a\"));": "say answer();", + "pick(a ?? 1, b);": "pick(a ?? 1, b);", + } + for input, want := range cases { + if got := stripHiddenValues(input); got != want { + t.Errorf("stripHiddenValues(%q) = %q, want %q", input, got, want) + } + } +} + +func TestCompareFractchIgnoresRotationCentreJitter(t *testing.T) { + before := "sprite \"A\";\ncostume \"Sign\" file \"assets/Sign.svg\" center 80.86252757812503,132.723585 current;\n" + after := "sprite \"A\";\ncostume \"Sign\" file \"assets/Sign.svg\" center 80.862527578125,132.72357500000004 current;\n" + if changes := compareFractch(before, after); len(changes.Items) != 0 || len(changes.State) != 0 { + t.Fatalf("float jitter in a rotation centre was reported: %+v", changes) + } + moved := strings.Replace(after, "center 80.862527578125", "center 40", 1) + if changes := compareFractch(before, moved); len(changes.Items) != 1 || changes.Items[0].Change != "changed" { + t.Fatalf("a moved rotation centre was not reported: %+v", changes.Items) + } +} + +func TestCompareFractchMatchesCostumesByAssetContent(t *testing.T) { + before := "sprite \"A\";\ncostume \"Magnibrowse\" file \"assets/Magnibrowse.svg\" center 10,10;\ncostume \"Idle\" file \"assets/Idle.svg\" center 10,10;\n" + after := "sprite \"A\";\ncostume \"iframe browse\" file \"assets/iframe_browse.svg\" center 10,10;\ncostume \"Idle\" file \"assets/Idle.svg\" center 10,10;\n" + beforeAssets := map[string]string{"assets/Magnibrowse.svg": "aaa", "assets/Idle.svg": "bbb"} + afterAssets := map[string]string{"assets/iframe_browse.svg": "aaa", "assets/Idle.svg": "ccc"} + changes := compareFractchAssets(before, after, assetLookup{ + before: func(path string) string { return beforeAssets[path] }, + after: func(path string) string { return afterAssets[path] }, + }) + got := []string{} + for _, item := range changes.Items { + got = append(got, item.Change+" "+item.Name) + } + sort.Strings(got) + if strings.Join(got, ", ") != "edited Idle, renamed iframe browse" { + t.Fatalf("items %v", got) + } +} + +func TestCompareFractchIgnoresOlderSnakeCaseSpellings(t *testing.T) { + before := "sprite \"A\";\nwhen flag at 0,0 {\n wait_until !mouseDown();\n if sprites[\"_stage_\"].backdrop_number == 3 {\n stop other_scripts_in_sprite;\n }\n}\n" + after := "sprite \"A\";\nwhen flag at 0,0 {\n waitUntil !mouseDown();\n if sprites[\"_stage_\"].backdropNumber == 3 {\n stop otherScriptsInSprite;\n }\n}\n" + if changes := compareFractch(before, after); len(changes.Scripts) != 0 { + t.Fatalf("a newer fractch spelling was reported as an edit: %+v", changes.Scripts) + } + renamedString := strings.Replace(after, `sprites["_stage_"]`, `sprites["_stage"]`, 1) + if changes := compareFractch(before, renamedString); len(changes.Scripts) != 1 { + t.Fatalf("an edit inside a string was hidden: %+v", changes.Scripts) + } +} + +func TestCamelIdentifiers(t *testing.T) { + cases := map[string]string{ + "wait_until x_pos;": "waitUntil xPos;", + "Big_Name _private a1_b2": "Big_Name _private a1B2", + "say \"snake_case\";": "say \"snake_case\";", + "// keep_this\nset_x 1;": "// keep_this\nsetX 1;", + "strings.menu_textCase(x)": "strings.menuTextCase(x)", + } + for input, want := range cases { + if got := camelIdentifiers(input); got != want { + t.Errorf("camelIdentifiers(%q) = %q, want %q", input, got, want) + } + } +} diff --git a/native/gitinspection/inspection.go b/native/gitinspection/inspection.go index d621614..e4e08d4 100644 --- a/native/gitinspection/inspection.go +++ b/native/gitinspection/inspection.go @@ -347,6 +347,8 @@ func Inspect(workspacePath, sha, operation, requestedPath, allowedHeads string) return failure("file was not found at this commit") case "commit-inline": return archive.inspectCommit(sha, commit, true) + case "changes": + return archive.inspectCommitChanges(sha, commit) default: return archive.inspectCommit(sha, commit, false) } diff --git a/opal.json b/opal.json index c179af4..a1a0de4 100644 --- a/opal.json +++ b/opal.json @@ -1,6 +1,6 @@ { "name": "mistwarp-api", - "version": "1.1.1", + "version": "1.2.0", "main": "main.osl", "scripts": { "dev": ["osl", "run", "main.osl"], diff --git a/scripts/e2e_smoke.py b/scripts/e2e_smoke.py index 37ad9e5..b8ddfaa 100644 --- a/scripts/e2e_smoke.py +++ b/scripts/e2e_smoke.py @@ -599,6 +599,8 @@ def head_and_edited(): step("upload remix change", "POST", "/v1/projects//upload", "alice", form=({"git": metadata}, {"project": plain, "workspace": archive})) status, pull = step("open pull", "POST", path + "/pulls", "alice", {"sourceProjectId": remix_id, "title": "Smoke pull", "body": "From the remix"}) pull_index = str(((pull.get("pull") or {}).get("index", "")) if isinstance(pull, dict) else "") + step("pull changes", "GET", path + "/pulls/" + pull_index + "/diff?view=changes", "sam") + step("pull changes as stranger", "GET", path + "/pulls/" + pull_index + "/diff?view=changes", "bob") status, bob_project = step("bob create", "POST", "/v1/projects", "bob", {"title": "Bob smoke"}) ids[str(bob_project.get("id", "")) if isinstance(bob_project, dict) else ""] = "" with server.faulty("projects"): @@ -639,6 +641,9 @@ def head_and_edited(): step("credit unknown commit", "PATCH", path + "/commits/" + "1" * 40, "sam", {"coAuthors": ["alice"]}) step("credit commit as stranger", "PATCH", path + "/commits/" + head, "bob", {"coAuthors": ["alice"]}) step("commit co-authors", "GET", path + "/commits/" + head + "/co-authors") + 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("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"}) diff --git a/src/git/git-inspection.osl b/src/git/git-inspection.osl index cdd65b1..b1274a4 100644 --- a/src/git/git-inspection.osl +++ b/src/git/git-inspection.osl @@ -259,6 +259,15 @@ def handleGetCommitInspection(*serve.Context c) ( gitInspectionResponse(c, res.project.assert(object), c.param("sha"), operation, "") ) +def handleGetCommitChanges(*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"), "changes", "") +) + def handleGetCommitTree(*serve.Context c) ( auto res = loadProject(c.param("id")) if !res.ok ( diff --git a/src/git/pulls.osl b/src/git/pulls.osl index 6a64433..f9f0ba8 100644 --- a/src/git/pulls.osl +++ b/src/git/pulls.osl @@ -685,6 +685,16 @@ def canAccessPullProjectCommit(object project, string targetId, string pullIndex return canInspectPull(pull, target, c) ) +def pullInspectionJSON(string view, string inline, string targetWorkspacePath, string baseCommit, string sourceWorkspacePath, string headCommit) string ( + if view == "changes" ( + return gitinspection.CompareArchivesChangesJSON(targetWorkspacePath, baseCommit, sourceWorkspacePath, headCommit) + ) + if inline == "1" ( + return gitinspection.CompareArchivesInlineJSON(targetWorkspacePath, baseCommit, sourceWorkspacePath, headCommit) + ) + return gitinspection.CompareArchivesJSON(targetWorkspacePath, baseCommit, sourceWorkspacePath, headCommit) +) + def handleGetPullDiff(*serve.Context c) ( c.setHeader("Cache-Control", "private, no-store") c.setHeader("CDN-Cache-Control", "private, no-store") @@ -713,7 +723,7 @@ def handleGetPullDiff(*serve.Context c) ( ) string baseCommit = pull.baseCommit.toStr() string headCommit = pull.headCommit.toStr() - string inspectionJson = if (c.query("inline") ?? "") == "1" then gitinspection.CompareArchivesInlineJSON(targetWorkspacePath, baseCommit, sourceWorkspacePath, headCommit) else gitinspection.CompareArchivesJSON(targetWorkspacePath, baseCommit, sourceWorkspacePath, headCommit) + string inspectionJson = pullInspectionJSON((c.query("view") ?? ""), (c.query("inline") ?? ""), targetWorkspacePath, baseCommit, sourceWorkspacePath, headCommit) auto parsedInspection = json.parse(inspectionJson, {type: "object"}) if parsedInspection.isErr() ( c.internalError("could not inspect pull request changes") diff --git a/src/git/routes.osl b/src/git/routes.osl index 89c650e..5f727c6 100644 --- a/src/git/routes.osl +++ b/src/git/routes.osl @@ -11,6 +11,7 @@ def registerGitRoutes(*serve.Router api) ( api.DELETE("/projects/:id/branches", requireAuth, handleDeleteProjectBranch) api.POST("/projects/:id/branches/merge", requireAuth, handlePrepareBranchMerge) api.GET("/projects/:id/commits/:sha", handleGetCommitInspection) + api.GET("/projects/:id/commits/:sha/changes", handleGetCommitChanges) api.GET("/projects/:id/commits/:sha/tree", handleGetCommitTree) api.GET("/projects/:id/commits/:sha/file", handleGetCommitFile) From 507987d8b43ddd9f8275fe320e81f49fe6d46d7c Mon Sep 17 00:00:00 2001 From: Mistium <92952823+Mistium@users.noreply.github.com> Date: Fri, 20 Nov 2026 16:51:02 +0000 Subject: [PATCH 2/5] Bound the changes view on large input and compare sprites as a whole Rename matching uses an index, same-hat pairing falls back to order past a work budget, and hidden-value stripping skips text nested deeper than 64 parentheses, so adversarial files stay linear. Each sprite folder's region files are compared together, so a script moved between regions is reported as moved. Statements may end without a semicolon, free comments stay apart from the next script, costume and sound order changes are reported, redrawn costumes are named even when the sprite file is unchanged, and totals report stateOnly instead of codeChanged. --- native/gitinspection/changes.go | 189 ++-- native/gitinspection/fractch.go | 1031 +++++++------------ native/gitinspection/fractch_limits_test.go | 150 +++ native/gitinspection/fractch_parse.go | 539 ++++++++++ native/gitinspection/fractch_test.go | 11 +- 5 files changed, 1183 insertions(+), 737 deletions(-) create mode 100644 native/gitinspection/fractch_limits_test.go create mode 100644 native/gitinspection/fractch_parse.go diff --git a/native/gitinspection/changes.go b/native/gitinspection/changes.go index 1f4d65b..adcb901 100644 --- a/native/gitinspection/changes.go +++ b/native/gitinspection/changes.go @@ -9,6 +9,7 @@ import ( type fractchTargetChanges struct { Path string `json:"path"` Status string `json:"status"` + Files []string `json:"files"` TooLarge bool `json:"tooLarge,omitempty"` Scripts []fractchScriptChange `json:"scripts"` Items []fractchItemChange `json:"items"` @@ -29,21 +30,38 @@ type assetCounts struct { } type changeTotals struct { - Scripts scriptCounts `json:"scripts"` - Items int `json:"items"` - State int `json:"state"` - Assets assetCounts `json:"assets"` - Files int `json:"files"` - CodeChanged bool `json:"codeChanged"` + Scripts scriptCounts `json:"scripts"` + Items int `json:"items"` + State int `json:"state"` + Assets assetCounts `json:"assets"` + Files int `json:"files"` + StateOnly bool `json:"stateOnly"` } -func targetAssets(files map[string]gitFile, targetPath string) func(string) string { - dir := "" - if slash := strings.LastIndexByte(targetPath, '/'); slash >= 0 { - dir = targetPath[:slash+1] +func pathDir(path string) string { + if slash := strings.LastIndexByte(path, '/'); slash >= 0 { + return path[:slash] + } + return "" +} + +func spriteDir(path string) string { + if index := strings.Index(path, "/assets/"); index >= 0 { + return path[:index] + } + if strings.HasPrefix(path, "assets/") { + return "" + } + return pathDir(path) +} + +func dirAssets(files map[string]gitFile, dir string) func(string) string { + prefix := "" + if dir != "" { + prefix = dir + "/" } return func(relative string) string { - return files[dir+relative].OID + return files[prefix+relative].OID } } @@ -55,90 +73,125 @@ func isAssetPath(path string) bool { return strings.HasPrefix(path, "assets/") || strings.Contains(path, "/assets/") || strings.HasPrefix(path, "fonts/") } -func blobText(source archive, files map[string]gitFile, path string) (string, bool, bool) { - file, ok := files[path] - if !ok { - return "", true, false +func dirFractchFiles(files map[string]gitFile, dir string) []string { + paths := []string{} + for path := range files { + if isFractchPath(path) && pathDir(path) == dir { + paths = append(paths, path) + } } - if file.Size > maxInlineDiffFileBytes { - return "", false, true + sort.Strings(paths) + return paths +} + +func readSources(source archive, files map[string]gitFile, paths []string, budget *int) ([]fractchSource, bool) { + sources := []fractchSource{} + for _, path := range paths { + file := files[path] + if file.Size > maxInlineDiffFileBytes || *budget+file.Size > maxInlineDiffTotalBytes { + return nil, false + } + kind, content, found := source.readObject(file.OID) + if !found || kind != "blob" { + return nil, false + } + *budget += file.Size + sources = append(sources, fractchSource{Path: path, Text: string(content)}) } - kind, content, found := source.readObject(file.OID) - if !found || kind != "blob" { - return "", false, false + return sources, true +} + +func (totals *changeTotals) countTarget(target fractchTargetChanges) { + for _, script := range target.Scripts { + switch script.Change { + case "added": + totals.Scripts.Added++ + case "removed": + totals.Scripts.Removed++ + case "moved": + totals.Scripts.Moved++ + default: + totals.Scripts.Changed++ + } } - return string(content), true, false + totals.Items += len(target.Items) + totals.State += len(target.State) } func structuredChanges(changes []map[string]any, baseArchive archive, parentFiles map[string]gitFile, headArchive archive, currentFiles map[string]gitFile) ([]fractchTargetChanges, []map[string]any, changeTotals) { - targets := []fractchTargetChanges{} others := []map[string]any{} totals := changeTotals{} - budget := 0 + dirs := map[string]bool{} + authoredFiles := 0 for _, change := range changes { path, _ := change["path"].(string) status, _ := change["status"].(string) - if !isFractchPath(path) { - if isAssetPath(path) { - switch status { - case "added": - totals.Assets.Added++ - case "removed": - totals.Assets.Removed++ - default: - totals.Assets.Modified++ - } - } else { - totals.Files++ - totals.CodeChanged = totals.CodeChanged || path != "fractch.json" + if isFractchPath(path) { + dirs[pathDir(path)] = true + continue + } + others = append(others, change) + if isAssetPath(path) { + dirs[spriteDir(path)] = true + switch status { + case "added": + totals.Assets.Added++ + case "removed": + totals.Assets.Removed++ + default: + totals.Assets.Modified++ } - others = append(others, change) continue } - target := fractchTargetChanges{Path: path, Status: status, Scripts: []fractchScriptChange{}, Items: []fractchItemChange{}, State: []fractchStateChange{}} - before, beforeOK, beforeLarge := blobText(baseArchive, parentFiles, path) - after, afterOK, afterLarge := blobText(headArchive, currentFiles, path) - size := len(before) + len(after) - if beforeLarge || afterLarge || budget+size > maxInlineDiffTotalBytes { - target.TooLarge = true - totals.CodeChanged = true - targets = append(targets, target) + totals.Files++ + if path != "fractch.json" { + authoredFiles++ + } + } + ordered := []string{} + for dir := range dirs { + ordered = append(ordered, dir) + } + sort.Strings(ordered) + targets := []fractchTargetChanges{} + budget := 0 + tooLarge := false + for _, dir := range ordered { + beforePaths := dirFractchFiles(parentFiles, dir) + afterPaths := dirFractchFiles(currentFiles, dir) + if len(beforePaths) == 0 && len(afterPaths) == 0 { continue } + status := "modified" + if len(beforePaths) == 0 { + status = "added" + } else if len(afterPaths) == 0 { + status = "removed" + } + listed := afterPaths + if len(listed) == 0 { + listed = beforePaths + } + target := fractchTargetChanges{Path: dir, Status: status, Files: listed, Scripts: []fractchScriptChange{}, Items: []fractchItemChange{}, State: []fractchStateChange{}} + before, beforeOK := readSources(baseArchive, parentFiles, beforePaths, &budget) + after, afterOK := readSources(headArchive, currentFiles, afterPaths, &budget) if !beforeOK || !afterOK { target.TooLarge = true - totals.CodeChanged = true + tooLarge = true targets = append(targets, target) continue } - budget += size - compared := compareFractchAssets(before, after, assetLookup{before: targetAssets(parentFiles, path), after: targetAssets(currentFiles, path)}) + compared := compareFractchSources(before, after, assetLookup{before: dirAssets(parentFiles, dir), after: dirAssets(currentFiles, dir)}) target.Scripts, target.Items, target.State = compared.Scripts, compared.Items, compared.State - for _, script := range target.Scripts { - switch script.Change { - case "added": - totals.Scripts.Added++ - case "removed": - totals.Scripts.Removed++ - default: - totals.Scripts.Changed++ - } - } - for _, state := range target.State { - if state.Kind == "script" && state.Field == "position" { - totals.Scripts.Moved++ - } - } - totals.Items += len(target.Items) - totals.State += len(target.State) - if len(target.Scripts) > 0 || len(target.Items) > 0 { - totals.CodeChanged = true - } if len(target.Scripts) == 0 && len(target.Items) == 0 && len(target.State) == 0 { continue } + totals.countTarget(target) targets = append(targets, target) } + scripts := totals.Scripts.Added + totals.Scripts.Removed + totals.Scripts.Changed + totals.Scripts.Moved + assets := totals.Assets.Added + totals.Assets.Removed + totals.Assets.Modified + totals.StateOnly = totals.State > 0 && scripts == 0 && totals.Items == 0 && authoredFiles == 0 && assets == 0 && !tooLarge return targets, others, totals } diff --git a/native/gitinspection/fractch.go b/native/gitinspection/fractch.go index 272bbc5..cbb3621 100644 --- a/native/gitinspection/fractch.go +++ b/native/gitinspection/fractch.go @@ -2,37 +2,28 @@ package gitinspection import ( "math" - "regexp" "sort" "strconv" "strings" ) const ( - maxStateValueChars = 160 - maxCrossPairProducts = 40_000 - crossPairSimilarity = 0.6 + maxStateValueChars = 160 + maxPairsPerGroup = 10_000 + maxSimilarityWork = 2_000_000 + crossPairSimilarity = 0.6 ) -type fractchStatement struct { - Kind string - Key string - Name string - Text string - Head string - At string - Body string - Norm string -} - type fractchScriptChange struct { - Change string `json:"change"` - Kind string `json:"kind"` - Head string `json:"head"` - Before string `json:"before,omitempty"` - After string `json:"after,omitempty"` - BeforeAt string `json:"beforeAt,omitempty"` - AfterAt string `json:"afterAt,omitempty"` + Change string `json:"change"` + Kind string `json:"kind"` + Head string `json:"head"` + File string `json:"file"` + BeforeFile string `json:"beforeFile,omitempty"` + Before string `json:"before,omitempty"` + After string `json:"after,omitempty"` + BeforeAt string `json:"beforeAt,omitempty"` + AfterAt string `json:"afterAt,omitempty"` } type fractchItemChange struct { @@ -57,6 +48,7 @@ type fractchFileChanges struct { Items []fractchItemChange `json:"items"` State []fractchStateChange `json:"state"` assets assetLookup + work int } type assetLookup struct { @@ -64,423 +56,13 @@ type assetLookup struct { after func(string) string } -func noAssets(string) string { - return "" -} - -func isIdentifierByte(c byte) bool { - return c == '_' || c == ')' || c == ']' || (c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') -} - -func stripHiddenValues(src string) string { - out, _, _ := stripHiddenFrom(src, 0, false) - return out -} - -func stripHiddenFrom(src string, i int, inParen bool) (string, int, bool) { - var builder strings.Builder - hidden := -1 - commaAfterHidden := false - for i < len(src) { - c := src[i] - if isFractchQuote(c) { - end := skipFractchString(src, i) - builder.WriteString(src[i:end]) - i = end - continue - } - if strings.HasPrefix(src[i:], "//") || strings.HasPrefix(src[i:], "/*") { - end := skipFractchComment(src, i) - builder.WriteString(src[i:end]) - i = end - continue - } - if c == '(' { - inner, next, stripped := stripHiddenFrom(src, i+1, true) - before := byte(' ') - if i > 0 { - before = src[i-1] - } - after := nextNonSpace(src, next) - standalone := !isIdentifierByte(before) && (after == 0 || strings.IndexByte(";,){]\n", after) >= 0) - if stripped && standalone { - builder.WriteString(inner) - } else { - builder.WriteString("(" + inner + ")") - } - i = next - continue - } - if c == ')' && inParen { - content := builder.String() - if hidden >= 0 && !commaAfterHidden { - return strings.TrimSpace(content[:hidden]), i + 1, true - } - return content, i + 1, false - } - if c == ',' && hidden >= 0 { - commaAfterHidden = true - } - if inParen && hidden < 0 && strings.HasPrefix(src[i:], "??") { - hidden = builder.Len() - } - builder.WriteByte(c) - i++ - } - return builder.String(), i, false -} - -func camelIdentifiers(src string) string { - var builder strings.Builder - for i := 0; i < len(src); { - c := src[i] - if isFractchQuote(c) { - end := skipFractchString(src, i) - builder.WriteString(src[i:end]) - i = end - continue - } - if strings.HasPrefix(src[i:], "//") || strings.HasPrefix(src[i:], "/*") { - end := skipFractchComment(src, i) - builder.WriteString(src[i:end]) - i = end - continue - } - if !(c == '_' || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) || (i > 0 && isIdentifierByte(src[i-1])) { - builder.WriteByte(c) - i++ - continue - } - end := i - for end < len(src) && (isIdentifierByte(src[end]) && src[end] != ')' && src[end] != ']') { - end++ - } - word := src[i:end] - if c >= 'a' && c <= 'z' && strings.Contains(word, "_") { - upper := false - for index := 0; index < len(word); index++ { - letter := word[index] - if letter == '_' { - upper = true - continue - } - if upper && letter >= 'a' && letter <= 'z' { - letter -= 'a' - 'A' - } - upper = false - builder.WriteByte(letter) - } - } else { - builder.WriteString(word) - } - i = end - } - return builder.String() -} - -func nextNonSpace(src string, i int) byte { - for i < len(src) { - if src[i] != ' ' && src[i] != '\t' && src[i] != '\r' { - return src[i] - } - i++ - } - return 0 -} - -var ( - positionSuffix = regexp.MustCompile(`\s+at\s+(-?[0-9.eE+-]+)\s*,\s*(-?[0-9.eE+-]+)\s*$`) - definitionName = regexp.MustCompile(`^def\s+(@?[^\s(]+)`) - variableIDSuffix = regexp.MustCompile(`\s+id\s+("(?:[^"\\]|\\.)*")\s*;?\s*$`) - firstWord = regexp.MustCompile(`^[A-Za-z_]+`) -) - -func skipFractchString(src string, i int) int { - if strings.HasPrefix(src[i:], `"""`) { - end := strings.Index(src[i+3:], `"""`) - if end < 0 { - return len(src) - } - return i + 3 + end + 3 - } - quote := src[i] - j := i + 1 - for j < len(src) && src[j] != quote && src[j] != '\n' { - if src[j] == '\\' { - j++ - } - j++ - } - if j < len(src) && src[j] == quote { - j++ - } - if j > len(src) { - return len(src) - } - return j -} - -func skipFractchComment(src string, i int) int { - if strings.HasPrefix(src[i:], "//") { - end := strings.IndexByte(src[i:], '\n') - if end < 0 { - return len(src) - } - return i + end - } - end := strings.Index(src[i+2:], "*/") - if end < 0 { - return len(src) - } - return i + 2 + end + 2 -} - -func isFractchQuote(c byte) bool { - return c == '"' || c == '`' -} - -func splitFractchStatements(src string) []string { - statements := []string{} - pending := []string{} - i := 0 - for i < len(src) { - c := src[i] - if c == ' ' || c == '\t' || c == '\r' || c == '\n' { - i++ - continue - } - if strings.HasPrefix(src[i:], "//") || strings.HasPrefix(src[i:], "/*") { - end := skipFractchComment(src, i) - text := src[i:end] - if !(strings.HasPrefix(text, "/**") && strings.Contains(text, "* target:")) { - pending = append(pending, strings.TrimSpace(text)) - } - i = end - continue - } - start := i - depth := 0 - for i < len(src) { - c = src[i] - if isFractchQuote(c) { - i = skipFractchString(src, i) - continue - } - if strings.HasPrefix(src[i:], "//") || strings.HasPrefix(src[i:], "/*") { - i = skipFractchComment(src, i) - continue - } - i++ - if c == '{' { - depth++ - } else if c == '}' { - depth-- - if depth <= 0 { - break - } - } else if c == ';' && depth == 0 { - break - } - } - text := strings.TrimSpace(src[start:i]) - if len(pending) > 0 { - text = strings.Join(pending, "\n") + "\n" + text - pending = pending[:0] - } - statements = append(statements, text) - } - if len(pending) > 0 { - statements = append(statements, strings.Join(pending, "\n")) - } - return statements -} - -func leadingFractchComments(text string) (string, string) { - lines := strings.Split(text, "\n") - inBlock := false - for index, line := range lines { - trimmed := strings.TrimSpace(line) - if inBlock { - if strings.Contains(trimmed, "*/") { - inBlock = false - } - continue - } - if strings.HasPrefix(trimmed, "//") { - continue - } - if strings.HasPrefix(trimmed, "/*") { - inBlock = !strings.Contains(trimmed[2:], "*/") - continue - } - return strings.Join(lines[:index], "\n"), strings.Join(lines[index:], "\n") - } - return text, "" -} - -func scriptHeadEnd(text string) int { - for i := 0; i < len(text); { - c := text[i] - if isFractchQuote(c) { - i = skipFractchString(text, i) - continue - } - if c == '{' { - return i - } - i++ - } - return len(text) -} - -func fractchTokens(text string) []string { - tokens := []string{} - i := 0 - for i < len(text) { - c := text[i] - if c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == ';' { - i++ - continue - } - start := i - if isFractchQuote(c) { - i = skipFractchString(text, i) - } else { - for i < len(text) && !strings.ContainsRune(" \t\r\n;", rune(text[i])) { - if isFractchQuote(text[i]) { - i = skipFractchString(text, i) - continue - } - i++ - } - } - tokens = append(tokens, text[start:i]) - } - return tokens -} - -func unquoteFractch(token string) string { - if len(token) >= 2 && isFractchQuote(token[0]) && token[len(token)-1] == token[0] { - inner := token[1 : len(token)-1] - if token[0] == '"' { - inner = strings.ReplaceAll(inner, `\"`, `"`) - inner = strings.ReplaceAll(inner, `\\`, `\`) - } - return inner - } - return token -} - -var flagAttributes = map[string]bool{"hidden": true, "draggable": true, "current": true, "minimized": true, "continuous": true, "large": true, "slider": true} - -func fractchAttributes(tokens []string) map[string]string { - attributes := map[string]string{} - for i := 0; i < len(tokens); i++ { - word := tokens[i] - if flagAttributes[word] { - attributes[word] = "true" - continue - } - if i+1 < len(tokens) { - attributes[word] = tokens[i+1] - i++ - } - } - return attributes -} - -func parseFractchStatement(text string) fractchStatement { - comments, code := leadingFractchComments(text) - statement := fractchStatement{Text: text} - if code == "" { - statement.Kind = "note" - statement.Key = text - return statement - } - statement.Kind = firstWord.FindString(code) - switch statement.Kind { - case "when", "def", "script": - headEnd := scriptHeadEnd(code) - head := strings.TrimSpace(code[:headEnd]) - if match := positionSuffix.FindStringSubmatch(head); match != nil { - statement.At = match[1] + "," + match[2] - head = strings.TrimSpace(head[:len(head)-len(match[0])]) - } - statement.Head = head - statement.Key = head - if match := definitionName.FindStringSubmatch(head); match != nil { - statement.Key = "def " + match[1] - } - body := head + " " + strings.TrimSpace(code[headEnd:]) - if comments != "" { - body = comments + "\n" + body - } - statement.Body = body - statement.Norm = camelIdentifiers(stripHiddenValues(body)) - statement.Name = head - statement.Kind = "script" - case "var", "cloud": - tokens := fractchTokens(code) - if len(tokens) > 1 { - statement.Name = unquoteFractch(tokens[1]) - } - statement.Key = statement.Kind + " " + statement.Name - if match := variableIDSuffix.FindStringSubmatch(code); match != nil { - statement.Key = "id " + unquoteFractch(match[1]) - } - statement.Body = code - case "watch": - tokens := fractchTokens(code) - name := "" - if len(tokens) > 2 && (tokens[1] == "var" || tokens[1] == "list") { - name = tokens[1] + " " + unquoteFractch(tokens[2]) - } else if len(tokens) > 1 { - name = tokens[1] - } - statement.Name = name - statement.Key = "watch " + name - attributes := fractchAttributes(tokens) - if id, ok := attributes["id"]; ok { - statement.Key = "watch id " + unquoteFractch(id) - } - statement.Body = code - case "sprite", "stage", "costume", "backdrop", "sound", "use", "platform", "comment": - tokens := fractchTokens(code) - if len(tokens) > 1 && isFractchQuote(tokens[1][0]) { - statement.Name = unquoteFractch(tokens[1]) - } - statement.Key = statement.Kind + " " + statement.Name - if statement.Kind == "stage" { - statement.Key = "stage" - statement.Name = "Stage" - } - if statement.Kind == "backdrop" { - statement.Key = "costume " + statement.Name - } - statement.Body = code - default: - statement.Kind = "other" - statement.Key = code - statement.Name = firstLine(code) - statement.Body = code - } - return statement -} - -func firstLine(text string) string { - if index := strings.IndexByte(text, '\n'); index >= 0 { - return text[:index] - } - return text +type fractchSource struct { + Path string + Text string } -func parseFractchFile(src string) []fractchStatement { - raw := splitFractchStatements(src) - statements := make([]fractchStatement, 0, len(raw)) - for _, text := range raw { - statements = append(statements, parseFractchStatement(text)) - } - return statements +func noAssets(string) string { + return "" } func clipStateValue(value string) (string, bool) { @@ -499,6 +81,10 @@ func (changes *fractchFileChanges) addState(kind, name, field, before, after str }) } +func (changes *fractchFileChanges) addItem(change, kind, name, before, after string) { + changes.Items = append(changes.Items, fractchItemChange{Change: change, Kind: kind, Name: name, Before: before, After: after}) +} + func variableValue(code string) string { equals := -1 for i := 0; i < len(code); { @@ -522,17 +108,25 @@ func variableValue(code string) string { return strings.TrimSuffix(strings.TrimSpace(value), ";") } -func variableKind(statement fractchStatement) string { - if strings.HasPrefix(variableValue(statement.Body), "[") { - return "list" - } - if statement.Kind == "cloud" { - return "cloud" +func itemKind(statement fractchStatement) string { + if statement.Kind == "var" || statement.Kind == "cloud" { + if strings.HasPrefix(variableValue(statement.Body), "[") { + return "list" + } + if statement.Kind == "cloud" { + return "cloud" + } + return "variable" } - return "variable" + return statement.Kind } -var targetStateFields = map[string]bool{"at": true, "size": true, "direction": true, "hidden": true, "layer": true, "volume": true, "tempo": true, "video": true, "transparency": true, "tts": true} +var targetStateFields = map[string]string{ + "at": "position", "size": "size", "direction": "direction", "hidden": "visible", "layer": "layer", "costume": "costume", + "volume": "volume", "tempo": "tempo", "video": "video", "transparency": "transparency", "tts": "tts", +} + +var targetDefaults = map[string]string{"at": "0,0", "size": "100", "direction": "90", "volume": "100", "tempo": "60", "video": "on", "transparency": "50", "hidden": "true"} func headerAttributes(statement fractchStatement) map[string]string { tokens := fractchTokens(statement.Body) @@ -543,7 +137,19 @@ func headerAttributes(statement fractchStatement) map[string]string { if statement.Kind == "sprite" && len(tokens) > 0 && isFractchQuote(tokens[0][0]) { tokens = tokens[1:] } - return fractchAttributes(tokens) + attributes := fractchAttributes(tokens) + delete(attributes, "visible") + if _, hidden := attributes["hidden"]; hidden { + attributes["hidden"] = "false" + } + return attributes +} + +func attributeValue(attributes map[string]string, key string) string { + if value, ok := attributes[key]; ok { + return unquoteFractch(value) + } + return targetDefaults[key] } func sortedUnion(a, b map[string]string) []string { @@ -560,54 +166,29 @@ func sortedUnion(a, b map[string]string) []string { return keys } -var targetDefaults = map[string]string{"at": "0,0", "size": "100", "direction": "90", "volume": "100", "tempo": "60", "video": "on", "transparency": "50"} - -func attributeValue(attributes map[string]string, key string) string { - if value, ok := attributes[key]; ok { - return unquoteFractch(value) - } - return targetDefaults[key] -} - func (changes *fractchFileChanges) compareHeader(before, after fractchStatement) { if before.Name != after.Name { - changes.Items = append(changes.Items, fractchItemChange{Change: "renamed", Kind: after.Kind, Name: after.Name, Before: before.Name, After: after.Name}) + changes.addItem("renamed", after.Kind, after.Name, before.Name, after.Name) } beforeAttributes := headerAttributes(before) afterAttributes := headerAttributes(after) authored := false for _, key := range sortedUnion(beforeAttributes, afterAttributes) { - if beforeAttributes[key] == afterAttributes[key] { + beforeValue, afterValue := attributeValue(beforeAttributes, key), attributeValue(afterAttributes, key) + if beforeValue == afterValue { continue } - if targetStateFields[key] { - field := key - if key == "at" { - field = "position" - } else if key == "hidden" { - field = "visible" - } - beforeValue, afterValue := attributeValue(beforeAttributes, key), attributeValue(afterAttributes, key) - if key == "hidden" { - beforeValue, afterValue = visibleText(beforeAttributes[key]), visibleText(afterAttributes[key]) - } + if field, ok := targetStateFields[key]; ok { changes.addState(after.Kind, after.Name, field, beforeValue, afterValue) continue } authored = true } if authored { - changes.Items = append(changes.Items, fractchItemChange{Change: "changed", Kind: after.Kind, Name: after.Name, Before: before.Body, After: after.Body}) + changes.addItem("changed", after.Kind, after.Name, before.Body, after.Body) } } -func visibleText(hidden string) string { - if hidden == "true" { - return "false" - } - return "true" -} - func withoutAttributes(statement fractchStatement, names ...string) string { tokens := fractchTokens(statement.Body) kept := []string{} @@ -629,67 +210,34 @@ func withoutAttributes(statement fractchStatement, names ...string) string { return strings.Join(kept, " ") } -func (changes *fractchFileChanges) compareKeyed(before, after fractchStatement) { - if before.Body == after.Body { - if (after.Kind == "costume" || after.Kind == "backdrop" || after.Kind == "sound") && changes.assetEdited(before, after) { - changes.Items = append(changes.Items, fractchItemChange{Change: "edited", Kind: after.Kind, Name: after.Name, Before: before.Body, After: after.Body}) - } - return - } - switch after.Kind { - case "sprite", "stage": - changes.compareHeader(before, after) - case "var", "cloud": - if before.Name != after.Name { - changes.Items = append(changes.Items, fractchItemChange{Change: "renamed", Kind: variableKind(after), Name: after.Name, Before: before.Name, After: after.Name}) - } - if beforeValue, afterValue := variableValue(before.Body), variableValue(after.Body); beforeValue != afterValue { - changes.addState(variableKind(after), after.Name, "value", beforeValue, afterValue) - } - case "watch": - changes.addState("monitor", after.Name, "display", before.Body, after.Body) - case "costume", "backdrop", "sound": - beforeCurrent := headerFlag(before, "current") - afterCurrent := headerFlag(after, "current") - if beforeCurrent != afterCurrent { - changes.addState(after.Kind, after.Name, "current", beforeCurrent, afterCurrent) - } - if assetSignature(before) != assetSignature(after) { - changes.Items = append(changes.Items, fractchItemChange{Change: "changed", Kind: after.Kind, Name: after.Name, Before: before.Body, After: after.Body}) - } else if changes.assetEdited(before, after) { - changes.Items = append(changes.Items, fractchItemChange{Change: "edited", Kind: after.Kind, Name: after.Name, Before: before.Body, After: after.Body}) - } - case "comment": - if withoutAttributes(before, "at", "size", "minimized", "for") == withoutAttributes(after, "at", "size", "minimized", "for") { - changes.addState("comment", after.Name, "layout", before.Body, after.Body) - return +func headerFlag(statement fractchStatement, flag string) string { + for _, token := range fractchTokens(statement.Body) { + if token == flag { + return "true" } - changes.Items = append(changes.Items, fractchItemChange{Change: "changed", Kind: "comment", Name: after.Name, Before: before.Body, After: after.Body}) - default: - changes.Items = append(changes.Items, fractchItemChange{Change: "changed", Kind: after.Kind, Name: after.Name, Before: before.Body, After: after.Body}) } + return "false" } func assetFile(statement fractchStatement) string { return unquoteFractch(fractchAttributes(fractchTokens(statement.Body))["file"]) } -func (changes *fractchFileChanges) assetEdited(before, after fractchStatement) bool { - beforeFile, afterFile := assetFile(before), assetFile(after) - if beforeFile == "" || afterFile == "" { - return false - } - beforeOID, afterOID := changes.assets.before(beforeFile), changes.assets.after(afterFile) - return beforeOID != "" && afterOID != "" && beforeOID != afterOID +func isAssetKind(kind string) bool { + return kind == "costume" || kind == "backdrop" || kind == "sound" } -func (changes *fractchFileChanges) sameAsset(before, after fractchStatement) bool { +func (changes *fractchFileChanges) assetOIDs(before, after fractchStatement) (string, string) { beforeFile, afterFile := assetFile(before), assetFile(after) if beforeFile == "" || afterFile == "" { - return false + return "", "" } - beforeOID, afterOID := changes.assets.before(beforeFile), changes.assets.after(afterFile) - return beforeOID != "" && beforeOID == afterOID + return changes.assets.before(beforeFile), changes.assets.after(afterFile) +} + +func (changes *fractchFileChanges) assetEdited(before, after fractchStatement) bool { + beforeOID, afterOID := changes.assetOIDs(before, after) + return beforeOID != "" && afterOID != "" && beforeOID != afterOID } func roundedCenter(value string) string { @@ -726,52 +274,219 @@ func assetSignature(statement fractchStatement) string { return strings.Join(kept, " ") } -func headerFlag(statement fractchStatement, flag string) string { - for _, token := range fractchTokens(statement.Body) { - if token == flag { - return "true" +func (changes *fractchFileChanges) compareKeyed(before, after fractchStatement) { + if before.Body == after.Body { + if isAssetKind(after.Kind) && changes.assetEdited(before, after) { + changes.addItem("edited", after.Kind, after.Name, before.Body, after.Body) } + return } - return "false" -} - -func itemKind(statement fractchStatement) string { - if statement.Kind == "var" || statement.Kind == "cloud" { - return variableKind(statement) + switch after.Kind { + case "sprite", "stage": + changes.compareHeader(before, after) + case "var", "cloud": + if before.Name != after.Name { + changes.addItem("renamed", itemKind(after), after.Name, before.Name, after.Name) + } + if beforeValue, afterValue := variableValue(before.Body), variableValue(after.Body); beforeValue != afterValue { + changes.addState(itemKind(after), after.Name, "value", beforeValue, afterValue) + } + case "watch": + changes.addState("monitor", after.Name, "display", before.Body, after.Body) + case "costume", "backdrop", "sound": + if beforeCurrent, afterCurrent := headerFlag(before, "current"), headerFlag(after, "current"); beforeCurrent != afterCurrent { + changes.addState(after.Kind, after.Name, "current", beforeCurrent, afterCurrent) + } + if assetSignature(before) != assetSignature(after) { + changes.addItem("changed", after.Kind, after.Name, before.Body, after.Body) + } else if changes.assetEdited(before, after) { + changes.addItem("edited", after.Kind, after.Name, before.Body, after.Body) + } + case "comment": + layout := []string{"at", "size", "minimized", "for"} + if withoutAttributes(before, layout...) == withoutAttributes(after, layout...) { + changes.addState("comment", after.Name, "layout", before.Body, after.Body) + return + } + changes.addItem("changed", "comment", after.Name, before.Body, after.Body) + default: + changes.addItem("changed", itemKind(after), after.Name, before.Body, after.Body) } - return statement.Kind } -func (changes *fractchFileChanges) addItem(change string, statement fractchStatement) { - item := fractchItemChange{Change: change, Kind: itemKind(statement), Name: statement.Name} +func (changes *fractchFileChanges) addWhole(change string, statement fractchStatement) { + before, after := "", statement.Body if change == "removed" { - item.Before = statement.Body - } else { - item.After = statement.Body + before, after = statement.Body, "" } if statement.Kind == "watch" { field := "shown" if change == "removed" { field = "removed" } - changes.addState("monitor", statement.Name, field, item.Before, item.After) + changes.addState("monitor", statement.Name, field, before, after) return } - changes.Items = append(changes.Items, item) + changes.addItem(change, itemKind(statement), statement.Name, before, after) +} + +func withoutName(statement fractchStatement) string { + tokens := fractchTokens(statement.Body) + if len(tokens) > 1 && isFractchQuote(tokens[1][0]) { + tokens = append([]string{tokens[0]}, tokens[2:]...) + } + return strings.Join(tokens, " ") +} + +func renameKeys(changes *fractchFileChanges, statement fractchStatement, before bool) []string { + keys := []string{} + if statement.Kind == "sprite" { + return append(keys, "sprite "+withoutName(statement)) + } + if !isAssetKind(statement.Kind) { + return keys + } + file := assetFile(statement) + if file == "" { + return keys + } + keys = append(keys, statement.Kind+" file "+file) + lookup := changes.assets.after + if before { + lookup = changes.assets.before + } + if oid := lookup(file); oid != "" { + keys = append(keys, statement.Kind+" oid "+oid) + } + return keys } -func lineSimilarity(a, b string) float64 { - aLines := strings.Split(a, "\n") - bLines := strings.Split(b, "\n") +func (changes *fractchFileChanges) compareItems(before, after []fractchStatement) { + queues := map[string][]int{} + for i, statement := range before { + queues[statement.Key] = append(queues[statement.Key], i) + } + beforeUsed := make([]bool, len(before)) + unmatched := []fractchStatement{} + for _, statement := range after { + queue := queues[statement.Key] + if len(queue) == 0 { + unmatched = append(unmatched, statement) + continue + } + i := queue[0] + queues[statement.Key] = queue[1:] + beforeUsed[i] = true + changes.compareKeyed(before[i], statement) + } + renameIndex := map[string][]int{} + for i, statement := range before { + if beforeUsed[i] { + continue + } + for _, key := range renameKeys(changes, statement, true) { + renameIndex[key] = append(renameIndex[key], i) + } + } + emptyStage := fractchStatement{Kind: "stage", Key: "stage", Name: "Stage", Body: "stage"} + for _, statement := range unmatched { + if statement.Kind == "stage" { + changes.compareHeader(emptyStage, statement) + continue + } + renamedFrom := -1 + for _, key := range renameKeys(changes, statement, false) { + for len(renameIndex[key]) > 0 && beforeUsed[renameIndex[key][0]] { + renameIndex[key] = renameIndex[key][1:] + } + if len(renameIndex[key]) > 0 { + renamedFrom = renameIndex[key][0] + break + } + } + if renamedFrom < 0 { + changes.addWhole("added", statement) + continue + } + beforeUsed[renamedFrom] = true + old := before[renamedFrom] + if statement.Kind == "sprite" { + changes.compareHeader(old, statement) + continue + } + changes.addItem("renamed", statement.Kind, statement.Name, old.Name, statement.Name) + } + for i, statement := range before { + if beforeUsed[i] { + continue + } + if statement.Kind == "stage" { + changes.compareHeader(statement, emptyStage) + continue + } + changes.addWhole("removed", statement) + } + changes.compareOrder(before, after, "costume", "costume") + changes.compareOrder(before, after, "sound", "sound") +} + +func (changes *fractchFileChanges) compareOrder(before, after []fractchStatement, kind, label string) { + matches := func(statement fractchStatement) bool { + return statement.Kind == kind || (kind == "costume" && statement.Kind == "backdrop") + } + inAfter := map[string]bool{} + for _, statement := range after { + if matches(statement) { + inAfter[statement.Key] = true + } + } + inBoth := map[string]bool{} + beforeOrder := []string{} + for _, statement := range before { + if matches(statement) && inAfter[statement.Key] && !inBoth[statement.Key] { + inBoth[statement.Key] = true + beforeOrder = append(beforeOrder, statement.Name) + } + } + afterOrder := []string{} + seen := map[string]bool{} + for _, statement := range after { + if matches(statement) && inBoth[statement.Key] && !seen[statement.Key] { + seen[statement.Key] = true + afterOrder = append(afterOrder, statement.Name) + } + } + if strings.Join(beforeOrder, "\x00") != strings.Join(afterOrder, "\x00") { + changes.addItem("reordered", label, "", strings.Join(beforeOrder, "\n"), strings.Join(afterOrder, "\n")) + } +} + +func statementLines(statement *fractchStatement) []string { + if statement.lines == nil { + raw := strings.Split(statement.Norm, "\n") + statement.lines = make([]string, len(raw)) + for index, line := range raw { + statement.lines[index] = strings.TrimSpace(line) + } + } + return statement.lines +} + +func (changes *fractchFileChanges) similarity(a, b *fractchStatement) (float64, bool) { + aLines, bLines := statementLines(a), statementLines(b) + cost := len(aLines) + len(bLines) + if changes.work+cost > maxSimilarityWork { + return 0, false + } + changes.work += cost counts := make(map[string]int, len(aLines)) for _, line := range aLines { - counts[strings.TrimSpace(line)]++ + counts[line]++ } common := 0 for _, line := range bLines { - key := strings.TrimSpace(line) - if counts[key] > 0 { - counts[key]-- + if counts[line] > 0 { + counts[line]-- common++ } } @@ -780,9 +495,9 @@ func lineSimilarity(a, b string) float64 { longest = len(bLines) } if longest == 0 { - return 1 + return 1, true } - return float64(common) / float64(longest) + return float64(common) / float64(longest), true } type scriptPair struct { @@ -791,24 +506,50 @@ type scriptPair struct { score float64 } -func pairBySimilarity(before, after []fractchStatement, beforeUsed, afterUsed []bool, candidates func(i, j int) bool, minimum float64) []scriptPair { - pairs := []scriptPair{} - for i := range before { - if beforeUsed[i] { - continue +func (changes *fractchFileChanges) pairGroup(before, after []fractchStatement, beforeIDs, afterIDs []int, beforeUsed, afterUsed []bool, minimum float64, byOrder bool) []scriptPair { + open := func(ids []int, used []bool) []int { + kept := []int{} + for _, id := range ids { + if !used[id] { + kept = append(kept, id) + } } - for j := range after { - if afterUsed[j] || !candidates(i, j) { - continue + return kept + } + beforeIDs, afterIDs = open(beforeIDs, beforeUsed), open(afterIDs, afterUsed) + chosen := []scriptPair{} + if len(beforeIDs) == 0 || len(afterIDs) == 0 { + return chosen + } + pairs := []scriptPair{} + tooMany := len(beforeIDs)*len(afterIDs) > maxPairsPerGroup + if !tooMany { + for _, i := range beforeIDs { + for _, j := range afterIDs { + score, ok := changes.similarity(&before[i], &after[j]) + if !ok { + tooMany = true + break + } + if score >= minimum { + pairs = append(pairs, scriptPair{before: i, after: j, score: score}) + } } - score := lineSimilarity(before[i].Norm, after[j].Norm) - if score >= minimum { - pairs = append(pairs, scriptPair{before: i, after: j, score: score}) + if tooMany { + break } } } + if tooMany { + if !byOrder { + return chosen + } + pairs = pairs[:0] + for index := 0; index < len(beforeIDs) && index < len(afterIDs); index++ { + pairs = append(pairs, scriptPair{before: beforeIDs[index], after: afterIDs[index]}) + } + } sort.SliceStable(pairs, func(a, b int) bool { return pairs[a].score > pairs[b].score }) - chosen := []scriptPair{} for _, pair := range pairs { if beforeUsed[pair.before] || afterUsed[pair.after] { continue @@ -820,6 +561,10 @@ func pairBySimilarity(before, after []fractchStatement, beforeUsed, afterUsed [] return chosen } +func scriptKind(statement fractchStatement) string { + return firstWord.FindString(statement.Head) +} + func (changes *fractchFileChanges) compareScripts(before, after []fractchStatement) { beforeUsed := make([]bool, len(before)) afterUsed := make([]bool, len(after)) @@ -840,148 +585,106 @@ func (changes *fractchFileChanges) compareScripts(before, after []fractchStateme byBody[statement.Norm] = queue[1:] beforeUsed[i] = true afterUsed[j] = true - if before[i].At != statement.At { + if before[i].File != statement.File { + changes.Scripts = append(changes.Scripts, fractchScriptChange{ + Change: "moved", Kind: scriptKind(statement), Head: statement.Head, File: statement.File, BeforeFile: before[i].File, + BeforeAt: before[i].At, AfterAt: statement.At, + }) + } else if before[i].At != statement.At { changes.addState("script", statement.Head, "position", before[i].At, statement.At) } } - sameKey := func(i, j int) bool { return before[i].Key == after[j].Key } - for _, pair := range pairBySimilarity(before, after, beforeUsed, afterUsed, sameKey, 0) { - partner[pair.after] = pair.before + beforeByKey := map[string][]int{} + afterByKey := map[string][]int{} + keys := []string{} + for i, statement := range before { + if !beforeUsed[i] { + beforeByKey[statement.Key] = append(beforeByKey[statement.Key], i) + } + } + for j, statement := range after { + if afterUsed[j] { + continue + } + if _, ok := afterByKey[statement.Key]; !ok { + keys = append(keys, statement.Key) + } + afterByKey[statement.Key] = append(afterByKey[statement.Key], j) + } + for _, key := range keys { + for _, pair := range changes.pairGroup(before, after, beforeByKey[key], afterByKey[key], beforeUsed, afterUsed, 0, true) { + partner[pair.after] = pair.before + } } - remainingBefore, remainingAfter := 0, 0 + remainingBefore, remainingAfter := []int{}, []int{} for i := range before { if !beforeUsed[i] { - remainingBefore++ + remainingBefore = append(remainingBefore, i) } } for j := range after { if !afterUsed[j] { - remainingAfter++ + remainingAfter = append(remainingAfter, j) } } - if remainingBefore*remainingAfter <= maxCrossPairProducts { - anyKey := func(i, j int) bool { return true } - for _, pair := range pairBySimilarity(before, after, beforeUsed, afterUsed, anyKey, crossPairSimilarity) { - partner[pair.after] = pair.before - } + for _, pair := range changes.pairGroup(before, after, remainingBefore, remainingAfter, beforeUsed, afterUsed, crossPairSimilarity, false) { + partner[pair.after] = pair.before } for j, statement := range after { if i := partner[j]; i >= 0 { - changes.Scripts = append(changes.Scripts, fractchScriptChange{ - Change: "changed", Kind: scriptKind(statement), Head: statement.Head, + change := fractchScriptChange{ + Change: "changed", Kind: scriptKind(statement), Head: statement.Head, File: statement.File, Before: before[i].Body, After: statement.Body, BeforeAt: before[i].At, AfterAt: statement.At, - }) + } + if before[i].File != statement.File { + change.BeforeFile = before[i].File + } + changes.Scripts = append(changes.Scripts, change) continue } if !afterUsed[j] { changes.Scripts = append(changes.Scripts, fractchScriptChange{ - Change: "added", Kind: scriptKind(statement), Head: statement.Head, After: statement.Body, AfterAt: statement.At, + Change: "added", Kind: scriptKind(statement), Head: statement.Head, File: statement.File, After: statement.Body, AfterAt: statement.At, }) } } for i, statement := range before { if !beforeUsed[i] { changes.Scripts = append(changes.Scripts, fractchScriptChange{ - Change: "removed", Kind: scriptKind(statement), Head: statement.Head, Before: statement.Body, BeforeAt: statement.At, + Change: "removed", Kind: scriptKind(statement), Head: statement.Head, File: statement.File, Before: statement.Body, BeforeAt: statement.At, }) } } } -func scriptKind(statement fractchStatement) string { - return firstWord.FindString(statement.Head) -} - -func (changes *fractchFileChanges) compareItems(before, after []fractchStatement) { - queues := map[string][]int{} - for i, statement := range before { - queues[statement.Key] = append(queues[statement.Key], i) - } - beforeUsed := make([]bool, len(before)) - unmatched := []fractchStatement{} - for _, statement := range after { - queue := queues[statement.Key] - if len(queue) == 0 { - unmatched = append(unmatched, statement) - continue - } - i := queue[0] - queues[statement.Key] = queue[1:] - beforeUsed[i] = true - changes.compareKeyed(before[i], statement) - } - removed := []fractchStatement{} - for i, statement := range before { - if !beforeUsed[i] { - removed = append(removed, statement) - } - } - emptyStage := fractchStatement{Kind: "stage", Key: "stage", Name: "Stage", Body: "stage"} - for _, statement := range unmatched { - if statement.Kind == "stage" { - changes.compareHeader(emptyStage, statement) - continue - } - renamedFrom := -1 - if statement.Kind == "costume" || statement.Kind == "backdrop" || statement.Kind == "sound" || statement.Kind == "sprite" { - for index, old := range removed { - if old.Kind == statement.Kind && (changes.sameAsset(old, statement) || (assetFile(old) != "" && assetFile(old) == assetFile(statement)) || (statement.Kind == "sprite" && withoutName(old) == withoutName(statement))) { - renamedFrom = index - break - } - } - } - if renamedFrom >= 0 { - old := removed[renamedFrom] - removed = append(removed[:renamedFrom], removed[renamedFrom+1:]...) - if statement.Kind == "sprite" { - changes.compareHeader(old, statement) +func partitionStatements(sources []fractchSource) ([]fractchStatement, []fractchStatement) { + scripts := []fractchStatement{} + items := []fractchStatement{} + for _, source := range sources { + for _, statement := range parseFractchFile(source.Text, source.Path) { + if statement.Kind == "script" { + scripts = append(scripts, statement) } else { - changes.Items = append(changes.Items, fractchItemChange{Change: "renamed", Kind: statement.Kind, Name: statement.Name, Before: old.Name, After: statement.Name}) + items = append(items, statement) } - continue } - changes.addItem("added", statement) } - for _, statement := range removed { - if statement.Kind == "stage" { - changes.compareHeader(statement, emptyStage) - continue - } - changes.addItem("removed", statement) - } -} - -func withoutName(statement fractchStatement) string { - tokens := fractchTokens(statement.Body) - if len(tokens) > 1 && isFractchQuote(tokens[1][0]) { - tokens = append([]string{tokens[0]}, tokens[2:]...) - } - return strings.Join(tokens, " ") -} - -func compareFractch(before, after string) fractchFileChanges { - return compareFractchAssets(before, after, assetLookup{before: noAssets, after: noAssets}) + return scripts, items } -func compareFractchAssets(before, after string, assets assetLookup) fractchFileChanges { +func compareFractchSources(before, after []fractchSource, assets assetLookup) fractchFileChanges { changes := fractchFileChanges{Scripts: []fractchScriptChange{}, Items: []fractchItemChange{}, State: []fractchStateChange{}, assets: assets} - beforeScripts, beforeItems := partitionStatements(parseFractchFile(before)) - afterScripts, afterItems := partitionStatements(parseFractchFile(after)) + beforeScripts, beforeItems := partitionStatements(before) + afterScripts, afterItems := partitionStatements(after) changes.compareItems(beforeItems, afterItems) changes.compareScripts(beforeScripts, afterScripts) return changes } -func partitionStatements(statements []fractchStatement) ([]fractchStatement, []fractchStatement) { - scripts := []fractchStatement{} - items := []fractchStatement{} - for _, statement := range statements { - if statement.Kind == "script" { - scripts = append(scripts, statement) - } else { - items = append(items, statement) - } - } - return scripts, items +func compareFractchAssets(before, after string, assets assetLookup) fractchFileChanges { + return compareFractchSources([]fractchSource{{Path: "main.fractch", Text: before}}, []fractchSource{{Path: "main.fractch", Text: after}}, assets) +} + +func compareFractch(before, after string) fractchFileChanges { + return compareFractchAssets(before, after, assetLookup{before: noAssets, after: noAssets}) } diff --git a/native/gitinspection/fractch_limits_test.go b/native/gitinspection/fractch_limits_test.go new file mode 100644 index 0000000..c180d07 --- /dev/null +++ b/native/gitinspection/fractch_limits_test.go @@ -0,0 +1,150 @@ +package gitinspection + +import ( + "fmt" + "strings" + "testing" + "time" +) + +func requireFast(t *testing.T, limit time.Duration, run func()) { + t.Helper() + start := time.Now() + run() + if elapsed := time.Since(start); elapsed > limit { + t.Fatalf("took %s, limit %s", elapsed, limit) + } +} + +func TestCompareFractchManyUnmatchedCostumesIsFast(t *testing.T) { + var before, after strings.Builder + before.WriteString("sprite \"A\";\n") + after.WriteString("sprite \"A\";\n") + for i := 0; i < 20000; i++ { + fmt.Fprintf(&before, "costume \"a%d\" file \"assets/a%d.svg\";\n", i, i) + fmt.Fprintf(&after, "costume \"b%d\" file \"assets/b%d.svg\";\n", i, i) + } + requireFast(t, 3*time.Second, func() { + changes := compareFractch(before.String(), after.String()) + if len(changes.Items) != 40000 { + t.Errorf("expected 40000 added and removed costumes, got %d", len(changes.Items)) + } + }) +} + +func TestCompareFractchManySameHatScriptsIsFast(t *testing.T) { + var before, after strings.Builder + for i := 0; i < 30000; i++ { + fmt.Fprintf(&before, "when flag {\n say %d;\n}\n", i) + fmt.Fprintf(&after, "when flag {\n think %d;\n}\n", i) + } + requireFast(t, 5*time.Second, func() { + changes := compareFractch(before.String(), after.String()) + if len(changes.Scripts) != 30000 { + t.Errorf("expected 30000 script changes, got %d", len(changes.Scripts)) + } + }) +} + +func TestCompareFractchDeepNestingIsFast(t *testing.T) { + deep := "when flag {\n say " + strings.Repeat("(", 500000) + "x ?? 1" + strings.Repeat(")", 500000) + ";\n}\n" + requireFast(t, 3*time.Second, func() { + compareFractch(deep, deep+"when flag {\n}\n") + }) +} + +func TestSplitFractchStatementsWithoutSemicolons(t *testing.T) { + src := "var a = 1 id \"a\"\nvar b = json([1,\n2]) id \"b\"\ncostume \"c\" file \"assets/c.svg\"\nwhen flag {\n say 1\n say 2\n}\n" + statements := splitFractchStatements(src) + if len(statements) != 4 { + t.Fatalf("expected four statements, got %d: %q", len(statements), statements) + } + before := src + after := strings.Replace(src, "var b = json([1,\n2])", "var b = json([1,\n3])", 1) + changes := compareFractch(before, after) + if len(changes.State) != 1 || changes.State[0].Name != "b" { + t.Fatalf("second variable's value change was not isolated: %+v", changes) + } +} + +func TestCompareFractchReportsCostumeOrder(t *testing.T) { + before := "sprite \"A\";\ncostume \"one\" file \"assets/one.svg\";\ncostume \"two\" file \"assets/two.svg\";\nsound \"pop\" file \"assets/pop.wav\";\n" + after := "sprite \"A\";\ncostume \"two\" file \"assets/two.svg\";\ncostume \"one\" file \"assets/one.svg\";\nsound \"pop\" file \"assets/pop.wav\";\n" + changes := compareFractch(before, after) + if len(changes.Items) != 1 || changes.Items[0].Change != "reordered" || changes.Items[0].Kind != "costume" || changes.Items[0].After != "two\none" { + t.Fatalf("costume reorder not reported: %+v", changes.Items) + } + if again := compareFractch(before, before); len(again.Items) != 0 { + t.Fatalf("unchanged order reported: %+v", again.Items) + } +} + +func TestCompareFractchSourcesReportsScriptMovedBetweenRegions(t *testing.T) { + script := "when flag at 0,0 {\n say 1;\n}\n" + other := "when key \"a\" pressed {\n say 2;\n}\n" + before := []fractchSource{{Path: "Cat/main.fractch", Text: "sprite \"Cat\";\n" + script + other}} + after := []fractchSource{{Path: "Cat/Enemies.fractch", Text: script}, {Path: "Cat/main.fractch", Text: "sprite \"Cat\";\n" + other}} + changes := compareFractchSources(before, after, assetLookup{before: noAssets, after: noAssets}) + if len(changes.Scripts) != 1 || changes.Scripts[0].Change != "moved" || changes.Scripts[0].File != "Cat/Enemies.fractch" || changes.Scripts[0].BeforeFile != "Cat/main.fractch" { + t.Fatalf("region move not reported: %+v", changes.Scripts) + } +} + +func TestSplitFractchStatementsKeepsFreeCommentsApart(t *testing.T) { + src := "// a note on its own\n\nwhen flag {\n say 1;\n}\n// attached\nwhen key \"a\" pressed {\n}\n" + statements := splitFractchStatements(src) + if len(statements) != 3 || statements[0] != "// a note on its own" || !strings.HasPrefix(statements[2], "// attached\nwhen key") { + t.Fatalf("statements %q", statements) + } + edited := strings.Replace(src, "a note on its own", "a better note", 1) + changes := compareFractch(src, edited) + if len(changes.Scripts) != 0 || len(changes.Items) != 2 { + t.Fatalf("editing a free comment changed a script: %+v", changes) + } +} + +func TestInspectChangesReportsRedrawnCostumeWithoutSpriteChange(t *testing.T) { + repo := newTestRepository() + source := repo.object("blob", []byte(fractchBase)) + first := repo.commit(repo.nestedTree(map[string]string{"Cat/main.fractch": source, "Cat/assets/Idle.svg": repo.object("blob", []byte(""))}), "", "Initial version") + second := repo.commit(repo.nestedTree(map[string]string{"Cat/main.fractch": source, "Cat/assets/Idle.svg": repo.object("blob", []byte(""))}), first, "Redraw") + result := decodeResult(t, Inspect(repo.write(t), second, "changes", "", second)) + targets := result["targets"].([]any) + if len(targets) != 1 { + t.Fatalf("targets %v", targets) + } + items := targets[0].(map[string]any)["items"].([]any) + if len(items) != 1 || items[0].(map[string]any)["change"] != "edited" || items[0].(map[string]any)["name"] != "Idle" { + t.Fatalf("redrawn costume not named: %v", items) + } + if result["totals"].(map[string]any)["stateOnly"] != false { + t.Fatalf("redraw counted as state only: %v", result["totals"]) + } +} + +func TestInspectChangesMarksTargetsPastTheBudget(t *testing.T) { + repo := newTestRepository() + large := "sprite \"A\";\nvar x = \"" + strings.Repeat("a", maxInlineDiffFileBytes+10) + "\" id \"x\";\n" + first := repo.commit(repo.nestedTree(map[string]string{"A/main.fractch": repo.object("blob", []byte("sprite \"A\";\n"))}), "", "Initial version") + second := repo.commit(repo.nestedTree(map[string]string{"A/main.fractch": repo.object("blob", []byte(large))}), first, "Grow") + result := decodeResult(t, Inspect(repo.write(t), second, "changes", "", second)) + target := result["targets"].([]any)[0].(map[string]any) + if target["tooLarge"] != true || result["totals"].(map[string]any)["stateOnly"] != false { + t.Fatalf("oversized sprite not marked: %v %v", target, result["totals"]) + } +} + +func FuzzCompareFractch(f *testing.F) { + f.Add(fractchBase, strings.Replace(fractchBase, "turn 15", "turn 30", 1)) + f.Add("when flag { say (a ?? (b ?? \"x\")); }", "when flag { say \"\"\"raw } ;\"\"\" }") + f.Add("var a = 1\nvar b = [\nwhen", "/* open comment\n` tick") + f.Add("((((((((((?? ,;", "))))))) ?? }}}{{{") + f.Fuzz(func(t *testing.T, before, after string) { + if len(before) > 4096 || len(after) > 4096 { + return + } + compareFractch(before, after) + stripHiddenValues(before) + camelIdentifiers(after) + }) +} diff --git a/native/gitinspection/fractch_parse.go b/native/gitinspection/fractch_parse.go new file mode 100644 index 0000000..f14321e --- /dev/null +++ b/native/gitinspection/fractch_parse.go @@ -0,0 +1,539 @@ +package gitinspection + +import ( + "regexp" + "strings" +) + +const maxHiddenValueDepth = 64 + +type fractchStatement struct { + Kind string + Key string + Name string + File string + Head string + At string + Body string + Norm string + lines []string +} + +var ( + positionSuffix = regexp.MustCompile(`\s+at\s+(-?[0-9.eE+-]+)\s*,\s*(-?[0-9.eE+-]+)\s*$`) + definitionName = regexp.MustCompile(`^def\s+(@?[^\s(]+)`) + variableIDSuffix = regexp.MustCompile(`\s+id\s+("(?:[^"\\]|\\.)*")\s*;?\s*$`) + firstWord = regexp.MustCompile(`^[A-Za-z_]+`) +) + +var topLevelKeywords = map[string]bool{ + "sprite": true, "stage": true, "var": true, "cloud": true, "use": true, "platform": true, "watch": true, + "comment": true, "costume": true, "backdrop": true, "sound": true, "when": true, "def": true, "script": true, +} + +func isFractchQuote(c byte) bool { + return c == '"' || c == '`' +} + +func isIdentifierByte(c byte) bool { + return c == '_' || c == ')' || c == ']' || (c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') +} + +func isCommentStart(src string, i int) bool { + return strings.HasPrefix(src[i:], "//") || strings.HasPrefix(src[i:], "/*") +} + +func skipFractchString(src string, i int) int { + if strings.HasPrefix(src[i:], `"""`) { + end := strings.Index(src[i+3:], `"""`) + if end < 0 { + return len(src) + } + return i + 3 + end + 3 + } + quote := src[i] + j := i + 1 + for j < len(src) && src[j] != quote && src[j] != '\n' { + if src[j] == '\\' { + j++ + } + j++ + } + if j < len(src) && src[j] == quote { + j++ + } + if j > len(src) { + return len(src) + } + return j +} + +func skipFractchComment(src string, i int) int { + if strings.HasPrefix(src[i:], "//") { + end := strings.IndexByte(src[i:], '\n') + if end < 0 { + return len(src) + } + return i + end + } + end := strings.Index(src[i+2:], "*/") + if end < 0 { + return len(src) + } + return i + 2 + end + 2 +} + +func skipSpace(src string, i int) int { + for i < len(src) && (src[i] == ' ' || src[i] == '\t' || src[i] == '\r' || src[i] == '\n') { + i++ + } + return i +} + +func startsTopLevelStatement(src string, i int) bool { + word := firstWord.FindString(src[skipSpace(src, i):]) + return topLevelKeywords[word] +} + +func readFractchStatement(src string, i int) int { + braces, brackets := 0, 0 + for i < len(src) { + c := src[i] + if isFractchQuote(c) { + i = skipFractchString(src, i) + continue + } + if isCommentStart(src, i) { + i = skipFractchComment(src, i) + continue + } + i++ + switch c { + case '{': + braces++ + case '}': + braces-- + if braces <= 0 { + return i + } + case '(', '[': + brackets++ + case ')', ']': + if brackets > 0 { + brackets-- + } + case ';': + if braces == 0 { + return i + } + case '\n': + if braces == 0 && brackets == 0 && startsTopLevelStatement(src, i) { + return i + } + } + } + return i +} + +func splitFractchStatements(src string) []string { + statements := []string{} + pending := []string{} + flush := func() { + if len(pending) > 0 { + statements = append(statements, strings.Join(pending, "\n")) + pending = pending[:0] + } + } + i := 0 + for i < len(src) { + next := skipSpace(src, i) + if len(pending) > 0 && strings.Count(src[i:next], "\n") > 1 { + flush() + } + i = next + if i >= len(src) { + break + } + if isCommentStart(src, i) { + end := skipFractchComment(src, i) + text := src[i:end] + if !(strings.HasPrefix(text, "/**") && strings.Contains(text, "* target:")) { + pending = append(pending, strings.TrimSpace(text)) + } + i = end + continue + } + end := readFractchStatement(src, i) + text := strings.TrimSpace(src[i:end]) + if len(pending) > 0 { + text = strings.Join(pending, "\n") + "\n" + text + pending = pending[:0] + } + statements = append(statements, text) + i = end + } + flush() + return statements +} + +func leadingFractchComments(text string) (string, string) { + lines := strings.Split(text, "\n") + inBlock := false + for index, line := range lines { + trimmed := strings.TrimSpace(line) + if inBlock { + if strings.Contains(trimmed, "*/") { + inBlock = false + } + continue + } + if strings.HasPrefix(trimmed, "//") { + continue + } + if strings.HasPrefix(trimmed, "/*") { + inBlock = !strings.Contains(trimmed[2:], "*/") + continue + } + return strings.Join(lines[:index], "\n"), strings.Join(lines[index:], "\n") + } + return text, "" +} + +func scriptHeadEnd(text string) int { + for i := 0; i < len(text); { + c := text[i] + if isFractchQuote(c) { + i = skipFractchString(text, i) + continue + } + if c == '{' { + return i + } + i++ + } + return len(text) +} + +func fractchTokens(text string) []string { + tokens := []string{} + i := 0 + for i < len(text) { + c := text[i] + if c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == ';' { + i++ + continue + } + start := i + if isFractchQuote(c) { + i = skipFractchString(text, i) + } else { + for i < len(text) && !strings.ContainsRune(" \t\r\n;", rune(text[i])) { + if isFractchQuote(text[i]) { + i = skipFractchString(text, i) + continue + } + i++ + } + } + tokens = append(tokens, text[start:i]) + } + return tokens +} + +func unquoteFractch(token string) string { + if len(token) >= 2 && isFractchQuote(token[0]) && token[len(token)-1] == token[0] { + inner := token[1 : len(token)-1] + if token[0] == '"' { + inner = strings.ReplaceAll(inner, `\"`, `"`) + inner = strings.ReplaceAll(inner, `\\`, `\`) + } + return inner + } + return token +} + +var flagAttributes = map[string]bool{"hidden": true, "visible": true, "draggable": true, "current": true, "minimized": true, "continuous": true, "large": true, "slider": true} + +func fractchAttributes(tokens []string) map[string]string { + attributes := map[string]string{} + for i := 0; i < len(tokens); i++ { + word := tokens[i] + if flagAttributes[word] { + attributes[word] = "true" + continue + } + if i+1 < len(tokens) { + attributes[word] = tokens[i+1] + i++ + } + } + return attributes +} + +func maxParenDepth(src string) int { + depth, deepest := 0, 0 + for i := 0; i < len(src); { + c := src[i] + if isFractchQuote(c) { + i = skipFractchString(src, i) + continue + } + if isCommentStart(src, i) { + i = skipFractchComment(src, i) + continue + } + if c == '(' { + depth++ + if depth > deepest { + deepest = depth + } + } else if c == ')' && depth > 0 { + depth-- + } + i++ + } + return deepest +} + +func stripHiddenValues(src string) string { + if !strings.Contains(src, "??") || maxParenDepth(src) > maxHiddenValueDepth { + return src + } + out, _, _ := stripHiddenFrom(src, 0, false) + return out +} + +func skipHiddenValue(src string, i int) int { + depth := 0 + for i < len(src) { + c := src[i] + if isFractchQuote(c) { + i = skipFractchString(src, i) + continue + } + switch c { + case '(', '[': + depth++ + case ')', ']': + if depth == 0 { + return i + } + depth-- + case ',', ';', '{', '}', '\n': + if depth == 0 { + return i + } + } + i++ + } + return i +} + +func stripHiddenFrom(src string, i int, inParen bool) (string, int, bool) { + var builder strings.Builder + hidden, comma := false, false + for i < len(src) { + c := src[i] + if isFractchQuote(c) { + end := skipFractchString(src, i) + builder.WriteString(src[i:end]) + i = end + continue + } + if isCommentStart(src, i) { + end := skipFractchComment(src, i) + builder.WriteString(src[i:end]) + i = end + continue + } + if c == '(' { + inner, next, stripped := stripHiddenFrom(src, i+1, true) + before := byte(' ') + if i > 0 { + before = src[i-1] + } + after := nextNonSpace(src, next) + standalone := !isIdentifierByte(before) && (after == 0 || strings.IndexByte(";,){]\n", after) >= 0) + if stripped && standalone { + builder.WriteString(inner) + } else { + builder.WriteString("(" + inner + ")") + } + i = next + continue + } + if c == ')' && inParen { + return builder.String(), i + 1, hidden && !comma + } + if c == ',' { + comma = true + } + if strings.HasPrefix(src[i:], "??") { + kept := strings.TrimRight(builder.String(), " \t") + builder.Reset() + builder.WriteString(kept) + hidden = true + i = skipHiddenValue(src, i+2) + continue + } + builder.WriteByte(c) + i++ + } + return builder.String(), i, false +} + +func nextNonSpace(src string, i int) byte { + for i < len(src) { + if src[i] != ' ' && src[i] != '\t' && src[i] != '\r' { + return src[i] + } + i++ + } + return 0 +} + +func camelIdentifiers(src string) string { + var builder strings.Builder + for i := 0; i < len(src); { + c := src[i] + if isFractchQuote(c) { + end := skipFractchString(src, i) + builder.WriteString(src[i:end]) + i = end + continue + } + if isCommentStart(src, i) { + end := skipFractchComment(src, i) + builder.WriteString(src[i:end]) + i = end + continue + } + if !(c == '_' || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) || (i > 0 && isIdentifierByte(src[i-1])) { + builder.WriteByte(c) + i++ + continue + } + end := i + for end < len(src) && isIdentifierByte(src[end]) && src[end] != ')' && src[end] != ']' { + end++ + } + word := src[i:end] + if c >= 'a' && c <= 'z' && strings.Contains(word, "_") { + upper := false + for index := 0; index < len(word); index++ { + letter := word[index] + if letter == '_' { + upper = true + continue + } + if upper && letter >= 'a' && letter <= 'z' { + letter -= 'a' - 'A' + } + upper = false + builder.WriteByte(letter) + } + } else { + builder.WriteString(word) + } + i = end + } + return builder.String() +} + +func parseFractchStatement(text, file string) fractchStatement { + comments, code := leadingFractchComments(text) + statement := fractchStatement{File: file, Body: text} + if code == "" { + statement.Kind = "comment" + statement.Key = "note " + text + statement.Name = firstLine(text) + return statement + } + statement.Kind = firstWord.FindString(code) + switch statement.Kind { + case "when", "def", "script": + headEnd := scriptHeadEnd(code) + head := strings.TrimSpace(code[:headEnd]) + if match := positionSuffix.FindStringSubmatch(head); match != nil { + statement.At = match[1] + "," + match[2] + head = strings.TrimSpace(head[:len(head)-len(match[0])]) + } + statement.Head = head + statement.Key = head + if match := definitionName.FindStringSubmatch(head); match != nil { + statement.Key = "def " + match[1] + } + body := head + " " + strings.TrimSpace(code[headEnd:]) + if comments != "" { + body = comments + "\n" + body + } + statement.Body = body + statement.Norm = camelIdentifiers(stripHiddenValues(body)) + statement.Name = head + statement.Kind = "script" + case "var", "cloud": + tokens := fractchTokens(code) + if len(tokens) > 1 { + statement.Name = unquoteFractch(tokens[1]) + } + statement.Key = statement.Kind + " " + statement.Name + if match := variableIDSuffix.FindStringSubmatch(code); match != nil { + statement.Key = "id " + unquoteFractch(match[1]) + } + statement.Body = code + case "watch": + tokens := fractchTokens(code) + name := "" + if len(tokens) > 2 && (tokens[1] == "var" || tokens[1] == "list") { + name = tokens[1] + " " + unquoteFractch(tokens[2]) + } else if len(tokens) > 1 { + name = tokens[1] + } + statement.Name = name + statement.Key = "watch " + name + if id, ok := fractchAttributes(tokens)["id"]; ok { + statement.Key = "watch id " + unquoteFractch(id) + } + statement.Body = code + case "sprite", "stage", "costume", "backdrop", "sound", "use", "platform", "comment": + tokens := fractchTokens(code) + if len(tokens) > 1 && isFractchQuote(tokens[1][0]) { + statement.Name = unquoteFractch(tokens[1]) + } + statement.Key = statement.Kind + " " + statement.Name + switch statement.Kind { + case "stage": + statement.Key = "stage" + statement.Name = "Stage" + case "backdrop": + statement.Key = "costume " + statement.Name + case "comment": + if statement.Name == "" { + statement.Key = "comment " + code + } + } + statement.Body = code + default: + statement.Kind = "other" + statement.Key = code + statement.Name = firstLine(code) + statement.Body = code + } + return statement +} + +func firstLine(text string) string { + if index := strings.IndexByte(text, '\n'); index >= 0 { + return text[:index] + } + return text +} + +func parseFractchFile(src, file string) []fractchStatement { + raw := splitFractchStatements(src) + statements := make([]fractchStatement, 0, len(raw)) + for _, text := range raw { + statements = append(statements, parseFractchStatement(text, file)) + } + return statements +} diff --git a/native/gitinspection/fractch_test.go b/native/gitinspection/fractch_test.go index b24eda7..37a1e7d 100644 --- a/native/gitinspection/fractch_test.go +++ b/native/gitinspection/fractch_test.go @@ -272,14 +272,14 @@ func TestInspectChangesSummarisesCommit(t *testing.T) { totals := result["totals"].(map[string]any) scripts := totals["scripts"].(map[string]any) assets := totals["assets"].(map[string]any) - if scripts["changed"] != float64(1) || scripts["added"] != float64(0) || totals["state"] != float64(1) || totals["codeChanged"] != true { + if scripts["changed"] != float64(1) || scripts["added"] != float64(0) || totals["state"] != float64(1) || totals["stateOnly"] != false { t.Fatalf("unexpected totals: %v", totals) } if assets["added"] != float64(1) || assets["removed"] != float64(1) || assets["modified"] != float64(1) || totals["files"] != float64(2) { t.Fatalf("unexpected asset totals: %v", totals) } targets := result["targets"].([]any) - if len(targets) != 2 || targets[0].(map[string]any)["path"] != "Cat/main.fractch" || targets[1].(map[string]any)["path"] != "Stage/main.fractch" { + if len(targets) != 2 || targets[0].(map[string]any)["path"] != "Cat" || targets[1].(map[string]any)["path"] != "Stage" { t.Fatalf("unexpected targets: %v", targets) } for _, raw := range result["files"].([]any) { @@ -294,7 +294,7 @@ func TestInspectChangesSummarisesCommit(t *testing.T) { initial := decodeResult(t, Inspect(path, first, "changes", "", second)) initialTotals := initial["totals"].(map[string]any) - if initialTotals["scripts"].(map[string]any)["added"] != float64(3) || initialTotals["codeChanged"] != true { + if initialTotals["scripts"].(map[string]any)["added"] != float64(3) || initialTotals["stateOnly"] != false { t.Fatalf("initial commit totals: %v", initialTotals) } } @@ -308,7 +308,7 @@ func TestInspectChangesStateOnlyCommitIsNotCode(t *testing.T) { second := repo.commit(secondTree, first, "Updated project") result := decodeResult(t, Inspect(repo.write(t), second, "changes", "", second)) totals := result["totals"].(map[string]any) - if totals["codeChanged"] != false || totals["state"] != float64(1) { + if totals["stateOnly"] != true || totals["state"] != float64(1) { t.Fatalf("state-only commit totals: %v", totals) } } @@ -350,7 +350,8 @@ func TestStripHiddenValuesKeepsCallsAndExpressions(t *testing.T) { "move (a + b ?? 3) * 2;": "move (a + b) * 2;", "say \"(x ?? 1)\";": "say \"(x ?? 1)\";", "say (answer() ?? (\"a\"));": "say answer();", - "pick(a ?? 1, b);": "pick(a ?? 1, b);", + "pick(a ?? 1, b);": "pick(a, b);", + "say answer() ?? \"hi\";": "say answer();", } for input, want := range cases { if got := stripHiddenValues(input); got != want { From 49b3052389422fe856b5699ad3d5f8b8630aa0b9 Mon Sep 17 00:00:00 2001 From: Mistium <92952823+Mistium@users.noreply.github.com> Date: Fri, 20 Nov 2026 19:48:55 +0000 Subject: [PATCH 3/5] Keep the changes view linear on many hidden values, blank lines and folders Hidden-value stripping trims in place instead of copying its output, the statement reader skips a whitespace run once, and region files are grouped by folder in one pass. Only a script's own body ends at a closing brace, so object values keep their id. A comment trailing a statement on the same line stays a free note instead of joining the next script. --- native/gitinspection/changes.go | 21 ++++--- native/gitinspection/fractch_limits_test.go | 58 +++++++++++++++++++ native/gitinspection/fractch_parse.go | 62 +++++++++++++-------- native/gitinspection/fractch_test.go | 1 + 4 files changed, 112 insertions(+), 30 deletions(-) diff --git a/native/gitinspection/changes.go b/native/gitinspection/changes.go index adcb901..50be52c 100644 --- a/native/gitinspection/changes.go +++ b/native/gitinspection/changes.go @@ -73,15 +73,18 @@ func isAssetPath(path string) bool { return strings.HasPrefix(path, "assets/") || strings.Contains(path, "/assets/") || strings.HasPrefix(path, "fonts/") } -func dirFractchFiles(files map[string]gitFile, dir string) []string { - paths := []string{} +func fractchFilesByDir(files map[string]gitFile) map[string][]string { + byDir := map[string][]string{} for path := range files { - if isFractchPath(path) && pathDir(path) == dir { - paths = append(paths, path) + if isFractchPath(path) { + dir := pathDir(path) + byDir[dir] = append(byDir[dir], path) } } - sort.Strings(paths) - return paths + for _, paths := range byDir { + sort.Strings(paths) + } + return byDir } func readSources(source archive, files map[string]gitFile, paths []string, budget *int) ([]fractchSource, bool) { @@ -153,12 +156,14 @@ func structuredChanges(changes []map[string]any, baseArchive archive, parentFile ordered = append(ordered, dir) } sort.Strings(ordered) + beforeByDir := fractchFilesByDir(parentFiles) + afterByDir := fractchFilesByDir(currentFiles) targets := []fractchTargetChanges{} budget := 0 tooLarge := false for _, dir := range ordered { - beforePaths := dirFractchFiles(parentFiles, dir) - afterPaths := dirFractchFiles(currentFiles, dir) + beforePaths := beforeByDir[dir] + afterPaths := afterByDir[dir] if len(beforePaths) == 0 && len(afterPaths) == 0 { continue } diff --git a/native/gitinspection/fractch_limits_test.go b/native/gitinspection/fractch_limits_test.go index c180d07..2410345 100644 --- a/native/gitinspection/fractch_limits_test.go +++ b/native/gitinspection/fractch_limits_test.go @@ -148,3 +148,61 @@ func FuzzCompareFractch(f *testing.F) { camelIdentifiers(after) }) } + +func TestCompareFractchManyHiddenValuesIsFast(t *testing.T) { + body := "when flag {\n" + strings.Repeat(" say a ?? 1;\n", 80000) + "}\n" + requireFast(t, 3*time.Second, func() { + compareFractch(body, strings.Replace(body, "say a", "think a", 1)) + }) +} + +func TestSplitFractchStatementsLongBlankRunsIsFast(t *testing.T) { + src := "var a = 1" + strings.Repeat(" \n", 1000000) + "var b = 2\n" + requireFast(t, 3*time.Second, func() { + if statements := splitFractchStatements(src); len(statements) != 2 { + t.Errorf("expected two statements, got %d", len(statements)) + } + }) +} + +func TestInspectChangesManySpriteFoldersIsFast(t *testing.T) { + repo := newTestRepository() + before := map[string]string{} + after := map[string]string{} + for i := 0; i < 5000; i++ { + path := fmt.Sprintf("S%d/main.fractch", i) + before[path] = repo.object("blob", []byte(fmt.Sprintf("sprite \"S%d\";\n", i))) + after[path] = repo.object("blob", []byte(fmt.Sprintf("sprite \"S%d\" at 1,1;\n", i))) + } + first := repo.commit(repo.nestedTree(before), "", "Initial version") + second := repo.commit(repo.nestedTree(after), first, "Move everything") + path := repo.write(t) + requireFast(t, 5*time.Second, func() { + result := decodeResult(t, Inspect(path, second, "changes", "", second)) + if result["totals"].(map[string]any)["state"] != float64(5000) { + t.Errorf("totals %v", result["totals"]) + } + }) +} + +func TestSplitFractchStatementsKeepsObjectValuesWhole(t *testing.T) { + src := "var o = {\"a\": 1} id \"o\"\nvar p = json({\"b\": {\"c\": 2}}) id \"p\";\nwhen flag {\n say 1;\n}\n" + statements := splitFractchStatements(src) + if len(statements) != 3 || statements[0] != "var o = {\"a\": 1} id \"o\"" { + t.Fatalf("statements %q", statements) + } + edited := strings.Replace(src, "{\"a\": 1}", "{\"a\": 2}", 1) + changes := compareFractch(src, edited) + if len(changes.State) != 1 || changes.State[0].Name != "o" || len(changes.Items) != 0 { + t.Fatalf("object value change not isolated: %+v", changes) + } +} + +func TestCompareFractchTrailingNoteStaysApartFromNextScript(t *testing.T) { + src := "var a = 1 id \"a\"; // note\nwhen flag {\n say 1;\n}\n" + edited := strings.Replace(src, "// note", "// a better note", 1) + changes := compareFractch(src, edited) + if len(changes.Scripts) != 0 || len(changes.Items) != 2 || changes.Items[0].Kind != "note" { + t.Fatalf("editing a trailing note changed a script: %+v", changes) + } +} diff --git a/native/gitinspection/fractch_parse.go b/native/gitinspection/fractch_parse.go index f14321e..179f4a9 100644 --- a/native/gitinspection/fractch_parse.go +++ b/native/gitinspection/fractch_parse.go @@ -90,13 +90,11 @@ func skipSpace(src string, i int) int { return i } -func startsTopLevelStatement(src string, i int) bool { - word := firstWord.FindString(src[skipSpace(src, i):]) - return topLevelKeywords[word] -} +var blockKeywords = map[string]bool{"when": true, "def": true, "script": true} func readFractchStatement(src string, i int) int { braces, brackets := 0, 0 + block := blockKeywords[firstWord.FindString(src[i:])] for i < len(src) { c := src[i] if isFractchQuote(c) { @@ -112,9 +110,11 @@ func readFractchStatement(src string, i int) int { case '{': braces++ case '}': - braces-- - if braces <= 0 { - return i + if braces > 0 { + braces-- + if braces == 0 && block { + return i + } } case '(', '[': brackets++ @@ -127,8 +127,12 @@ func readFractchStatement(src string, i int) int { return i } case '\n': - if braces == 0 && brackets == 0 && startsTopLevelStatement(src, i) { - return i + if braces == 0 && brackets == 0 { + next := skipSpace(src, i) + if topLevelKeywords[firstWord.FindString(src[next:])] { + return i + } + i = next } } } @@ -145,6 +149,7 @@ func splitFractchStatements(src string) []string { } } i := 0 + lastEnd := -1 for i < len(src) { next := skipSpace(src, i) if len(pending) > 0 && strings.Count(src[i:next], "\n") > 1 { @@ -157,10 +162,17 @@ func splitFractchStatements(src string) []string { if isCommentStart(src, i) { end := skipFractchComment(src, i) text := src[i:end] - if !(strings.HasPrefix(text, "/**") && strings.Contains(text, "* target:")) { + if strings.HasPrefix(text, "/**") && strings.Contains(text, "* target:") { + i = end + continue + } + if lastEnd >= 0 && len(pending) == 0 && !strings.Contains(src[lastEnd:i], "\n") { + statements = append(statements, strings.TrimSpace(text)) + } else { pending = append(pending, strings.TrimSpace(text)) } i = end + lastEnd = end continue } end := readFractchStatement(src, i) @@ -171,6 +183,7 @@ func splitFractchStatements(src string) []string { } statements = append(statements, text) i = end + lastEnd = end } flush() return statements @@ -307,6 +320,9 @@ func skipHiddenValue(src string, i int) int { depth := 0 for i < len(src) { c := src[i] + if isCommentStart(src, i) { + return i + } if isFractchQuote(c) { i = skipFractchString(src, i) continue @@ -330,19 +346,19 @@ func skipHiddenValue(src string, i int) int { } func stripHiddenFrom(src string, i int, inParen bool) (string, int, bool) { - var builder strings.Builder + out := make([]byte, 0, 64) hidden, comma := false, false for i < len(src) { c := src[i] if isFractchQuote(c) { end := skipFractchString(src, i) - builder.WriteString(src[i:end]) + out = append(out, src[i:end]...) i = end continue } if isCommentStart(src, i) { end := skipFractchComment(src, i) - builder.WriteString(src[i:end]) + out = append(out, src[i:end]...) i = end continue } @@ -355,31 +371,33 @@ func stripHiddenFrom(src string, i int, inParen bool) (string, int, bool) { after := nextNonSpace(src, next) standalone := !isIdentifierByte(before) && (after == 0 || strings.IndexByte(";,){]\n", after) >= 0) if stripped && standalone { - builder.WriteString(inner) + out = append(out, inner...) } else { - builder.WriteString("(" + inner + ")") + out = append(out, '(') + out = append(out, inner...) + out = append(out, ')') } i = next continue } if c == ')' && inParen { - return builder.String(), i + 1, hidden && !comma + return string(out), i + 1, hidden && !comma } if c == ',' { comma = true } if strings.HasPrefix(src[i:], "??") { - kept := strings.TrimRight(builder.String(), " \t") - builder.Reset() - builder.WriteString(kept) + for len(out) > 0 && (out[len(out)-1] == ' ' || out[len(out)-1] == '\t') { + out = out[:len(out)-1] + } hidden = true i = skipHiddenValue(src, i+2) continue } - builder.WriteByte(c) + out = append(out, c) i++ } - return builder.String(), i, false + return string(out), i, false } func nextNonSpace(src string, i int) byte { @@ -444,7 +462,7 @@ func parseFractchStatement(text, file string) fractchStatement { comments, code := leadingFractchComments(text) statement := fractchStatement{File: file, Body: text} if code == "" { - statement.Kind = "comment" + statement.Kind = "note" statement.Key = "note " + text statement.Name = firstLine(text) return statement diff --git a/native/gitinspection/fractch_test.go b/native/gitinspection/fractch_test.go index 37a1e7d..87c40ad 100644 --- a/native/gitinspection/fractch_test.go +++ b/native/gitinspection/fractch_test.go @@ -351,6 +351,7 @@ func TestStripHiddenValuesKeepsCallsAndExpressions(t *testing.T) { "say \"(x ?? 1)\";": "say \"(x ?? 1)\";", "say (answer() ?? (\"a\"));": "say answer();", "pick(a ?? 1, b);": "pick(a, b);", + "say a ?? 1 // x, y\n": "say a// x, y\n", "say answer() ?? \"hi\";": "say answer();", } for input, want := range cases { From 9238d51018a7d1f09d33fa354952710c647a9f55 Mon Sep 17 00:00:00 2001 From: Mistium <92952823+Mistium@users.noreply.github.com> Date: Fri, 20 Nov 2026 21:59:55 +0000 Subject: [PATCH 4/5] Name each changed target and normalise identifiers inside template strings --- native/gitinspection/changes.go | 3 +- native/gitinspection/fractch.go | 14 +++++++++ native/gitinspection/fractch_parse.go | 43 ++++++++++++++++++++++++++- native/gitinspection/fractch_test.go | 19 ++++++++---- 4 files changed, 72 insertions(+), 7 deletions(-) diff --git a/native/gitinspection/changes.go b/native/gitinspection/changes.go index 50be52c..80d3d2a 100644 --- a/native/gitinspection/changes.go +++ b/native/gitinspection/changes.go @@ -8,6 +8,7 @@ import ( type fractchTargetChanges struct { Path string `json:"path"` + Name string `json:"name,omitempty"` Status string `json:"status"` Files []string `json:"files"` TooLarge bool `json:"tooLarge,omitempty"` @@ -187,7 +188,7 @@ func structuredChanges(changes []map[string]any, baseArchive archive, parentFile continue } compared := compareFractchSources(before, after, assetLookup{before: dirAssets(parentFiles, dir), after: dirAssets(currentFiles, dir)}) - target.Scripts, target.Items, target.State = compared.Scripts, compared.Items, compared.State + target.Name, target.Scripts, target.Items, target.State = compared.Name, compared.Scripts, compared.Items, compared.State if len(target.Scripts) == 0 && len(target.Items) == 0 && len(target.State) == 0 { continue } diff --git a/native/gitinspection/fractch.go b/native/gitinspection/fractch.go index cbb3621..16acc7b 100644 --- a/native/gitinspection/fractch.go +++ b/native/gitinspection/fractch.go @@ -47,6 +47,7 @@ type fractchFileChanges struct { Scripts []fractchScriptChange `json:"scripts"` Items []fractchItemChange `json:"items"` State []fractchStateChange `json:"state"` + Name string `json:"-"` assets assetLookup work int } @@ -676,11 +677,24 @@ func compareFractchSources(before, after []fractchSource, assets assetLookup) fr changes := fractchFileChanges{Scripts: []fractchScriptChange{}, Items: []fractchItemChange{}, State: []fractchStateChange{}, assets: assets} beforeScripts, beforeItems := partitionStatements(before) afterScripts, afterItems := partitionStatements(after) + changes.Name = targetHeaderName(afterItems) + if changes.Name == "" { + changes.Name = targetHeaderName(beforeItems) + } changes.compareItems(beforeItems, afterItems) changes.compareScripts(beforeScripts, afterScripts) return changes } +func targetHeaderName(items []fractchStatement) string { + for _, statement := range items { + if statement.Kind == "sprite" || statement.Kind == "stage" { + return statement.Name + } + } + return "" +} + func compareFractchAssets(before, after string, assets assetLookup) fractchFileChanges { return compareFractchSources([]fractchSource{{Path: "main.fractch", Text: before}}, []fractchSource{{Path: "main.fractch", Text: after}}, assets) } diff --git a/native/gitinspection/fractch_parse.go b/native/gitinspection/fractch_parse.go index 179f4a9..42555d4 100644 --- a/native/gitinspection/fractch_parse.go +++ b/native/gitinspection/fractch_parse.go @@ -410,13 +410,54 @@ func nextNonSpace(src string, i int) byte { return 0 } +func camelTemplate(builder *strings.Builder, text string) { + for i := 0; i < len(text); { + if text[i] == '\\' && i+1 < len(text) { + builder.WriteString(text[i : i+2]) + i += 2 + continue + } + if !strings.HasPrefix(text[i:], "${") { + builder.WriteByte(text[i]) + i++ + continue + } + end := i + 2 + depth := 1 + for end < len(text) && depth > 0 { + switch text[end] { + case '"': + end = skipFractchString(text, end) + continue + case '{': + depth++ + case '}': + depth-- + } + end++ + } + inner := end + if depth == 0 { + inner = end - 1 + } + builder.WriteString("${") + builder.WriteString(camelIdentifiers(text[i+2 : inner])) + builder.WriteString(text[inner:end]) + i = end + } +} + func camelIdentifiers(src string) string { var builder strings.Builder for i := 0; i < len(src); { c := src[i] if isFractchQuote(c) { end := skipFractchString(src, i) - builder.WriteString(src[i:end]) + if c == '`' { + camelTemplate(&builder, src[i:end]) + } else { + builder.WriteString(src[i:end]) + } i = end continue } diff --git a/native/gitinspection/fractch_test.go b/native/gitinspection/fractch_test.go index 87c40ad..1207ee2 100644 --- a/native/gitinspection/fractch_test.go +++ b/native/gitinspection/fractch_test.go @@ -282,6 +282,12 @@ func TestInspectChangesSummarisesCommit(t *testing.T) { if len(targets) != 2 || targets[0].(map[string]any)["path"] != "Cat" || targets[1].(map[string]any)["path"] != "Stage" { t.Fatalf("unexpected targets: %v", targets) } + if name := targets[0].(map[string]any)["name"]; name != "Cat" { + t.Fatalf("target name = %v, want the sprite name", name) + } + if _, named := targets[1].(map[string]any)["name"]; named { + t.Fatalf("a stage file without a header should have no name: %v", targets[1]) + } for _, raw := range result["files"].([]any) { file := raw.(map[string]any) if file["path"] == "README.md" && file["newData"] == nil { @@ -406,11 +412,14 @@ func TestCompareFractchIgnoresOlderSnakeCaseSpellings(t *testing.T) { func TestCamelIdentifiers(t *testing.T) { cases := map[string]string{ - "wait_until x_pos;": "waitUntil xPos;", - "Big_Name _private a1_b2": "Big_Name _private a1B2", - "say \"snake_case\";": "say \"snake_case\";", - "// keep_this\nset_x 1;": "// keep_this\nsetX 1;", - "strings.menu_textCase(x)": "strings.menuTextCase(x)", + "wait_until x_pos;": "waitUntil xPos;", + "Big_Name _private a1_b2": "Big_Name _private a1B2", + "say \"snake_case\";": "say \"snake_case\";", + "// keep_this\nset_x 1;": "// keep_this\nsetX 1;", + "strings.menu_textCase(x)": "strings.menuTextCase(x)", + "say `${costume_name} x_y`;": "say `${costumeName} x_y`;", + "say `a ${f(\"}\", b_c)} ${d_e`": "say `a ${f(\"}\", bC)} ${dE`", + "say `\\${not_this}`;": "say `\\${not_this}`;", } for input, want := range cases { if got := camelIdentifiers(input); got != want { From 740a19f3ea4abccf29ee05b90278ba5102d361ef Mon Sep 17 00:00:00 2001 From: Mistium <92952823+Mistium@users.noreply.github.com> Date: Sat, 21 Nov 2026 00:46:39 +0000 Subject: [PATCH 5/5] Pin template normalisation edge cases --- native/gitinspection/fractch_test.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/native/gitinspection/fractch_test.go b/native/gitinspection/fractch_test.go index 1207ee2..33f2feb 100644 --- a/native/gitinspection/fractch_test.go +++ b/native/gitinspection/fractch_test.go @@ -418,6 +418,8 @@ func TestCamelIdentifiers(t *testing.T) { "// keep_this\nset_x 1;": "// keep_this\nsetX 1;", "strings.menu_textCase(x)": "strings.menuTextCase(x)", "say `${costume_name} x_y`;": "say `${costumeName} x_y`;", + "say `${a_b \\": "say `${aB \\", + "say `${\"x_y": "say `${\"x_y", "say `a ${f(\"}\", b_c)} ${d_e`": "say `a ${f(\"}\", bC)} ${dE`", "say `\\${not_this}`;": "say `\\${not_this}`;", }