github.com/google/syzkaller@v0.0.0-20240517125934-c0f1611a36d6/pkg/covermerger/repos.go (about)

     1  // Copyright 2024 syzkaller project authors. All rights reserved.
     2  // Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
     3  
     4  package covermerger
     5  
     6  import (
     7  	"fmt"
     8  	"os"
     9  
    10  	"github.com/google/syzkaller/pkg/vcs"
    11  	"github.com/google/syzkaller/sys/targets"
    12  )
    13  
    14  func getFileVersions(c *Config, targetFilePath string, rbcs []RepoBranchCommit,
    15  ) (map[RepoBranchCommit]string, error) {
    16  	reposPath := c.Workdir + "/repos"
    17  	for _, rbc := range rbcs {
    18  		commitPath := reposPath + "/" + rbc.Commit
    19  		if _, err := os.Stat(commitPath); err == nil || c.skipRepoClone {
    20  			continue
    21  		}
    22  		repo, err := vcs.NewRepo(targets.Linux, "none", commitPath)
    23  		if err != nil {
    24  			return nil, fmt.Errorf("failed to create new repo at %s: %w", commitPath, err)
    25  		}
    26  		if _, err = repo.CheckoutCommit(rbc.Repo, rbc.Commit); err != nil {
    27  			return nil, fmt.Errorf("failed to get commit %s from repo %s to folder %s: %w",
    28  				rbc.Commit, rbc.Repo, commitPath, err)
    29  		}
    30  	}
    31  
    32  	res := make(map[RepoBranchCommit]string)
    33  	for _, rbc := range rbcs {
    34  		filePath := reposPath + "/" + rbc.Commit + "/" + targetFilePath
    35  		fileBytes, err := os.ReadFile(filePath)
    36  		// It is ok if some file doesn't exist. It means we have repo FS diff.
    37  		if err == nil {
    38  			res[rbc] = string(fileBytes)
    39  		}
    40  	}
    41  
    42  	return res, nil
    43  }