EYPCPIP762LUUJ6TLBXU4ZFERH3DEXPYJP65DNCSQTIGFHGW35ZQC
package pijul
import (
"fmt"
"sort"
)
// A Block is a vertex in the graph representing the repository,
// with its associated content.
type Block struct {
Change Hash
Start ChangePosition
End ChangePosition
Content []byte
Edges []*Edge
ReverseEdges []*Edge
}
// An Edge is a link between two blocks.
type Edge struct {
Change Hash
Flag EdgeFlags
From *Block
To *Block
}
// A graph represents the current content of the repository.
type Graph struct {
Root *Block
// For each change that has been applied to the graph, the index has a
// slice of the blocks added by that change, sorted by starting position.
Index map[Hash][]*Block
}
func NewGraph() *Graph {
g := &Graph{}
g.Root = &Block{}
g.Index = map[Hash][]*Block{
Hash{}: []*Block{g.Root},
}
return g
}
// blockContaining returns the block containing the specified location,
// or nil if it is not found.
func (g *Graph) blockContaining(change Hash, pos ChangePosition) *Block {
blocks := g.Index[change]
if len(blocks) == 0 {
return nil
}
i := sort.Search(len(blocks), func(i int) bool {
return blocks[i].End >= pos
})
if i == len(blocks) {
return nil
}
if blocks[i].Start <= pos {
return blocks[i]
}
return nil
}
type blockList []*Block
func (b blockList) Len() int { return len(b) }
func (b blockList) Less(i, j int) bool { return b[i].Start < b[j].Start }
func (b blockList) Swap(i, j int) { b[i], b[j] = b[j], b[i] }
func (g *Graph) ApplyChange(h Hash, c Change) error {
if _, ok := g.Index[h]; ok {
return fmt.Errorf("already applied change %v", h)
}
for _, dep := range c.Dependencies {
if _, ok := g.Index[dep]; !ok {
return fmt.Errorf("missing dependency: %v", dep)
}
}
var blocks []*Block
g.Index[h] = blocks
for _, hunk := range c.Changes {
for _, atom := range hunk.atoms() {
switch a := atom.(type) {
case NewVertex:
b := &Block{
Change: h,
Start: a.Start,
End: a.End,
Content: c.Contents[a.Start:a.End],
}
for _, pos := range a.UpContext {
parent := g.blockContaining(coalesceHash(pos.Change, h), pos.Pos)
if parent == nil || parent.End != pos.Pos {
// TODO split blocks.
return fmt.Errorf("not found: block ending at %v:%d", coalesceHash(pos.Change, h), pos.Pos)
}
makeEdge(h, a.Flag, parent, b)
}
for _, pos := range a.DownContext {
child := g.blockContaining(coalesceHash(pos.Change, h), pos.Pos)
if child == nil || child.Start != pos.Pos {
// TODO split blocks.
return fmt.Errorf("not found: block starting at %v:%d", coalesceHash(pos.Change, h), pos.Pos)
}
makeEdge(h, a.Flag, b, child)
}
blocks = append(blocks, b)
if len(blocks) >= 2 && blocks[len(blocks)-2].Start > blocks[len(blocks)-1].Start {
sort.Sort(blockList(blocks))
}
g.Index[h] = blocks
default:
return fmt.Errorf("not implemented yet: %T", a)
}
}
}
return nil
}
func coalesceHash(o OptionalHash, h Hash) Hash {
if o.Valid {
return o.Hash
}
return h
}
func makeEdge(change Hash, flag EdgeFlags, from *Block, to *Block) {
e := &Edge{
Change: change,
Flag: flag,
From: from,
To: to,
}
from.Edges = append(from.Edges, e)
to.ReverseEdges = append(to.ReverseEdges, e)
}
github.com/PuerkitoBio/goquery v1.8.0 h1:PJTF7AmFCFKk1N6V6jmKfrNH9tV5pNE6lZMkG0gta/U=
github.com/PuerkitoBio/goquery v1.8.0/go.mod h1:ypIiRMtY7COPGk+I/YbZLbxsxn9g5ejnI2HSMtkjZvI=
github.com/andybalholm/cascadia v1.3.1 h1:nhxRkql1kdYCc8Snf7D5/D3spOX+dBgjA6u8x004T2c=
github.com/andybalholm/cascadia v1.3.1/go.mod h1:R4bJ1UQfqADjvDa4P6HZHLh/3OxWWEqc0Sk8XGwHqvA=
github.com/boombuler/barcode v1.0.0/go.mod h1:paBWMcWSl3LHKBqUq+rly7CNSldXjb2rDl3JlRe0mD8=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dlclark/regexp2 v1.4.1-0.20201116162257-a2a8dda75c91/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc=
github.com/dlclark/regexp2 v1.7.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
github.com/dlclark/regexp2 v1.8.1 h1:6Lcdwya6GjPUNsBct8Lg/yRPwMhABj269AAzdGSiR+0=
github.com/dlclark/regexp2 v1.8.1/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
github.com/dop251/goja v0.0.0-20211022113120-dc8c55024d06/go.mod h1:R9ET47fwRVRPZnOGvHxxhuZcbrMCuiqOz3Rlrh4KSnk=
github.com/dop251/goja v0.0.0-20230122112309-96b1610dd4f7 h1:kgvzE5wLsLa7XKfV85VZl40QXaMCaeFtHpPwJ8fhotY=
github.com/dop251/goja v0.0.0-20230122112309-96b1610dd4f7/go.mod h1:yRkwfj0CBpOGre+TwBsqPV0IH0Pk73e4PXJOeNDboGs=
github.com/dop251/goja_nodejs v0.0.0-20210225215109-d91c329300e7/go.mod h1:hn7BA7c8pLvoGndExHudxTDKZ84Pyvv+90pbBjbTz0Y=
github.com/dop251/goja_nodejs v0.0.0-20211022123610-8dd9abb0616d/go.mod h1:DngW8aVqWbuLRMHItjPUyqdj+HWPvnQe8V8y1nDpIbM=
github.com/go-sourcemap/sourcemap v2.1.3+incompatible h1:W1iEw64niKVGogNgBN3ePyLFfuisuzeidWPMPWmECqU=
github.com/go-sourcemap/sourcemap v2.1.3+incompatible/go.mod h1:F8jJfvm2KbVjc5NqelyYJmf/v5J0dwNLS2mL4sNA1Jg=
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g=
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k=
github.com/jung-kurt/gofpdf v1.0.0/go.mod h1:7Id9E/uU8ce6rXgefFLlgrJj/GYY22cpxn+r32jIOes=
github.com/jung-kurt/gofpdf v1.16.2 h1:jgbatWHfRlPYiK85qgevsZTHviWXKwB1TTiKdz5PtRc=
github.com/jung-kurt/gofpdf v1.16.2/go.mod h1:1hl7y57EsiPAkLbOwzpzqgx1A30nQCk/YmFV8S2vmK0=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/mazznoer/csscolorparser v0.1.3 h1:vug4zh6loQxAUxfU1DZEu70gTPufDPspamZlHAkKcxE=
github.com/mazznoer/csscolorparser v0.1.3/go.mod h1:Aj22+L/rYN/Y6bj3bYqO3N6g1dtdHtGfQ32xZ5PJQic=
github.com/phpdave11/gofpdi v1.0.7/go.mod h1:vBmVV0Do6hSBHC8uKUQ71JGW+ZGQq74llk/7bXwjDoI=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rivo/uniseg v0.4.3 h1:utMvzDsuh3suAEnhH0RdHmoPbU648o6CvXxTx4SBMOw=
github.com/rivo/uniseg v0.4.3/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
github.com/ruudk/golang-pdf417 v0.0.0-20181029194003-1af4ab5afa58/go.mod h1:6lfFZQK844Gfx8o5WFuvpxWRwnSoipWe/p622j1v06w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
github.com/yuin/goldmark v1.5.3 h1:3HUJmBFbQW9fhQOzMgseU134xfi6hU+mjWywx5Ty+/M=
github.com/yuin/goldmark v1.5.3/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/image v0.0.0-20190910094157-69e4b8554b2a/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/image v0.3.0 h1:HTDXbdK9bjfSWkPzDJIw89W8CAtfFGduujWs33NLLsg=
golang.org/x/image v0.3.0/go.mod h1:fXd9211C/0VTlYuAcOhW8dY/RtEJqODXOWBDpmYBf+A=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20210916014120-12bc252f5db8/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.8.0 h1:Zrh2ngAOFYneWTAIAPethzeaQLuHwhuBkuV6ZiRnUaQ=
golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.6.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.8.0 h1:57P1ETyNKtuIjB4SRd15iJxuhj8Gc416Y78H3qgMh68=
golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 h1:H2TDz8ibqkAF6YGhCdN3jS9O0/s90v0rJh3X/OLHEUk=
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2/go.mod h1:K8+ghG5WaK9qNqU5K3HdILfMLy1f3aNYFI/wnl100a8=
gonum.org/v1/plot v0.12.0 h1:y1ZNmfz/xHuHvtgFe8USZVyykQo5ERXPnspQNVK15Og=
gonum.org/v1/plot v0.12.0/go.mod h1:PgiMf9+3A3PnZdJIciIXmyN1FwdAA6rXELSN761oQkw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
oss.terrastruct.com/d2 v0.3.0 h1:hhrtD2P8mLbUDlxMzyu7sgJ3O7Y59EYpLozDdi+DT3g=
oss.terrastruct.com/d2 v0.3.0/go.mod h1:7aLT1RP98WkuV6PQFtmCvdO83gOk2unuDiqYHFP2Ww8=
oss.terrastruct.com/util-go v0.0.0-20230320053557-dcb5aac7d972 h1:HS7fg2GzGsqRLApsoh7ztaLMvXzxSln/Hfz4wy4tIDA=
oss.terrastruct.com/util-go v0.0.0-20230320053557-dcb5aac7d972/go.mod h1:eMWv0sOtD9T2RUl90DLWfuShZCYp4NrsqNpI8eqO6U4=
github.com/dlclark/regexp2 v1.8.1 // indirect
github.com/dop251/goja v0.0.0-20230122112309-96b1610dd4f7 // indirect
github.com/go-sourcemap/sourcemap v2.1.3+incompatible // indirect
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect
github.com/jung-kurt/gofpdf v1.16.2 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mazznoer/csscolorparser v0.1.3 // indirect
github.com/rivo/uniseg v0.4.3 // indirect
github.com/yuin/goldmark v1.5.3 // indirect
golang.org/x/image v0.3.0 // indirect
golang.org/x/net v0.8.0 // indirect
golang.org/x/text v0.8.0 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
gonum.org/v1/plot v0.12.0 // indirect
oss.terrastruct.com/d2 v0.3.0 // indirect
oss.terrastruct.com/util-go v0.0.0-20230320053557-dcb5aac7d972 // indirect
// The tijo-graph command generates a graph to help visualize the internal
// structure of a pijul repository. It is similar to `pijul debug`, but
// it uses D2 instead of GraphViz. It is primarily intended to help dubug
// the pijul-go package.
package main
import (
"bytes"
"fmt"
"os"
"os/exec"
"strings"
"pijul-go"
"github.com/klausman/hexdump"
)
func printErrorAndExit(description string, err error) {
msg := err.Error()
if err, ok := err.(*exec.ExitError); ok && len(err.Stderr) > 0 {
msg = string(err.Stderr)
}
fmt.Fprintln(os.Stderr, description, msg)
os.Exit(2)
}
func main() {
hashLog, err := exec.Command("pijul", "log", "--hash-only").Output()
if err != nil {
printErrorAndExit("error listing changes:", err)
}
hashes := strings.Fields(string(hashLog))
parsedHashes := make([]pijul.Hash, 0, len(hashes))
for i := len(hashes) - 1; i >= 0; i-- {
h, err := pijul.HashFromBase32(hashes[i])
if err != nil {
printErrorAndExit("", fmt.Errorf("error parsing hash %q: %v", hashes[i], err))
}
parsedHashes = append(parsedHashes, h)
}
pristine, err := loadChanges(parsedHashes)
if err != nil {
printErrorAndExit("error loading changes:", err)
}
graph := makeGraph(pristine, parsedHashes)
os.Stdout.Write(graph)
}
func loadChanges(hashes []pijul.Hash) (*pijul.Graph, error) {
pristine := pijul.NewGraph()
for _, h := range hashes {
hs := h.String()
data, err := os.ReadFile(fmt.Sprintf(".pijul/changes/%s/%s.change", hs[:2], hs[2:]))
if err != nil {
return nil, fmt.Errorf("error reading change %s: %w", hs, err)
}
c, err := pijul.DeserializeChange(data)
if err != nil {
return nil, fmt.Errorf("error deserializing change %s: %w", hs, err)
}
err = pristine.ApplyChange(h, c)
if err != nil {
return nil, fmt.Errorf("error applying change %s: %w", hs, err)
}
}
return pristine, nil
}
func makeGraph(pristine *pijul.Graph, hashes []pijul.Hash) []byte {
b := new(bytes.Buffer)
graphBlock(b, pristine.Root)
for _, h := range hashes {
for _, block := range pristine.Index[h] {
graphBlock(b, block)
}
}
return b.Bytes()
}
func blockID(block *pijul.Block) string {
hash := block.Change.String()
if len(hash) > 8 {
hash = hash[:8]
}
return fmt.Sprintf("%s:%d:%d", hash, block.Start, block.End)
}
func graphBlock(b *bytes.Buffer, block *pijul.Block) {
fmt.Fprintf(b, "%q: {\n", blockID(block))
if bytes.ContainsAny(block.Content, "\x00") {
fmt.Fprintf(b, " content: %q {shape: code}\n", hexdump.Dump(block.Content, 0, 4))
} else if len(block.Content) > 0 {
fmt.Fprintf(b, " content: %q {shape: code}\n", block.Content)
}
fmt.Fprintf(b, "}\n")
for _, e := range block.Edges {
stroke := "black"
switch e.Flag {
case 17:
stroke = "royalblue"
case 1:
stroke = "forestgreen"
}
fmt.Fprintf(b, "%q -> %q {style.stroke: %s}\n", blockID(e.From), blockID(e.To), stroke)
}
}