From 9f4290e8b8949b8f109cf236581f67b9a9eb79d2 Mon Sep 17 00:00:00 2001 From: Mistium <92952823+Mistium@users.noreply.github.com> Date: Sat, 21 Nov 2026 10:15:46 +0000 Subject: [PATCH 1/2] Expose uncommitted worktree changes and remix divergence --- native/gitinspection/divergence.go | 83 ++++++++ native/gitinspection/divergence_test.go | 63 ++++++ native/gitinspection/inspection.go | 43 +++- native/gitinspection/worktree_inspect.go | 194 ++++++++++++++++++ native/gitinspection/worktree_inspect_test.go | 154 ++++++++++++++ opal.json | 2 +- src/git/git-inspection.osl | 119 ++++++++++- src/git/pulls.osl | 14 +- src/git/pulls.test.osl | 3 + src/git/remix-divergence.osl | 100 +++++++++ src/git/remix-divergence.test.osl | 75 +++++++ src/projects/project-response.osl | 41 ++++ 12 files changed, 877 insertions(+), 14 deletions(-) create mode 100644 native/gitinspection/divergence.go create mode 100644 native/gitinspection/divergence_test.go create mode 100644 native/gitinspection/worktree_inspect.go create mode 100644 native/gitinspection/worktree_inspect_test.go create mode 100644 src/git/remix-divergence.osl create mode 100644 src/git/remix-divergence.test.osl diff --git a/native/gitinspection/divergence.go b/native/gitinspection/divergence.go new file mode 100644 index 0000000..585fda9 --- /dev/null +++ b/native/gitinspection/divergence.go @@ -0,0 +1,83 @@ +package gitinspection + +func (a archive) reachableSet(head string) (map[string]bool, bool) { + if len(head) != 40 { + return nil, false + } + if _, ok := a.parseCommit(head); !ok { + return nil, false + } + visited := make(map[string]bool) + queue := []string{head} + for len(queue) > 0 { + oid := queue[0] + queue = queue[1:] + if visited[oid] { + continue + } + if len(visited) >= maxReachable { + return nil, false + } + visited[oid] = true + commit, ok := a.parseCommit(oid) + if !ok { + continue + } + queue = append(queue, commit.Parents...) + } + return visited, true +} + +func CompareDivergence(childPath, childHead, parentPath, parentHead string) string { + if childHead == "" || parentHead == "" { + return encode(map[string]any{"ok": true, "commonAncestor": false}, "could not encode divergence") + } + if childHead == parentHead { + return encode(map[string]any{"ok": true, "commonAncestor": true, "ahead": 0, "behind": 0}, "could not encode divergence") + } + releaseSlot := acquireInspectionSlot() + defer releaseSlot() + + childShared, err := acquireSharedArchive(childPath) + if err != nil { + return failure("child workspace archive is unavailable") + } + defer childShared.release() + + parentShared, err := acquireSharedArchive(parentPath) + if err != nil { + return failure("parent workspace archive is unavailable") + } + defer parentShared.release() + + childSet, ok := childShared.archive.reachableSet(childHead) + if !ok { + return encode(map[string]any{"ok": true, "commonAncestor": false}, "could not encode divergence") + } + parentSet, ok := parentShared.archive.reachableSet(parentHead) + if !ok { + return encode(map[string]any{"ok": true, "commonAncestor": false}, "could not encode divergence") + } + + shared := false + ahead := 0 + for sha := range childSet { + if parentSet[sha] { + shared = true + } else { + ahead++ + } + } + if !shared { + return encode(map[string]any{"ok": true, "commonAncestor": false}, "could not encode divergence") + } + behind := 0 + for sha := range parentSet { + if !childSet[sha] { + behind++ + } + } + return encode(map[string]any{ + "ok": true, "commonAncestor": true, "ahead": ahead, "behind": behind, + }, "could not encode divergence") +} diff --git a/native/gitinspection/divergence_test.go b/native/gitinspection/divergence_test.go new file mode 100644 index 0000000..29afdd5 --- /dev/null +++ b/native/gitinspection/divergence_test.go @@ -0,0 +1,63 @@ +package gitinspection + +import "testing" + +func TestCompareDivergenceAheadBehindAndUpToDate(t *testing.T) { + parentRepo := newTestRepository() + blob1 := parentRepo.object("blob", []byte("v1\n")) + tree1 := parentRepo.tree("main.fractch", blob1) + base := parentRepo.commit(tree1, "", "Base commit") + + blob2 := parentRepo.object("blob", []byte("v2\n")) + tree2 := parentRepo.tree("main.fractch", blob2) + p1 := parentRepo.commit(tree2, base, "Parent commit 1") + + blob3 := parentRepo.object("blob", []byte("v3\n")) + tree3 := parentRepo.tree("main.fractch", blob3) + p2 := parentRepo.commit(tree3, p1, "Parent commit 2") + + childRepo := newTestRepository() + for oid, data := range parentRepo.objects { + if oid == base || oid == tree1 || oid == blob1 { + childRepo.objects[oid] = data + } + } + cBlob1 := childRepo.object("blob", []byte("child1\n")) + cTree1 := childRepo.tree("main.fractch", cBlob1) + c1 := childRepo.commit(cTree1, base, "Child commit 1") + cBlob2 := childRepo.object("blob", []byte("child2\n")) + cTree2 := childRepo.tree("main.fractch", cBlob2) + c2 := childRepo.commit(cTree2, c1, "Child commit 2") + cBlob3 := childRepo.object("blob", []byte("child3\n")) + cTree3 := childRepo.tree("main.fractch", cBlob3) + c3 := childRepo.commit(cTree3, c2, "Child commit 3") + + parentPath := parentRepo.write(t) + childPath := childRepo.write(t) + + diverged := decodeResult(t, CompareDivergence(childPath, c3, parentPath, p2)) + if diverged["ok"] != true || diverged["commonAncestor"] != true || diverged["ahead"] != float64(3) || diverged["behind"] != float64(2) { + t.Fatalf("expected 3 ahead 2 behind, got %#v", diverged) + } + + upToDate := decodeResult(t, CompareDivergence(childPath, base, parentPath, base)) + if upToDate["ok"] != true || upToDate["commonAncestor"] != true || upToDate["ahead"] != float64(0) || upToDate["behind"] != float64(0) { + t.Fatalf("expected 0 ahead 0 behind, got %#v", upToDate) + } + + onlyBehind := decodeResult(t, CompareDivergence(childPath, base, parentPath, p2)) + if onlyBehind["ok"] != true || onlyBehind["commonAncestor"] != true || onlyBehind["ahead"] != float64(0) || onlyBehind["behind"] != float64(2) { + t.Fatalf("expected 0 ahead 2 behind, got %#v", onlyBehind) + } + + unrelatedRepo := newTestRepository() + uBlob := unrelatedRepo.object("blob", []byte("other\n")) + uTree := unrelatedRepo.tree("other.fractch", uBlob) + uHead := unrelatedRepo.commit(uTree, "", "Unrelated") + unrelatedPath := unrelatedRepo.write(t) + + unrelated := decodeResult(t, CompareDivergence(unrelatedPath, uHead, parentPath, p2)) + if unrelated["ok"] != true || unrelated["commonAncestor"] != false { + t.Fatalf("expected no common ancestor, got %#v", unrelated) + } +} diff --git a/native/gitinspection/inspection.go b/native/gitinspection/inspection.go index 70abb75..a97dfd6 100644 --- a/native/gitinspection/inspection.go +++ b/native/gitinspection/inspection.go @@ -39,9 +39,11 @@ type gitCommit struct { } type archive struct { - objects map[string]*zip.File - trusted map[string]bool - memo *objectMemo + objects map[string]*zip.File + worktree map[string]*zip.File + worktreeByOID map[string]*zip.File + trusted map[string]bool + memo *objectMemo } type treeItem struct { @@ -64,19 +66,50 @@ func openArchive(path string) (*zip.ReadCloser, archive, error) { } func indexArchive(files []*zip.File) archive { - result := archive{objects: make(map[string]*zip.File)} + result := archive{ + objects: make(map[string]*zip.File), + worktree: make(map[string]*zip.File), + } for _, item := range files { - if strings.HasPrefix(item.Name, ".git/objects/") && !item.FileInfo().IsDir() { + if item.FileInfo().IsDir() { + continue + } + if strings.HasPrefix(item.Name, ".git/objects/") { result.objects[item.Name] = item + } else if safeWorktreePath(item.Name) { + result.worktree[item.Name] = item } } return result } +func readWorktreeEntry(item *zip.File) ([]byte, bool) { + if item == nil || item.UncompressedSize64 > maxObjectBytes { + return nil, false + } + source, err := item.Open() + if err != nil { + return nil, false + } + content, readErr := io.ReadAll(io.LimitReader(source, maxObjectBytes+1)) + closeErr := source.Close() + if readErr != nil || closeErr != nil || len(content) > maxObjectBytes { + return nil, false + } + return content, true +} + func (a archive) readObject(oid string) (string, []byte, bool) { if len(oid) != 40 { return "", nil, false } + if item := a.worktreeByOID[oid]; item != nil { + content, ok := readWorktreeEntry(item) + if !ok { + return "", nil, false + } + return "blob", content, true + } item := a.objects[".git/objects/"+oid[:2]+"/"+oid[2:]] if item == nil || item.UncompressedSize64 > maxObjectBytes { return "", nil, false diff --git a/native/gitinspection/worktree_inspect.go b/native/gitinspection/worktree_inspect.go new file mode 100644 index 0000000..9fc9b93 --- /dev/null +++ b/native/gitinspection/worktree_inspect.go @@ -0,0 +1,194 @@ +package gitinspection + +import ( + "archive/zip" + "crypto/sha1" + "encoding/hex" + "sort" + "strconv" +) + +func gitBlobOID(content []byte) string { + hash := sha1.New() + hash.Write([]byte("blob ")) + hash.Write([]byte(strconv.Itoa(len(content)))) + hash.Write([]byte{0}) + hash.Write(content) + return hex.EncodeToString(hash.Sum(nil)) +} + +func (a archive) worktreeFiles() (map[string]gitFile, archive, bool) { + if len(a.worktree) == 0 || len(a.worktree) > maxTreeFiles { + return nil, archive{}, false + } + files := make(map[string]gitFile, len(a.worktree)) + byOID := make(map[string]*zip.File, len(a.worktree)) + for path, item := range a.worktree { + content, ok := readWorktreeEntry(item) + if !ok { + return nil, archive{}, false + } + oid := gitBlobOID(content) + info := blobSummary(content) + files[path] = gitFile{ + Path: path, + OID: oid, + Mode: "100644", + Size: info.size, + Binary: info.binary, + } + byOID[oid] = item + } + clone := a + clone.worktreeByOID = byOID + return files, clone, true +} + +func diffFileMaps(parentFiles, currentFiles map[string]gitFile) ([]map[string]any, int, int, bool) { + changes := []map[string]any{} + additions, deletions := 0, 0 + 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, + }) + deletions++ + legacy = legacy || path == "project.sb3" + } 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, + }) + additions++ + deletions++ + 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, + }) + additions++ + legacy = legacy || path == "project.sb3" + } + sort.Slice(changes, func(i, j int) bool { + return changes[i]["path"].(string) < changes[j]["path"].(string) + }) + return changes, additions, deletions, legacy +} + +func InspectWorktree(workspacePath, headSHA, operation, requestedPath, allowedHeads string) string { + releaseSlot := acquireInspectionSlot() + defer releaseSlot() + shared, err := acquireSharedArchive(workspacePath) + if err != nil { + return failure("workspace archive is unavailable") + } + defer shared.release() + baseArchive := shared.archive + + if len(baseArchive.worktree) == 0 { + if operation == "summary" { + return encode(map[string]any{"ok": true, "dirty": false}, "could not encode worktree summary") + } + return failure("no uncommitted changes in this workspace") + } + + headCommit, ok := baseArchive.parseCommit(headSHA) + if !ok { + return failure("commit was not found in this workspace") + } + if !baseArchive.reachable(headSHA, allowedHeads) { + return failure("commit is not reachable from this project's history") + } + + if operation == "file" { + item := baseArchive.worktree[requestedPath] + if item == nil { + return failure("file was not found in uncommitted changes") + } + content, ok := readWorktreeEntry(item) + if !ok { + return failure("file object is unavailable") + } + info := blobSummary(content) + file := gitFile{ + Path: requestedPath, + OID: gitBlobOID(content), + Mode: "100644", + Size: info.size, + Binary: info.binary, + } + return encode(map[string]any{"ok": true, "sha": "worktree", "uncommitted": true, "file": file, "content": content}, "could not encode file") + } + + worktreeFiles, worktreeArchive, ok := baseArchive.worktreeFiles() + if !ok { + return failure("worktree is invalid") + } + + if operation == "tree" { + listed := make([]gitFile, 0, len(worktreeFiles)) + for _, file := range worktreeFiles { + listed = append(listed, file) + } + sort.Slice(listed, func(i, j int) bool { + return listed[i].Path < listed[j].Path + }) + synthetic := gitCommit{Parents: []string{headSHA}, Message: "Uncommitted changes"} + return encode(map[string]any{"ok": true, "sha": "worktree", "uncommitted": true, "commit": synthetic, "files": listed}, "could not encode worktree tree") + } + + parentFiles, ok := treeFiles(baseArchive, headCommit.Tree) + if !ok { + return failure("commit tree is invalid") + } + changes, additions, deletions, legacy := diffFileMaps(parentFiles, worktreeFiles) + if len(changes) == 0 { + if operation == "summary" { + return encode(map[string]any{"ok": true, "dirty": false}, "could not encode worktree summary") + } + return failure("no uncommitted changes in this workspace") + } + + synthetic := gitCommit{Parents: []string{headSHA}, Message: "Uncommitted changes"} + + switch operation { + case "summary": + targets, others, totals := structuredChanges(changes, baseArchive, parentFiles, worktreeArchive, worktreeFiles) + computed := commitChanges{ + legacy: legacy, targets: targets, others: others, totals: totals, + spriteDirs: spriteDirsOf(parentFiles, worktreeFiles), + } + summary := summarizeComputed(computed) + summary["ok"] = true + return encode(map[string]any{ + "ok": true, "dirty": true, "head": headSHA, "changedFiles": len(changes), "summary": summary, + }, "could not encode worktree summary") + case "changes": + targets, others, totals := structuredChanges(changes, baseArchive, parentFiles, worktreeArchive, worktreeFiles) + inlineDiffContent(others, baseArchive, parentFiles, worktreeArchive, worktreeFiles) + return encode(map[string]any{ + "ok": true, "sha": "worktree", "uncommitted": true, "parent": headSHA, "commit": synthetic, + "legacy": legacy, "targets": targets, "files": others, "totals": totals, + }, "could not encode worktree changes") + case "commit-inline": + inlineDiffContent(changes, baseArchive, parentFiles, worktreeArchive, worktreeFiles) + return encode(map[string]any{ + "ok": true, "sha": "worktree", "uncommitted": true, "parent": headSHA, "parentTree": headCommit.Tree, + "commit": synthetic, "files": changes, "additions": additions, "deletions": deletions, "legacy": legacy, + }, "could not encode worktree inspection") + default: + return encode(map[string]any{ + "ok": true, "sha": "worktree", "uncommitted": true, "parent": headSHA, "parentTree": headCommit.Tree, + "commit": synthetic, "files": changes, "additions": additions, "deletions": deletions, "legacy": legacy, + }, "could not encode worktree inspection") + } +} diff --git a/native/gitinspection/worktree_inspect_test.go b/native/gitinspection/worktree_inspect_test.go new file mode 100644 index 0000000..378db19 --- /dev/null +++ b/native/gitinspection/worktree_inspect_test.go @@ -0,0 +1,154 @@ +package gitinspection + +import ( + "archive/zip" + "encoding/base64" + "os" + "testing" +) + +func (r *testRepository) writeWithWorktree(t testing.TB, worktree map[string][]byte) string { + t.Helper() + file, err := os.CreateTemp(t.TempDir(), "workspace-wt-*.mwp") + if err != nil { + t.Fatal(err) + } + writer := zip.NewWriter(file) + for oid, data := range r.objects { + entry, err := writer.Create(".git/objects/" + oid[:2] + "/" + oid[2:]) + if err != nil { + t.Fatal(err) + } + if _, err := entry.Write(data); err != nil { + t.Fatal(err) + } + } + for path, data := range worktree { + entry, err := writer.Create(path) + if err != nil { + t.Fatal(err) + } + if _, err := entry.Write(data); err != nil { + t.Fatal(err) + } + } + if err := writer.Close(); err != nil { + t.Fatal(err) + } + if err := file.Close(); err != nil { + t.Fatal(err) + } + return file.Name() +} + +func TestInspectWorktreeCleanArchive(t *testing.T) { + repository := newTestRepository() + blob := repository.object("blob", []byte("when flagClicked {\n move(10);\n}\n")) + tree := repository.tree("sprite.fractch", blob) + head := repository.commit(tree, "", "Initial version") + + path := repository.write(t) + result := decodeResult(t, InspectWorktree(path, head, "summary", "", head)) + if result["ok"] != true || result["dirty"] != false { + t.Fatalf("expected clean summary, got %#v", result) + } + changes := decodeResult(t, InspectWorktree(path, head, "changes", "", head)) + if changes["ok"] != false { + t.Fatalf("expected failure when no worktree exists, got %#v", changes) + } +} + +func TestInspectWorktreeIdenticalToHeadIsClean(t *testing.T) { + repository := newTestRepository() + content := []byte("when flagClicked {\n move(10);\n}\n") + blob := repository.object("blob", content) + tree := repository.tree("sprite.fractch", blob) + head := repository.commit(tree, "", "Initial version") + + path := repository.writeWithWorktree(t, map[string][]byte{ + "sprite.fractch": content, + "mwp.json": []byte(`{"worktree":true}`), + }) + result := decodeResult(t, InspectWorktree(path, head, "summary", "", head)) + if result["ok"] != true || result["dirty"] != false { + t.Fatalf("expected identical worktree to be clean, got %#v", result) + } +} + +func TestInspectWorktreeDirtySummaryChangesInlineTreeAndFile(t *testing.T) { + repository := newTestRepository() + oldSrc := []byte("sprite Player {\n when flagClicked {\n move(10);\n }\n}\n") + newSrc := []byte("sprite Player {\n when flagClicked {\n move(25);\n }\n}\n") + oldBlob := repository.object("blob", oldSrc) + playerTree := repository.tree("player.fractch", oldBlob) + rootTree := repository.object("tree", append([]byte("40000 Player\x00"), mustDecodeHex(t, playerTree)...)) + head := repository.commit(rootTree, "", "Initial version") + + path := repository.writeWithWorktree(t, map[string][]byte{ + "Player/player.fractch": newSrc, + "notes.txt": []byte("draft note\n"), + "mwp.json": []byte(`{"worktree":true}`), + }) + + summaryRes := decodeResult(t, InspectWorktree(path, head, "summary", "", head)) + if summaryRes["ok"] != true || summaryRes["dirty"] != true || summaryRes["changedFiles"] != float64(2) { + t.Fatalf("unexpected worktree summary: %#v", summaryRes) + } + summary := summaryRes["summary"].(map[string]any) + sprites := summary["sprites"].([]any) + if len(sprites) != 1 || sprites[0] != "Player" { + t.Fatalf("expected Player sprite in summary, got %#v", summary) + } + + changesRes := decodeResult(t, InspectWorktree(path, head, "changes", "", head)) + if changesRes["ok"] != true || changesRes["sha"] != "worktree" || changesRes["uncommitted"] != true || changesRes["parent"] != head { + t.Fatalf("unexpected worktree changes header: %#v", changesRes) + } + targets := changesRes["targets"].([]any) + if len(targets) != 1 || targets[0].(map[string]any)["path"] != "Player" { + t.Fatalf("expected Player target change, got %#v", targets) + } + + inlineRes := decodeResult(t, InspectWorktree(path, head, "commit-inline", "", head)) + if inlineRes["ok"] != true || inlineRes["uncommitted"] != true || inlineRes["additions"] != float64(2) { + t.Fatalf("unexpected worktree inline inspection: %#v", inlineRes) + } + + treeRes := decodeResult(t, InspectWorktree(path, head, "tree", "", head)) + if treeRes["ok"] != true || len(treeRes["files"].([]any)) != 2 { + t.Fatalf("unexpected worktree tree: %#v", treeRes) + } + + fileRes := decodeResult(t, InspectWorktree(path, head, "file", "notes.txt", head)) + if fileRes["ok"] != true { + t.Fatalf("unexpected worktree file result: %#v", fileRes) + } + decoded, err := base64.StdEncoding.DecodeString(fileRes["content"].(string)) + if err != nil || string(decoded) != "draft note\n" { + t.Fatalf("unexpected worktree file content: %q err=%v", string(decoded), err) + } +} + +func mustDecodeHex(t *testing.T, value string) []byte { + t.Helper() + out := make([]byte, len(value)/2) + for i := 0; i < len(out); i++ { + hi := fromHex(value[i*2]) + lo := fromHex(value[i*2+1]) + out[i] = (hi << 4) | lo + } + return out +} + +func fromHex(c byte) byte { + switch { + case c >= '0' && c <= '9': + return c - '0' + case c >= 'a' && c <= 'f': + return c - 'a' + 10 + case c >= 'A' && c <= 'F': + return c - 'A' + 10 + default: + return 0 + } +} diff --git a/opal.json b/opal.json index ed54a8c..31d2c54 100644 --- a/opal.json +++ b/opal.json @@ -1,6 +1,6 @@ { "name": "mistwarp-api", - "version": "1.5.0", + "version": "1.6.0", "main": "main.osl", "scripts": { "dev": ["osl", "run", "main.osl"], diff --git a/src/git/git-inspection.osl b/src/git/git-inspection.osl index 297d008..36ea0f6 100644 --- a/src/git/git-inspection.osl +++ b/src/git/git-inspection.osl @@ -1,6 +1,9 @@ export { cachedProjectWorkspacePath, + cachedRemixArchiveDivergence, projectRecordsCommit, + projectRemixDivergence, + projectUncommittedSummary, projectWorkspaceFingerprint, validGitObjectId, writeForkHistoryArchive @@ -178,7 +181,7 @@ def cachedWorkspaceGitInspectionJSON(object project, string workspacePath, strin if fs.exists(cachePath) and fs.size(cachePath).unwrapOr(0) <= 33554432 ( return fs.readFile(cachePath).unwrapOr("") ) - string resultJson = gitinspection.Inspect(workspacePath, sha, operation, requestedPath, allowedHeads) + string resultJson = if sha == "worktree" then gitinspection.InspectWorktree(workspacePath, projectGitHead(project), operation, requestedPath, allowedHeads) else gitinspection.Inspect(workspacePath, sha, operation, requestedPath, allowedHeads) if resultJson.toBytes().len <= 1048576 and gitinspection.InspectionError(resultJson) == "" ( string temporaryPath = cachePath ++ ".tmp." ++ randomString(8) if !fs.writeFile(temporaryPath, resultJson) or !fs.rename(temporaryPath, cachePath) ( @@ -188,21 +191,122 @@ def cachedWorkspaceGitInspectionJSON(object project, string workspacePath, strin return resultJson ) +def projectUncommittedSummary(object project) object ( + if !projectHasWorkspace(project) ( + return {} + ) + string head = projectGitHead(project) + string allowedHeads = projectGitAllowedHeadsString(project) + if head == "" or allowedHeads == "" ( + return {} + ) + string workspacePath = cachedProjectWorkspacePath(project) + if workspacePath == "" ( + return {} + ) + string raw = cachedWorkspaceGitInspectionJSON(project, workspacePath, "worktree", "summary", "", allowedHeads) + auto parsed = json.parse(raw, {type: "object"}) + if parsed.isErr() ( + return {} + ) + object result = parsed.unwrap().assert(object) + if (result.ok ?? false) != true or (result.dirty ?? false) != true ( + return {} + ) + string branch = "main" + if project.contains("gitBranch") and project.gitBranch != null and project.gitBranch.toStr() != "" ( + branch = project.gitBranch.toStr() + ) + return { + head: head, + branch: branch, + date: project.edited ?? 0, + author: project.owner ?? "", + files: (result.changedFiles ?? 0).toNum(), + summary: (result.summary ?? {}).assertElse(object, {}) + } +) + +def cachedRemixArchiveDivergence(object child, object parent) object ( + if !projectHasWorkspace(child) or !projectHasWorkspace(parent) ( + return {known: false} + ) + string childHead = projectGitHead(child) + string parentHead = projectGitHead(parent) + if childHead == "" or parentHead == "" ( + return {known: false} + ) + string cacheDir = tmpDir ++ "git-inspection-results/" + void fs.mkdir(cacheDir, {recursive: true}) + string cacheKey = "divergence." ++ projectWorkspaceFingerprint(child) ++ "." ++ childHead ++ "." ++ projectWorkspaceFingerprint(parent) ++ "." ++ parentHead + string cacheName = gitInspectionCacheVersion() ++ "." ++ cacheKey.hashSHA256() ++ ".json" + pruneGitInspectionCache(cacheDir, cacheName) + string cachePath = cacheDir ++ cacheName + string raw = "" + if fs.exists(cachePath) and fs.size(cachePath).unwrapOr(0) <= 65536 ( + raw = fs.readFile(cachePath).unwrapOr("") + ) + if raw == "" ( + string childPath = cachedProjectWorkspacePath(child) + string parentPath = cachedProjectWorkspacePath(parent) + if childPath == "" or parentPath == "" ( + return {known: false} + ) + raw = gitinspection.CompareDivergence(childPath, childHead, parentPath, parentHead) + if raw.toBytes().len <= 65536 and gitinspection.InspectionError(raw) == "" ( + string temporaryPath = cachePath ++ ".tmp." ++ randomString(8) + if !fs.writeFile(temporaryPath, raw) or !fs.rename(temporaryPath, cachePath) ( + void fs.remove(temporaryPath) + ) + ) + ) + auto parsed = json.parse(raw, {type: "object"}) + if parsed.isErr() ( + return {known: false} + ) + object result = parsed.unwrap().assert(object) + if (result.ok ?? false) != true or (result.commonAncestor ?? false) != true ( + return {known: false} + ) + return { + known: true, + ahead: (result.ahead ?? 0).toNum(), + behind: (result.behind ?? 0).toNum() + } +) + +def projectRemixDivergence(object child, object parent) object ( + string childHead = projectGitHead(child) + string parentHead = projectGitHead(parent) + object childGraph = if child.contains("gitGraph") and child.gitGraph != null then child.gitGraph.assertElse(object, {}) else {} + object parentGraph = if parent.contains("gitGraph") and parent.gitGraph != null then parent.gitGraph.assertElse(object, {}) else {} + object quick = remixGraphDivergence(childHead, childGraph, parentHead, parentGraph) + if (quick.resolved ?? false) == true ( + return quick + ) + return cachedRemixArchiveDivergence(child, parent) +) + def gitInspectionResponse(*serve.Context c, object project, string sha, string operation, string requestedPath) ( c.setHeader("Cache-Control", "private, no-store") c.setHeader("CDN-Cache-Control", "private, no-store") string pullIndex = (c.query("pull") ?? "") string pullTarget = (c.query("pullTarget") ?? "") - boolean pullCommit = pullIndex != "" and pullTarget != "" and canAccessPullProjectCommit(project, pullTarget, pullIndex, sha, c) + boolean isWorktree = sha == "worktree" and pullIndex == "" and pullTarget == "" and operation != "last-commits" + boolean pullCommit = !isWorktree and pullIndex != "" and pullTarget != "" and canAccessPullProjectCommit(project, pullTarget, pullIndex, sha, c) if !canSeeInsideProject(project, c) and !pullCommit ( c.notFound("commit not found") return ) - if !validGitObjectId(sha) ( + if !isWorktree and !validGitObjectId(sha) ( c.notFound("commit not found") return ) - if !projectRecordsCommit(project, sha) and !pullCommit ( + if !isWorktree and !projectRecordsCommit(project, sha) and !pullCommit ( + c.notFound("commit not found") + return + ) + if isWorktree and projectGitHead(project) == "" ( c.notFound("commit not found") return ) @@ -231,7 +335,12 @@ def gitInspectionResponse(*serve.Context c, object project, string sha, string o c.notFound("workspace not found") return ) - string resultJson = if operation == "file" then gitinspection.Inspect(workspacePath, sha, operation, requestedPath, allowedHeads) else cachedWorkspaceGitInspectionJSON(project, workspacePath, sha, operation, requestedPath, allowedHeads) + string resultJson = "" + if operation == "file" ( + resultJson = if isWorktree then gitinspection.InspectWorktree(workspacePath, projectGitHead(project), operation, requestedPath, allowedHeads) else gitinspection.Inspect(workspacePath, sha, operation, requestedPath, allowedHeads) + ) else ( + resultJson = cachedWorkspaceGitInspectionJSON(project, workspacePath, sha, operation, requestedPath, allowedHeads) + ) string problem = gitinspection.InspectionError(resultJson) if problem == "could not inspect project history" ( c.internalError(problem) diff --git a/src/git/pulls.osl b/src/git/pulls.osl index f9f0ba8..82e62c0 100644 --- a/src/git/pulls.osl +++ b/src/git/pulls.osl @@ -216,7 +216,8 @@ def handleGetCommits(*serve.Context c) ( ) object project = res.project.assert(object) object history = projectVersionHistory(project) - string etag = "\"project-history-" ++ projectWorkspaceFingerprint(project) ++ "-" ++ projectGitHead(project) ++ "-" ++ json.stringify(history).hashSHA256() ++ "\"" + boolean canSeeInside = canSeeInsideProject(project, c) + string etag = "\"project-history-v2-" ++ projectWorkspaceFingerprint(project) ++ "-" ++ projectGitHead(project) ++ "-" ++ (project.edited ?? 0).toStr() ++ "-" ++ (if canSeeInside then "1" else "0") ++ "-" ++ json.stringify(history).hashSHA256() ++ "\"" c.setHeader("ETag", etag) c.setHeader("Vary", "Authorization, Cookie") c.setHeader("Cache-Control", "private, no-store") @@ -225,14 +226,21 @@ def handleGetCommits(*serve.Context c) ( c.status(304) return ) - c.json(200, { + object response = { ok: true, head: projectGitHead(project), branch: history.branch, commits: history.commits, graph: history.graph, virtual: history.virtual - }) + } + if canSeeInside and projectHasWorkspace(project) ( + object uncommitted = projectUncommittedSummary(project) + if uncommitted.len > 0 ( + response.uncommitted = uncommitted + ) + ) + c.json(200, response) ) def projectBranchHead(object project, string branch) string ( diff --git a/src/git/pulls.test.osl b/src/git/pulls.test.osl index df84c4d..83a7025 100644 --- a/src/git/pulls.test.osl +++ b/src/git/pulls.test.osl @@ -101,6 +101,9 @@ def projectVersionHistory(object project) object ( def projectWorkspaceFingerprint(object project) string ( return "" ) +def projectUncommittedSummary(object project) object ( + return {} +) def requestJSON(*serve.Context c) object ( return {} ) diff --git a/src/git/remix-divergence.osl b/src/git/remix-divergence.osl new file mode 100644 index 0000000..c08ff6c --- /dev/null +++ b/src/git/remix-divergence.osl @@ -0,0 +1,100 @@ +export { + remixGraphDivergence +} + +def collectGraphParents(object graph, object parentsBySha) ( + if !graph.contains("nodes") or graph.nodes == null ( + return + ) + array nodes = graph.nodes.assertElse(array, []) + for i nodes.len ( + object node = nodes[i].assertElse(object, {}) + string sha = (node.sha ?? "").toStr() + if sha != "" and !parentsBySha.contains(sha) ( + array parents = [] + if node.contains("parents") and node.parents != null ( + parents = node.parents.assertElse(array, []) + ) + parentsBySha[sha] = parents + ) + ) +) + +def walkGraphReachable(string head, object parentsBySha) object ( + object visited = {} + array queue = [head] + number index = 1 + while index <= queue.len ( + string sha = queue[index].toStr() + index += 1 + if sha != "" and !visited.contains(sha) ( + visited[sha] = true + if parentsBySha.contains(sha) ( + array parents = parentsBySha[sha].assertElse(array, []) + for j parents.len ( + string parentSha = parents[j].toStr() + if parentSha != "" and !visited.contains(parentSha) ( + queue.append(parentSha) + ) + ) + ) + ) + ) + return visited +) + +def diffCountAndTruncation(object sourceSet, object otherSet, object parentsBySha) object ( + array keys = sourceSet.getKeys() + number count = 0 + boolean truncated = false + boolean shared = false + for i keys.len ( + string sha = keys[i].toStr() + if otherSet.contains(sha) ( + shared = true + ) else ( + count += 1 + if !parentsBySha.contains(sha) ( + truncated = true + ) else ( + array parents = parentsBySha[sha].assertElse(array, []) + for j parents.len ( + string parentSha = parents[j].toStr() + if parentSha != "" and !parentsBySha.contains(parentSha) ( + truncated = true + ) + ) + ) + ) + ) + return {count: count, truncated: truncated, shared: shared} +) + +def remixGraphDivergence(string childHead, object childGraph, string parentHead, object parentGraph) object ( + if childHead == "" or parentHead == "" ( + return {resolved: true, known: false} + ) + if childHead == parentHead ( + return {resolved: true, known: true, ahead: 0, behind: 0} + ) + object parentsBySha = {} + collectGraphParents(childGraph, parentsBySha) + collectGraphParents(parentGraph, parentsBySha) + if !parentsBySha.contains(childHead) or !parentsBySha.contains(parentHead) ( + return {resolved: false, known: false} + ) + object childSet = walkGraphReachable(childHead, parentsBySha) + object parentSet = walkGraphReachable(parentHead, parentsBySha) + object aheadInfo = diffCountAndTruncation(childSet, parentSet, parentsBySha) + object behindInfo = diffCountAndTruncation(parentSet, childSet, parentsBySha) + if aheadInfo.shared == true ( + if aheadInfo.truncated == true or behindInfo.truncated == true ( + return {resolved: false, known: false} + ) + return {resolved: true, known: true, ahead: aheadInfo.count, behind: behindInfo.count} + ) + if aheadInfo.truncated == true or behindInfo.truncated == true ( + return {resolved: false, known: false} + ) + return {resolved: true, known: false} +) diff --git a/src/git/remix-divergence.test.osl b/src/git/remix-divergence.test.osl new file mode 100644 index 0000000..99565b5 --- /dev/null +++ b/src/git/remix-divergence.test.osl @@ -0,0 +1,75 @@ +import "std:testing" +import "./remix-divergence.osl" + +object emptyGraph = {nodes: []} +object missingHead = remixGraphDivergence("", emptyGraph, "a", emptyGraph) +testing.assert(missingHead.resolved == true and missingHead.known == false, "missing head resolves as unknown") + +object sameHead = remixGraphDivergence("a", emptyGraph, "a", emptyGraph) +testing.assert(sameHead.resolved == true and sameHead.known == true and sameHead.ahead == 0 and sameHead.behind == 0, "equal heads are 0 ahead and 0 behind") + +object parentGraph = { + nodes: [ + {sha: "p2", parents: ["p1"]}, + {sha: "p1", parents: ["base"]}, + {sha: "base", parents: []} + ] +} +object childGraph = { + nodes: [ + {sha: "c3", parents: ["c2"]}, + {sha: "c2", parents: ["c1"]}, + {sha: "c1", parents: ["base"]}, + {sha: "base", parents: []} + ] +} +object diverged = remixGraphDivergence("c3", childGraph, "p2", parentGraph) +testing.assert(diverged.resolved == true and diverged.known == true and diverged.ahead == 3 and diverged.behind == 2, "diverged remix counts commits ahead and behind") + +object aheadOnly = remixGraphDivergence("c2", childGraph, "base", parentGraph) +testing.assert(aheadOnly.resolved == true and aheadOnly.known == true and aheadOnly.ahead == 2 and aheadOnly.behind == 0, "ahead-only remix counts 0 behind") + +object behindOnly = remixGraphDivergence("base", childGraph, "p2", parentGraph) +testing.assert(behindOnly.resolved == true and behindOnly.known == true and behindOnly.ahead == 0 and behindOnly.behind == 2, "behind-only remix counts 0 ahead") + +object mergedChildGraph = { + nodes: [ + {sha: "m1", parents: ["c1", "p1"]}, + {sha: "c1", parents: ["base"]}, + {sha: "p1", parents: ["base"]}, + {sha: "base", parents: []} + ] +} +object afterMerge = remixGraphDivergence("m1", mergedChildGraph, "p2", parentGraph) +testing.assert(afterMerge.resolved == true and afterMerge.known == true and afterMerge.ahead == 2 and afterMerge.behind == 1, "merged upstream commit is excluded from behind count") + +object truncatedParent = { + nodes: [ + {sha: "p2", parents: ["p1"]}, + {sha: "p1", parents: ["missing-old"]} + ] +} +object truncatedChild = { + nodes: [ + {sha: "c1", parents: ["p1"]}, + {sha: "p1", parents: ["missing-old"]} + ] +} +object sharedTruncated = remixGraphDivergence("c1", truncatedChild, "p2", truncatedParent) +testing.assert(sharedTruncated.resolved == true and sharedTruncated.known == true and sharedTruncated.ahead == 1 and sharedTruncated.behind == 1, "shared truncation below common ancestor still resolves accurately") + +object disjointTruncatedChild = { + nodes: [ + {sha: "c1", parents: ["missing-child-anc"]} + ] +} +object unresolved = remixGraphDivergence("c1", disjointTruncatedChild, "p2", truncatedParent) +testing.assert(unresolved.resolved == false, "truncated history without shared ancestor falls back to archive check") + +object unrelatedGraph = { + nodes: [ + {sha: "u1", parents: []} + ] +} +object unrelated = remixGraphDivergence("u1", unrelatedGraph, "p2", parentGraph) +testing.assert(unrelated.resolved == true and unrelated.known == false, "complete unrelated histories resolve as unknown without archive check") diff --git a/src/projects/project-response.osl b/src/projects/project-response.osl index e2ca8af..7f8d7df 100644 --- a/src/projects/project-response.osl +++ b/src/projects/project-response.osl @@ -97,6 +97,43 @@ def projectCommentDonationAnalytics(object project) object ( } ) +def projectRemixFrom(object project, *serve.Context c) object ( + string parentId = "" + if project.contains("remixParent") and project.remixParent != null ( + parentId = project.remixParent.toStr() + ) + if parentId == "" or parentId == project.id.toStr() ( + return {} + ) + auto parentRes = loadProject(parentId) + if !parentRes.ok ( + return {} + ) + object parent = parentRes.project.assert(object) + if !canViewProject(parent, c) ( + return {} + ) + object parentVanity = vanityState(parent) + string parentBranch = "main" + if parent.contains("gitBranch") and parent.gitBranch != null and parent.gitBranch.toStr() != "" ( + parentBranch = parent.gitBranch.toStr() + ) + object info = { + id: parent.id, + owner: parent.owner, + title: parent.title, + vanitySlug: parentVanity.slug.toStr(), + branch: parentBranch, + head: projectGitHead(parent) + } + object divergence = projectRemixDivergence(project, parent) + if (divergence.known ?? false) == true ( + info.ahead = (divergence.ahead ?? 0).toNum() + info.behind = (divergence.behind ?? 0).toNum() + ) + return info +) + def projectResponse(object project,*serve.Context c) object ( array loves = project.loves.assert(array) array broken = [] @@ -234,6 +271,10 @@ def projectResponse(object project,*serve.Context c) object ( if project.contains("remixBaseCommit") and project.remixBaseCommit != null ( response.remixBaseCommit = project.remixBaseCommit ) + object remixFrom = projectRemixFrom(project, c) + if remixFrom.len > 0 ( + response.remixFrom = remixFrom + ) response.needsHistoryBootstrap = isOwner and hasContent and response.workspaceUrl.toStr() == "" object searchEntry = indexEntryFromProject(project) response.collaboration = { From 4b0e6d9806a3a6d7d1a6a6a806f99bd8d0fa4a4a Mon Sep 17 00:00:00 2001 From: Mistium <92952823+Mistium@users.noreply.github.com> Date: Sat, 21 Nov 2026 10:19:09 +0000 Subject: [PATCH 2/2] Rename local result variables in git inspection to avoid OSL type collision --- src/git/git-inspection.osl | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/git/git-inspection.osl b/src/git/git-inspection.osl index 36ea0f6..8e2f1e9 100644 --- a/src/git/git-inspection.osl +++ b/src/git/git-inspection.osl @@ -209,8 +209,8 @@ def projectUncommittedSummary(object project) object ( if parsed.isErr() ( return {} ) - object result = parsed.unwrap().assert(object) - if (result.ok ?? false) != true or (result.dirty ?? false) != true ( + object payload = parsed.unwrap().assert(object) + if (payload.ok ?? false) != true or (payload.dirty ?? false) != true ( return {} ) string branch = "main" @@ -222,8 +222,8 @@ def projectUncommittedSummary(object project) object ( branch: branch, date: project.edited ?? 0, author: project.owner ?? "", - files: (result.changedFiles ?? 0).toNum(), - summary: (result.summary ?? {}).assertElse(object, {}) + files: (payload.changedFiles ?? 0).toNum(), + summary: (payload.summary ?? {}).assertElse(object, {}) } ) @@ -264,14 +264,14 @@ def cachedRemixArchiveDivergence(object child, object parent) object ( if parsed.isErr() ( return {known: false} ) - object result = parsed.unwrap().assert(object) - if (result.ok ?? false) != true or (result.commonAncestor ?? false) != true ( + object payload = parsed.unwrap().assert(object) + if (payload.ok ?? false) != true or (payload.commonAncestor ?? false) != true ( return {known: false} ) return { known: true, - ahead: (result.ahead ?? 0).toNum(), - behind: (result.behind ?? 0).toNum() + ahead: (payload.ahead ?? 0).toNum(), + behind: (payload.behind ?? 0).toNum() } )