diff --git a/native/gitinspection/changes.go b/native/gitinspection/changes.go
index 80d3d2a..81eeed7 100644
--- a/native/gitinspection/changes.go
+++ b/native/gitinspection/changes.go
@@ -216,14 +216,32 @@ func treeFiles(source archive, tree string) (map[string]gitFile, bool) {
return files, true
}
-func changesBetween(baseArchive archive, baseSHA string, baseCommit gitCommit, headArchive archive, headSHA string, headCommit gitCommit) string {
+type commitChanges struct {
+ legacy bool
+ targets []fractchTargetChanges
+ others []map[string]any
+ totals changeTotals
+ spriteDirs map[string]bool
+}
+
+func spriteDirsOf(trees ...map[string]gitFile) map[string]bool {
+ dirs := map[string]bool{}
+ for _, files := range trees {
+ for dir := range fractchFilesByDir(files) {
+ dirs[dir] = true
+ }
+ }
+ return dirs
+}
+
+func computeChanges(baseArchive archive, baseCommit gitCommit, headArchive archive, headCommit gitCommit, inline bool) (commitChanges, string) {
parentFiles, ok := treeFiles(baseArchive, baseCommit.Tree)
if !ok {
- return failure("parent commit tree is invalid")
+ return commitChanges{}, "parent commit tree is invalid"
}
currentFiles, ok := treeFiles(headArchive, headCommit.Tree)
if !ok || headCommit.Tree == "" {
- return failure("commit tree is invalid")
+ return commitChanges{}, "commit tree is invalid"
}
changes := []map[string]any{}
legacy := false
@@ -247,13 +265,101 @@ func changesBetween(baseArchive archive, baseSHA string, baseCommit gitCommit, h
}
sortChanges(changes)
targets, others, totals := structuredChanges(changes, baseArchive, parentFiles, headArchive, currentFiles)
- inlineDiffContent(others, baseArchive, parentFiles, headArchive, currentFiles)
+ if inline {
+ inlineDiffContent(others, baseArchive, parentFiles, headArchive, currentFiles)
+ }
+ return commitChanges{legacy: legacy, targets: targets, others: others, totals: totals, spriteDirs: spriteDirsOf(parentFiles, currentFiles)}, ""
+}
+
+func changesBetween(baseArchive archive, baseSHA string, baseCommit gitCommit, headArchive archive, headSHA string, headCommit gitCommit) string {
+ computed, problem := computeChanges(baseArchive, baseCommit, headArchive, headCommit, true)
+ if problem != "" {
+ return failure(problem)
+ }
return encode(map[string]any{
- "ok": true, "sha": headSHA, "parent": baseSHA, "commit": headCommit, "legacy": legacy,
- "targets": targets, "files": others, "totals": totals,
+ "ok": true, "sha": headSHA, "parent": baseSHA, "commit": headCommit, "legacy": computed.legacy,
+ "targets": computed.targets, "files": computed.others, "totals": computed.totals,
}, "could not encode commit changes")
}
+const maxSummarySprites = 5
+
+func summarizeComputed(computed commitChanges) map[string]any {
+ assetDirs := map[string]bool{}
+ files := 0
+ for _, file := range computed.others {
+ path, _ := file["path"].(string)
+ if isAssetPath(path) {
+ if dir := spriteDir(path); dir != "" && computed.spriteDirs[dir] {
+ assetDirs[dir] = true
+ }
+ } else if path != "fractch.json" {
+ files++
+ }
+ }
+ sprites := []string{}
+ count := 0
+ tooLarge := false
+ for _, target := range computed.targets {
+ tooLarge = tooLarge || target.TooLarge
+ if len(target.Scripts) == 0 && len(target.Items) == 0 && !target.TooLarge && !assetDirs[target.Path] {
+ continue
+ }
+ delete(assetDirs, target.Path)
+ count++
+ if len(sprites) < maxSummarySprites {
+ sprites = append(sprites, summaryName(target.Path, target.Name))
+ }
+ }
+ remaining := make([]string, 0, len(assetDirs))
+ for dir := range assetDirs {
+ remaining = append(remaining, dir)
+ }
+ sort.Strings(remaining)
+ for _, dir := range remaining {
+ count++
+ if len(sprites) < maxSummarySprites {
+ sprites = append(sprites, summaryName(dir, ""))
+ }
+ }
+ return map[string]any{
+ "legacy": computed.legacy, "totals": computed.totals, "sprites": sprites, "spriteCount": count,
+ "authoredFiles": files, "tooLarge": tooLarge,
+ }
+}
+
+func summaryName(dir, name string) string {
+ if dir == "Stage" {
+ return "Stage"
+ }
+ if name != "" {
+ return name
+ }
+ return dir
+}
+
+func (a archive) inspectCommitSummary(sha string, commit gitCommit) string {
+ base := archive{objects: map[string]*zip.File{}}
+ parentCommit := gitCommit{}
+ initial := len(commit.Parents) == 0
+ if !initial {
+ parsed, ok := a.parseCommit(commit.Parents[0])
+ if !ok {
+ return failure("parent commit is unavailable")
+ }
+ base, parentCommit = a, parsed
+ }
+ computed, problem := computeChanges(base, parentCommit, a, commit, false)
+ if problem != "" {
+ return failure(problem)
+ }
+ summary := summarizeComputed(computed)
+ summary["ok"] = true
+ summary["sha"] = sha
+ summary["initial"] = initial
+ return encode(summary, "could not encode commit summary")
+}
+
func (a archive) inspectCommitChanges(sha string, commit gitCommit) string {
if len(commit.Parents) == 0 {
return changesBetween(archive{objects: map[string]*zip.File{}}, "", gitCommit{}, a, sha, commit)
diff --git a/native/gitinspection/fractch_test.go b/native/gitinspection/fractch_test.go
index 33f2feb..b268ad4 100644
--- a/native/gitinspection/fractch_test.go
+++ b/native/gitinspection/fractch_test.go
@@ -2,6 +2,7 @@ package gitinspection
import (
"encoding/hex"
+ "encoding/json"
"sort"
"strings"
"testing"
@@ -298,6 +299,27 @@ func TestInspectChangesSummarisesCommit(t *testing.T) {
}
}
+ summary := decodeResult(t, Inspect(path, second, "summary", "", second))
+ if summary["ok"] != true || summary["initial"] != false || summary["spriteCount"] != float64(2) {
+ t.Fatalf("unexpected summary: %v", summary)
+ }
+ if names := summary["sprites"].([]any); len(names) != 2 || names[0] != "Cat" || names[1] != "Stage" {
+ t.Fatalf("unexpected summary sprites: %v", names)
+ }
+ if summary["totals"].(map[string]any)["scripts"].(map[string]any)["changed"] != float64(1) {
+ t.Fatalf("summary totals differ from the changes view: %v", summary["totals"])
+ }
+ if summary["authoredFiles"] != float64(1) || summary["tooLarge"] != false {
+ t.Fatalf("summary should count the readme and nothing else: %v", summary)
+ }
+ if _, inlined := summary["files"]; inlined {
+ t.Fatalf("a summary should not carry file contents: %v", summary)
+ }
+ firstSummary := decodeResult(t, Inspect(path, first, "summary", "", second))
+ if firstSummary["initial"] != true {
+ t.Fatalf("the first commit should be marked initial: %v", firstSummary)
+ }
+
initial := decodeResult(t, Inspect(path, first, "changes", "", second))
initialTotals := initial["totals"].(map[string]any)
if initialTotals["scripts"].(map[string]any)["added"] != float64(3) || initialTotals["stateOnly"] != false {
@@ -319,6 +341,60 @@ func TestInspectChangesStateOnlyCommitIsNotCode(t *testing.T) {
}
}
+func TestInspectSummaryNamesOnlySpriteFolders(t *testing.T) {
+ repo := newTestRepository()
+ code := repo.object("blob", []byte(fractchBase))
+ firstTree := repo.nestedTree(map[string]string{
+ "Cat/main.fractch": code,
+ "Cat/assets/a.svg": repo.object("blob", []byte("")),
+ "fonts/Brand.ttf": repo.object("blob", []byte("font one")),
+ "assets/loose.png": repo.object("blob", []byte("png one")),
+ "Stage/stage.fractch": repo.object("blob", []byte("stage \"Stage\";\n")),
+ })
+ first := repo.commit(firstTree, "", "Initial version")
+ secondTree := repo.nestedTree(map[string]string{
+ "Cat/main.fractch": code,
+ "Cat/assets/a.svg": repo.object("blob", []byte("