public static async Task BuildFileTree(ILocalTempFileLocks tempFiles, ApplicationDbContext database, LfsProject project, ILogger logger, CancellationToken cancellationToken) { var semaphore = tempFiles.GetTempFilePath($"gitFileTrees/{project.Slug}", out string tempPath); await semaphore.WaitAsync(TimeSpan.FromMinutes(10), cancellationToken); try { await GitRunHelpers.EnsureRepoIsCloned(project.CloneUrl, tempPath, true, cancellationToken); try { await GitRunHelpers.Checkout(tempPath, project.BranchToBuildFileTreeFor, true, cancellationToken); } catch (Exception) { // In case the branch refers to a new branch await GitRunHelpers.Fetch(tempPath, true, cancellationToken); await GitRunHelpers.Checkout(tempPath, project.BranchToBuildFileTreeFor, true, cancellationToken); } await GitRunHelpers.Pull(tempPath, true, cancellationToken, true); // Skip if commit has not changed var newCommit = await GitRunHelpers.GetCurrentCommit(tempPath, cancellationToken); if (newCommit == project.FileTreeCommit) { logger.LogDebug("Commit is still the same ({FileTreeCommit}), skipping tree update " + "for {Id}", project.FileTreeCommit, project.Id); return; } logger.LogInformation("New commit {NewCommit} to build file tree from (previous: {FileTreeCommit}) " + "for project {Id}", newCommit, project.FileTreeCommit, project.Id); project.FileTreeCommit = newCommit; // Make sure we don't have any extra files locally await GitRunHelpers.Clean(tempPath, cancellationToken); // And then make sure the DB file tree entries are fine await UpdateFileTreeForProject(database, tempPath, project, cancellationToken); } finally { semaphore.Release(); } project.FileTreeUpdated = DateTime.UtcNow; await database.SaveChangesAsync(cancellationToken); }