2 files changed, +69, -2
ea26d57Hardlink release files into the site
src/main.rs
+13, -0
src/repo_info.rs
+56, -2
+13, -0
@@ -67,6 +67,13 @@ fn write_file(path: &Path, contents: &str) -> Result<()> {
67 67 .with_context(|| path.display().to_string())
68 68 }
69 69
70+fn link_file(from: &Path, to: &Path) -> Result<()> {
71+ let dir = to.parent().unwrap_or(Path::new(""));
72+ fs::create_dir_all(dir)
73+ .and_then(|()| fs::hard_link(from, to).or_else(|_| fs::copy(from, to).map(drop)))
74+ .with_context(|| to.display().to_string())
75+}
76+
70 77 fn last_changes<'a>(
71 78 commits: &'a [CommitInfo],
72 79 changes: &HashMap<String, Vec<String>>,
@@ -164,6 +171,12 @@ fn write_repo(repo: &RepoInfo, repo_dir: &Path) -> Result<()> {
164171 {
165172 return Err(e).with_context(|| repo_dir.display().to_string());
166173 }
174+ for release in &repo.releases {
175+ let release_dir = repo_dir.join("releases").join(&release.tag);
176+ for name in &release.files {
177+ link_file(&release.dir.join(name), &release_dir.join(name))?;
178+ }
179+ }
167180 write_file(&repo_dir.join("branches.html"), &gen_branches(repo))?;
168181 if !repo.tags.is_empty() {
169182 write_file(&repo_dir.join("tags.html"), &gen_tags(repo))?;
+56, -2
@@ -8,9 +8,9 @@ use git2::{
8 8 use std::{
9 9 cmp::Ordering,
10 10 collections::{BTreeMap, HashMap},
11 - fs::read_to_string,
11+ fs::{self, read_to_string},
12 12 io::ErrorKind::NotFound,
13 - path::Path,
13+ path::{Path, PathBuf},
14 14 };
15 15
16 16 const DEFAULT_DESCRIPTION: &str = "[no description]";
@@ -102,6 +102,12 @@ impl RefInfo {
102102 }
103103 }
104104
105+pub struct Release {
106+ pub tag: String,
107+ pub dir: PathBuf,
108+ pub files: Vec<String>,
109+}
110+
105111 pub struct RepoInfo {
106112 pub git: Repository,
107113 pub path: String,
@@ -111,6 +117,7 @@ pub struct RepoInfo {
111117 pub head_dir: String,
112118 pub branches: Vec<RefInfo>,
113119 pub tags: Vec<RefInfo>,
120+ pub releases: Vec<Release>,
114121 pub commit_list: Vec<CommitInfo>,
115122 }
116123
@@ -140,6 +147,7 @@ impl RepoInfo {
140147 }
141148 let branches = handle_branches(&repo)?;
142149 let tags = handle_tags(&repo)?;
150+ let releases = read_releases(path, repo_path, &tags)?;
143151 let commit_list = walk(&repo, &mut repo.revwalk()?, head_id)?;
144152 Ok(Ok(Self {
145153 git: repo,
@@ -150,6 +158,7 @@ impl RepoInfo {
150158 head_dir,
151159 branches,
152160 tags,
161+ releases,
153162 commit_list,
154163 }))
155164 }
@@ -458,6 +467,51 @@ fn to_date(time: git2::Time) -> Option<DateTime<FixedOffset>> {
458467 Some(date.with_timezone(&offset))
459468 }
460469
470+fn read_releases(path: &str, repo_path: &Path, tags: &[RefInfo]) -> Result<Vec<Release>> {
471+ let releases_dir = repo_path.join("releases");
472+ let folders = match fs::read_dir(&releases_dir) {
473+ Ok(folders) => folders,
474+ Err(e) if e.kind() == NotFound => return Ok(Vec::new()),
475+ Err(e) => return Err(e).context("releases"),
476+ };
477+ for folder in folders {
478+ let name = folder.context("releases")?.file_name();
479+ let name = name.to_string_lossy();
480+ let nested = format!("{name}/");
481+ if !tags
482+ .iter()
483+ .any(|t| t.name == name || t.name.starts_with(&nested))
484+ {
485+ eprintln!("{path}: releases/{name}: no such tag, skipped");
486+ }
487+ }
488+ let mut releases = Vec::new();
489+ for tag in tags {
490+ let dir = releases_dir.join(&tag.name);
491+ let context = || format!("releases/{}", tag.name);
492+ let entries = match fs::read_dir(&dir) {
493+ Ok(entries) => entries,
494+ Err(e) if e.kind() == NotFound => continue,
495+ Err(e) => return Err(e).with_context(context),
496+ };
497+ let mut files = Vec::new();
498+ for entry in entries {
499+ let entry = entry.with_context(context)?;
500+ if entry.file_type().with_context(context)?.is_file()
501+ && let Ok(name) = entry.file_name().into_string()
502+ {
503+ files.push(name);
504+ }
505+ }
506+ if !files.is_empty() {
507+ files.sort();
508+ let tag = tag.name.clone();
509+ releases.push(Release { tag, dir, files });
510+ }
511+ }
512+ Ok(releases)
513+}
514+
461515 fn read_optional_file(repo_path: &Path, file_name: &str) -> Result<String> {
462516 let real_path = repo_path.join(file_name);
463517 match read_to_string(real_path) {