Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions native/gitinspection/divergence.go
Original file line number Diff line number Diff line change
@@ -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")
}
63 changes: 63 additions & 0 deletions native/gitinspection/divergence_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
43 changes: 38 additions & 5 deletions native/gitinspection/inspection.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down
Loading
Loading