Vendor branches
The problem. Your project (A) needs someone else's code (B). You copy B into your tree, and then two things happen: you change a few lines of B for your own needs, and B keeps putting out new releases. Copying each new release over your copy throws your changes away.
The fix. Keep B's releases, exactly as shipped, on a branch of their own, and merge that branch into your project. Fossil then works out what changed upstream since the last release you took, and merges only that into your copy, so your own changes stay.
This repository does exactly that. Its trunk is project A, notes, a small tool
that prints text wrapped to a width. The vendor-wrap branch holds B, a tiny
library called wrap, through three releases.
See it all at once on the timeline.
Where the vendor branch starts
The vendor branch starts from the repository's first check-in, the
"initial empty check-in" that fossil new makes. It has no
files, so the vendor branch carries none of A's files, only B. But trunk
descends from it, so the two branches share an ancestor and merge normally.
A truly separate root (made with fossil open --empty and a first commit)
doesn't work for this: fossil merge refuses it with "cannot find a common
ancestor". Branching from your latest code does work, but it puts all of A's
files on the vendor branch too.
The history
Time runs up the page, newest at the top, as on Fossil's timeline. Upstream
releases arrive on vendor-wrap exactly as shipped, and each one is merged
across into trunk. From the moment A patches its copy of wrap, that change
rides along trunk through every later release, including the one that renames
the file.
arrowht = 0.07
$vx = 1.5
$dy = 0.56
$lx = -0.3
$rx = $vx + 0.3
# Check-ins, oldest at the bottom: time runs up, as on Fossil's timeline.
Start: circle rad 0.1in thickness 1.5px fill white at (0, 1*$dy)
Root: circle same dashed at (0, 0)
Cont: circle same as Start at (0, 2*$dy)
V10: circle same as Start at ($vx, 3*$dy)
M10: circle same as Start at (0, 4*$dy)
Use: circle same as Start at (0, 5*$dy)
Patch: circle same as Start at (0, 6*$dy)
V11: circle same as Start at ($vx, 7*$dy)
M11: circle same as Start at (0, 8*$dy)
V20: circle same as Start at ($vx, 9*$dy)
M20: circle same as Start at (0, 10*$dy)
# Parent to child.
arrow from Root to Start chop
arrow from Start to Cont chop
arrow from Cont to M10 chop
arrow from M10 to Use chop
arrow from Use to Patch chop
arrow from Patch to M11 chop
arrow from M11 to M20 chop
arrow from M20.n up 0.3
arrow from Root to V10 chop
arrow from V10 to V11 chop
arrow from V11 to V20 chop
# Merges.
arrow dashed 0.03 from V10 to M10 chop
arrow dashed 0.03 from V11 to M11 chop
arrow dashed 0.03 from V20 to M20 chop
# Bands, drawn underneath.
layer = 0
VB: box fill 0x9bcdfc color 0x9bcdfc wid 0.42 ht V20.n.y - V10.s.y + 0.36 with .s at (V10.x, V10.s.y - 0.18)
text "vendor-wrap" small at VB.s - (0, 0.14)
PB: box fill 0xe8d8d0 color 0xe8d8d0 wid 0.42 ht M20.n.y - Patch.s.y + 0.36 with .s at (Patch.x, Patch.s.y - 0.18)
text "trunk" small at Root.s - (0, 0.16)
layer = 1000
# Trunk notes, left of the line.
define tl { text $1 rjust at ($lx, $4.y + 0.13)
text $2 mono small rjust at ($lx, $4.y - 0.02)
text $3 small rjust at ($lx, $4.y - 0.16) }
tl("INITIAL EMPTY CHECK-IN", "f2e1f9f4c8", "fossil new; 0 files", Root)
tl("START notes", "cd2c8ca84b", "+notes.c +Makefile +README.md", Start)
tl("CONTINUE", "930eb4ff6b", "no wrap library yet", Cont)
tl("MERGE wrap 1.0", "e8cd137bb0", "ADDED vendor/wrap/* (4 files)", M10)
tl("USE wrap", "5781ae8096", "notes.c, Makefile edited", Use)
tl("PATCH wrap.c:7", "b9eed52216", "is_space() also breaks at '\\t'", Patch)
tl("MERGE wrap 1.1", "c977a16fd1", "MERGE wrap.c +wrap_test.c −legacy.c; patch kept", M11)
tl("MERGE wrap 2.0", "e305905700", "RENAME wrap.c → textwrap.c; patch kept", M20)
# Vendor notes, right of the line.
define vl { text $1 ljust at ($rx, $4.y + 0.13)
text $2 mono small ljust at ($rx, $4.y - 0.02)
text $3 small ljust at ($rx, $4.y - 0.16) }
vl("IMPORT wrap 1.0", "ab245b7902", "4 files into vendor/wrap/", V10)
vl("IMPORT wrap 1.1", "6e37e4e613", "wrap.c:20 >= → >; +wrap_test.c −legacy.c", V11)
vl("IMPORT wrap 2.0", "b6163fde93", "fossil mv wrap.c textwrap.c", V20)
text "A's patch," small italic ljust at PB.se + (0.08, 0.26)
text "carried forward" small italic ljust at PB.se + (0.08, 0.12)
→ /pikchrshowCheck-ins: notes starts · wrap 1.0 · merge 1.0 · A patches wrap · wrap 1.1 · merge 1.1 · wrap 2.0 · merge 2.0
What the merges of 1.1 and 2.0 printed:
fossil merge vendor-wrap (wrap 1.1)
UPDATE vendor/wrap/README.md
UPDATE vendor/wrap/wrap.h
MERGE vendor/wrap/wrap.c
DELETE vendor/wrap/legacy.c
ADDED vendor/wrap/wrap_test.c
fossil merge vendor-wrap (wrap 2.0)
UPDATE vendor/wrap/README.md
UPDATE vendor/wrap/wrap.h
MERGE vendor/wrap/wrap.c
RENAME vendor/wrap/wrap.c -> vendor/wrap/textwrap.c
Doing it yourself
Once, to set up. Use a second folder for the vendor branch, so your project's checkout is never emptied:
mkdir ../vendor && cd ../vendor
fossil open ../project.fossil start:trunk
# copy B's release into vendor/b/
fossil addremove
fossil commit --branch vendor-b -m "Import B 1.0 exactly as upstream ships it."
cd ../project
fossil merge vendor-b
fossil commit -m "Merge B 1.0 from vendor-b."
start:trunk names the first check-in on trunk. Fossil versions before 2.19
don't know that name: use the hash of the "initial empty check-in" from the
bottom of the timeline instead.
For each new release of B:
cd ../vendor
fossil update vendor-b
rm -rf vendor/b && mkdir vendor/b
# copy the new release into vendor/b/
fossil addremove
fossil commit -m "Import B 1.1 exactly as upstream ships it."
cd ../project
fossil merge vendor-b
# build and test, then:
fossil commit -m "Merge B 1.1 from vendor-b."
If a release renames a file, record it with fossil mv OLD NEW before
addremove, as the wrap 2.0 import does, and the merge carries your
changes to the new name. Without fossil mv, Fossil sees a deleted file and a
new one, and your changes to it don't follow.
Seeing your own changes to B
The vendor branch always holds B exactly as upstream shipped it, so comparing it with trunk shows exactly what your project has changed in B and nothing else:
fossil diff --from vendor-b --to trunk vendor/b
Here, that's the one-line tab patch:
- the diff between vendor-wrap and trunk
- who changed what in textwrap.c
- textwrap.c's history, through the rename
If B is already pasted into your project
If A already has a copy of B, perhaps with changes, the first merge from the vendor branch conflicts, because both branches added the same files. Resolve it once by keeping A's version of each file, then commit:
fossil merge vendor-b
fossil cat vendor/b/FILE -r trunk > vendor/b/FILE # for each conflicting file
fossil commit -m "Merge B from vendor-b, keeping our copy."
After that, each new release merges as above. (This repository's history doesn't show this case. It was tested separately.)
When not to use it
- If you never change B, a plain copy is simpler: replace the folder with each new release.
- If your changes to B could go upstream, send them there, and you won't have to carry them at all.
- If your changes are large, or B moves files around a lot, three-way merges struggle. Dan Shearer's Not-forking is built for that case.