Pair tags in one walk, run no hooks on pushes, suggest each name once
Browse filesThe tags page found each tag's predecessor with a git describe per tag; if the page's time budget ran out part way, the incomplete answer was cached until the tags changed. One first-parent walk of the tagged history now answers for every tag, so it succeeds or caches nothing, and a merge's tag pairs with the release before it on the main line. Pushes ran any hooks in a repository's own folder. Key holders can't write there, but `gitv import` copies whole folders in as root, so every push now runs with core.hooksPath=/dev/null; GITv uses no hooks. The compare form suggested a name twice when a branch and a tag shared it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Author
- Um9i vulgarmessiah@gmail.com
- Date
- 22 Nov 2026, 23:50
- Commit
- caf63194fe9b
- Parent
- 2188fe3
6 files changed +115−33
+1−1
| @@ -76,7 +76,7 @@ Three crates. `gitv` depends only on `gitv-core` and a few small crates, so buil | ||
| - **Pushes.** | ||
| - Pushing to a missing name creates the repository. It is built in a private staging folder and published by rename. | ||
| - A push holds the repository's `flock` shared. The backup takes it exclusive. | ||
| - A push runs with `RECOVERY_CONFIG` (reflogs on every ref, unreachable objects kept 30 days), and appends ref changes to `gitv-push.log`. A repository with a `gitv-protected` marker (`gitv protect`) also gets `PROTECTED_CONFIG` (no non-fast-forward or deleting branch updates; git applies neither to tags). | ||
| - A push runs with `PUSH_CONFIG` (no hooks, reflogs on every ref, unreachable objects kept 30 days), and appends ref changes to `gitv-push.log`. A repository with a `gitv-protected` marker (`gitv protect`) also gets `PROTECTED_CONFIG` (no non-fast-forward or deleting branch updates; git applies neither to tags). | ||
| - **Deletion** moves a repository to `<owner>/.trash/<name>.<unix time>`. `gitv purge`, run daily by a timer, removes entries older than 30 days. After that, `gitv-restore <snapshot> DIR OWNER/NAME` copies one repository out of a backup, and `gitv import` publishes it through the same staging and no-replace rename as `create` (`Contents::CopyOf`). | ||
| - **Logging.** SSH users only ever see generic errors. Details go to syslog, or to `$GITV_LOG` when it is set. | ||
| @@ -17,9 +17,16 @@ pub(crate) fn view_compare(repo: &Repo, q: &Q) -> Option<View> { | ||
| let from = given("from"); | ||
| // Only `from` is needed: comparing against the default branch is the common case. | ||
| let to = given("to").or_else(|| from.as_ref().map(|_| repo.reader.default_ref())); | ||
| let mut refs: Vec<String> = | ||
| repo.reader.heads().iter().chain(repo.reader.tags()).map(|name| lossy(name).into_owned()).collect(); | ||
| refs.dedup(); | ||
| // A branch and a tag may share a name; suggest it once. | ||
| let mut seen = std::collections::HashSet::new(); | ||
| let refs: Vec<String> = repo | ||
| .reader | ||
| .heads() | ||
| .iter() | ||
| .chain(repo.reader.tags()) | ||
| .map(|name| lossy(name).into_owned()) | ||
| .filter(|name| seen.insert(name.clone())) | ||
| .collect(); | ||
| let mut page = templates::Compare { | ||
| title: "Compare revisions".into(), | ||
| signed_in: repo.signed_in, | ||
| @@ -1,5 +1,7 @@ | ||
| //! The tags in a repository. | ||
| use std::collections::HashMap; | ||
| use data_encoding::HEXLOWER; | ||
| use sha2::{Digest, Sha256}; | ||
| @@ -40,8 +42,7 @@ pub(crate) fn view_tags(repo: &Repo) -> Option<View> { | ||
| }); | ||
| } | ||
| // Each tag is compared with the release before it: the nearest tag in its own history. | ||
| for (row, (i, previous)) in rows.iter_mut().zip(previous_tags(repo, &out, &names, &targets).into_iter().enumerate()) | ||
| { | ||
| for (row, (i, previous)) in rows.iter_mut().zip(previous_tags(repo, &out, &targets).into_iter().enumerate()) { | ||
| if let Some(previous) = previous.and_then(|p| names.get(p)) { | ||
| let (from, to) = (repo.reader.tag_ref(previous), repo.reader.tag_ref(names[i])); | ||
| row.compare = Some((repo.compare_url(&from, &to), format!("Changes since {}", lossy(previous)))); | ||
| @@ -58,30 +59,43 @@ pub(crate) fn view_tags(repo: &Repo) -> Option<View> { | ||
| )) | ||
| } | ||
| /// For each tag, the index in `names` of the nearest tag on an earlier commit in its history, | ||
| /// as `git describe` finds it. Creation dates can't order releases: tags made in the same | ||
| /// second, or added late, would pair with the wrong neighbour. | ||
| /// For each tag, the index in `names` of the tag before it: the nearest tagged commit back | ||
| /// along its first parents, so a merge's tag follows its main line rather than the branch it | ||
| /// merged. Creation dates can't order releases: tags made in the same second, or added late, | ||
| /// would pair with the wrong neighbour. | ||
| /// | ||
| /// One git command per tag, so the answer is cached for the whole listing; `listing` (every | ||
| /// tag's name, target and date) names it, so any change to the tags makes a new entry. | ||
| fn previous_tags(repo: &Repo, listing: &[u8], names: &[&[u8]], targets: &[String]) -> Vec<Option<usize>> { | ||
| /// One walk of the tagged history answers for every tag. `--simplify-by-decoration` keeps only | ||
| /// tagged commits and rewrites each one's parents to the nearest tagged ones. The answer is | ||
| /// cached for the whole listing; `listing` (every tag's name, target and date) names it, so any | ||
| /// change to the tags makes a new entry. If git fails, nothing is cached and no links show. | ||
| fn previous_tags(repo: &Repo, listing: &[u8], targets: &[String]) -> Vec<Option<usize>> { | ||
| let key = repo.key("previous-tags", &HEXLOWER.encode(&Sha256::digest(listing)), ""); | ||
| let found = cached(key, || { | ||
| let previous: Vec<String> = targets | ||
| .iter() | ||
| .map(|target| { | ||
| // Only a commit has history; a tag of a tree or blob has no predecessor. | ||
| let commit = repo.reader.commit_object(target)?; | ||
| let name = repo.reader.git_bytes(&["describe", "--tags", "--abbrev=0", &format!("{commit}^")])?; | ||
| let name = name.strip_suffix(b"\n").unwrap_or(&name); | ||
| names.iter().position(|n| *n == name).map(|i| i.to_string()) | ||
| }) | ||
| .map(Option::unwrap_or_default) | ||
| .collect(); | ||
| Some(previous.join(",")) | ||
| let walk = cached(key, || { | ||
| repo.reader.git(&[ | ||
| "log", | ||
| "--first-parent", | ||
| "--simplify-by-decoration", | ||
| "--decorate-refs=refs/tags/", | ||
| "--parents", | ||
| "--format=%H %P", | ||
| "--tags", | ||
| "--", | ||
| ]) | ||
| }); | ||
| let found = found.as_deref().unwrap_or_default(); | ||
| let mut previous: Vec<Option<usize>> = found.split(',').map(|i| i.parse().ok()).collect(); | ||
| previous.resize(names.len(), None); | ||
| previous | ||
| // Each tagged commit and its first rewritten parent, if it has one. | ||
| let parent: HashMap<&str, &str> = walk | ||
| .as_deref() | ||
| .unwrap_or_default() | ||
| .lines() | ||
| .filter_map(|line| { | ||
| let mut ids = line.split(' '); | ||
| Some((ids.next()?, ids.next()?)) | ||
| }) | ||
| .collect(); | ||
| // The first listed tag at a commit (the newest, as the page lists them) stands for it. | ||
| let mut tag_at: HashMap<&str, usize> = HashMap::new(); | ||
| for (i, target) in targets.iter().enumerate() { | ||
| tag_at.entry(target.as_str()).or_insert(i); | ||
| } | ||
| targets.iter().map(|target| parent.get(target.as_str()).and_then(|p| tag_at.get(p).copied())).collect() | ||
| } | ||
| @@ -1210,3 +1210,38 @@ fn tags_download_as_source_archives() { | ||
| // A private repository's archive needs sign-in. | ||
| assert_eq!(s.get_signed_out("/ada/site/archive?ref=refs/tags/v0.1.0").status, 303); | ||
| } | ||
| #[test] | ||
| fn each_tag_compares_with_the_one_before_it_on_its_own_line() { | ||
| let host = Host::new("web-tag-pairs"); | ||
| let work = common::scratch("tag-pairs-work"); | ||
| git(&work, &["init", "-qb", "main"]); | ||
| let commit = |message: &str| git(&work, &["commit", "-q", "--allow-empty", "-m", message]); | ||
| commit("Root"); | ||
| commit("A"); | ||
| git(&work, &["tag", "v1"]); | ||
| commit("B"); | ||
| git(&work, &["checkout", "-qb", "side"]); | ||
| commit("S"); | ||
| git(&work, &["tag", "side-1"]); | ||
| git(&work, &["checkout", "-q", "main"]); | ||
| commit("C"); | ||
| git(&work, &["merge", "-q", "--no-ff", "-m", "Merge side", "side"]); | ||
| git(&work, &["tag", "-a", "v2", "-m", "Two"]); | ||
| commit("D"); | ||
| git(&work, &["tag", "v3"]); | ||
| let out = host.git(&work, &["push", "-q", "ada@x:ada/pairs.git", "main", "--tags"]); | ||
| assert!(out.status.success(), "{}", common::stderr(&out)); | ||
| let server = Server::start(&host.repos); | ||
| let html = server.get("/ada/pairs/tags").text(); | ||
| // Each row, by tag name, and the comparison it offers. | ||
| let offered = |tag: &str| { | ||
| let row = html.split("<li class=\"tag-row\">").find(|row| row.contains(&format!(">{tag}</span>"))).unwrap(); | ||
| row.split(">Changes since ").nth(1).map(|rest| rest.split('<').next().unwrap().to_string()) | ||
| }; | ||
| assert_eq!(offered("v3").as_deref(), Some("v2")); | ||
| assert_eq!(offered("v2").as_deref(), Some("v1"), "a merge's tag follows its main line: {html}"); | ||
| assert_eq!(offered("side-1").as_deref(), Some("v1")); | ||
| assert_eq!(offered("v1"), None, "the first release has nothing before it"); | ||
| assert!(html.contains("href=\"/ada/pairs/compare?from=v1&to=v2\">Changes since v1"), "{html}"); | ||
| } | ||
| @@ -968,7 +968,7 @@ fn serve_git(owner: &str, program: &str, path: &str) -> Result<u8, String> { | ||
| // applies these to refs/heads only; tags stay movable, and the push log records them). | ||
| // Given here, not in the repository's own config, so only `gitv protect` changes it. | ||
| let protection: &[(&str, &str)] = if repo.is_protected() { &PROTECTED_CONFIG } else { &[] }; | ||
| let settings: Vec<_> = RECOVERY_CONFIG.iter().chain(protection).collect(); | ||
| let settings: Vec<_> = PUSH_CONFIG.iter().chain(protection).collect(); | ||
| command.envs(settings.iter().enumerate().flat_map(|(i, (key, value))| { | ||
| [(format!("GIT_CONFIG_KEY_{i}"), *key), (format!("GIT_CONFIG_VALUE_{i}"), *value)] | ||
| })); | ||
| @@ -991,11 +991,15 @@ fn serve_git(owner: &str, program: &str, path: &str) -> Result<u8, String> { | ||
| Ok(status.code().unwrap_or(1) as u8) | ||
| } | ||
| /// Configuration every push runs with, so overwritten or deleted history can be recovered for | ||
| /// [`TRASH_DAYS`]: reflogs on every ref, and unreachable objects kept as long as their entries. | ||
| /// Configuration every push runs with: no hooks, and what lets overwritten or deleted history be | ||
| /// recovered for [`TRASH_DAYS`] (reflogs on every ref, unreachable objects kept as long as | ||
| /// their entries). | ||
| /// The gc a push may trigger runs within the push rather than detached, so it finishes under | ||
| /// the push's lock too. | ||
| const RECOVERY_CONFIG: [(&str, &str); 4] = [ | ||
| const PUSH_CONFIG: [(&str, &str); 5] = [ | ||
| // GITv runs no hooks. A hook in the repository's own folder (one copied in by `gitv | ||
| // import`, say) would run as the git account on every push, so none ever runs. | ||
| ("core.hooksPath", "/dev/null"), | ||
| ("core.logAllRefUpdates", "always"), | ||
| ("gc.reflogExpireUnreachable", "30.days.ago"), | ||
| ("gc.pruneExpire", "30.days.ago"), | ||
| @@ -915,3 +915,25 @@ fn a_protected_repository_refuses_rewrites_and_deletions() { | ||
| assert!(out.status.success(), "{}", stderr(&out)); | ||
| assert_ne!(tip("main"), second); | ||
| } | ||
| #[test] | ||
| fn hooks_in_a_repository_never_run() { | ||
| use std::os::unix::fs::PermissionsExt; | ||
| let host = Host::new("ssh-no-hooks"); | ||
| let dir = work("ssh-no-hooks-work", "main"); | ||
| assert!(host.git(&dir, &["push", "-q", "ada@x:ada/tools.git", "main"]).status.success()); | ||
| // A hook planted in the repository, as one copied in with a restored folder could be. | ||
| let ran = host.dir.join("hook-ran"); | ||
| let hooks = host.repo("ada", "tools").join("hooks"); | ||
| fs::create_dir_all(&hooks).unwrap(); | ||
| for hook in ["pre-receive", "post-receive", "update"] { | ||
| let path = hooks.join(hook); | ||
| fs::write(&path, format!("#!/bin/sh\ntouch '{}'\n", ran.display())).unwrap(); | ||
| fs::set_permissions(&path, fs::Permissions::from_mode(0o755)).unwrap(); | ||
| } | ||
| fs::write(dir.join("hello.txt"), "again\n").unwrap(); | ||
| git(&dir, &["commit", "-qam", "Second"]); | ||
| let out = host.git(&dir, &["push", "-q", "ada@x:ada/tools.git", "main"]); | ||
| assert!(out.status.success(), "{}", stderr(&out)); | ||
| assert!(!ran.exists(), "a hook ran"); | ||
| } | ||