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("