public async Task SyncGitLocalRepositoryDirToWebRootDirAsync(string gitLocalRootDir, string webLocalRootDir, CancellationToken cancel = default) { string gitCommitId = await Lfs.GetCurrentGitCommitIdAsync(gitLocalRootDir, cancel); var param = new CopyDirectoryParams( CopyDirectoryFlags.AsyncCopy | CopyDirectoryFlags.Overwrite | CopyDirectoryFlags.Recursive | CopyDirectoryFlags.DeleteNotExistDirs | CopyDirectoryFlags.DeleteNotExistFiles | CopyDirectoryFlags.SilenceSuccessfulReport, FileFlags.WriteOnlyIfChanged, new FileMetadataCopier(FileMetadataCopyMode.TimeAll), new FileMetadataCopier(FileMetadataCopyMode.TimeAll), determineToCopyCallback: (d, e) => { if (e.IsFile && e.Name._IsSamei(Consts.FileNames.GitLabMainte_PublishFileName)) { return(false); } if (e.IsDirectory && e.Name._IsSamei(".git")) { return(false); } return(true); }, determineToDeleteCallback: (e) => { if (e.IsFile && e.Name._IsSamei(Consts.FileNames.GitLabMainte_CommitIdFileName)) { return(false); } if (e.IsFile && e.Name._IsSamei(Consts.FileNames.LogBrowserSecureJson)) { return(false); } if (e.IsDirectory && e.Name._IsSamei(Consts.FileNames.LogBrowserAccessLogDirName)) { return(false); } return(true); } ); string publishPath = gitLocalRootDir._CombinePath(Consts.FileNames.GitLabMainte_PublishFileName); await Lfs.CopyDirAsync(gitLocalRootDir, webLocalRootDir, param : param, cancel : cancel); await Lfs.WriteStringToFileAsync(webLocalRootDir._CombinePath(Consts.FileNames.GitLabMainte_CommitIdFileName), gitCommitId + "\n", FileFlags.WriteOnlyIfChanged, cancel : cancel); PublishConfigData?config = await Lfs.ReadJsonFromFileAsync <PublishConfigData>(publishPath, cancel : cancel, nullIfError : true); if (config == null) { if (await Lfs.IsFileExistsAsync(publishPath)) { config = new PublishConfigData(); } } string secureJsonPath = webLocalRootDir._CombinePath(Consts.FileNames.LogBrowserSecureJson); if (config == null) { try { await Lfs.DeleteFileAsync(secureJsonPath, cancel : cancel); } catch (Exception ex) { ex._Error(); } } else { bool requireAuth = config.Username._IsFilled() && config.Password._IsFilled(); LogBrowserSecureJson sj = new LogBrowserSecureJson(); sj.AuthRequired = requireAuth; if (requireAuth) { sj.AuthDatabase = new KeyValueList <string, string>(); sj.AuthDatabase.Add(config.Username, config.Password); sj.AuthSubDirName = "auth"; } sj.DisableAccessLog = false; sj.AllowZipDownload = true; await Lfs.WriteJsonToFileAsync(secureJsonPath, sj, FileFlags.WriteOnlyIfChanged, cancel : cancel); } }
// Git リポジトリの自動ダウンロード async Task Loop2_MainteUsersAsync(CancellationToken cancel = default) { long lastHookTick = -1; while (cancel.IsCancellationRequested == false) { try { await Lfs.CreateDirectoryAsync(this.Settings.GitMirrorDataRootDir); await Lfs.CreateDirectoryAsync(this.Settings.GitWebDataRootDir); } catch { } try { // Git リポジトリを列挙 var projects = await this.GitLabClient.EnumProjectsAsync(cancel); ConcurrentHashSet <string> dirNames = new ConcurrentHashSet <string>(StrCmpi); // メモ: last_activity_at の値を信用してはならない。これは GitLab がキャッシュしているので、なかなか更新されない。 var targetProjects = projects.Where(p => p.empty_repo == false && p.path_with_namespace._IsFilled() && p.default_branch._IsFilled() && p.visibility._IsDiffi("private")).OrderByDescending(x => x.last_activity_at).ThenBy(x => x.path_with_namespace, StrCmpi); await TaskUtil.ForEachAsync(8, targetProjects, async (proj, index, cancel) => { cancel.ThrowIfCancellationRequested(); try { string dirname = proj.GenerateDirName(); dirNames.Add(dirname); string gitRoot = this.Settings.GitMirrorDataRootDir._CombinePath(dirname); string webRoot = this.Settings.GitWebDataRootDir._CombinePath(dirname); await this.GitLabClient.GitPullFromRepositoryAsync(proj.path_with_namespace !, gitRoot, proj.default_branch !, cancel); await this.SyncGitLocalRepositoryDirToWebRootDirAsync(gitRoot, webRoot, cancel); } catch (Exception ex) { ex._Error(); } }, cancel); // GitLab 上に存在せず local に存在する gitRoot を列挙して削除する var existingLocalGitDirs = await Lfs.EnumDirectoryAsync(this.Settings.GitMirrorDataRootDir, cancel : cancel); foreach (var d in existingLocalGitDirs.Where(x => x.IsDirectory && dirNames.Contains(x.Name) == false)) { cancel.ThrowIfCancellationRequested(); try { await Lfs.DeleteDirectoryAsync(d.FullPath, true, cancel, true); } catch { } } // GitLab 上に存在せず local に存在する webRoot を列挙して削除する var existingLocalWebDirs = await Lfs.EnumDirectoryAsync(this.Settings.GitWebDataRootDir, cancel : cancel); foreach (var d in existingLocalWebDirs.Where(x => x.IsDirectory && dirNames.Contains(x.Name) == false)) { cancel.ThrowIfCancellationRequested(); try { await Lfs.DeleteFileAsync(d.FullPath._CombinePath(Consts.FileNames.LogBrowserSecureJson), cancel : cancel); } catch { } } } catch (Exception ex) { ex._Error(); } await TaskUtil.AwaitWithPollAsync(this.Settings.ForceRepositoryUpdateIntervalMsecs, 500, () => { long currentHookTick = this.HookFiredTick; if (lastHookTick != currentHookTick) { lastHookTick = currentHookTick; return(true); } return(false); }, cancel, true); } }