The Analogy: An Engineering Drawing's Revision Block
In the corner of every formal engineering drawing sits a small boxed table called the revision block. It is unglamorous -- a few ruled rows -- but it is the single most important administrative feature on the sheet. Each row records one issued change: a revision letter such as A, B or C, the date it went out, a one-line description of what changed, and the initials of whoever approved it. Alongside it, the title block carries a "current revision" field, and the drawing number is stamped so that the part is identified not by "the drawing" but by the drawing number together with its revision.
The whole system rests on a discipline: you never alter the geometry on the sheet without, in the same controlled issue, advancing the revision letter, adding a row to the block, and updating the current-revision field. These are not three optional chores; they are one atomic act of issuing a revision. A drawing whose lines have changed but whose revision block still reads "Rev B" is a lie on paper, and a drafting office that tolerates it will not survive its first audit.
The revision letter is how the shop floor stays sane. When a part is superseded, the old sheet -- Rev B -- is stamped SUPERSEDED and withdrawn from the bench, and only the current issue is worked from. Nobody builds from "whatever copy is nearest"; they pull the drawing by number and check the revision matches the one the work order calls for. That indirection means hundreds of people can build the same part for years and always agree on exactly which dimensions are authoritative.
The failure mode is a revision that is not unique or not recorded. A draughtsman tweaks a hole position but forgets to bump the block, so two physically different sheets both claim to be Rev C; now the drawing number no longer identifies what is actually on the paper, and the shop machines the wrong part with complete confidence. Software releases fail in precisely the same way. Change the code but forget to bump the version and the number lies about what shipped; tag two different commits as v1.5.0 and the version stops identifying a build at all. A version number is only worth something if bumping it, recording it, and publishing it happen together as one deliberate, unrepeatable step.
Syntax or Core Structure
Automating a release begins with the humblest operation: taking a version string like 1.4.2 apart into its three numeric fields and putting it back together one higher. A semantic version is three numbers joined by dots -- major, minor and patch -- so splitting on the dot and incrementing the field you mean is the atom that every release script is built from.
#!/usr/bin/env bash
set -euo pipefail
version="1.4.2"
IFS=. read -r major minor patch <<< "$version"
patch=$((patch + 1))
echo "$major.$minor.$patch"
Output:
1.4.3
Setting IFS=. for the single read tells it to break the here-string on dots, so major, minor and patch receive 1, 4 and 2. Arithmetic expansion with $((patch + 1)) does the increment in the shell without spawning a tool, and the three fields are reassembled with literal dots between them. This is a patch bump: the smallest possible release, advancing only the rightmost number. The same three-step shape -- split, increment one field, reassemble -- underpins minor and major bumps too, which is what the next section generalises.
Technical Depth: Bumping Any Field and Honouring Semantic Versioning
A real release tool must bump whichever field the change calls for, and it must respect the rule that raising a higher field resets the lower ones -- a new minor version starts its patch count at zero, and a new major version zeroes both minor and patch. Encoding that rule once, in a single function, keeps every release consistent and turns "what is the next version?" into a mechanical answer rather than a judgement call made under pressure.
A General Bump Function
The function below takes a version and the field to raise. It splits the string exactly as before, then a case selects which field to increment and which lower fields to reset. An unrecognised field is rejected rather than silently ignored, so a typo like bump 1.4.2 minr fails loudly instead of doing nothing.
#!/usr/bin/env bash
set -euo pipefail
bump() {
local version="$1" part="$2"
local major minor patch
IFS=. read -r major minor patch <<< "$version"
case "$part" in
major) major=$((major + 1)); minor=0; patch=0 ;;
minor) minor=$((minor + 1)); patch=0 ;;
patch) patch=$((patch + 1)) ;;
*) echo "unknown part: $part" >&2; return 1 ;;
esac
echo "$major.$minor.$patch"
}
bump 1.4.2 patch
bump 1.4.2 minor
bump 1.4.2 major
Output:
1.4.3
1.5.0
2.0.0
Notice how a minor bump takes 1.4.2 to 1.5.0, not 1.5.2: raising the minor field resets patch to zero, because the patch count is meaningless against a version that did not exist yet. A major bump zeroes both lower fields for the same reason. This reset behaviour is the part people most often get wrong by hand, which is exactly why it belongs in code.
What Each Bump Means
Semantic versioning gives each field a contract, so choosing the bump is not a matter of taste -- it is dictated by the nature of the change. The table sums up the rule that a release tool encodes.
| Bump | When to use it | Result on 1.4.2 |
|---|---|---|
| Major | A breaking change to the public interface | 2.0.0 |
| Minor | A new backward-compatible feature | 1.5.0 |
| Patch | A backward-compatible bug fix | 1.4.3 |
The direction of the rule matters: you pick the field from the change, then the numbers follow mechanically. A caller who has to reason about whether the patch field should carry over is a caller about to ship an inconsistent version. Deciding "this release adds a feature and breaks nothing" gives you "minor" directly, and the bump function turns that word into 1.5.0 without any further thought. That is the whole value of automating versioning: it removes the arithmetic, and with it the mistakes, from a step that happens under the time pressure of a release.
Real World Example: A release.sh That Bumps, Records and Prepares a Tag
A release script ties the pieces together: it reads the current version from a VERSION file, bumps the field named on the command line, writes the new version back, and reports the git steps a maintainer would run to commit and tag it. Printing the git commands rather than running them -- a dry run -- lets you inspect exactly what a release would do before any history is touched. The script below defaults to a patch bump and takes the field as its first argument.
#!/usr/bin/env bash
set -euo pipefail
part="${1:-patch}"
version="$(cat VERSION)"
IFS=. read -r major minor patch <<< "$version"
case "$part" in
major) major=$((major + 1)); minor=0; patch=0 ;;
minor) minor=$((minor + 1)); patch=0 ;;
patch) patch=$((patch + 1)) ;;
*) echo "unknown part: $part" >&2; exit 1 ;;
esac
new="$major.$minor.$patch"
echo "$new" > VERSION
echo "bumped $part: $version -> $new"
printf 'would run: git commit -am "release %s"\n' "$new"
printf 'would run: git tag -a v%s -m "version %s"\n' "$new" "$new"
echo "release $new prepared"
With a VERSION file containing 1.4.2 and the script invoked as release.sh minor, every run prints the same four lines and leaves VERSION reading 1.5.0:
Output:
bumped minor: 1.4.2 -> 1.5.0
would run: git commit -am "release 1.5.0"
would run: git tag -a v1.5.0 -m "version 1.5.0"
release 1.5.0 prepared
The script issues a release in a single readable pass. It reads the old version, computes the new one through the same split-and-increment logic, records the new version by overwriting the VERSION file, and then reports the commit and tag a maintainer would run. Because the git lines are printed rather than executed, you can run it, read the plan, and only then decide to perform the real commit and tag by hand -- a safe rehearsal of an irreversible act.
The headline flaw is that the rehearsal is not actually safe, because one of its steps is real. The script writes the bumped number to VERSION unconditionally, but only pretends to commit and tag. Run it a second time without committing in between and it reads the already-bumped 1.5.0, bumps again to 1.6.0, and rewrites the file -- so the VERSION on disk races ahead of the last version that was ever committed or tagged. The one operation with a side effect and the operations that are merely described have been mixed in the same script, which is the precise recipe for state drift: the file claims a release that git has no record of.
Three narrower gaps compound it. There is no semantic-version validation, so a VERSION file reading abc is bumped to abc.1.0 and reported as a perfectly ordinary release -- garbage in, garbage out. There is no check that the working tree is clean, so the suggested git commit -am would sweep up whatever unrelated edits happen to be staged into the release commit. And the commit and tag are two independent steps with nothing making them atomic: if the tag step later fails because v1.5.0 already exists, you are left with a release commit and no tag, the mirror image of the very inconsistency versioning is meant to prevent.
Common Mistakes
- Changing the code but not the version. Shipping a fix or feature without advancing the version number leaves the number describing an older build -- the software equivalent of altering a drawing and leaving the revision block on Rev B. The version must be bumped in the same act as the release, never "later".
- Carrying a lower field across a higher bump. Going from
1.4.2to1.5.2on a minor bump instead of1.5.0. Raising major or minor must reset every field below it to zero, because those lower counts are meaningless against a version that did not exist a moment ago.
- Storing the version in several places and bumping only some. When the number lives in package metadata, a
VERSIONfile, and a source header at once, a manual bump updates whichever the author remembered. Keep a single source of truth and derive every other copy from it at build time.
- Feeding an unvalidated string into the bump. Passing a version straight into the split-and-increment logic with no check means a malformed input like
abcyields a malformed release likeabc.1.0that is reported as normal. Validate the shape first and reject anything that is not three integers joined by dots.
- Mixing a real side effect with a dry run. Writing the new version to disk for real while only printing the git commands makes the script non-idempotent: run it twice and it double-bumps, because the recorded state advanced but the release it describes never happened. Either do it all or describe it all.
- Committing a dirty working tree into the release. Running
git commit -amwithout first checking that the tree holds only the intended change sweeps whatever else is staged into the release commit, so the tag ends up pointing at a mixture nobody reviewed. Verify a clean tree before you commit a release.
- Assuming commit and tag succeed together. They are two separate operations. If the commit succeeds and the tag then fails -- because the tag already exists, say -- you are left with a release commit and no tag. Order them deliberately, check each result, and stop on the first failure rather than plough on.
- Reusing or force-moving an existing tag. Pointing
v1.5.0at a second, different commit makes one version number identify two builds, which is the two-different-Rev-C failure exactly. Treat a published tag as immutable; if you shipped a mistake, bump to a new version rather than moving the old tag.
- Deriving the version from a timestamp or commit count. A number built from the clock or from
git rev-list --countis neither reproducible nor reliably ordered across branches, so two builds can collide or sort wrongly. Bump deliberately from the previous version so the sequence is intentional and comparable.
Best Practices
- Keep one source of truth for the version. Store the number in a single place -- a
VERSIONfile or the package manifest -- and derive every other copy at build time, so a bump can never leave one file disagreeing with another.
- Bump, record, and publish as one atomic step. Advance the number, commit the change, and create the tag together, checking each result, so the version, the history, and the tag can never tell three different stories.
- Validate the version before touching it. Reject anything that is not three integers joined by dots before running the arithmetic, so a malformed input fails with a clear message instead of producing a malformed release.
- Reset lower fields on every higher bump. Encode the semantic-versioning rule in code so a minor bump always zeroes patch and a major bump zeroes both, removing the field most often mishandled by hand.
- Insist on a clean working tree before releasing. Check the repository state and refuse to release when unrelated changes are staged, so a release commit contains only what was reviewed.
- Rehearse with a dry run that has no side effects. If the script prints the plan, it should print all of it and change nothing; never write a file for real while only describing the git commands, because that makes a rehearsal into a partial release.
- Treat a published tag as immutable. Never move or reuse a version tag; if a release was wrong, bump to a new version and ship that, so a given number always identifies exactly one build.
- Make the release script safely re-runnable. Design it so that running it twice either is harmless or refuses with a clear message, so a retry after an interrupted release does not silently double-bump the version.
Practice Exercises
- Add semantic-version validation. Write a
validatefunction that accepts a version only when it is exactly three integers joined by dots -- use acaseglob or a regular expression -- and have the release script call it before bumping. Prove that aVERSIONfile readingabcnow aborts with a clear message instead of producingabc.1.0.
- Close the double-bump gap. Modify the script so the recorded version cannot race ahead of git: either perform the commit and tag for real, or refuse to write the
VERSIONfile until they have succeeded. Demonstrate the fix by running the script twice in a row and showing the version advances by exactly one step, not two.
- Guard the tag against reuse. Before creating the tag, check whether it already exists with
git rev-parseorgit tag -l, and refuse rather than move it. Show that a second attempt to release the same version stops with an explicit error instead of silently repointing the tag.
- Collapse two version sources into one. Take a project that stores the version in both a
VERSIONfile and a source header, refactor so the file is the single source of truth and the header is generated from it at build time, and verify that one bump updates both without any second manual edit.