(nameVersion string, native bool)
| 202 | } |
| 203 | |
| 204 | func (p Port) doGetCommitHash(nameVersion string, native bool) (string, error) { |
| 205 | var port = Port{DevDep: native} |
| 206 | if err := port.Init(p.ctx, nameVersion); err != nil { |
| 207 | return "", err |
| 208 | } |
| 209 | |
| 210 | // Check if repo cloned or downloaded. |
| 211 | if !fileio.PathExists(port.MatchedConfig.PortConfig.RepoDir) { |
| 212 | return "", errors.ErrRepoNotExit |
| 213 | } |
| 214 | |
| 215 | // No commit hash for virtual project. |
| 216 | if port.Package.Url == "_" { |
| 217 | return "", nil |
| 218 | } |
| 219 | |
| 220 | // Get commit hash or archive checksum. |
| 221 | if strings.HasSuffix(port.Package.Url, ".git") { |
| 222 | commit, err := git.GetCommitHash(port.MatchedConfig.PortConfig.RepoDir) |
| 223 | if err != nil { |
| 224 | return "", fmt.Errorf("failed to read git commit hash -> %w", err) |
| 225 | } |
| 226 | return commit, nil |
| 227 | } else { |
| 228 | // Get archive file path. |
| 229 | var filePath string |
| 230 | if after, ok := strings.CutPrefix(port.Package.Url, "file:///"); ok { |
| 231 | filePath = after |
| 232 | } else { |
| 233 | archive := expr.If(port.Package.Archive != "", port.Package.Archive, filepath.Base(port.Package.Url)) |
| 234 | filePath = filepath.Join(p.ctx.Downloads(), archive) |
| 235 | } |
| 236 | |
| 237 | // Auto-download source archive if missing, then continue checksum. |
| 238 | if !fileio.PathExists(filePath) { |
| 239 | if err := os.RemoveAll(port.MatchedConfig.PortConfig.RepoDir); err != nil { |
| 240 | return "", err |
| 241 | } |
| 242 | archive := expr.If(port.Package.Archive != "", port.Package.Archive, filepath.Base(port.Package.Url)) |
| 243 | if err := port.MatchedConfig.Clone( |
| 244 | port.Package.Url, |
| 245 | port.Package.Ref, |
| 246 | archive, |
| 247 | port.Package.Depth, |
| 248 | ); err != nil { |
| 249 | return "", fmt.Errorf("archive file is missing and auto-download failed for %s -> %w", nameVersion, err) |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | // Calculate checksum of archive file. |
| 254 | commit, err := fileio.ComputeSHA256(filePath) |
| 255 | if err != nil { |
| 256 | return "", fmt.Errorf("failed to get checksum of part's archive %s -> %w", nameVersion, err) |
| 257 | } |
| 258 | return commit, nil |
| 259 | } |
| 260 | } |
| 261 |
no test coverage detected