From f892037a0e4279e0f8114c5ebfe654afc931bffc Mon Sep 17 00:00:00 2001 From: Runxi Yu Date: Fri, 6 Mar 2026 11:23:44 +0800 Subject: format/commitgraph: Split files --- format/commitgraph/open.go | 149 -------------------------------------- format/commitgraph/open_chain.go | 133 ++++++++++++++++++++++++++++++++++ format/commitgraph/open_single.go | 32 ++++++++ 3 files changed, 165 insertions(+), 149 deletions(-) create mode 100644 format/commitgraph/open_chain.go create mode 100644 format/commitgraph/open_single.go (limited to 'format/commitgraph') diff --git a/format/commitgraph/open.go b/format/commitgraph/open.go index 7d4cbd5d..5eff8934 100644 --- a/format/commitgraph/open.go +++ b/format/commitgraph/open.go @@ -1,13 +1,9 @@ package commitgraph import ( - "bufio" - "errors" "fmt" "os" - "strings" - "codeberg.org/lindenii/furgit/internal/intconv" "codeberg.org/lindenii/furgit/objectid" ) @@ -26,148 +22,3 @@ func Open(root *os.Root, algo objectid.Algorithm, mode OpenMode) (*Reader, error return nil, fmt.Errorf("format/commitgraph: invalid open mode %d", mode) } } - -func openSingle(root *os.Root, algo objectid.Algorithm) (*Reader, error) { - graph, err := openLayer(root, "info/commit-graph", algo) - if err != nil { - return nil, err - } - - graph.baseCount = 0 - graph.globalFrom = 0 - - hashVersion, err := intconv.Uint32ToUint8(algo.PackHashID()) - if err != nil { - return nil, err - } - - out := &Reader{ - algo: algo, - hashVersion: hashVersion, - layers: []layer{*graph}, - total: graph.numCommits, - } - - return out, nil -} - -func openChain(root *os.Root, algo objectid.Algorithm) (*Reader, error) { - chainPath := "info/commit-graphs/commit-graph-chain" - - file, err := root.Open(chainPath) - if err != nil { - if errors.Is(err, os.ErrNotExist) { - return nil, &ErrMalformed{Path: chainPath, Reason: "missing commit-graph-chain"} - } - - return nil, err - } - - scanner := bufio.NewScanner(file) - hashes := make([]string, 0) - - for scanner.Scan() { - line := strings.TrimSpace(scanner.Text()) - if line == "" { - continue - } - - hashes = append(hashes, line) - } - - scanErr := scanner.Err() - closeErr := file.Close() - - if scanErr != nil { - return nil, scanErr - } - - if closeErr != nil { - return nil, closeErr - } - - if len(hashes) == 0 { - return nil, &ErrMalformed{Path: chainPath, Reason: "empty chain"} - } - - layers := make([]layer, 0, len(hashes)) - - var total uint32 - - hashVersion, err := intconv.Uint32ToUint8(algo.PackHashID()) - if err != nil { - return nil, err - } - - for i, hashHex := range hashes { - expectedBaseCount, err := intconv.IntToUint32(i) - if err != nil { - closeLayers(layers) - - return nil, err - } - - if len(hashHex) != algo.HexLen() { - closeLayers(layers) - - return nil, &ErrMalformed{ - Path: chainPath, - Reason: fmt.Sprintf("invalid graph hash length at line %d", i+1), - } - } - - relPath := fmt.Sprintf("info/commit-graphs/graph-%s.graph", hashHex) - - loaded, loadErr := openLayer(root, relPath, algo) - if loadErr != nil { - closeLayers(layers) - - return nil, loadErr - } - - if loaded.baseCount != expectedBaseCount { - _ = loaded.close() - - closeLayers(layers) - - return nil, &ErrMalformed{ - Path: relPath, - Reason: fmt.Sprintf("BASE count %d does not match chain depth %d", loaded.baseCount, i), - } - } - - validateErr := validateChainBaseHashes(algo, hashes, i, loaded) - if validateErr != nil { - _ = loaded.close() - - closeLayers(layers) - - return nil, validateErr - } - - loaded.globalFrom = total - loaded.baseCount = expectedBaseCount - - totalNext := total + loaded.numCommits - if totalNext < total { - _ = loaded.close() - - closeLayers(layers) - - return nil, &ErrMalformed{Path: relPath, Reason: "total commit count overflow"} - } - - total = totalNext - - layers = append(layers, *loaded) - } - - out := &Reader{ - algo: algo, - hashVersion: hashVersion, - layers: layers, - total: total, - } - - return out, nil -} diff --git a/format/commitgraph/open_chain.go b/format/commitgraph/open_chain.go new file mode 100644 index 00000000..212a4049 --- /dev/null +++ b/format/commitgraph/open_chain.go @@ -0,0 +1,133 @@ +package commitgraph + +import ( + "bufio" + "errors" + "fmt" + "os" + "strings" + + "codeberg.org/lindenii/furgit/internal/intconv" + "codeberg.org/lindenii/furgit/objectid" +) + +func openChain(root *os.Root, algo objectid.Algorithm) (*Reader, error) { + chainPath := "info/commit-graphs/commit-graph-chain" + + file, err := root.Open(chainPath) + if err != nil { + if errors.Is(err, os.ErrNotExist) { + return nil, &ErrMalformed{Path: chainPath, Reason: "missing commit-graph-chain"} + } + + return nil, err + } + + scanner := bufio.NewScanner(file) + hashes := make([]string, 0) + + for scanner.Scan() { + line := strings.TrimSpace(scanner.Text()) + if line == "" { + continue + } + + hashes = append(hashes, line) + } + + scanErr := scanner.Err() + closeErr := file.Close() + + if scanErr != nil { + return nil, scanErr + } + + if closeErr != nil { + return nil, closeErr + } + + if len(hashes) == 0 { + return nil, &ErrMalformed{Path: chainPath, Reason: "empty chain"} + } + + layers := make([]layer, 0, len(hashes)) + + var total uint32 + + hashVersion, err := intconv.Uint32ToUint8(algo.PackHashID()) + if err != nil { + return nil, err + } + + for i, hashHex := range hashes { + expectedBaseCount, err := intconv.IntToUint32(i) + if err != nil { + closeLayers(layers) + + return nil, err + } + + if len(hashHex) != algo.HexLen() { + closeLayers(layers) + + return nil, &ErrMalformed{ + Path: chainPath, + Reason: fmt.Sprintf("invalid graph hash length at line %d", i+1), + } + } + + relPath := fmt.Sprintf("info/commit-graphs/graph-%s.graph", hashHex) + + loaded, loadErr := openLayer(root, relPath, algo) + if loadErr != nil { + closeLayers(layers) + + return nil, loadErr + } + + if loaded.baseCount != expectedBaseCount { + _ = loaded.close() + + closeLayers(layers) + + return nil, &ErrMalformed{ + Path: relPath, + Reason: fmt.Sprintf("BASE count %d does not match chain depth %d", loaded.baseCount, i), + } + } + + validateErr := validateChainBaseHashes(algo, hashes, i, loaded) + if validateErr != nil { + _ = loaded.close() + + closeLayers(layers) + + return nil, validateErr + } + + loaded.globalFrom = total + loaded.baseCount = expectedBaseCount + + totalNext := total + loaded.numCommits + if totalNext < total { + _ = loaded.close() + + closeLayers(layers) + + return nil, &ErrMalformed{Path: relPath, Reason: "total commit count overflow"} + } + + total = totalNext + + layers = append(layers, *loaded) + } + + out := &Reader{ + algo: algo, + hashVersion: hashVersion, + layers: layers, + total: total, + } + + return out, nil +} diff --git a/format/commitgraph/open_single.go b/format/commitgraph/open_single.go new file mode 100644 index 00000000..255abf39 --- /dev/null +++ b/format/commitgraph/open_single.go @@ -0,0 +1,32 @@ +package commitgraph + +import ( + "os" + + "codeberg.org/lindenii/furgit/internal/intconv" + "codeberg.org/lindenii/furgit/objectid" +) + +func openSingle(root *os.Root, algo objectid.Algorithm) (*Reader, error) { + graph, err := openLayer(root, "info/commit-graph", algo) + if err != nil { + return nil, err + } + + graph.baseCount = 0 + graph.globalFrom = 0 + + hashVersion, err := intconv.Uint32ToUint8(algo.PackHashID()) + if err != nil { + return nil, err + } + + out := &Reader{ + algo: algo, + hashVersion: hashVersion, + layers: []layer{*graph}, + total: graph.numCommits, + } + + return out, nil +} -- cgit v1.3.1-10-gc9f91