Not-forking Source Integration
Not-forking obtains source from one or more upstream projects and applies a recipe of local changes. Use it when a build or test system must create the same combined source tree repeatedly.
A recipe is a directory, separate from the upstream repositories and the output directory. It can select versions, use several repositories for one upstream, select a source subtree, replace or rename files, edit text, append files, and apply patches inside anchored source fragments.
The tool was developed for LumoSQL. LumoSQL combines SQLite with LMDB or libdb and maintains separate recipes for SQLite, LMDB 0.9.x, LMDB 1.0 and libdb. Its current recipes are the main worked example for this document.
Not-forking prepares source. It does not replace Git, Fossil, package managers, or build systems, and it does not decide whether a combination is correct. The recipe author supplies those decisions.
The Basic Model
A configuration directory contains one subdirectory for each upstream project:
not-fork.d/
sqlite3/
upstream.conf
upstream.lock
backend-rename.mod
replace-files.mod
vdbe-changes.mod
files/
lmdb/
upstream.conf
upstream.lock
*.mod
files/
Each subdirectory needs an upstream.conf. Modification files, whose names
end in .mod, are optional.
Not-forking processes files in C-locale lexical order. If one file replaces an
upstream file and another edits that replacement, give the replacement the
earlier name.
Not-forking obtains the requested version, applies the .mod files to copies
in its cache, and installs the result in OUTPUT/NAME, ready for the project's
normal build.
A LumoSQL-Style Configuration
The SQLite recipe in LumoSQL follows Git tags with the version- prefix and
can name the current branch tip:
vcs = git
repos = https://github.com/sqlite/sqlite
branch = master
version = version-
version_from_branch = 3.55.0
srcmirror = sqlite3-$C sqlite3-$C.tar.gz
version_from_branch gives the current branch tip a version name when the
upstream has not tagged that release. The name must begin with a digit and
the branch must be specified in the same block. When a lockfile is generated,
the name is mapped to the commit present at that time.
The srcmirror value gives source names to look for in the directories given
with --local-mirror=DIR.
$C expands to the selected commit ID, $V to the selected version, and
$$ and $D to a literal dollar sign.
The LMDB 0.9.x recipe uses a version filter, a subtree, a tag prefix, and two repository blocks:
vcs = git
version_filter = >= 0.9.14 < 1.0-alpha
subtree = libraries/liblmdb
version = LMDB_
block
repos = https://github.com/openldap/openldap
block
repos = https://github.com/LMDB/lmdb
Not-forking fetches every block and takes each version from the first block
that has it. The second block supplies older releases that are not present in
the OpenLDAP repository.
A version_filter removes versions that the downstream cannot use. LumoSQL
keeps LMDB 1.0 in a separate recipe because it has different internals and
different build requirements:
vcs = git
subtree = libraries/liblmdb
version = LMDB_
block
repos = https://github.com/openldap/openldap
branch = mdb.RE/1.0
version_from_branch = 1.0
Use separate recipe directories when two upstream lines need different local source files or represent different implementations.
upstream.conf
The file uses one key = value pair per line. Blank lines and lines whose
first nonblank character is # are ignored. A trailing backslash continues a
long value on the next line.
The vcs key is required. Its value is one of git, fossil, or download.
The other keys depend on that source method.
Version Selection
version converts a human version into a tag name. A single value is a
prefix; two values provide a prefix and suffix:
version = v
version = release- -src
compare sets the version comparison method. The only method, and the
default, is version. It orders digit runs numerically and letter runs
alphabetically.
Missing numeric components count as zero, so 3.48 and 3.48.0 are equal.
These suffixes sort before the version without a suffix, in this order:
-git, -alpha, -beta and -branch (equal), -gamma, -delta, -pre,
-rc, -snapshot, -tip. A number after -pre or -rc orders versions
with the same suffix, so 1.0-rc < 1.0-rc1 < 1.0-rc10. Other text stays
in the version and compares as text, so 1.0a sorts after 1.0 and
1.0-patched sorts before it.
0.9a < 0.9z < 0.10 < 1.0 < 1.1-alpha < 1.1-beta < 1.1-rc1 < 1.1 < 1.1a
version_filter restricts the versions that can be selected:
version_filter = >= 3.43.0 < 3.54.0
Use this when a modification has a known supported range, or when one source line must stop where another source line begins.
Conditional Values
An if block selects values by version, operating system, or file. elsif,
also spelled elseif, and else are optional:
if version >= 2.0
subtree = src
elsif version >= 1.0
subtree = lib/src
else
subtree = old/src
endif
Available conditions are:
if version COMPARISON VERSION [COMPARISON VERSION ...]
if osname =|!= NAME [=|!= NAME ...]
if hasfile [!]/PATH [ [!]/PATH ...]
In a version condition, a COMPARISON is one of =, ==, !=, <, <=,
> and >=. osname also accepts ==. Every comparison on the line must
hold. osname is the last part of Perl's $Config{myarchname}, such as
linux, freebsd or netbsd. PATH is absolute. A trailing / tests for a
directory, otherwise for a regular file, and ! requires that the path is not
present.
Conditionals cannot be nested. If a key occurs more than once, the last value wins.
Use block, not an if version conditional, when versions come from
different repositories. With no version requested, only > and >=
comparisons hold, so a conditional selects one repository and
--list-versions shows only that repository's versions. A
block starts a new source definition. Keys before the first block form the
common part.
Git
Required keys:
vcs = git
repos = https://example.org/project.git
Optional keys include branch, version, version_filter, subtree,
srcmirror, and version_from_branch. If the upstream later creates a tag
with the same version, the branch tip still takes priority and not-forking
prints a warning. To build the tag, change version_from_branch to a later
version or remove it.
Fossil
Fossil uses the same version and branch keys:
vcs = fossil
repos = https://sqlite.org/src
branch = trunk
version_from_branch = 3.55.0
For Fossil, a branch is a propagating tag. The selected branch version is the most recent check-in with that tag. A lockfile records the check-in's artifact ID.
repos can also be the path of a repository file. not-forking converts it to a
file:// URL, the form Fossil records for the clone, and resolves a relative
path from the directory in which not-fork runs.
Download
A download recipe gives the source URL of each version and can give a checksum to verify it:
vcs = download
source-1.3.1 = https://zlib.net/fossils/zlib-1.3.1.tar.gz
sha256-1.3.1 = mpOyt9/ax3zrpaVYpYDnRmfdb+3kWFuR7vtg8Dty3yM=
prefix = 1
prefix removes leading directories after unpacking. Not-forking identifies
the archive with file and unpacks tar archives, uncompressed or compressed
with gzip, bzip2 or xz. A checksum key is sha224-, sha256-, sha384- or
sha512- followed by the version, with the digest in hex or base64.
Modification Files
A .mod file has a header followed by a line consisting only of dashes, such
as --. The header contains key = value pairs. The remaining text is
interpreted by the selected method.
The header of a .mod file can hold version, osname and hasfile
conditions, written as in an if line without the word if:
method = fragment_patch
version >= 3.35
The whole file is ignored when the condition is false. Use this for changes that were accepted upstream or for source ranges whose structure differs.
replace
Replace complete upstream files with files from the recipe directory:
method = replace
--
src/btree.c = files/btree.c
src/btreeInt.h = files/btreeInt.h
The left side is relative to the extracted upstream. The right side is
relative to the recipe directory, such as not-fork.d/sqlite3/. Neither can
contain whitespace. Blank lines and lines starting with # are ignored. A +
before the right side, as in src/version.h = +files/version.h, replaces $LUMO_VERSION and
$LUMO_COMMIT_ID in the file's text with the selected version and commit
while copying it; other $LUMO_ names become empty.
append
Append text to an existing file:
method = append
--
src/btree.h
#include "lumo-btree-additions.h"
Multiple files are separated by a line of dashes. Where an appended line
starts with - and whitespace, not-forking removes them, so to append a line
of dashes, or a line that starts with -, put - in front of it.
sed
Replace every match of a regular expression in every matching file. The left side is a file glob:
method = sed
--
Makefile.in : '\bbackup\.c\b' = lumo_backup.c
src/*.c : sqlite3PagerIsMemdb = BackendIsMemdb
The glob matches either the file's path or its last component, and cannot
contain whitespace or :. Quote a regular expression with ' or " when it
contains spaces or =; it cannot then contain its quote character. The
regular expression runs over the complete file, so \n in it matches a line
end. The replacement is
the rest of the line, taken literally except that \n, \t and \r become
newline, tab and carriage return, and $LUMO_VERSION and $LUMO_COMMIT_ID
become the selected version and commit. Other $LUMO_ names become empty. The
replacement cannot refer to captured groups.
patch
The patch method passes its final part to the system patch program. Use it
for an ordinary unified diff or when an existing patch series must remain
compatible. The options key supplies options to that program; the default is
-tNsp1.
fragment_patch
fragment_patch handles the larger LumoSQL source edits. It selects a region
with anchors and applies unified-diff hunks inside that region. The applier
lives in not-forking and is deterministic. It does
not use patch(1), fuzz, offset search, or whitespace tolerance.
The fragment grammar is:
PATH
START
END
@@ -a,b +c,d @@
context line
-removed line
+added line
context line
---
-----
--- ends one fragment. ----- ends the file block. There must be no blank
line between them.
The start anchor is either start or /REGEX/ VALUE. The regular expression
must contain a capture group, and VALUE must equal that group's value. The
end anchor is either end or /REGEX/ with no value. The start line belongs
to the fragment; the end line does not.
For each hunk, not-forking builds the old image from context and removed lines
and finds an exact contiguous match inside the selected fragment. It applies
the new image at that location. Header counts are advisory and are used only
in diagnostics. A missing or ambiguous match stops the build. The full
behaviour, including newline handling, is specified in
fragment_patch-specification.md.
Upstream changes outside the anchored fragment do not affect the patch. A change inside the fragment stops the build, and the recipe must then be regenerated or repaired.
Generate a fragment patch from two files with fragment-diff:
fragment-diff -t functions.tmpl -x 'version >= 3.35' \
sqlite-old/src/vdbe.c sqlite-new/src/vdbe.c src/vdbe.c
Each line of the template file is a regular expression with one capture group,
and each line of the source it matches starts a fragment. Without -t, the
whole file is one fragment. -x adds a line to the recipe header. Use the
output as a fragment_patch .mod file. LumoSQL generates its
vdbe-changes.mod this way and maintains the resulting recipe in its source
tree.
Lockfiles and Source Mirrors
An upstream recipe describes a version query. An upstream.lock file records
the result of that query: repository information, version names, commit IDs,
and timestamps. It prevents a changed or moved upstream tag from changing a
build silently.
Build or use an upstream lockfile with:
not-fork --build-upstream-lock -i not-fork.d NAME
not-fork --use-upstream-lock -i not-fork.d -v VERSION NAME
Put -v VERSION before the name it selects. The lock options apply to every
name on the command line. A branch-tip version resolves to its current commit
when the lockfile is built, and a build that uses the lockfile checks out that
commit. To move the lock to a later branch tip, build the lockfile again.
--build-json-lock=FILE creates a JSON description of known versions and
their source locations. It can record a VCS commit or a download URL and
checksum. For a Fossil source, --prefer-tarball=URL records a tarball URL in
place of the commit, --distribution=DIR keeps the generated tarballs in DIR,
and --filehash=SPEC or --dirhash=SPEC records a hash of each tarball. For a
download source, only the hash options apply. Like -v, these options apply to
the next name.
When a mirror entry matches, not-forking installs the files from it in place
of the cached working tree. It still updates the cached repository and checks
out the version. A mirror entry can be an unpacked directory or an archive. The selected version
and commit expand into the names declared by srcmirror. The normal source
cache remains the fallback when no mirror entry matches.
Example Configuration Directory
The repository contains small examples under doc/examples. They exercise the
tool; they do not reproduce all of LumoSQL.
The sqlite example shows a complete-file replacement, a historical unified
patch, and a pattern substitution. The lmdb example shows a tag prefix, a
source subtree, a version filter, a source mirror, and multiple repositories.
The not-forking example tracks this tool from its own Fossil repository and
includes a generated lockfile. The bdb example shows a tarball download and
archive prefix handling; add a sha256-VERSION key when the source needs
checksum verification.
The current LumoSQL recipes are the more realistic example. They add a separate LMDB 1.0 recipe, generated replacement files and version-gated SQLite changes, and LumoSQL tests the resulting build matrix.
Command-Line Tool
The command is:
not-fork [OPTIONS] [NAME]...
Options:
-h,--help: print the options.-i DIR,--input=DIR: recipe directory; default./not-fork.d.-o DIR,--output=DIR: output directory; default./sources.-k DIR,--cache=DIR: source cache; default$HOME/.cache/LumoSQL/not-fork.-v VERSION,--version=VERSION: select a version for the next name.-c ID,--commit=ID: select a commit for the next name.--list-versions: list known versions.--version-range=MIN:MAX: list versions inside a range.--metadata: list versions with source and commit information.-q,--query: obtain source information without installing output.--update: update cached repositories; this is the default.-n,--no-update: skip fetching from upstream when the source is already cached.--online,--offline: permit or prohibit network access.--local-mirror=DIR: search local archives and unpacked source trees first.--build-upstream-lock: writeupstream.lockfiles.--use-upstream-lock: resolve versions fromupstream.lock.--build-json-lock=FILE: write a JSON version map.--check-prereq: check dependencies needed by the selected recipes.--check-recommend: check every dependency that the tool can use.--list-cache: list cached sources.--remove-cache=NAME: remove one cache entry.--verbose=LEVEL,--quiet: set how much progress is printed; the default is 1, and--quietis--verbose=0.-V,--my-version: print the not-forking version.--check-version=NUMBER: exit with status 0 if this not-forking is version NUMBER or later; otherwise print its version and exit with status 1.--find-version=MIN[:MAX]: print, one word per line, the command that runs a not-forking version in that range, extracting one from the not-forking repository when the running version is outside it.--use-version=MIN[:MAX]: run the rest of the command with a not-forking version in that range, found as--find-versiondoes.--config=FILE: read options from FILE in place of$HOME/.config/LumoSQL/not-fork.conf. Each line holds one long option without its leading dashes, asofflineorcache = DIR.--configmust be the first option.
When no name is supplied, every recipe subdirectory in the input directory is processed. The version and commit options apply to all names in that case. When an output directory was created by a previous run and has been modified, not-forking refuses to overwrite it. Move or remove the output first.
not-fork exits with status 0 on success. It exits with status 1 for an
invalid option, an unknown --remove-cache entry, a missing dependency or
prerequisite, an output directory changed since the last run, and a
--check-version failure. It exits with status 2 when it cannot find its own
modules, or when --check-prereq finds no recipe directory.
Other errors, such as a failed download or a fragment that does not apply,
exit with a nonzero status after printing a message.
Caches and Offline Use
The cache contains upstream repositories and working copies. An online run creates the cache automatically. Later runs can use:
not-fork --offline --no-update -i not-fork.d -v VERSION NAME
The requested source must already be in the cache. Each entry has an index
file recording the repository. After a recipe with modifications is
extracted, a mods/ directory holds the modified files. Git entries also
contain a clone without a working tree in main_clone/, a working tree of the
selected version in vcs/, and the tag or commit checked out there in
vcsdata. Fossil entries contain a repository database in db and an opened
checkout in vcs/. Each run restores the working tree in vcs/ to the
selected version.
Not-forking checks that the repository URL in the recipe matches the URL
recorded in the cached clone, and stops if they differ.
Use --local-mirror and srcmirror when a project has large local source
archives or unpacked source trees. This is the pattern LumoSQL uses for
repeated version-matrix builds.
Fragment Tools
fragment-diff generates fragment_patch files:
fragment-diff [OPTIONS] OLD NEW NAME [OLD NEW NAME]...
OLD and NEW are files to compare. NAME is the path that the generated
recipe will patch. Use -t FILE for a file of regular-expression fragment
templates, and -c NUM for NUM lines of hunk context in place of 3. fragment-diff --help lists the other options.
fragment-patch applies one or more generated recipe files directly to a
directory:
fragment-patch [OPTIONS] PATCH_FILE [PATCH_FILE]...
It uses the same deterministic fragment applier as not-forking.