| 311 | } |
| 312 | |
| 313 | func (b BuildConfig) Clone(repoUrl, repoRef, archive string, depth int) error { |
| 314 | if fileio.PathExists(b.PortConfig.RepoDir) { |
| 315 | entities, err := os.ReadDir(b.PortConfig.RepoDir) |
| 316 | if err != nil { |
| 317 | return fmt.Errorf("failed to check if empty -> %w", err) |
| 318 | } |
| 319 | if len(entities) > 0 { |
| 320 | if b.PortConfig.Checksum != "" { |
| 321 | // Hard reset to specified git commit hash. |
| 322 | if strings.HasSuffix(repoUrl, ".git") { |
| 323 | if err := git.HardReset(b.PortConfig.RepoDir, b.PortConfig.Checksum); err != nil { |
| 324 | return err |
| 325 | } |
| 326 | return nil |
| 327 | } |
| 328 | |
| 329 | // For the archive repository: extract again, restore from pkgcache again, or download again. |
| 330 | if err := os.RemoveAll(b.PortConfig.RepoDir); err != nil { |
| 331 | return fmt.Errorf("failed to remove local repo src dir -> %w", err) |
| 332 | } |
| 333 | } else { |
| 334 | // Pin to resolved commit if available (from pre-deploy ref resolution). |
| 335 | if commit := refs.GetResolvedCommit(b.PortConfig.nameVersion()); commit != "" { |
| 336 | if err := git.HardReset(b.PortConfig.RepoDir, commit); err != nil { |
| 337 | return err |
| 338 | } |
| 339 | } |
| 340 | return nil |
| 341 | } |
| 342 | } else { |
| 343 | // Remove empty folder to let download or clone again. |
| 344 | if err := os.RemoveAll(b.PortConfig.RepoDir); err != nil { |
| 345 | return fmt.Errorf("failed to remove empty src dir -> %w", err) |
| 346 | } |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | // p.PortConfig.nameVersion() may contains "-dev", here must use a clear name@version. |
| 351 | nameVersion := fmt.Sprintf("%s@%s", b.PortConfig.LibName, b.PortConfig.LibVersion) |
| 352 | |
| 353 | // Try to fetch repo from pkgcache. |
| 354 | var repoCache context.RepoCache |
| 355 | pkgCacheConfig := b.Ctx.PkgCacheConfig() |
| 356 | if pkgCacheConfig != nil { |
| 357 | repoCache = pkgCacheConfig.GetRepoCache() |
| 358 | } |
| 359 | |
| 360 | // Restore repo from pkgcache condition: |
| 361 | // - not offline. |
| 362 | // - not virtual port. |
| 363 | // - pkgcache is configured. |
| 364 | // - current port is one of third-party ports. |
| 365 | // - checksum is not empty. |
| 366 | if repoUrl != "_" && pkgCacheConfig != nil && repoCache != nil { |
| 367 | if fromWhere, err := repoCache.Restore(nameVersion, repoUrl, b.PortConfig.RepoDir, b.PortConfig.Checksum); err != nil { |
| 368 | color.PrintWarning("failed to restore %s from repo cache, because of %s", nameVersion, err) |
| 369 | color.PrintHint("Location: %s\n", fromWhere) |
| 370 | } else if fromWhere != "" { |