diff --git a/native/gitinspection/changes.go b/native/gitinspection/changes.go new file mode 100644 index 0000000..80d3d2a --- /dev/null +++ b/native/gitinspection/changes.go @@ -0,0 +1,295 @@ +package gitinspection + +import ( + "archive/zip" + "sort" + "strings" +) + +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"` + 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"` + StateOnly bool `json:"stateOnly"` +} + +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[prefix+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 fractchFilesByDir(files map[string]gitFile) map[string][]string { + byDir := map[string][]string{} + for path := range files { + if isFractchPath(path) { + dir := pathDir(path) + byDir[dir] = append(byDir[dir], path) + } + } + for _, paths := range byDir { + sort.Strings(paths) + } + return byDir +} + +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)}) + } + 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++ + } + } + 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) { + others := []map[string]any{} + totals := changeTotals{} + dirs := map[string]bool{} + authoredFiles := 0 + for _, change := range changes { + path, _ := change["path"].(string) + status, _ := change["status"].(string) + 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++ + } + continue + } + totals.Files++ + if path != "fractch.json" { + authoredFiles++ + } + } + ordered := []string{} + for dir := range dirs { + ordered = append(ordered, dir) + } + sort.Strings(ordered) + beforeByDir := fractchFilesByDir(parentFiles) + afterByDir := fractchFilesByDir(currentFiles) + targets := []fractchTargetChanges{} + budget := 0 + tooLarge := false + for _, dir := range ordered { + beforePaths := beforeByDir[dir] + afterPaths := afterByDir[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 + tooLarge = true + targets = append(targets, target) + continue + } + compared := compareFractchSources(before, after, assetLookup{before: dirAssets(parentFiles, dir), after: dirAssets(currentFiles, dir)}) + 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 + } + 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 +} + +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..16acc7b --- /dev/null +++ b/native/gitinspection/fractch.go @@ -0,0 +1,704 @@ +package gitinspection + +import ( + "math" + "sort" + "strconv" + "strings" +) + +const ( + maxStateValueChars = 160 + maxPairsPerGroup = 10_000 + maxSimilarityWork = 2_000_000 + crossPairSimilarity = 0.6 +) + +type fractchScriptChange struct { + 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 { + 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"` + Name string `json:"-"` + assets assetLookup + work int +} + +type assetLookup struct { + before func(string) string + after func(string) string +} + +type fractchSource struct { + Path string + Text string +} + +func noAssets(string) string { + return "" +} + +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 (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); { + 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 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 statement.Kind +} + +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) + 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:] + } + 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 { + 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 +} + +func (changes *fractchFileChanges) compareHeader(before, after fractchStatement) { + if before.Name != 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) { + beforeValue, afterValue := attributeValue(beforeAttributes, key), attributeValue(afterAttributes, key) + if beforeValue == afterValue { + continue + } + if field, ok := targetStateFields[key]; ok { + changes.addState(after.Kind, after.Name, field, beforeValue, afterValue) + continue + } + authored = true + } + if authored { + changes.addItem("changed", after.Kind, after.Name, before.Body, after.Body) + } +} + +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 headerFlag(statement fractchStatement, flag string) string { + for _, token := range fractchTokens(statement.Body) { + if token == flag { + return "true" + } + } + return "false" +} + +func assetFile(statement fractchStatement) string { + return unquoteFractch(fractchAttributes(fractchTokens(statement.Body))["file"]) +} + +func isAssetKind(kind string) bool { + return kind == "costume" || kind == "backdrop" || kind == "sound" +} + +func (changes *fractchFileChanges) assetOIDs(before, after fractchStatement) (string, string) { + beforeFile, afterFile := assetFile(before), assetFile(after) + if beforeFile == "" || afterFile == "" { + return "", "" + } + 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 { + 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 (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 + } + 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) + } +} + +func (changes *fractchFileChanges) addWhole(change string, statement fractchStatement) { + before, after := "", statement.Body + if change == "removed" { + before, after = statement.Body, "" + } + if statement.Kind == "watch" { + field := "shown" + if change == "removed" { + field = "removed" + } + changes.addState("monitor", statement.Name, field, before, after) + return + } + 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 (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[line]++ + } + common := 0 + for _, line := range bLines { + if counts[line] > 0 { + counts[line]-- + common++ + } + } + longest := len(aLines) + if len(bLines) > longest { + longest = len(bLines) + } + if longest == 0 { + return 1, true + } + return float64(common) / float64(longest), true +} + +type scriptPair struct { + before int + after int + score float64 +} + +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) + } + } + 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}) + } + } + 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 }) + 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 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)) + 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].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) + } + } + 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 := []int{}, []int{} + for i := range before { + if !beforeUsed[i] { + remainingBefore = append(remainingBefore, i) + } + } + for j := range after { + if !afterUsed[j] { + remainingAfter = append(remainingAfter, j) + } + } + 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 { + 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, 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, File: statement.File, Before: statement.Body, BeforeAt: statement.At, + }) + } + } +} + +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 { + items = append(items, statement) + } + } + } + return scripts, items +} + +func compareFractchSources(before, after []fractchSource, assets assetLookup) fractchFileChanges { + 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) +} + +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..2410345 --- /dev/null +++ b/native/gitinspection/fractch_limits_test.go @@ -0,0 +1,208 @@ +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) + }) +} + +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 new file mode 100644 index 0000000..42555d4 --- /dev/null +++ b/native/gitinspection/fractch_parse.go @@ -0,0 +1,598 @@ +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 +} + +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) { + i = skipFractchString(src, i) + continue + } + if isCommentStart(src, i) { + i = skipFractchComment(src, i) + continue + } + i++ + switch c { + case '{': + braces++ + case '}': + if braces > 0 { + braces-- + if braces == 0 && block { + return i + } + } + case '(', '[': + brackets++ + case ')', ']': + if brackets > 0 { + brackets-- + } + case ';': + if braces == 0 { + return i + } + case '\n': + if braces == 0 && brackets == 0 { + next := skipSpace(src, i) + if topLevelKeywords[firstWord.FindString(src[next:])] { + return i + } + i = next + } + } + } + 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 + lastEnd := -1 + 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:") { + 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) + 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 + lastEnd = 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 isCommentStart(src, i) { + return 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) { + out := make([]byte, 0, 64) + hidden, comma := false, false + for i < len(src) { + c := src[i] + if isFractchQuote(c) { + end := skipFractchString(src, i) + out = append(out, src[i:end]...) + i = end + continue + } + if isCommentStart(src, i) { + end := skipFractchComment(src, i) + out = append(out, 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 { + out = append(out, inner...) + } else { + out = append(out, '(') + out = append(out, inner...) + out = append(out, ')') + } + i = next + continue + } + if c == ')' && inParen { + return string(out), i + 1, hidden && !comma + } + if c == ',' { + comma = true + } + if strings.HasPrefix(src[i:], "??") { + 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 + } + out = append(out, c) + i++ + } + return string(out), 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 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) + if c == '`' { + camelTemplate(&builder, src[i:end]) + } else { + 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 = "note" + 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 new file mode 100644 index 0000000..33f2feb --- /dev/null +++ b/native/gitinspection/fractch_test.go @@ -0,0 +1,431 @@ +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["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" || 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 { + 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["stateOnly"] != false { + 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["stateOnly"] != true || 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, b);", + "say a ?? 1 // x, y\n": "say a// x, y\n", + "say answer() ?? \"hi\";": "say answer();", + } + 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)", + "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}`;", + } + 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)