vole
Site
Clone
Read only, no credentials:
https://rodent.software/vole.git
Write over SSH with a registered key:
git clone ssh://git@rodent.software/vole.git
Self-hosted forge that lives in your Git repo.
Let a release change until a channel names it
214e7ea67ccb37d356a52097f8ce838bdf4c1731 · Keenan Nemetz · 17d ago · parent 502119f4
Let a release change until a channel names it A release is mutable again. Notes can be edited and files added, replaced, or removed after publication, because a mistyped note or a rebuilt binary is not a reason to cut a new version. Editing the notes re-tags the same commit, which the old rule refused from the first attached file. The one thing that stays frozen is a release a channel names. A channel records the tag name and the tag object, so a re-tag would dangle it and a changed pointer would serve different bytes under a URL a package manager already cached. Pre-receive reads the channel table from the instance database and refuses every change to a channeled release — tag moves and deletes, and added, replaced, or removed pointers alike — naming the channel and how to move it. The browser applies the same rule: the edit form, the attach form, and the list link are all drawn from it. Attaching also works from the browser now, at parity with the CLI. The Artifacts card takes files, or a directory, and an optional directory to publish under; the handler streams each part to a temp file while hashing it, checks the object-store quota before storing anything, writes bytes first and pointers second, and calls the same metadata writer the CLI does. A directory upload keeps its paths, which is why the body is read as a multipart stream rather than through ParseForm.
server/hook/releases.go +98−54
| @@ -1,10 +1,22 @@ | |||
| 1 | 1 | package hook | |
| 2 | 2 | ||
| 3 | // Published releases are immutable, and this is the only place that can enforce | ||
| 4 | // it: the tag and the pointers arrive in ordinary pushes, and nothing else sees | ||
| 5 | // both. A download link whose bytes can change is worse than no link — the ETag | ||
| 6 | // moves with the bytes, so a cache tells the truth while the human who saved the | ||
| 7 | // old file has no way to notice. | ||
| 3 | // Releases are mutable; the releases a channel names are not. | ||
| 4 | // | ||
| 5 | // A release is an annotated tag plus the artifact pointers under | ||
| 6 | // releases/<tag>/. Its notes can be edited and its files replaced or removed | ||
| 7 | // after publication, because a mistyped note or a rebuilt binary is not a reason | ||
| 8 | // to cut a new version. Editing the notes re-tags the name, so the pointer is a | ||
| 9 | // new tag object; that is ordinary history, not a rewrite of anything the forge | ||
| 10 | // stored separately. | ||
| 11 | // | ||
| 12 | // A channel is different: it is the promise that one URL keeps serving one | ||
| 13 | // release. It records the tag name and the tag object, so a re-tag would leave it | ||
| 14 | // dangling and a changed pointer would put different bytes under a name a package | ||
| 15 | // manager has already cached. The rule is therefore a precondition: a release a | ||
| 16 | // channel targets may not change at all until the channel targets something else. | ||
| 17 | // Adding a path is included because the channel serves the whole tree, and a | ||
| 18 | // rule with an exception per file kind is harder to state and to check than one | ||
| 19 | // that says "move the channel first". | ||
| 8 | 20 | ||
| 9 | 21 | import ( | |
| 10 | 22 | "fmt" | |
| @@ -12,31 +24,35 @@ import ( | |||
| 12 | 24 | "strings" | |
| 13 | 25 | ||
| 14 | 26 | "rodent.software/vole/core/git" | |
| 27 | "rodent.software/vole/internal/format" | ||
| 15 | 28 | "rodent.software/vole/metadata" | |
| 16 | 29 | ) | |
| 17 | 30 | ||
| 18 | // releaseFindings reports the changes that would detach a published artifact from | ||
| 19 | // the bytes its link serves: | ||
| 20 | // | ||
| 21 | // - moving or deleting a tag that releases/<tag>/ holds artifacts for; | ||
| 22 | // - deleting a pointer, or re-pointing one at different bytes. | ||
| 23 | // | ||
| 24 | // Adding artifacts is allowed: an artifact is published when its pointer lands, so | ||
| 25 | // there is nothing for an addition to invalidate. | ||
| 26 | func releaseFindings(g *git.Repo, c change) []string { | ||
| 31 | // releaseFindings reports the changes a channel would notice. channels answers | ||
| 32 | // with the channel names that target a tag, in name order; an error means the | ||
| 33 | // list could not be read, and an unreadable list refuses the change rather than | ||
| 34 | // guessing that nothing names the release. | ||
| 35 | func releaseFindings(g *git.Repo, c change, channels func(tag string) ([]string, error)) []string { | ||
| 27 | 36 | if strings.HasPrefix(c.ref, "refs/tags/") { | |
| 37 | if c.old == "" || c.old == git.ZeroSHA || c.new == c.old { | ||
| 38 | return nil // a new tag, or one whose object is unchanged | ||
| 39 | } | ||
| 28 | 40 | name := strings.TrimPrefix(c.ref, "refs/tags/") | |
| 29 | if !tagHasArtifacts(g, name) { | ||
| 41 | names, err := channels(name) | ||
| 42 | if err != nil { | ||
| 43 | return []string{fmt.Sprintf("%s: cannot read the channel list: %v", c.ref, err)} | ||
| 44 | } | ||
| 45 | if len(names) == 0 { | ||
| 30 | 46 | return nil | |
| 31 | 47 | } | |
| 32 | verb := "moved" | ||
| 33 | if c.new == git.ZeroSHA || c.new == "" { | ||
| 48 | verb := "edited" | ||
| 49 | if c.new == "" || c.new == git.ZeroSHA { | ||
| 34 | 50 | verb = "deleted" | |
| 35 | 51 | } | |
| 36 | 52 | return []string{fmt.Sprintf( | |
| 37 | "%s: this tag has release artifacts attached, so it cannot be %s; "+ | ||
| 38 | "a release is immutable once published. Tag the new commit with a new name", | ||
| 39 | c.ref, verb)} | ||
| 53 | "%s: %s this release, so it cannot be %s; point each channel at "+ | ||
| 54 | "another release first (`vole release channel set %s <tag>`)", | ||
| 55 | c.ref, channelTargets(names), verb, names[0])} | ||
| 40 | 56 | } | |
| 41 | 57 | if c.ref != metadata.Ref { | |
| 42 | 58 | return nil | |
| @@ -45,27 +61,77 @@ func releaseFindings(g *git.Repo, c change) []string { | |||
| 45 | 61 | return nil // a new metadata branch publishes nothing old | |
| 46 | 62 | } | |
| 47 | 63 | before := releaseBlobs(g, c.old) | |
| 48 | if len(before) == 0 { | ||
| 49 | return nil | ||
| 50 | } | ||
| 51 | 64 | after := releaseBlobs(g, c.new) // an empty map when the branch is deleted | |
| 65 | // Every path either side, so an addition is compared as well: a channel | ||
| 66 | // serves the tree, and a new download under it is a change like any other. | ||
| 67 | paths := make([]string, 0, len(before)+len(after)) | ||
| 68 | for path := range before { | ||
| 69 | paths = append(paths, path) | ||
| 70 | } | ||
| 71 | for path := range after { | ||
| 72 | if _, seen := before[path]; !seen { | ||
| 73 | paths = append(paths, path) | ||
| 74 | } | ||
| 75 | } | ||
| 76 | sort.Strings(paths) | ||
| 77 | |||
| 52 | 78 | var problems []string | |
| 53 | for path, sha := range before { | ||
| 54 | now, ok := after[path] | ||
| 79 | for _, path := range paths { | ||
| 80 | now, inAfter := after[path] | ||
| 81 | was, inBefore := before[path] | ||
| 82 | if inAfter && inBefore && now == was { | ||
| 83 | continue // unchanged | ||
| 84 | } | ||
| 85 | tag := releaseTag(path) | ||
| 86 | if tag == "" { | ||
| 87 | continue | ||
| 88 | } | ||
| 89 | names, err := channels(tag) | ||
| 90 | if err != nil { | ||
| 91 | problems = append(problems, fmt.Sprintf("%s: cannot read the channel list: %v", path, err)) | ||
| 92 | continue | ||
| 93 | } | ||
| 94 | if len(names) == 0 { | ||
| 95 | continue | ||
| 96 | } | ||
| 55 | 97 | switch { | |
| 56 | case !ok: | ||
| 98 | case !inAfter: | ||
| 57 | 99 | problems = append(problems, fmt.Sprintf( | |
| 58 | "%s: a published artifact cannot be removed; publish a new release instead", path)) | ||
| 59 | case now != sha: | ||
| 100 | "%s: %s the release %s, so a file cannot be removed; "+ | ||
| 101 | "point each channel at another release first", path, channelTargets(names), tag)) | ||
| 102 | case !inBefore: | ||
| 60 | 103 | problems = append(problems, fmt.Sprintf( | |
| 61 | "%s: a published artifact cannot be re-pointed at different bytes; "+ | ||
| 62 | "attach the new file under a new path", path)) | ||
| 104 | "%s: %s the release %s, so a file cannot be added; "+ | ||
| 105 | "point each channel at another release first", path, channelTargets(names), tag)) | ||
| 106 | default: | ||
| 107 | problems = append(problems, fmt.Sprintf( | ||
| 108 | "%s: %s the release %s, so a file cannot be re-pointed at different bytes; "+ | ||
| 109 | "point each channel at another release first", path, channelTargets(names), tag)) | ||
| 63 | 110 | } | |
| 64 | 111 | } | |
| 65 | sort.Strings(problems) | ||
| 66 | 112 | return problems | |
| 67 | 113 | } | |
| 68 | 114 | ||
| 115 | // channelTargets names the channels and agrees with its verb: "channel stable | ||
| 116 | // targets" or "channels stable and beta target". | ||
| 117 | func channelTargets(names []string) string { | ||
| 118 | if len(names) == 1 { | ||
| 119 | return "channel " + names[0] + " targets" | ||
| 120 | } | ||
| 121 | return "channels " + format.Names(names) + " target" | ||
| 122 | } | ||
| 123 | |||
| 124 | // releaseTag is the tag a metadata path publishes under: releases/<tag>/<path>. | ||
| 125 | // A path that is not inside a release tree has no tag, and a tag containing "/" | ||
| 126 | // is refused by the writers, so the first segment is the whole answer. | ||
| 127 | func releaseTag(path string) string { | ||
| 128 | parts := strings.SplitN(path, "/", 3) | ||
| 129 | if len(parts) != 3 || parts[0] != metadata.ReleasesDir || parts[1] == "" { | ||
| 130 | return "" | ||
| 131 | } | ||
| 132 | return parts[1] | ||
| 133 | } | ||
| 134 | |||
| 69 | 135 | // releaseBlobs maps every path under releases/ in a commit to its blob id. The | |
| 70 | 136 | // blob id is the pointer text, so an unchanged artifact has an unchanged id: the | |
| 71 | 137 | // comparison needs no parsing and cannot be fooled by a reformatted pointer. | |
| @@ -79,31 +145,9 @@ func releaseBlobs(g *git.Repo, commit string) map[string]string { | |||
| 79 | 145 | } | |
| 80 | 146 | out := map[string]string{} | |
| 81 | 147 | for _, e := range entries { | |
| 82 | if strings.HasPrefix(e.Name, metadata.ReleasesDir+"/") { | ||
| 148 | if e.Type == "blob" && strings.HasPrefix(e.Name, metadata.ReleasesDir+"/") { | ||
| 83 | 149 | out[e.Name] = e.SHA | |
| 84 | 150 | } | |
| 85 | 151 | } | |
| 86 | 152 | return out | |
| 87 | 153 | } | |
| 88 | |||
| 89 | // tagHasArtifacts reports whether the metadata branch has a releases/<tag>/ | ||
| 90 | // directory. It reads the current tip rather than the pushed tree: a tag push and | ||
| 91 | // a pointer push can arrive in either order, and a tag is only bound to artifacts | ||
| 92 | // that already exist. | ||
| 93 | func tagHasArtifacts(g *git.Repo, tag string) bool { | ||
| 94 | tip, err := g.RevParse(metadata.Ref) | ||
| 95 | if err != nil || tip == "" { | ||
| 96 | return false | ||
| 97 | } | ||
| 98 | entries, err := g.LsTreeR(tip) | ||
| 99 | if err != nil { | ||
| 100 | return false | ||
| 101 | } | ||
| 102 | prefix := metadata.ReleasesDir + "/" + tag + "/" | ||
| 103 | for _, e := range entries { | ||
| 104 | if strings.HasPrefix(e.Name, prefix) { | ||
| 105 | return true | ||
| 106 | } | ||
| 107 | } | ||
| 108 | return false | ||
| 109 | } | ||