Diff & impact analysis¶
CLI Beta
Compare traceability data between different states of a project or needs.json files, similar to how Git compares commits.
Quick Start¶
Compare two needs.json files:
ubc diff --needs old.json --needs new.json # first is baseline (old), second is new
Compare two projects:
ubc diff --project old_path --project new_path # first is baseline (old), second is new
Compare project with needs.json:
ubc diff --project old_path --needs new.json # first is baseline (old), second is new
ubc diff --needs old.json --project new_path # order is always: old, new
Note
Order matters: The first input is always treated as the baseline (old state), and the second as the new state. This applies to all comparison modes.
Git-based comparisons:
# Run in current directory (expects ubproject.toml)
ubc diff git # working directory vs HEAD
ubc diff git --staged # staging area vs HEAD
ubc diff git main develop # compare two branches
# Specify a project path in the repository
ubc diff git --project path/to/project # working directory vs HEAD
ubc diff git main develop --project path/to/project
# Compare a needs.json file in the repository
ubc diff git --needs path/to/needs.json # working directory vs HEAD
ubc diff git v1.0.0 v2.0.0 --needs path/to/needs.json
Output Formats¶
Results can be displayed in different formats:
Console output (default):
ubc diff --needs old.json --needs new.json
HTML report:
ubc diff --needs old.json --needs new.json --format html --outpath report.html
ubc diff --needs old.json --needs new.json --format html --html-theme dark --outpath report.html
Available themes: auto (default), light, dark
When the code linked to the needs is compared (see Code changes since linked),
the console adds a CODE DIFF section after SCHEMA DIFF,
and the HTML report a section per need after the needs.
Understanding Impact Analysis¶
Impact analysis tracks how changes propagate through requirement links. When a need changes, the analysis follows incoming and outgoing links to identify all potentially affected needs.
How it works:
Identifies new, removed, or changed needs, and the needs whose linked code changed or is gone (Code changes since linked)
Follows links from/to these needs up to the configured depth
Builds chains showing the impact path (e.g., REQ-1 → implements → STORY-5 → depends_on → EPIC-2)
Reports all affected needs with their link relationships
Use cases:
Understand the scope of a requirement change
Identify downstream tests affected by a feature change
Track upstream requirements when implementation changes
Generate change impact reports for reviews
Code changes since linked¶
CLI Alpha
When both sides are projects whose ubproject.toml writes a [codelinks_coverage] table,
ubc diff also compares the source code traced to each need
(by a codelinks marker, or from the need’s own fields with
symbol_fields),
and lists what changed in a CODE DIFF section after SCHEMA DIFF:
CODE DIFF
---------
REQ_1 cpp:src/brake.cpp#engage(int) changed (via direct)
REQ_3 cpp:src/brake.cpp#Pedal::press() changed (via Pedal)
TEST_1 cpp:tests/test_brake.cpp#test_engage() changed (via direct)
Each line names the need, the function (by its symbol ID),
what happened, and how the need traces it (via):
direct: a marker on the function itself (or on a function nested in it, or on its declaration);a class, namespace or module name: a marker on a container the function is in;
file: a file-level marker of the function’s own file;file:<path>: one of another file;lines <first>-<last>: a marker on an unnamed scope (an anonymous namespace) the function is in;requirement:<field>: a link from the need’s side, in its<field>.
A need tracing a function by several routes lists them all, comma-separated. The lines are grouped by need, sorted by need and then function.
Entry |
Meaning |
|---|---|
|
The need traces the function on both sides, and its code changed. |
|
The need traces the function on the new side only. |
|
The need traced the function on the old side only, and the function is still there. |
|
The need traced the function on the old side only, and the function is gone. |
A rename (or a move to another file) is a removed entry and a linked one.
A function gone from the old side and a function new on the new side are paired as a rename
when they have exactly the same code and each is the only one of its side with that code
(among the functions gone, respectively new);
both entries then name the other (probably renamed to …, probably renamed from …).
When several gone or several new functions share that code, no rename is guessed,
and each of their entries says how many share it
(same code in 2 gone and 1 new units (no rename guessed);
the count takes in every gone function with that code, traced or not).
A function present on both sides is never part of a rename, whatever its code.
A C++ function whose parameter types change gets a new ID (the signature is part of it) and different code
(the parameters are part of its tokens): it reads as removed and linked, with no rename suggested.
A link whose route changed while the code did not (a marker moved from a class onto its method,
a marker replaced by a symbol field) is not listed.
A need deleted while a marker still names it reads as removed under NEEDS DIFF
and as unlinked here (the marker names no need any more); a need added with a marker, as linked.
When it runs¶
Both sides are projects that write
[codelinks_coverage]: the code is compared. This holds for--projecttwice,--config, andubc diff gitwith a project (working directory,--stagedand two references).One project side does not write the table: the code is not compared, and one line says so:
code: not compared (the new side does not write [codelinks_coverage]; …).--codecompares it anyway, reading a side without the table with its defaults (so a need’ssymbol_fieldslinks are not read on that side).A side is a needs.json: a needs.json carries no code, so the code is never compared. Beside a project that writes the table, or with
--code, one line says so:code: not compared (a needs.json input has no code).Neither side writes the table: nothing about code is compared or printed.
--no-codeturns the comparison off.A codelinks project whose code is outside the repository, in
ubc diff git(itssrc_dirlies outside the git repository, such as../firmware): the checkout of a revision does not hold that code, so it is not compared, and one line names the project:code: not compared for codelinks project `firmware` (its code is outside the repository).Code in a git submodule or nested repository, in
ubc diff git:ubc diff gitreads each revision from a clone of the repository without its submodules, so the code of a submodule (or of an embedded repository, or of a directory with its own.gitat or above asrc_dir) is in no checkout. It is not compared, whether a codelinks project’ssrc_diris the submodule, lies inside it or holds it (the rest of that project is compared), and one line names it:code: not compared in `fw` (a git submodule or nested repository, whose code a checkout does not hold). Submodules are found in each side’s index: a revision’s for a revision, the staging area’s for--staged, the repository’s for the working directory. A path that is a submodule on either side is left out of both, so vendored code turned into a submodule (or the reverse) reads as not compared, not as removed (or linked).A codelinks project whose source directory is missing on a side, in
ubc diff git(deleted, or not yet created): its code is compared as absent there, and one line says on which side:code: codelinks project `tests` has no source directory on the new side(not for a directory in a submodule, which the submodule’s line covers).
A code: … line is no difference: it does not change the exit code.
What it compares¶
Every function, method, constructor, destructor and operator of every codelinks project,
whatever the project’s [codelinks_coverage] table leaves out of its coverage report:
test code and generated files are included, and exclude, exclude_names, kinds,
visibility, projects and inheritance = "strict" do not apply
(a marker on a class traces its methods).
The table’s symbol_fields apply, and its ledger is not read.
A test need traced to a test function is exactly the link whose change a reviewer wants to see.
The need filters apply as they do to NEEDS DIFF (see Filter Options):
with --allow-type, --deny-type or --deny-externals, an entry is listed (and counts for the exit code
and the impact chains) only when its need passes the filter on either side.
A need ID that names no need on either side (a one-line need in a source file no src-trace reads)
is in neither side’s filtered needs: it has no type for a type filter to test, and no source for
--deny-externals to test, so any of these filters leaves it out too.
The field filters (--allow-core, --deny-extra, …) do not apply to code.
The comparison is over each function’s content hash (the hash a justification ledger entry records): its tokens, without its comments and its own name.
Exit code and impact analysis¶
Any CODE DIFF entry is a difference: ubc diff exits with 1 when the code linked to a need changed,
even when no need did.
A CI job that runs ubc diff for the needs alone, on a project that writes [codelinks_coverage],
passes --no-code to keep the exit code it had.
With --impact, each need with a changed or removed entry starts impact chains,
as a changed need does (a linked or unlinked entry changed no code, and starts none);
they are printed under the need’s CODE DIFF lines,
or only under NEEDS DIFF when the need itself changed too.
Limits¶
Only the function’s own tokens count. A change to a function it calls, a macro it expands, a
typedefor a constant in a header does not mark it changed. Code outside functions (a struct’s fields, a global) is in no function, and is not compared.Whitespace, comments and markers never mark a function changed: reformatting a file, editing a comment or editing the marker itself lists nothing (changing which needs a marker names does:
linked,unlinked).Changed between these two revisions, not “suspect until acknowledged”. Nothing is stored: the next comparison, against a later revision, no longer lists a change that was reviewed. A need’s link is not marked suspect until someone confirms it.
The function IDs must match between the sides. An ID names the file by its path relative to the project root. With
--projecttwice, code outside the project root (a codelinkssrc_dirsuch as../firmware) gets an ID that depends on where the two directories sit, so comparing two project directories laid out differently can see every such function as removed and added again (#4548).ubc diff gitreads both sides from one repository, so code inside it keeps its ID, except the code of a git submodule or nested repository, which is not compared there, nor is code outside the repository (see When it runs).It costs a parse of every source file of every codelinks project, with its symbol inventory, on each side;
--no-codeskips it.
Commands Reference¶
Basic Diff Command¶
Compare two files or projects:
# Compare two needs.json files
ubc diff --needs file1.json --needs file2.json
# Compare two projects
ubc diff --project path1 --project path2
# Mix: compare project with needs.json
ubc diff --project path --needs file.json
Git Diff Command¶
Compare Git references (branches, tags, commits):
# Compare working directory with HEAD (default: current directory)
ubc diff git
ubc diff git --project path
# Compare staging area with HEAD
ubc diff git --staged
# Compare two Git references
ubc diff git main develop
ubc diff git v1.0.0 HEAD
ubc diff git abc123 def456
# Work with needs.json in repository
ubc diff git --needs path/to/needs.json
ubc diff git main develop --needs path/to/needs.json
Impact Analysis Options¶
Track how changes affect linked requirements:
# Enable impact analysis
ubc diff --needs old.json --needs new.json --impact
# Configure impact tracking
ubc diff --needs old.json --needs new.json \
--impact \
--impact-depth 2 \
--impact-direction both \
--impact-max-chains 50
Options:
--impact— Enable impact analysis--impact-depth N— Follow links up to N levels (default: 1)--impact-direction {incoming|outgoing|both}— Link direction (default: both)--impact-max-chains N— Max chains per changed need (default: 20, 0=unlimited)
Filter Options¶
Control which needs and fields are compared. All filter options can be used multiple times.
The need type filters and --deny-externals also select the needs whose linked code is compared
(Code changes since linked).
Need Type Filters:
# Only compare specific need types
ubc diff --needs old.json --needs new.json --allow-type req --allow-type story
# Exclude specific need types
ubc diff --needs old.json --needs new.json --deny-type internal
Field Filters:
# Only compare specific core fields (id, title, type, content, status, tags, links)
ubc diff --needs old.json --needs new.json --allow-core title --allow-core status
# Exclude core fields from comparison
ubc diff --needs old.json --needs new.json --deny-core tags
# Filter extra/custom fields
ubc diff --needs old.json --needs new.json --allow-extra my_field
ubc diff --needs old.json --needs new.json --deny-extra temp_field
Link Type Filters:
# Only compare specific link types
ubc diff --needs old.json --needs new.json --allow-link implements --allow-link depends_on
# Exclude link types from comparison
ubc diff --needs old.json --needs new.json --deny-link obsoletes
A summary link type (marked "summary": true in a needs.json’s schema)
is compared as any link type, but impact chains do not follow it: its links are shortcuts across the trace, not steps in it.
Listing it with --allow-link beside the link types you compare makes the impact analysis follow it too
(--allow-link is an allow-filter: the link types it does not list are left out of the comparison).
Note
Allow-filters (--allow-*) and deny-filters (--deny-*) are mutually exclusive for each category.
Use allow-filters to start with an empty set and add specific items.
Use deny-filters to start with all items and remove specific ones.
Common Workflows¶
Review changes before committing:
# See what changed in your working directory
ubc diff git --impact
# Check what's staged for commit
ubc diff git --staged --impact
Compare feature branches:
# Compare your branch against main
ubc diff git main feature/my-feature --impact --format html --outpath review.html
Release impact analysis:
# See all requirements affected between versions
ubc diff git v1.0.0 v2.0.0 \
--impact \
--impact-depth 3 \
--allow-type requirement --allow-type specification
Track test impact:
# Find which tests are affected by requirement changes
ubc diff git main HEAD \
--impact \
--impact-direction outgoing \
--allow-link tests \
--deny-core tags
Compare documentation states:
# Compare exported needs.json files from different builds
ubc diff --needs old-build/needs.json --needs new-build/needs.json \
--impact \
--format html --html-theme light --outpath comparison.html