|
1 | 1 | # Change Detection
|
2 | 2 |
|
3 |
| -TODO |
| 3 | +## Lifecycle of an operation on the codebase graph |
| 4 | + |
| 5 | +Changes will go through 4 states. By default, we do not apply changes to the codebase graph, only to the filesystem. |
| 6 | + |
| 7 | +### Pending transactions |
| 8 | + |
| 9 | +After calling an edit or other transaction method, the changes are stored in a pending transaction. Pending transactions will be committed as described in the previous chapter. |
| 10 | + |
| 11 | +### Pending syncs |
| 12 | + |
| 13 | +After a transaction is committed, the file is marked as a pending sync. This means the filesystem state has been updated, but the codebase graph has not been updated yet. |
| 14 | + |
| 15 | +### Applied syncs |
| 16 | + |
| 17 | +When we sync the graph, we apply all the pending syncs and clear them. The codebase graph is updated to reflect the changes. We track all the applied syncs in the codebase graph. |
| 18 | + |
| 19 | +### Saved/baseline state |
| 20 | + |
| 21 | +Finally, we can set the baseline state to a git commit. This is the state we target when we reset the codebase graph. When we checkout branches, we update the baseline state. |
| 22 | + |
| 23 | +## Change Detection |
| 24 | + |
| 25 | +When we sync or build the graph, first we build a list of all files in 3 categories: |
| 26 | + |
| 27 | +- Removed files |
| 28 | +- Added files |
| 29 | +- Files to repase |
| 30 | + |
| 31 | +For example, if we move a file, it will be in the added and removed files |
| 32 | +If we add a file, it will be in the added files even if we peformed edits on it later. |
| 33 | + |
| 34 | +## Codebase.commit logic |
| 35 | + |
| 36 | +We follow the following logic |
| 37 | + |
| 38 | +1. Commit all pending transactions |
| 39 | +1. Write all buffered files to the disk |
| 40 | +1. Store this to pending changes (usually we will skip the remaining steps if we commit without syncing the graph) |
| 41 | +1. Build list of removed, added and modified files from pending changes |
| 42 | +1. For removed files, we need to remove all the edges that point to the file. |
| 43 | +1. For added files, we need to add all the edges that point to the file. |
| 44 | +1. For modified files, we remove all the edges that point to the file and add all the edges that point to the new file. This is complicated since edges may pass through the modified file and need to be intelligently updated. |
| 45 | +1. Mark all pending changes as applied |
| 46 | + |
| 47 | +## Reset logic |
| 48 | + |
| 49 | +Reset is just the inverse of commit. We need to |
| 50 | + |
| 51 | +1. Cancel all pending transactions |
| 52 | +1. Restore file state to the state to the target git commit |
| 53 | +1. Clear all pending changes to the graph |
| 54 | +1. Reverse all applied syncs to the graph |
4 | 55 |
|
5 | 56 | ## Next Step
|
6 | 57 |
|
|
0 commit comments