Time: 20 minutes. Track: developer. Before this: lesson 2.
By the end of this lesson you will be able to:
- make a workspace that holds only some paths, and list them;
- add and remove paths with
af sparse, asjj sparsedoes; - say which landing a path you add comes from, and how to bring a newer one;
- say where an edit goes when its path leaves the workspace.
A repository of three parts
A workspace holds the files of main that match its paths, every file by default. Start a forge and land a repository with two services and a library, from a workspace of every file:
$ af-server --dev
...$ source ~/af-dev/dev/env
$ af workspace add ~/all
Workspace at /home/you/all
Paths: **
main has no landing yet
$ cd ~/all
$ af new -m "Lay out the services"
Working on change znunmtrrkrlqvvuuvumpnqkoyymzzmvq at commit ab4393cb6b26af53761071d9e3f161a544ff6020bf3fa5058dbb59b9e59ff12d
$ mkdir -p services/api services/web libs
$ printf 'serve the api\n' > services/api/main.txt
$ printf 'serve the pages\n' > services/web/main.txt
$ printf 'shared code\n' > libs/common.txt
$ af submit
Submitted commit b27159c0174453ee28a1273155c17a78bcd3c128a99ec27b9b9c41d6c1b2252e, 3 edits in its last snapshot$ af status
Change znunmtrrkrlqvvuuvumpnqkoyymzzmvq (landed) at commit b27159c0174453ee28a1273155c17a78bcd3c128a99ec27b9b9c41d6c1b2252eHold only what you work on
--path takes a glob from the repository’s root, and repeats. A workspace for the API holds its service and the library, and nothing else is written:
$ af workspace add ~/api --path 'services/api/**' --path 'libs/**'
Workspace at /home/you/api
Paths: services/api/** libs/**
At landing 1, 2 files written
$ cd ~/api
$ af sparse list
services/api/**
libs/**
$ find . -path ./.af -prune -o -type f -print | sort
./libs/common.txt
./services/api/main.txtA workspace that holds less is faster to sync and to snapshot, and keeps what you, or an agent, reads to what matters.
Add a path
Meanwhile, the web service changes on main, from the other workspace:
$ cd ~/all
$ af new -m "Add a footer"
Working on change rsosxlkurvoukxpotzqqoprwzzvvtukw at commit e3e889a9146b8fc7f0bc418fcee1c0f10e57ce68a0da436ebc15e4b496b1d2fb
$ printf 'serve the pages, with a footer\n' > services/web/main.txt
$ af submit
Submitted commit 648eda4a7df6df8ef70bc65a40c4c9b71ec03e903b009f2209b88831309c920f, 1 edit in its last snapshot$ af status
Change rsosxlkurvoukxpotzqqoprwzzvvtukw (landed) at commit 648eda4a7df6df8ef70bc65a40c4c9b71ec03e903b009f2209b88831309c920f
Based on landing 1Back in the API’s workspace, add the web service:
$ cd ~/api
$ af sparse set --add 'services/web/**'
Added 1 file, removed 0 files
$ cat services/web/main.txt
serve the pagesNo footer: the file comes as your workspace’s landing holds it, landing 1. af sparse changes which files you hold, never which landing they are from, as jj sparse set leaves @ where it is. af sync brings the newer landings, to every path you hold:
$ af sync
At landing 2: 1 file written, 0 deleted
$ cat services/web/main.txt
serve the pages, with a footerRemove a path you edited
Start a change that edits both services, then take the web service away:
$ af new -m "Log each request"
Working on change xyzvqzmklvoyrzouwvotxtnltpskowrr at commit dba2e130aeda561f815cd928cd58fe23126ac231c7ec44a164516f2e573bb0cf
$ printf 'serve the api, logging each request\n' > services/api/main.txt
$ printf 'serve the pages, logging each request\n' > services/web/main.txt
$ af sparse set --remove 'services/web/**'
Added 0 files, removed 1 file
$ find . -path ./.af -prune -o -type f -print | sort
./libs/common.txt
./services/api/main.txtThe file left your disk, but not your change: af sparse snapshots your files before it changes anything, so the edit was in the change first. The change still holds it:
$ af diff
M services/api/main.txt
M services/web/main.txt
--- a/services/api/main.txt
+++ b/services/api/main.txt
@@ -1 +1 @@
-serve the api
+serve the api, logging each request
--- a/services/web/main.txt
+++ b/services/web/main.txt
@@ -1 +1 @@
-serve the pages, with a footer
+serve the pages, logging each requestHold every file again with af sparse reset, and the edit comes back from your change:
$ af sparse reset
Added 1 file, removed 0 files
$ cat services/web/main.txt
serve the pages, logging each requestTwo more things af sparse never does: it never touches a file you have not recorded, under a path you add or one you remove, and it never removes a file you edited after its snapshot; that one stays on disk, named in what af sparse prints.
Exercises
-
Hold only the library: start from no paths and add
libs/**. Your edits to both services stay in your change.$ af sparse set --clear --add 'libs/**' Added 0 files, removed 2 files $ af sparse list libs/** $ find . -path ./.af -prune -o -type f -print | sort ./libs/common.txt -
Hold every file again, and check that your edit of the API came back.
$ af sparse reset Added 2 files, removed 0 files $ cat services/api/main.txt serve the api, logging each request
Check your understanding
You add a path, and its files are older than main’s. Why, and how do you get the newest?
af sparse brings a path’s files as the landing your workspace is at holds them, so every file you hold is from one landing, and it moves nothing else, as jj sparse set leaves @ where it is. af sync brings the newer landings, to every path you hold.
You remove a path where you had edits. Where did they go?
Into your change: af sparse snapshots your files before it changes anything, so the edits are in the change, then it removes the files from disk. Add the path again, or af sparse reset, and they come back from the change.
An untracked file sits in a folder you add. What happens to it?
Nothing: af sparse never touches a file it has not recorded. It is in view now, so your next snapshot takes it into your change, as jj takes in a new file. Where main has a file at the same path, af sparse leaves yours and names it.
Next
That is the developer track. The curriculum map lists the other tracks; Connect an agent is the place to start the agent builder’s. For every option of af sparse, see the reference.