Time: 15 minutes. Track: developer. Before this: lesson 1.
By the end of this lesson you will be able to:
- search main’s code with a regular expression, and narrow it by file;
- find files by name;
- say what search sees: main’s landed files you may read, a moment after they land, and not work in progress.
Code to search
af search asks the forge, which keeps an index of main’s files, as Zoekt and Google’s code search do. The server builds it in the background as code lands. af search does not read your workspace, so it finds code you have never checked out. Start a forge and land a little code:
$ af-server --dev
...$ source ~/af-dev/dev/env
$ af workspace add ~/search
Workspace at /home/you/search
Paths: **
main has no landing yet
$ cd ~/search
$ af new -m "Add the service"
Working on change rmmskmtwrkpmlzumroxoqqzstutxsnmr at commit 98b6f14d027730a5b2a4a8797283ce81a02f0a754994e3aa66c7b018ef180d65
$ mkdir -p src docs
$ printf 'fn connect() {\n // TODO: retry on failure\n}\n\nfn serve() {\n connect();\n}\n' > src/server.rs
$ printf 'fn parse() {}\n' > src/config.rs
$ printf '# Operations\n\nTODO: document the retry limit\n' > docs/operations.md
$ af submit
Submitted commit 317b8220d84e69a0c0cce89e530089db832397ea8de32f152a33c2134f692579, 3 edits in its last snapshotSearch by text
Once the landing is in the index, each matching line comes back as path:line:text:
$ af search TODO
docs/operations.md:3:TODO: document the retry limit
src/server.rs:2: // TODO: retry on failureThe pattern is a regular expression, in Rust’s syntax. (?i) makes it ignore case, and \b marks a word’s edge:
$ af search '(?i)\bretry\b'
docs/operations.md:3:TODO: document the retry limit
src/server.rs:2: // TODO: retry on failureNarrow it by file
-f keeps files whose path matches a second expression:
$ af search TODO -f '\.rs$'
src/server.rs:2: // TODO: retry on failureWith -f alone, it lists the paths that match, a quick way to find files:
$ af search -f '^src/'
src/config.rs
src/server.rsWhat search sees
Search reads main as it has landed. Work in progress is not there until it lands, and a landing is searchable a moment after it lands: the index takes main in at most once a second. Start a change with a new word in it:
$ af new -m "Add a health check"
Working on change syyorwlrrsuummnqvpzqxyxyllzmkllr at commit 285c268b12bef259c3d3278e5fe089e93706d6164d615638e10c327db40c9d80
$ printf 'fn health() {\n // FIXME: report the database\n}\n' > src/health.rs
$ af search FIXMENothing: the change is open, not on main. Land it, and search finds it:
$ af submit
Submitted commit b9ae7b063ff30c246beb1b64b367e8e8c6668e36bc727621394893151684d10d, 1 edit in its last snapshot$ af search FIXME
src/health.rs:2: // FIXME: report the databaseThe search above is tried again until the line shows, because the index takes a landing in a moment after it lands.
Search also leaves out files in folders you may not read. The forge searches for you as you, so what it returns is only what you could check out.
And it leaves out files that are not text. A file is in the index only if it is valid UTF-8, has no line longer than 2,000 bytes and is at most a mebibyte; Search, caches and releases has the whole rule. A file that is not text is not in the index at all, so -f does not list it either.
Exercises
-
Find every function definition in
src.$ af search '^fn \w+' -f '^src/' src/config.rs:1:fn parse() {} src/health.rs:1:fn health() { src/server.rs:1:fn connect() { src/server.rs:5:fn serve() { -
Find the line that calls
connect, but not the line that defines it.$ af search '^\s+connect\(' src/server.rs:6: connect();
Check your understanding
You add a TODO in an open change, and af search TODO does not show it. Why?
Search reads main’s landed files, from the forge’s index of them. An open change is not on main; once it lands, and the index has taken the landing in, its lines are found.
Does af search need a workspace that holds the files?
No. The forge answers from its own index of main, so what a search finds does not depend on what your workspace holds: it finds files your workspace has never held.
You land a change and search for a word in it straight away, and nothing shows. Is something wrong?
Not necessarily. The index takes main in at most once a second while landings keep coming, so a search can be a moment behind a landing. Search again.
Next
Lesson 6: Approvals asks a person to approve a change before it lands.