diff --git a/pkg/dib/generate_dag.go b/pkg/dib/generate_dag.go index 49bf7585..c2644ca3 100644 --- a/pkg/dib/generate_dag.go +++ b/pkg/dib/generate_dag.go @@ -28,6 +28,15 @@ const ( // GenerateDAG discovers and parses all Dockerfiles at a given path, // and generates the DAG representing the relationships between images. func GenerateDAG(buildPath, registryPrefix, customHashListPath string, buildArgs map[string]string) (*dag.DAG, error) { + graph, err := buildGraph(buildPath, registryPrefix) + if err != nil { + return nil, err + } + + return computeHashes(graph, customHashListPath, buildArgs) +} + +func buildGraph(buildPath, registryPrefix string) (*dag.DAG, error) { var allFiles []string cache := make(map[string]*dag.Node) allParents := make(map[string][]dockerfile.ImageRef) @@ -121,19 +130,6 @@ func GenerateDAG(buildPath, registryPrefix, customHashListPath string, buildArgs } } - if err := generateHashes(graph, allFiles, customHashListPath, buildArgs); err != nil { - return nil, err - } - - return graph, nil -} - -func generateHashes(graph *dag.DAG, allFiles []string, customHashListPath string, buildArgs map[string]string) error { - customHumanizedHashList, err := LoadCustomHashList(customHashListPath) - if err != nil { - return fmt.Errorf("could not load custom humanized hash list: %w", err) - } - fileBelongsTo := map[string]*dag.Node{} for _, file := range allFiles { fileBelongsTo[file] = nil @@ -173,6 +169,15 @@ func generateHashes(graph *dag.DAG, allFiles []string, customHashListPath string } }) + return graph, nil +} + +func computeHashes(graph *dag.DAG, customHashListPath string, buildArgs map[string]string) (*dag.DAG, error) { + customHumanizedHashList, err := LoadCustomHashList(customHashListPath) + if err != nil { + return nil, fmt.Errorf("could not load custom humanized hash list: %w", err) + } + for { needRepass := false err := graph.WalkErr(func(node *dag.Node) error { @@ -221,10 +226,10 @@ func generateHashes(graph *dag.DAG, allFiles []string, customHashListPath string return nil }) if err != nil { - return err + return nil, err } if !needRepass { - return nil + return graph, nil } } } diff --git a/pkg/dib/generate_dag_internal_test.go b/pkg/dib/generate_dag_internal_test.go new file mode 100644 index 00000000..205d8052 --- /dev/null +++ b/pkg/dib/generate_dag_internal_test.go @@ -0,0 +1,9 @@ +package dib + +import "testing" + +func Test_buildGraph(t *testing.T) { + t.Parallel() + + // Implement me. +}