From 101fe6a0d74543bab7cb9876e3bea1f2d21a592a Mon Sep 17 00:00:00 2001 From: Runxi Yu Date: Sun, 29 Mar 2026 13:29:01 +0000 Subject: cmd/index-pack: Split files --- cmd/index-pack/algorithm.go | 25 ++++++++++ cmd/index-pack/main.go | 108 -------------------------------------------- cmd/index-pack/run.go | 95 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 120 insertions(+), 108 deletions(-) create mode 100644 cmd/index-pack/algorithm.go create mode 100644 cmd/index-pack/run.go diff --git a/cmd/index-pack/algorithm.go b/cmd/index-pack/algorithm.go new file mode 100644 index 00000000..fc57991a --- /dev/null +++ b/cmd/index-pack/algorithm.go @@ -0,0 +1,25 @@ +package main + +import ( + "fmt" + + objectid "codeberg.org/lindenii/furgit/object/id" + "codeberg.org/lindenii/furgit/repository" +) + +func resolveAlgorithm(repo *repository.Repository, objectFormat string) (objectid.Algorithm, error) { + if objectFormat != "" { + algo, ok := objectid.ParseAlgorithm(objectFormat) + if !ok { + return objectid.AlgorithmUnknown, fmt.Errorf("invalid object format %q", objectFormat) + } + + return algo, nil + } + + if repo != nil { + return repo.Algorithm(), nil + } + + return objectid.AlgorithmSHA1, nil +} diff --git a/cmd/index-pack/main.go b/cmd/index-pack/main.go index fb6e9062..2034f83d 100644 --- a/cmd/index-pack/main.go +++ b/cmd/index-pack/main.go @@ -3,15 +3,7 @@ package main import ( "flag" - "fmt" "log" - "os" - "path/filepath" - - "codeberg.org/lindenii/furgit/format/packfile/ingest" - objectid "codeberg.org/lindenii/furgit/object/id" - objectstore "codeberg.org/lindenii/furgit/object/store" - "codeberg.org/lindenii/furgit/repository" ) func main() { @@ -32,103 +24,3 @@ func main() { log.Fatalf("run: %v", err) } } - -func run(repoPath, destinationPath, objectFormat string, fixThin, writeRev bool) error { - var ( - algo objectid.Algorithm - base objectstore.ReadingStore - repo *repository.Repository - ) - - if repoPath != "" { - repoRoot, err := os.OpenRoot(repoPath) - if err != nil { - return fmt.Errorf("open repo root: %w", err) - } - - defer func() { _ = repoRoot.Close() }() - - repo, err = repository.Open(repoRoot) - if err != nil { - return fmt.Errorf("open repository: %w", err) - } - - defer func() { _ = repo.Close() }() - } - - algo, err := resolveAlgorithm(repo, objectFormat) - if err != nil { - return err - } - - if fixThin { - if repo == nil { - return fmt.Errorf("fix-thin requires -r ") - } - - if repo.Algorithm() != algo { - return fmt.Errorf("algorithm mismatch: repo=%s flag=%s", repo.Algorithm(), algo) - } - - base = repo.Objects() - } - - absDestination, err := filepath.Abs(destinationPath) - if err != nil { - return fmt.Errorf("absolute destination path: %w", err) - } - - destinationRoot, err := os.OpenRoot(absDestination) - if err != nil { - return fmt.Errorf("open destination root: %w", err) - } - - defer func() { _ = destinationRoot.Close() }() - - pending, err := ingest.Ingest(os.Stdin, algo, ingest.Options{ - FixThin: fixThin, - WriteRev: writeRev, - Base: base, - RequireTrailingEOF: true, - }) - if err != nil { - return err - } - - if pending.Header().ObjectCount == 0 { - discarded, err := pending.Discard() - if err != nil { - return err - } - - _, _ = fmt.Fprintf(os.Stdout, "pack\t%s\n", discarded.PackHash.String()) - - return nil - } - - result, err := pending.Continue(destinationRoot) - if err != nil { - return err - } - - _, _ = fmt.Fprintf(os.Stdout, "pack\t%s\n", result.PackHash.String()) - - return nil -} - -func resolveAlgorithm(repo *repository.Repository, objectFormat string) (objectid.Algorithm, error) { - if objectFormat != "" { - algo, ok := objectid.ParseAlgorithm(objectFormat) - if !ok { - return objectid.AlgorithmUnknown, fmt.Errorf("invalid object format %q", objectFormat) - } - - return algo, nil - } - - if repo != nil { - return repo.Algorithm(), nil - } - - return objectid.AlgorithmSHA1, nil -} diff --git a/cmd/index-pack/run.go b/cmd/index-pack/run.go new file mode 100644 index 00000000..80e1db25 --- /dev/null +++ b/cmd/index-pack/run.go @@ -0,0 +1,95 @@ +package main + +import ( + "fmt" + "os" + "path/filepath" + + "codeberg.org/lindenii/furgit/format/packfile/ingest" + objectid "codeberg.org/lindenii/furgit/object/id" + objectstore "codeberg.org/lindenii/furgit/object/store" + "codeberg.org/lindenii/furgit/repository" +) + +func run(repoPath, destinationPath, objectFormat string, fixThin, writeRev bool) error { + var ( + algo objectid.Algorithm + base objectstore.ReadingStore + repo *repository.Repository + ) + + if repoPath != "" { + repoRoot, err := os.OpenRoot(repoPath) + if err != nil { + return fmt.Errorf("open repo root: %w", err) + } + + defer func() { _ = repoRoot.Close() }() + + repo, err = repository.Open(repoRoot) + if err != nil { + return fmt.Errorf("open repository: %w", err) + } + + defer func() { _ = repo.Close() }() + } + + algo, err := resolveAlgorithm(repo, objectFormat) + if err != nil { + return err + } + + if fixThin { + if repo == nil { + return fmt.Errorf("fix-thin requires -r ") + } + + if repo.Algorithm() != algo { + return fmt.Errorf("algorithm mismatch: repo=%s flag=%s", repo.Algorithm(), algo) + } + + base = repo.Objects() + } + + absDestination, err := filepath.Abs(destinationPath) + if err != nil { + return fmt.Errorf("absolute destination path: %w", err) + } + + destinationRoot, err := os.OpenRoot(absDestination) + if err != nil { + return fmt.Errorf("open destination root: %w", err) + } + + defer func() { _ = destinationRoot.Close() }() + + pending, err := ingest.Ingest(os.Stdin, algo, ingest.Options{ + FixThin: fixThin, + WriteRev: writeRev, + Base: base, + RequireTrailingEOF: true, + }) + if err != nil { + return err + } + + if pending.Header().ObjectCount == 0 { + discarded, err := pending.Discard() + if err != nil { + return err + } + + _, _ = fmt.Fprintf(os.Stdout, "pack\t%s\n", discarded.PackHash.String()) + + return nil + } + + result, err := pending.Continue(destinationRoot) + if err != nil { + return err + } + + _, _ = fmt.Fprintf(os.Stdout, "pack\t%s\n", result.PackHash.String()) + + return nil +} -- cgit v1.3.1-10-gc9f91