go-git
go-git copied to clipboard
Permission error unpacking objects when cloning multiple repositories in parallel
When cloning many repos in parallel, I get the following error
Failed to clone repo {[email protected]:some-repo.git ... }: open some-repo/.git/objects/pack/pack-5679a0a8cedc4f12fb64a8a36584196e350a7c00.idx: permission denied
func CreateWorkspace(cmd *cobra.Command, args []string) {
name, path = args[0], args[1]
logging.Log.Info("Creating workspace")
logging.Log.Debugf("Name: %s", name)
repoURL := getTopLevelUrl(name)
depth := 0
if shallow, _ := cmd.Flags().GetBool("shallow"); shallow {
depth = 1
}
_, err := git.PlainClone(filepath.Join(path, "toplevel"), false, &git.CloneOptions{
URL: repoURL,
ReferenceName: plumbing.ReferenceName(ref),
SingleBranch: true,
Depth: depth,
})
if err != nil {
logging.Log.Errorf("Failed to create workspace: %s", err)
os.Exit(1)
}
repos_json := filepath.Join(path, "toplevel", "repos.json")
if _, err := os.Stat(repos_json); os.IsNotExist(err) {
logging.Log.Errorf("Failed to find repos.json in the workspace")
os.Exit(1)
}
// unmarshal repos.json
file, err := os.ReadFile(repos_json)
if err != nil {
logging.Log.Errorf("Failed to read repos.json: %s", err)
os.Exit(1)
}
var repos RepoJson
err = json.Unmarshal(file, &repos)
if err != nil {
logging.Log.Errorf("Failed to unmarshal repos.json: %s", err)
os.Exit(1)
}
var wg sync.WaitGroup
for _, repo := range repos.Repos {
logging.Log.Infof("Cloning %v", repo.URL)
// Start cloning each repository in a separate goroutine
wg.Add(1)
go cloneRepo(repo, depth, &wg)
if err != nil {
logging.Log.Errorf("Failed to clone repo %s: %s", repo, err)
os.Exit(1)
}
}
wg.Wait()
os.Exit(0)
}
func cloneRepo(repo Repo, depth int, wg *sync.WaitGroup) {
defer wg.Done()
var name string
if repo.Rename == "" {
nameDotGit := filepath.Base(repo.URL)
name = strings.Split(nameDotGit, ".")[0]
} else {
name = repo.Rename
}
_, err := git.PlainClone(filepath.Join(path, repo.Location, name), false, &git.CloneOptions{
URL: repo.URL,
ReferenceName: plumbing.ReferenceName(repo.Reference),
Depth: depth,
})
if err != nil {
logging.Log.Errorf("Failed to clone repo %s: %s", repo, err)
os.Exit(1)
}
return
}
Given the first clone works fine, that would suggest this is not an issue associated with the authentication configuration.
Is it possible this is the result of some filesystem lock? What is the proper way to create many clones concurrently
[UPDATE]: the issue seems to be intermittent, as I was able to successfully clone all of the repos a few times. Failures still pop up frequently though